Skip to content

Commit

Permalink
global muted words done
Browse files Browse the repository at this point in the history
  • Loading branch information
trinkey committed Dec 18, 2024
1 parent 803b9d9 commit 5bf0527
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
21 changes: 15 additions & 6 deletions smiggins/backend/api/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from django.db.utils import IntegrityError
from posts.models import Comment, M2MLikeC, Notification, Post, User

from ..helper import (DEFAULT_LANG, can_view_post, create_api_ratelimit,
create_notification, delete_notification,
ensure_ratelimit, find_mentions, get_lang, get_post_json,
trim_whitespace)
from ..helper import (DEFAULT_LANG, can_view_post, check_muted_words,
create_api_ratelimit, create_notification,
delete_notification, ensure_ratelimit, find_mentions,
get_lang, get_post_json, trim_whitespace)
from ..variables import (API_TIMINGS, ENABLE_CONTENT_WARNINGS,
ENABLE_LOGGED_OUT_CONTENT, ENABLE_POST_DELETION,
MAX_CONTENT_WARNING_LENGTH, MAX_POST_LENGTH,
Expand All @@ -31,8 +31,6 @@ def comment_create(request, data: NewComment) -> APIResponse:
"message": lang["generic"]["ratelimit"]
}

content = data.content.replace("\r", "")

content = trim_whitespace(data.content)
c_warning = trim_whitespace(data.c_warning, True) if ENABLE_CONTENT_WARNINGS else ""

Expand All @@ -50,6 +48,17 @@ def comment_create(request, data: NewComment) -> APIResponse:
"message": lang["post"]["invalid_length"].replace("%s", str(MAX_POST_LENGTH))
}

if check_muted_words(
content,
c_warning
):
create_api_ratelimit("api_post_create", API_TIMINGS["create post failure"], token)
lang = get_lang(user)
return 400, {
"success": False,
"message": lang["post"]["muted"]
}

create_api_ratelimit("api_comment_create", API_TIMINGS["create comment"], token)

timestamp = round(time.time())
Expand Down
11 changes: 9 additions & 2 deletions smiggins/backend/api/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

from posts.models import PrivateMessage, PrivateMessageContainer, User

from ..helper import get_badges, get_container_id, get_lang, trim_whitespace
from ..helper import (check_muted_words, get_badges, get_container_id,
get_lang, trim_whitespace)
from ..variables import MAX_POST_LENGTH, MESSAGES_PER_REQUEST
from .schema import APIResponse, NewContainer, NewMessage


def container_create(request, data: NewContainer) -> APIResponse:
# Called when a new comment is created.


self_user = User.objects.get(token=request.COOKIES.get("token"))
username = data.username.strip().lower()

Expand Down Expand Up @@ -94,6 +94,13 @@ def send_message(request, data: NewMessage) -> APIResponse:
"message": lang["messages"]["invalid_size"]
}

if check_muted_words(content):
lang = get_lang(user)
return 400, {
"success": False,
"message": lang["message"]["muted"]
}

timestamp = round(time.time())

x = container.user_one.messages
Expand Down
2 changes: 1 addition & 1 deletion smiggins/backend/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def check_muted_words(*content: str) -> bool:
if mw.is_regex:
word = re.compile(mw.string)
else:
word = re.compile("\\b" + mw.string.replace(" ", "\b.+\b") + "\\b", re.DOTALL | re.IGNORECASE)
word = re.compile("\\b" + mw.string.replace(" ", "\\b.+\\b") + "\\b", re.DOTALL | re.IGNORECASE)

for val in content:
if word.match(val):
Expand Down
1 change: 1 addition & 0 deletions smiggins/lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@
"input_placeholder": "What would you like to tell %s?", // Ex: What would you like to tell gerard?

"no_messages": "No messages sent",
"muted": "This message contains a word blocked by the instance administration",

"list_title": "Messages",
"list_subtitle": "Recent Messages",
Expand Down

0 comments on commit 5bf0527

Please sign in to comment.