MarseyWorld/files/routes/flagging.py

82 lines
2.3 KiB
Python
Raw Normal View History

2021-08-04 15:35:10 +00:00
from files.helpers.wrappers import *
from files.helpers.get import *
2021-07-21 01:12:26 +00:00
from flask import g
2021-08-04 15:35:10 +00:00
from files.__main__ import app
from files.helpers.sanitize import sanitize
2021-08-15 03:17:45 +00:00
from os import path
2021-07-21 01:12:26 +00:00
2021-07-31 04:48:47 +00:00
@app.post("/flag/post/<pid>")
2021-07-21 01:12:26 +00:00
@auth_desired
def api_flag_post(pid, v):
post = get_post(pid)
if v:
2021-08-05 14:58:25 +00:00
existing = g.db.query(Flag).filter_by(user_id=v.id, post_id=post.id).first()
2021-07-21 01:12:26 +00:00
2021-08-02 14:39:59 +00:00
if existing: return "", 409
2021-08-15 03:17:45 +00:00
reason = request.form.get("reason", "").strip()[:100]
2021-08-19 16:10:44 +00:00
if "<" in reason: return {"error": f"Reasons can't contain <"}
2021-08-15 03:17:45 +00:00
for i in re.finditer(':(.{1,30}?):', reason):
if path.isfile(f'./files/assets/images/emojis/{i.group(1)}.gif'):
2021-09-03 14:08:32 +00:00
reason = reason.replace(f':{i.group(1)}:', f'<img loading="lazy" data-toggle="tooltip" title="{i.group(1)}" delay="0" height=20 src="https://{site}/assets/images/emojis/{i.group(1)}.gif"<span>')
2021-08-02 14:39:59 +00:00
2021-07-21 01:12:26 +00:00
flag = Flag(post_id=post.id,
user_id=v.id,
2021-07-24 18:15:26 +00:00
reason=reason,
2021-07-21 01:12:26 +00:00
)
g.db.add(flag)
2021-09-08 11:05:31 +00:00
return {"message": "Post flagged!"}
2021-07-21 01:12:26 +00:00
2021-07-31 04:48:47 +00:00
@app.post("/flag/comment/<cid>")
2021-07-21 01:12:26 +00:00
@auth_desired
def api_flag_comment(cid, v):
comment = get_comment(cid)
if v:
existing = g.db.query(CommentFlag).filter_by(
2021-08-01 04:25:26 +00:00
user_id=v.id, comment_id=comment.id).first()
2021-07-21 01:12:26 +00:00
2021-08-02 14:39:59 +00:00
if existing: return "", 409
2021-08-15 03:17:45 +00:00
reason = request.form.get("reason", "").strip()[:100]
2021-08-19 16:10:44 +00:00
if "<" in reason: return {"error": f"Reasons can't contain <"}
2021-08-15 03:17:45 +00:00
for i in re.finditer(':(.{1,30}?):', reason):
if path.isfile(f'./files/assets/images/emojis/{i.group(1)}.gif'):
2021-09-03 14:08:32 +00:00
reason = reason.replace(f':{i.group(1)}:', f'<img loading="lazy" data-toggle="tooltip" title="{i.group(1)}" delay="0" height=20 src="https://{site}/assets/images/emojis/{i.group(1)}.gif"<span>')
2021-07-21 01:12:26 +00:00
2021-08-15 22:37:35 +00:00
flag = CommentFlag(comment_id=comment.id,
2021-08-15 22:35:54 +00:00
user_id=v.id,
reason=reason,
)
2021-07-21 01:12:26 +00:00
g.db.add(flag)
2021-09-08 11:05:31 +00:00
return {"message": "Comment flagged!"}
2021-08-25 12:15:53 +00:00
2021-08-25 12:22:20 +00:00
@app.post('/del_report/<report_fn>')
2021-08-25 12:15:53 +00:00
@auth_required
@validate_formkey
def remove_report(report_fn, v):
if v.admin_level < 6:
return {"error": "go outside"}, 403
if report_fn.startswith('c'):
report = g.db.query(CommentFlag).filter_by(id=int(report_fn.lstrip('c'))).first()
elif report_fn.startswith('p'):
report = g.db.query(Flag).filter_by(id=int(report_fn.lstrip('p'))).first()
else:
return {"error": "Invalid report ID"}, 400
g.db.delete(report)
2021-09-03 13:52:37 +00:00
return {"message": "Removed report"}