From 501fbbf5d3b83988470f428c3dc4cec609d9c686 Mon Sep 17 00:00:00 2001 From: justcool393 Date: Tue, 11 Oct 2022 22:11:20 -0700 Subject: [PATCH] move antispam code to actions --- files/helpers/actions.py | 80 ++++++++++++++++++++++++++++++++++++++++ files/helpers/const.py | 1 + files/routes/comments.py | 72 +----------------------------------- files/routes/posts.py | 43 +-------------------- 4 files changed, 84 insertions(+), 112 deletions(-) diff --git a/files/helpers/actions.py b/files/helpers/actions.py index b319f7ecc..4bd3694a4 100644 --- a/files/helpers/actions.py +++ b/files/helpers/actions.py @@ -311,3 +311,83 @@ def execute_basedbot(c, level, body, parent_submission, parent_post, v): n = Notification(comment_id=c_based.id, user_id=v.id) g.db.add(n) + +def execute_antispam_submission_check(title, v, url): + now = int(time.time()) + cutoff = now - 60 * 60 * 24 + + similar_posts = g.db.query(Submission).filter( + Submission.author_id == v.id, + Submission.title.op('<->')(title) < SPAM_SIMILARITY_THRESHOLD, + Submission.created_utc > cutoff + ).all() + + if url: + similar_urls = g.db.query(Submission).filter( + Submission.author_id == v.id, + Submission.url.op('<->')(url) < SPAM_URL_SIMILARITY_THRESHOLD, + Submission.created_utc > cutoff + ).all() + else: similar_urls = [] + + threshold = SPAM_SIMILAR_COUNT_THRESHOLD + if v.age >= (60 * 60 * 24 * 7): threshold *= 3 + elif v.age >= (60 * 60 * 24): threshold *= 2 + + if max(len(similar_urls), len(similar_posts)) >= threshold: + text = "Your account has been banned for **1 day** for the following reason:\n\n> Too much spam!" + send_repeatable_notification(v.id, text) + + v.ban(reason="Spamming.", + days=1) + + for post in similar_posts + similar_urls: + post.is_banned = True + post.is_pinned = False + post.ban_reason = "AutoJanny" + g.db.add(post) + ma=ModAction( + user_id=AUTOJANNY_ID, + target_submission_id=post.id, + kind="ban_post", + _note="spam" + ) + g.db.add(ma) + return False + return True + +def execute_antispam_comment_check(body, v): + if len(body) <= COMMENT_SPAM_LENGTH_THRESHOLD: return + now = int(time.time()) + cutoff = now - 60 * 60 * 24 + + similar_comments = g.db.query(Comment).filter( + Comment.author_id == v.id, + Comment.body.op('<->')(body) < COMMENT_SPAM_SIMILAR_THRESHOLD, + Comment.created_utc > cutoff + ).all() + + threshold = COMMENT_SPAM_COUNT_THRESHOLD + if v.age >= (60 * 60 * 24 * 7): + threshold *= 3 + elif v.age >= (60 * 60 * 24): + threshold *= 2 + + if len(similar_comments) <= threshold: return + text = "Your account has been banned for **1 day** for the following reason:\n\n> Too much spam!" + send_repeatable_notification(v.id, text) + v.ban(reason="Spamming.", + days=1) + for comment in similar_comments: + comment.is_banned = True + comment.ban_reason = "AutoJanny" + g.db.add(comment) + ma=ModAction( + user_id=AUTOJANNY_ID, + target_comment_id=comment.id, + kind="ban_comment", + _note="spam" + ) + g.db.add(ma) + g.db.commit() + abort(403, "Too much spam!") \ No newline at end of file diff --git a/files/helpers/const.py b/files/helpers/const.py index 79975c89b..a0d51e17b 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -341,6 +341,7 @@ TRANSFER_MESSAGE_LENGTH_LIMIT = 200 # do not make larger than 10000 characters ( LOGGEDIN_ACTIVE_TIME = 15 * 60 PFP_DEFAULT_MARSEY = True NOTIFICATION_SPAM_AGE_THRESHOLD = 0.5 * 86400 +COMMENT_SPAM_LENGTH_THRESHOLD = 50 HOLE_NAME = 'hole' HOLE_STYLE_FLAIR = False diff --git a/files/routes/comments.py b/files/routes/comments.py index 2075668ab..8c4f69b66 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -260,43 +260,7 @@ def comment(v): is_bot = v.id != BBBB_ID and (bool(request.headers.get("Authorization")) or (SITE == 'pcmemes.net' and v.id == SNAPPY_ID)) - if len(body) > 50: - now = int(time.time()) - cutoff = now - 60 * 60 * 24 - - similar_comments = g.db.query(Comment).filter( - Comment.author_id == v.id, - Comment.body.op('<->')(body) < COMMENT_SPAM_SIMILAR_THRESHOLD, - Comment.created_utc > cutoff - ).all() - - threshold = COMMENT_SPAM_COUNT_THRESHOLD - if v.age >= (60 * 60 * 24 * 7): - threshold *= 3 - elif v.age >= (60 * 60 * 24): - threshold *= 2 - - if len(similar_comments) > threshold: - text = "Your account has been banned for **1 day** for the following reason:\n\n> Too much spam!" - send_repeatable_notification(v.id, text) - - v.ban(reason="Spamming.", - days=1) - - for comment in similar_comments: - comment.is_banned = True - comment.ban_reason = "AutoJanny" - g.db.add(comment) - ma=ModAction( - user_id=AUTOJANNY_ID, - target_comment_id=comment.id, - kind="ban_comment", - _note="spam" - ) - g.db.add(ma) - - g.db.commit() - abort(403, "Too much spam!") + execute_antispam_comment_check(body, v) if len(body_html) > COMMENT_BODY_HTML_LENGTH_LIMIT: abort(400) @@ -502,39 +466,7 @@ def edit_comment(cid, v): ) g.db.add(option) - if len(body) > 50: - now = int(time.time()) - cutoff = now - 60 * 60 * 24 - - similar_comments = g.db.query(Comment - ).filter( - Comment.author_id == v.id, - Comment.body.op('<->')(body) < SPAM_SIMILARITY_THRESHOLD, - Comment.created_utc > cutoff - ).all() - - threshold = SPAM_SIMILAR_COUNT_THRESHOLD - if v.age >= (60 * 60 * 24 * 30): - threshold *= 4 - elif v.age >= (60 * 60 * 24 * 7): - threshold *= 3 - elif v.age >= (60 * 60 * 24): - threshold *= 2 - - if len(similar_comments) > threshold: - text = "Your account has been banned for **1 day** for the following reason:\n\n> Too much spam!" - send_repeatable_notification(v.id, text) - - v.ban(reason="Spamming.", - days=1) - - for comment in similar_comments: - comment.is_banned = True - comment.ban_reason = "AutoJanny" - g.db.add(comment) - - g.db.commit() - abort(403, "Too much spam!") + execute_antispam_comment_check(body, v) body += process_files() body = body.strip()[:COMMENT_BODY_LENGTH_LIMIT] # process_files potentially adds characters to the post diff --git a/files/routes/posts.py b/files/routes/posts.py index 3851a9a3c..186a0b85b 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -832,48 +832,7 @@ def submit_post(v, sub=None): if dup and SITE != 'localhost': return redirect(dup.permalink) - now = int(time.time()) - cutoff = now - 60 * 60 * 24 - - - similar_posts = g.db.query(Submission).filter( - Submission.author_id == v.id, - Submission.title.op('<->')(title) < SPAM_SIMILARITY_THRESHOLD, - Submission.created_utc > cutoff - ).all() - - if url: - similar_urls = g.db.query(Submission).filter( - Submission.author_id == v.id, - Submission.url.op('<->')(url) < SPAM_URL_SIMILARITY_THRESHOLD, - Submission.created_utc > cutoff - ).all() - else: similar_urls = [] - - threshold = SPAM_SIMILAR_COUNT_THRESHOLD - if v.age >= (60 * 60 * 24 * 7): threshold *= 3 - elif v.age >= (60 * 60 * 24): threshold *= 2 - - if max(len(similar_urls), len(similar_posts)) >= threshold: - - text = "Your account has been banned for **1 day** for the following reason:\n\n> Too much spam!" - send_repeatable_notification(v.id, text) - - v.ban(reason="Spamming.", - days=1) - - for post in similar_posts + similar_urls: - post.is_banned = True - post.is_pinned = False - post.ban_reason = "AutoJanny" - g.db.add(post) - ma=ModAction( - user_id=AUTOJANNY_ID, - target_submission_id=post.id, - kind="ban_post", - _note="spam" - ) - g.db.add(ma) + if not execute_antispam_submission_check(title, v, url): return redirect("/notifications") if len(url) > 2048: