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.
master
Snakes 2022-06-22 17:21:12 -04:00
parent 5f78b4e365
commit 36d24cf213
3 changed files with 55 additions and 40 deletions

View File

@ -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"<a href='/h/{sub_name}'>/h/{sub_name}</a> 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"<a href='/h/{sub_name}'>/h/{sub_name}</a> 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

View File

@ -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/<pid>")
@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'<a href="/h/{sub_from}">/h/{sub_from}</a>'
sub_to_str = 'frontpage' if sub_to is None else \
f'<a href="/h/{sub_to}">/h/{sub_to}</a>'
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)

View File

@ -320,6 +320,53 @@ def kick(v, pid):
return {"message": "Post kicked successfully!"}
@app.post("/rehole/<pid>", defaults={'hole': ''})
@app.post("/rehole/<pid>/<hole>")
@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'<a href="/h/{sub_from}">/h/{sub_from}</a>'
sub_to_str = 'frontpage' if sub_to is None else \
f'<a href="/h/{sub_to}">/h/{sub_to}</a>'
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"<a href='/h/{hole}'>/h/{hole}</a> 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/<sub>/settings')
@is_not_permabanned