From 8fe73cb68e0d58735f44777132ee349f6c81df54 Mon Sep 17 00:00:00 2001 From: justcool393 Date: Thu, 20 Oct 2022 19:28:05 -0500 Subject: [PATCH] refactor blackjack a bit --- files/helpers/actions.py | 29 ++++++++++++++++++++++++++++- files/routes/chat.py | 9 ++------- files/routes/comments.py | 21 ++------------------- files/routes/posts.py | 14 ++++---------- files/routes/reporting.py | 18 +++--------------- files/routes/users.py | 24 +++--------------------- 6 files changed, 42 insertions(+), 73 deletions(-) diff --git a/files/helpers/actions.py b/files/helpers/actions.py index 201984f6d..2a99224a7 100644 --- a/files/helpers/actions.py +++ b/files/helpers/actions.py @@ -357,6 +357,33 @@ def execute_antispam_submission_check(title, v, url): return False return True +def execute_blackjack(v, target, body, type): + if not blackjack: return + if any(i in body.lower() for i in blackjack.split()): + v.shadowbanned = 'AutoJanny' + if not v.is_banned: v.ban_reason = f"Blackjack" + g.db.add(v) + notif = None + extra_info = None + if type == 'submission': + extra_info = f"submission ({target.permalink})" + send_repeatable_notification(CARP_ID, target.permalink) + elif type == 'comment' or type == 'message': + extra_info = f"{type} ({target.permalink})" + notif = Notification(comment_id=target.id, user_id=CARP_ID) + elif type == 'chat': + extra_info = "chat message" + elif type == 'flag': + extra_info = f"reports on {target.permalink}" + else: + extra_info = "unknown entity" + if notif: + g.db.add(notif) + g.db.flush() + elif extra_info: send_repeatable_notification(CARP_ID, f"Blackjack for {v.name}: {extra_info}") + return False + return True + def execute_antispam_comment_check(body, v): if len(body) <= COMMENT_SPAM_LENGTH_THRESHOLD: return now = int(time.time()) @@ -391,4 +418,4 @@ def execute_antispam_comment_check(body, v): ) g.db.add(ma) g.db.commit() - abort(403, "Too much spam!") \ No newline at end of file + abort(403, "Too much spam!") diff --git a/files/routes/chat.py b/files/routes/chat.py index f848dff6a..c68773d69 100644 --- a/files/routes/chat.py +++ b/files/routes/chat.py @@ -6,6 +6,7 @@ from files.helpers.sanitize import sanitize from files.helpers.const import * from files.helpers.alerts import * from files.helpers.regex import * +from files.helpers.actions import * from flask_socketio import SocketIO, emit from files.__main__ import app, limiter, cache from flask import render_template @@ -78,14 +79,8 @@ def speak(data, v): "time": int(time.time()), } - if v.shadowbanned: + if v.shadowbanned or not execute_blackjack(v, None, text, "chat"): emit('speak', data) - elif blackjack and any(i in text.lower() for i in blackjack.split()): - emit('speak', data) - v.shadowbanned = 'AutoJanny' - if not v.is_banned: v.ban_reason = 'Blackjack' - g.db.add(v) - send_repeatable_notification(CARP_ID, f"{v.username} has been shadowbanned because of a chat message.") elif ('discord.gg' in text or 'discord.com' in text or 'discordapp.com' in text): # Follows same logic as in users.py:message2/messagereply; TODO: unify? emit('speak', data) diff --git a/files/routes/comments.py b/files/routes/comments.py index 1a61c3a70..c219127a0 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -279,11 +279,7 @@ def comment(v): g.db.add(c) g.db.flush() - if blackjack and any(i in c.body.lower() for i in blackjack.split()): - v.shadowbanned = 'AutoJanny' - if not v.is_banned: v.ban_reason = 'Blackjack' - notif = Notification(comment_id=c.id, user_id=CARP_ID) - g.db.add(notif) + execute_blackjack(v, c, c.body, "comment") if c.level == 1: c.top_comment_id = c.id else: c.top_comment_id = parent.top_comment_id @@ -308,19 +304,13 @@ def comment(v): execute_basedbot(c, level, body, parent_submission, parent_post, v) if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in c.body.lower() and parent_post.sub != 'chudrama': - c.is_banned = True c.ban_reason = "AutoJanny" - g.db.add(c) - body = AGENDAPOSTER_MSG.format(username=v.username, type='comment', AGENDAPOSTER_PHRASE=AGENDAPOSTER_PHRASE) - body_jannied_html = AGENDAPOSTER_MSG_HTML.format(id=v.id, username=v.username, type='comment', AGENDAPOSTER_PHRASE=AGENDAPOSTER_PHRASE) - - c_jannied = Comment(author_id=AUTOJANNY_ID, parent_submission=parent_submission, distinguish_level=6, @@ -488,14 +478,7 @@ def edit_comment(cid, v): c.body = body c.body_html = body_html - if blackjack and any(i in c.body.lower() for i in blackjack.split()): - v.shadowbanned = 'AutoJanny' - if not v.is_banned: v.ban_reason = 'Blackjack' - g.db.add(v) - notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=CARP_ID).one_or_none() - if not notif: - notif = Notification(comment_id=c.id, user_id=CARP_ID) - g.db.add(notif) + execute_blackjack(v, c, c.body, "comment") if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in c.body.lower() and c.post.sub != 'chudrama': abort(403, f'You have to include "{AGENDAPOSTER_PHRASE}" in your comment!') diff --git a/files/routes/posts.py b/files/routes/posts.py index 85f724ff7..05688fd2f 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -474,11 +474,8 @@ def edit_post(pid, v): p.body = body - if blackjack and any(i in f'{p.body} {p.title} {p.url}'.lower() for i in blackjack.split()): - v.shadowbanned = 'AutoJanny' - if not v.is_banned: v.ban_reason = 'Blackjack' - g.db.add(v) - send_repeatable_notification(CARP_ID, p.permalink) + for text in [p.body, p.title, p.url]: + if not execute_blackjack(v, p, text, 'submission'): break if len(body_html) > POST_BODY_HTML_LENGTH_LIMIT: abort(400, f"Submission body_html too long! (max {POST_BODY_HTML_LENGTH_LIMIT} characters)") @@ -907,11 +904,8 @@ def submit_post(v, sub=None): g.db.add(post) g.db.flush() - if blackjack and any(i in f'{post.body} {post.title} {post.url}'.lower() for i in blackjack.split()): - v.shadowbanned = 'AutoJanny' - if not v.is_banned: v.ban_reason = 'Blackjack' - g.db.add(v) - send_repeatable_notification(CARP_ID, post.permalink) + for text in [p.body, p.title, p.url]: + if not execute_blackjack(v, p, text, 'submission'): break for option in options: option = SubmissionOption( diff --git a/files/routes/reporting.py b/files/routes/reporting.py index fa39a3dcd..797fd8ad4 100644 --- a/files/routes/reporting.py +++ b/files/routes/reporting.py @@ -1,6 +1,7 @@ from files.helpers.wrappers import * from files.helpers.get import * from files.helpers.alerts import * +from files.helpers.actions import * from flask import g from files.__main__ import app, limiter from os import path @@ -14,17 +15,10 @@ def flag_post(pid, v): post = get_post(pid) reason = request.values.get("reason", "").strip() - if blackjack and any(i in reason.lower() for i in blackjack.split()): - v.shadowbanned = 'AutoJanny' - if not v.is_banned: v.ban_reason = 'Blackjack' - send_repeatable_notification(CARP_ID, f"reports on {post.permalink}") - + execute_blackjack(v, post, reason, 'flag') if v.is_muted: abort(400, "You are forbidden from making reports.") - reason = reason[:100] - reason = filter_emojis_only(reason) - if len(reason) > 350: abort(400, "Too long.") if reason.startswith('!') and (v.admin_level >= PERMS['POST_COMMENT_MODERATION'] or post.sub and v.mods(post.sub)): @@ -120,14 +114,8 @@ def flag_comment(cid, v): if existing: abort(409, "You already reported this comment!") reason = request.values.get("reason", "").strip() - - if blackjack and any(i in reason.lower() for i in blackjack.split()): - v.shadowbanned = 'AutoJanny' - if not v.is_banned: v.ban_reason = 'Blackjack' - send_repeatable_notification(CARP_ID, f"reports on {comment.permalink}") - + execute_blackjack(v, comment, reason, 'flag') reason = reason[:100] - reason = filter_emojis_only(reason) if len(reason) > 350: abort(400, "Too long.") diff --git a/files/routes/users.py b/files/routes/users.py index 9d36cf222..f211e22a5 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -8,6 +8,7 @@ from files.helpers.alerts import * from files.helpers.sanitize import * from files.helpers.const import * from files.helpers.sorting_and_time import * +from files.helpers.actions import * from files.mail import * from flask import * from files.__main__ import app, limiter, db_session @@ -542,17 +543,7 @@ def message2(v, username): ) g.db.add(c) g.db.flush() - - if blackjack and any(i in c.body_html.lower() for i in blackjack.split()): - v.shadowbanned = 'AutoJanny' - if not v.is_banned: v.ban_reason = 'Blackjack' - g.db.add(v) - notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=CARP_ID).one_or_none() - if not notif: - notif = Notification(comment_id=c.id, user_id=CARP_ID) - g.db.add(notif) - g.db.flush() - + execute_blackjack(v, c, c.body_html, 'message') c.top_comment_id = c.id if user.id not in bots: @@ -619,16 +610,7 @@ def messagereply(v): ) g.db.add(c) g.db.flush() - - if blackjack and any(i in c.body_html.lower() for i in blackjack.split()): - v.shadowbanned = 'AutoJanny' - if not v.is_banned: v.ban_reason = 'Blackjack' - g.db.add(v) - notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=CARP_ID).one_or_none() - if not notif: - notif = Notification(comment_id=c.id, user_id=CARP_ID) - g.db.add(notif) - g.db.flush() + execute_blackjack(v, c, c.body_html, 'message') if user_id and user_id not in (v.id, 2, bots): notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=user_id).one_or_none()