From 36d24cf213c0606efda063575442be0b08c4555a Mon Sep 17 00:00:00 2001 From: TLSM Date: Wed, 22 Jun 2022 17:21:12 -0400 Subject: [PATCH] Refactor hole follow notifs to encapsulate. Fixes bug where admins moving a post into a hole doesn't notify followers of the destination hole. Also, we now have a route endpoint for reholing that is potentially usable for e.g. an actual post_actions button to rehole, rather than the report command UI at present. --- files/routes/posts.py | 23 +++++-------------- files/routes/reporting.py | 25 +++------------------ files/routes/subs.py | 47 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 40 deletions(-) diff --git a/files/routes/posts.py b/files/routes/posts.py index 76f0435b0..4fc0f0659 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -8,6 +8,7 @@ from files.helpers.discord import send_discord_message from files.helpers.const import * from files.helpers.slots import * from files.classes import * +from files.routes.subs import on_post_hole_entered from flask import * from io import BytesIO from files.__main__ import app, limiter, cache, db_session @@ -85,15 +86,8 @@ def publish(pid, v): if post.club and not user.paid_dues: continue add_notif(cid, user.id) - if post.sub and post.subr: - sub_name = post.subr.name - text = f"/h/{sub_name} has a new " \ - + f"post: [{post.title}]({post.shortlink})" - cid = notif_comment(text, autojanny=True) - for follow in post.subr.followers: - user = get_account(follow.user_id) - if post.club and not user.paid_dues: continue - add_notif(cid, user.id) + if post.sub: + on_post_hole_entered(post) g.db.commit() @@ -1059,15 +1053,8 @@ def submit_post(v, sub=None): if post.club and not user.paid_dues: continue add_notif(cid, user.id) - if post.sub and post.subr: - sub_name = post.subr.name - text = f"/h/{sub_name} has a new " \ - + f"post: [{post.title}]({post.shortlink})" - cid = notif_comment(text, autojanny=True) - for follow in post.subr.followers: - user = get_account(follow.user_id) - if post.club and not user.paid_dues: continue - add_notif(cid, user.id) + if post.sub: + on_post_hole_entered(post) if v.agendaposter and not v.marseyawarded and AGENDAPOSTER_PHRASE not in f'{post.body}{post.title}'.lower(): post.is_banned = True diff --git a/files/routes/reporting.py b/files/routes/reporting.py index 64e78fb4b..989956c18 100644 --- a/files/routes/reporting.py +++ b/files/routes/reporting.py @@ -4,6 +4,7 @@ from flask import g from files.__main__ import app, limiter from os import path from files.helpers.sanitize import filter_emojis_only +from files.routes.subs import rehole_post @app.post("/report/post/") @limiter.limit("1/second;30/minute;200/hour;1000/day") @@ -39,28 +40,8 @@ def api_flag_post(pid, v): _note=f'"{post.flair}"' ) g.db.add(ma) - elif reason.startswith('/h/') and v.admin_level > 1: - sub_from = post.sub - sub_to = reason[3:].strip().lower() - sub_to = g.db.query(Sub).filter_by(name=sub_to).one_or_none() - sub_to = sub_to.name if sub_to else None - - if sub_from == sub_to: - abort(404) - post.sub = sub_to - g.db.add(post) - - sub_from_str = 'frontpage' if sub_from is None else \ - f'/h/{sub_from}' - sub_to_str = 'frontpage' if sub_to is None else \ - f'/h/{sub_to}' - ma = ModAction( - kind='move_hole', - user_id=v.id, - target_submission_id=post.id, - _note=f'{sub_from_str} → {sub_to_str}', - ) - g.db.add(ma) + elif reason.startswith('/h/') and v.admin_level >= 2: + rehole_post(pid=post.id, hole=reason[3:]) else: flag = Flag(post_id=post.id, user_id=v.id, reason=reason) g.db.add(flag) diff --git a/files/routes/subs.py b/files/routes/subs.py index c46134064..0a903991a 100644 --- a/files/routes/subs.py +++ b/files/routes/subs.py @@ -320,6 +320,53 @@ def kick(v, pid): return {"message": "Post kicked successfully!"} +@app.post("/rehole/", defaults={'hole': ''}) +@app.post("/rehole//") +@admin_level_required(2) +def rehole_post(v, pid, hole): + post = get_post(pid) + + sub_from = post.sub + sub_to = hole.strip().lower() + sub_to = g.db.query(Sub).filter_by(name=sub_to).one_or_none() + sub_to = sub_to.name if sub_to else None + + if sub_from == sub_to: + abort(400) + post.sub = sub_to + g.db.add(post) + + sub_from_str = 'frontpage' if sub_from is None else \ + f'/h/{sub_from}' + sub_to_str = 'frontpage' if sub_to is None else \ + f'/h/{sub_to}' + ma = ModAction( + kind='move_hole', + user_id=v.id, + target_submission_id=post.id, + _note=f'{sub_from_str} → {sub_to_str}', + ) + g.db.add(ma) + + on_post_hole_entered(post) + g.db.commit() + + return {"message": f"Post moved to {sub_to_str}!"} + +def on_post_hole_entered(post): + if not post.sub or not post.subr: + return + hole = post.subr.name + + # Notify hole followers + if not post.ghost and not post.private: + text = f"/h/{hole} has a new " \ + + f"post: [{post.title}]({post.shortlink})" + cid = notif_comment(text, autojanny=True) + for follow in post.subr.followers: + user = get_account(follow.user_id) + if post.club and not user.paid_dues: continue + add_notif(cid, user.id) @app.get('/h//settings') @is_not_permabanned