MarseyWorld/files/routes/reporting.py

91 lines
2.6 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
@app.post("/flag/post/<pid>")
@limiter.limit("1/second")
@auth_desired
def api_flag_post(pid, v):
post = get_post(pid)
if v and not v.shadowbanned:
2021-11-06 15:52:48 +00:00
existing = g.db.query(Flag.id).filter_by(user_id=v.id, post_id=post.id).first()
2021-10-15 14:08:27 +00:00
if existing: return "", 409
reason = request.values.get("reason", "").strip()[:100]
if "<" in reason: return {"error": f"Reasons can't contain <"}
for i in re.finditer(':(.{1,30}?):', reason):
if path.isfile(f'./files/assets/images/emojis/{i.group(1)}.webp'):
2021-12-05 19:45:08 +00:00
reason = reason.replace(f':{i.group(1)}:', f'<img loading="lazy" data-bs-toggle="tooltip" alt=":{i.group(1)}:" title=":{i.group(1)}:" delay="0" height=20 src="https://{site}/assets/images/emojis/{i.group(1)}.webp">')
2021-11-22 14:16:58 +00:00
if len(reason) > 350: return {"error": f"Too long."}
2021-10-15 14:08:27 +00:00
flag = Flag(post_id=post.id,
user_id=v.id,
reason=reason,
)
g.db.add(flag)
g.db.commit()
return {"message": "Post reported!"}
@app.post("/flag/comment/<cid>")
@limiter.limit("1/second")
@auth_desired
def api_flag_comment(cid, v):
comment = get_comment(cid)
if v and not v.shadowbanned:
2021-11-06 15:52:48 +00:00
existing = g.db.query(CommentFlag.id).filter_by(
2021-10-15 14:08:27 +00:00
user_id=v.id, comment_id=comment.id).first()
if existing: return "", 409
reason = request.values.get("reason", "").strip()[:100]
if "<" in reason: return {"error": f"Reasons can't contain <"}
for i in re.finditer(':(.{1,30}?):', reason):
if path.isfile(f'./files/assets/images/emojis/{i.group(1)}.webp'):
2021-12-05 19:45:08 +00:00
reason = reason.replace(f':{i.group(1)}:', f'<img loading="lazy" data-bs-toggle="tooltip" alt=":{i.group(1)}:" title=":{i.group(1)}:" delay="0" height=20 src="https://{site}/assets/images/emojis/{i.group(1)}.webp">')
2021-11-22 14:16:58 +00:00
if len(reason) > 350: return {"error": f"Too long."}
2021-10-15 14:08:27 +00:00
flag = CommentFlag(comment_id=comment.id,
user_id=v.id,
reason=reason,
)
g.db.add(flag)
g.db.commit()
return {"message": "Comment reported!"}
@app.post('/del_report/<report_fn>')
@limiter.limit("1/second")
2021-11-16 15:24:20 +00:00
@admin_level_required(2)
2021-10-15 14:08:27 +00:00
@validate_formkey
def remove_report(report_fn, v):
if report_fn.startswith('c'):
2021-11-06 15:52:48 +00:00
report = g.db.query(CommentFlag).filter_by(id=int(report_fn.lstrip('c'))).first()
2021-10-15 14:08:27 +00:00
elif report_fn.startswith('p'):
2021-11-06 15:52:48 +00:00
report = g.db.query(Flag).filter_by(id=int(report_fn.lstrip('p'))).first()
2021-10-15 14:08:27 +00:00
else:
return {"error": "Invalid report ID"}, 400
g.db.delete(report)
g.db.commit()
2021-09-03 13:52:37 +00:00
return {"message": "Removed report"}