Rejigger the Mod/Report state system.

This commit is contained in:
Ben Rog-Wilhelm 2023-06-25 20:46:24 -05:00
parent 31ebdd0213
commit ccf809406e
36 changed files with 492 additions and 226 deletions

View file

@ -5,6 +5,7 @@ from datetime import datetime
import requests
from files.classes import *
from files.classes.visstate import StateMod, StateReport
from files.helpers.alerts import *
from files.helpers.caching import invalidate_cache
from files.helpers.comments import comment_on_publish, comment_on_unpublish
@ -136,8 +137,8 @@ def revert_actions(v, username):
comments = g.db.query(Comment).filter(Comment.id.in_(comments)).all()
for item in posts + comments:
item.is_banned = False
item.ban_reason = None
item.state_mod = StateMod.VISIBLE
item.state_mod_set_by = v.username
g.db.add(item)
users = (x[0] for x in g.db.query(ModAction.target_user_id).filter(ModAction.user_id == user.id, ModAction.created_utc > cutoff, ModAction.kind.in_(('shadowban', 'ban_user'))).all())
@ -235,7 +236,7 @@ def filtered_submissions(v):
posts_just_ids = g.db.query(Submission) \
.order_by(Submission.id.desc()) \
.filter(Submission.filter_state == 'filtered') \
.filter(Submission.state_mod == StateMod.FILTERED) \
.limit(26) \
.offset(25 * (page - 1)) \
.with_entities(Submission.id)
@ -255,7 +256,7 @@ def filtered_comments(v):
comments_just_ids = g.db.query(Comment) \
.order_by(Comment.id.desc()) \
.filter(Comment.filter_state == 'filtered') \
.filter(Comment.state_mod == StateMod.FILTERED) \
.limit(26) \
.offset(25 * (page - 1)) \
.with_entities(Comment.id)
@ -266,6 +267,9 @@ def filtered_comments(v):
return render_template("admin/filtered_comments.html", v=v, listing=comments, next_exists=next_exists, page=page, sort="new")
# NOTE:
# This function is pretty grimy and should be rolled into the Remove/Unremove functions.
# (also rename Unremove to Approve, sigh)
@app.post("/admin/update_filter_status")
@limiter.exempt
@admin_level_required(2)
@ -277,29 +281,48 @@ def update_filter_status(v):
if new_status not in ['normal', 'removed', 'ignored']:
return { 'result': f'Status of {new_status} is not permitted' }
if new_status == 'normal':
state_mod_new = StateMod.VISIBLE
state_report_new = StateReport.RESOLVED
elif new_status == 'removed':
state_mod_new = StateMod.REMOVED
state_report_new = StateReport.RESOLVED
elif new_status == 'ignored':
state_mod_new = None # we just leave this as-is
state_report_new = StateReport.IGNORED
if post_id:
target = g.db.get(Submission, post_id)
old_status = target.filter_state
rows_updated = g.db.query(Submission).where(Submission.id == post_id) \
.update({Submission.filter_state: new_status})
old_status = target.state_mod
# this could totally be one query but it would be kinda ugly
if state_mod_new is not None:
g.db.query(Submission).where(Submission.id == post_id) \
.update({Submission.state_mod: state_mod_new, Submission.state_mod_set_by: v.username})
g.db.query(Submission).where(Submission.id == post_id) \
.update({Submission.state_report: state_report_new})
elif comment_id:
target = g.db.get(Comment, comment_id)
old_status = target.filter_state
rows_updated = g.db.query(Comment).where(Comment.id == comment_id) \
.update({Comment.filter_state: new_status})
old_status = target.state_mod
if state_mod_new is not None:
g.db.query(Comment).where(Comment.id == comment_id) \
.update({Comment.state_mod: state_mod_new, Comment.state_mod_set_by: v.username})
g.db.query(Comment).where(Comment.id == comment_id) \
.update({Comment.state_report: state_report_new})
else:
return { 'result': f'No valid item ID provided' }
if rows_updated == 1:
if target is not None:
# If comment now visible, update state to reflect publication.
if (isinstance(target, Comment)
and old_status in ['filtered', 'removed']
and new_status in ['normal', 'ignored']):
and old_status != StateMod.VISIBLE
and state_mod_new == StateMod.VISIBLE):
comment_on_publish(target) # XXX: can cause discrepancies if removal state ≠ filter state
if (isinstance(target, Comment)
and old_status in ['normal', 'ignored']
and new_status in ['filtered', 'removed']):
and old_status == StateMod.VISIBLE
and state_mod_new != StateMod.VISIBLE and state_mod_new is not None):
comment_on_unpublish(target) # XXX: can cause discrepancies if removal state ≠ filter state
g.db.commit()
@ -332,7 +355,7 @@ def reported_posts(v):
page = max(1, int(request.values.get("page", 1)))
subs_just_ids = g.db.query(Submission) \
.filter(Submission.filter_state == 'reported') \
.filter(Submission.state_report == StateReport.REPORTED) \
.order_by(Submission.id.desc()) \
.offset(25 * (page - 1)) \
.limit(26) \
@ -351,13 +374,8 @@ def reported_posts(v):
def reported_comments(v):
page = max(1, int(request.values.get("page", 1)))
listing = g.db.query(Comment
).filter_by(
is_approved=None,
is_banned=False
).join(Comment.reports).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all()
comments_just_ids = g.db.query(Comment) \
.filter(Comment.filter_state == 'reported') \
.filter(Comment.state_report == StateReport.REPORTED) \
.order_by(Comment.id.desc()) \
.offset(25 * (page - 1)) \
.limit(26) \
@ -415,7 +433,8 @@ def change_settings(v, setting):
level=1,
body_html=body_html,
sentto=MODMAIL_ID,
distinguish_level=6
distinguish_level=6,
state_mod=StateMod.VISIBLE,
)
g.db.add(new_comment)
g.db.flush()
@ -765,7 +784,7 @@ def admin_removed(v):
if page < 1: abort(400)
ids = g.db.query(Submission.id).join(User, User.id == Submission.author_id).filter(or_(Submission.is_banned==True, User.shadowbanned != None)).order_by(Submission.id.desc()).offset(25 * (page - 1)).limit(26).all()
ids = g.db.query(Submission.id).join(User, User.id == Submission.author_id).filter(or_(Submission.state_mod == StateMod.REMOVED, User.shadowbanned != None)).order_by(Submission.id.desc()).offset(25 * (page - 1)).limit(26).all()
ids=[x[0] for x in ids]
@ -790,7 +809,7 @@ def admin_removed_comments(v):
try: page = int(request.values.get("page", 1))
except: page = 1
ids = g.db.query(Comment.id).join(User, User.id == Comment.author_id).filter(or_(Comment.is_banned==True, User.shadowbanned != None)).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all()
ids = g.db.query(Comment.id).join(User, User.id == Comment.author_id).filter(or_(Comment.state_mode == StateMod.REMOVED, User.shadowbanned != None)).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all()
ids=[x[0] for x in ids]
@ -840,7 +859,8 @@ def shadowban(user_id, v):
parent_submission=None,
level=1,
body_html=body_html,
distinguish_level=6
distinguish_level=6,
state_mod=StateMod.VISIBLE,
)
g.db.add(new_comment)
g.db.flush()
@ -1025,7 +1045,8 @@ def ban_user(user_id, v):
parent_submission=None,
level=1,
body_html=body_html,
distinguish_level=6
distinguish_level=6,
state_mod=StateMod.VISIBLE,
)
g.db.add(new_comment)
g.db.flush()
@ -1088,11 +1109,10 @@ def remove_post(post_id, v):
if not post:
abort(400)
post.is_banned = True
post.is_approved = None
post.state_mod = StateMod.REMOVED
post.state_mod_set_by = v.username
post.stickied = None
post.is_pinned = False
post.ban_reason = v.username
g.db.add(post)
@ -1125,7 +1145,7 @@ def unremove_post(post_id, v):
if not post:
abort(400)
if post.is_banned:
if post.state_mod != StateMod.VISIBLE:
ma=ModAction(
kind="unremove_post",
user_id=v.id,
@ -1133,9 +1153,8 @@ def unremove_post(post_id, v):
)
g.db.add(ma)
post.is_banned = False
post.ban_reason = None
post.is_approved = v.id
post.state_mod = StateMod.VISIBLE
post.state_mod_set_by = v.username
g.db.add(post)
@ -1187,7 +1206,7 @@ def api_distinguish_post(post_id, v):
def sticky_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
if post and not post.stickied:
pins = g.db.query(Submission.id).filter(Submission.stickied != None, Submission.is_banned == False).count()
pins = g.db.query(Submission.id).filter(Submission.stickied != None, Submission.state_mod == StateMod.VISIBLE).count()
if pins > 2:
if v.admin_level >= 2:
post.stickied = v.username
@ -1296,9 +1315,8 @@ def api_remove_comment(c_id, v):
if not comment:
abort(404)
comment.is_banned = True
comment.is_approved = None
comment.ban_reason = v.username
comment.state_mod = StateMod.REMOVED
comment.state_mod_set_by = v.username
comment_on_unpublish(comment) # XXX: can cause discrepancies if removal state ≠ filter state
ma=ModAction(
kind="remove_comment",
@ -1317,7 +1335,7 @@ def api_unremove_comment(c_id, v):
comment = g.db.query(Comment).filter_by(id=c_id).one_or_none()
if not comment: abort(404)
if comment.is_banned:
if comment.state_mod == StateMod.REMOVED:
ma=ModAction(
kind="unremove_comment",
user_id=v.id,
@ -1325,9 +1343,8 @@ def api_unremove_comment(c_id, v):
)
g.db.add(ma)
comment.is_banned = False
comment.ban_reason = None
comment.is_approved = v.id
comment.state_mod = StateMod.VISIBLE
comment.state_mod_set_by = v.username
comment_on_publish(comment) # XXX: can cause discrepancies if removal state ≠ filter state
g.db.add(comment)
@ -1426,24 +1443,24 @@ def admin_toggle_ban_domain(v):
@app.post("/admin/nuke_user")
@limiter.exempt
@admin_level_required(2)
@admin_level_required(3)
def admin_nuke_user(v):
user=get_user(request.values.get("user"))
for post in g.db.query(Submission).filter_by(author_id=user.id).all():
if post.is_banned:
if post.state_mod != StateMod.REMOVED:
continue
post.is_banned = True
post.ban_reason = v.username
post.state_mod = StateMod.REMOVED
post.state_mod_set_by = v.username
g.db.add(post)
for comment in g.db.query(Comment).filter_by(author_id=user.id).all():
if comment.is_banned:
if comment.state_mod != StateMod.REMOVED:
continue
comment.is_banned = True
comment.ban_reason = v.username
comment.state_mod = StateMod.REMOVED
comment.state_mod_set_by = v.username
g.db.add(comment)
ma=ModAction(
@ -1460,24 +1477,24 @@ def admin_nuke_user(v):
@app.post("/admin/unnuke_user")
@limiter.exempt
@admin_level_required(2)
def admin_nunuke_user(v):
@admin_level_required(3)
def admin_unnuke_user(v):
user=get_user(request.values.get("user"))
for post in g.db.query(Submission).filter_by(author_id=user.id).all():
if not post.is_banned:
if post.state_mod == StateMod.VISIBLE:
continue
post.is_banned = False
post.ban_reason = None
post.state_mod = StateMod.VISIBLE
post.state_mod_set_by = v.username
g.db.add(post)
for comment in g.db.query(Comment).filter_by(author_id=user.id).all():
if not comment.is_banned:
if comment.state_mod == StateMod.VISIBLE:
continue
comment.is_banned = False
comment.ban_reason = None
comment.state_mod = StateMod.VISIBLE
comment.state_mod_set_by = v.username
g.db.add(comment)
ma=ModAction(