MarseyWorld/files/routes/reporting.py

89 lines
2.3 KiB
Python
Raw Normal View History

2021-10-15 14:08:27 +00:00
from files.helpers.wrappers import *
from files.helpers.get import *
from flask import g
from files.__main__ import app, limiter
from os import path
2021-12-07 23:18:06 +00:00
from files.helpers.sanitize import filter_emojis_only
2021-10-15 14:08:27 +00:00
2021-12-05 20:22:57 +00:00
@app.post("/report/post/<pid>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-12-05 20:22:57 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def api_flag_post(pid, v):
post = get_post(pid)
2021-12-05 20:22:57 +00:00
if not v.shadowbanned:
2021-10-15 14:08:27 +00:00
reason = request.values.get("reason", "").strip()[:100]
2021-12-05 20:22:57 +00:00
if not reason.startswith('!'):
2022-01-02 00:06:46 +00:00
existing = g.db.query(Flag.id).filter_by(user_id=v.id, post_id=post.id).one_or_none()
2021-12-05 20:22:57 +00:00
if existing: return "", 409
2021-12-07 23:18:06 +00:00
reason = filter_emojis_only(reason)
2021-11-22 14:16:58 +00:00
2022-01-07 21:03:14 +00:00
if len(reason) > 350: return {"error": "Too long."}
2021-10-15 14:08:27 +00:00
2021-12-05 20:22:57 +00:00
if reason.startswith('!') and v.admin_level > 1:
post.flair = reason[1:]
g.db.add(post)
else:
flag = Flag(post_id=post.id, user_id=v.id, reason=reason)
g.db.add(flag)
2021-10-15 14:08:27 +00:00
g.db.commit()
return {"message": "Post reported!"}
2021-12-05 20:22:57 +00:00
@app.post("/report/comment/<cid>")
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-12-05 20:22:57 +00:00
@auth_required
2021-10-15 14:08:27 +00:00
def api_flag_comment(cid, v):
comment = get_comment(cid)
2021-12-05 20:22:57 +00:00
if not v.shadowbanned:
2022-01-02 00:06:46 +00:00
existing = g.db.query(CommentFlag.id).filter_by( user_id=v.id, comment_id=comment.id).one_or_none()
2021-10-15 14:08:27 +00:00
if existing: return "", 409
2021-12-07 23:18:06 +00:00
reason = request.values.get("reason", "").strip()[:100]
reason = filter_emojis_only(reason)
2021-11-22 14:16:58 +00:00
2022-01-07 21:03:14 +00:00
if len(reason) > 350: return {"error": "Too long."}
2021-10-15 14:08:27 +00:00
2021-12-07 23:18:06 +00:00
flag = CommentFlag(comment_id=comment.id, user_id=v.id, reason=reason)
2021-10-15 14:08:27 +00:00
g.db.add(flag)
g.db.commit()
return {"message": "Comment reported!"}
@app.post('/del_report/<report_fn>')
2022-01-15 06:31:17 +00:00
@limiter.limit("1/second;30/minute;200/hour;1000/day")
2021-11-16 15:24:20 +00:00
@admin_level_required(2)
2021-10-15 14:08:27 +00:00
def remove_report(report_fn, v):
if report_fn.startswith('c'):
2022-01-02 00:06:46 +00:00
report = g.db.query(CommentFlag).filter_by(id=int(report_fn.lstrip('c'))).one_or_none()
2022-02-04 13:11:11 +00:00
ma=ModAction(
2022-02-04 13:11:14 +00:00
kind="delete_report",
2022-02-04 13:11:11 +00:00
user_id=v.id,
target_comment_id=report.comment_id
)
2021-10-15 14:08:27 +00:00
elif report_fn.startswith('p'):
2022-01-02 00:06:46 +00:00
report = g.db.query(Flag).filter_by(id=int(report_fn.lstrip('p'))).one_or_none()
2022-02-04 13:11:11 +00:00
ma=ModAction(
2022-02-04 13:11:14 +00:00
kind="delete_report",
2022-02-04 13:11:11 +00:00
user_id=v.id,
target_submission_id=report.post_id
)
else: return {"error": "Invalid report ID"}, 400
2021-10-15 14:08:27 +00:00
g.db.delete(report)
2022-02-04 13:11:11 +00:00
g.db.add(ma)
2021-10-15 14:08:27 +00:00
g.db.commit()
2021-09-03 13:52:37 +00:00
return {"message": "Removed report"}