rDrama/files/routes/awards.py

222 lines
6.0 KiB
Python
Raw Normal View History

2021-08-04 15:35:10 +00:00
from files.__main__ import app
from files.helpers.wrappers import *
from files.helpers.alerts import *
from files.helpers.get import *
from files.classes.award import *
2021-07-31 05:33:32 +00:00
from flask import g, request
2021-07-27 11:06:52 +00:00
def banaward_trigger(post=None, comment=None):
author = post.author if post else comment.author
2021-07-27 21:07:41 +00:00
link = f"[this post]({post.permalink})" if post else f"[this comment]({comment.permalink})"
2021-07-27 11:06:52 +00:00
2021-07-27 21:07:41 +00:00
if author.admin_level < 1:
if not author.is_suspended:
author.ban(reason="1-day ban award used", days=1)
2021-07-27 11:06:52 +00:00
2021-08-04 16:00:57 +00:00
send_notification(1046, author, f"Your account has been suspended for a day for {link}. It sucked and you should feel bad.")
2021-07-27 21:07:41 +00:00
elif author.unban_utc > 0:
author.unban_utc += 24*60*60
g.db.add(author)
2021-07-27 11:06:52 +00:00
2021-08-04 16:00:57 +00:00
send_notification(1046, author, f"Your account has been suspended for yet another day for {link}. Seriously man?")
2021-07-27 11:06:52 +00:00
ACTIONS = {
"ban": banaward_trigger
}
2021-07-27 21:07:41 +00:00
ALLOW_MULTIPLE = (
"ban",
)
2021-07-27 11:06:52 +00:00
2021-07-31 04:48:47 +00:00
@app.get("/awards")
2021-07-27 11:06:52 +00:00
@auth_required
def get_awards(v):
return_value = list(AWARDS.values())
user_awards = v.awards
for val in return_value:
val['owned'] = len([x for x in user_awards if x.kind == val['kind'] and not x.given])
2021-07-31 06:09:00 +00:00
return jsonify(return_value)
2021-07-27 11:06:52 +00:00
2021-07-31 04:48:47 +00:00
@app.put("/post/<pid>/awards")
2021-07-27 11:06:52 +00:00
@auth_required
@validate_formkey
def award_post(pid, v):
if v.is_suspended and v.unban_utc == 0:
2021-07-31 05:28:05 +00:00
return {"error": "forbidden"}, 403
2021-07-27 11:06:52 +00:00
kind = request.form.get("kind", "")
if kind not in AWARDS:
2021-07-31 05:28:05 +00:00
return {"error": "That award doesn't exist."}, 404
2021-07-27 11:06:52 +00:00
post_award = g.db.query(AwardRelationship).filter(
and_(
AwardRelationship.kind == kind,
AwardRelationship.user_id == v.id,
AwardRelationship.submission_id == None,
AwardRelationship.comment_id == None
)
).first()
if not post_award:
2021-07-31 05:28:05 +00:00
return {"error": "You don't have that award."}, 404
2021-07-27 11:06:52 +00:00
post = g.db.query(Submission).filter_by(id=pid).first()
if not post or post.is_banned or post.deleted_utc > 0:
2021-07-31 05:28:05 +00:00
return {"error": "That post doesn't exist or has been deleted or removed."}, 404
2021-07-27 11:06:52 +00:00
if post.author_id == v.id:
2021-07-31 05:28:05 +00:00
return {"error": "You can't award yourself."}, 403
2021-07-27 11:06:52 +00:00
2021-07-27 21:16:32 +00:00
existing_award = g.db.query(AwardRelationship).filter(
and_(
AwardRelationship.submission_id == post.id,
AwardRelationship.user_id == v.id,
AwardRelationship.kind == kind
)
).first()
if existing_award and kind not in ALLOW_MULTIPLE:
2021-07-31 05:28:05 +00:00
return {"error": "You can't give that award multiple times to the same post."}, 409
2021-07-27 11:06:52 +00:00
post_award.submission_id = post.id
2021-07-27 21:17:01 +00:00
#print(f"give award to pid {post_award.submission_id} ({post.id})")
2021-07-27 11:06:52 +00:00
g.db.add(post_award)
2021-07-27 16:56:43 +00:00
msg = f"@{v.username} has given your [post]({post.permalink}) the {AWARDS[kind]['title']} Award!"
2021-07-27 11:06:52 +00:00
note = request.form.get("note", "")
if note:
msg += f"\n\n> {note}"
send_notification(1046, post.author, msg)
if kind in ACTIONS:
ACTIONS[kind](post=post)
return "", 204
2021-07-31 04:48:47 +00:00
@app.put("/comment/<cid>/awards")
2021-07-27 11:06:52 +00:00
@auth_required
@validate_formkey
def award_comment(cid, v):
if v.is_suspended and v.unban_utc == 0:
2021-07-31 05:28:05 +00:00
return {"error": "forbidden"}, 403
2021-07-27 11:06:52 +00:00
kind = request.form.get("kind", "")
if kind not in AWARDS:
2021-07-31 05:28:05 +00:00
return {"error": "That award doesn't exist."}, 404
2021-07-27 11:06:52 +00:00
comment_award = g.db.query(AwardRelationship).filter(
and_(
AwardRelationship.kind == kind,
AwardRelationship.user_id == v.id,
AwardRelationship.submission_id == None,
AwardRelationship.comment_id == None
)
).first()
if not comment_award:
2021-07-31 05:28:05 +00:00
return {"error": "You don't have that award."}, 404
2021-07-27 11:06:52 +00:00
c = g.db.query(Comment).filter_by(id=cid).first()
if not c or c.is_banned or c.deleted_utc > 0:
2021-07-31 05:28:05 +00:00
return {"error": "That comment doesn't exist or has been deleted or removed."}, 404
2021-07-27 11:06:52 +00:00
if c.author_id == v.id:
2021-07-31 05:28:05 +00:00
return {"error": "You can't award yourself."}, 403
2021-07-27 11:06:52 +00:00
2021-07-27 21:16:32 +00:00
existing_award = g.db.query(AwardRelationship).filter(
and_(
AwardRelationship.comment_id == c.id,
AwardRelationship.user_id == v.id,
AwardRelationship.kind == kind
)
).first()
2021-07-27 11:06:52 +00:00
2021-07-27 21:16:32 +00:00
if existing_award and kind not in ALLOW_MULTIPLE:
2021-07-31 05:28:05 +00:00
return {"error": "You can't give that award multiple times to the same comment."}, 409
2021-07-27 11:06:52 +00:00
comment_award.comment_id = c.id
g.db.add(comment_award)
2021-07-27 16:56:43 +00:00
msg = f"@{v.username} has given your [comment]({c.permalink}) the {AWARDS[kind]['title']} Award!"
2021-07-27 11:06:52 +00:00
note = request.form.get("note", "")
if note:
msg += f"\n\n> {note}"
send_notification(1046, c.author, msg)
if kind in ACTIONS:
ACTIONS[kind](comment=c)
return "", 204
2021-07-27 22:28:21 +00:00
@app.get("/admin/user_award")
@auth_required
def admin_userawards_get(v):
if v.admin_level < 6:
abort(403)
return render_template("admin/user_award.html", awards=list(AWARDS.values()), v=v)
@app.post("/admin/user_award")
@auth_required
@validate_formkey
def admin_userawards_post(v):
if v.admin_level < 6:
abort(403)
u = get_user(request.form.get("username", '1'), graceful=False, v=v)
awards = []
2021-07-30 16:44:39 +00:00
notify_awards = {}
2021-07-27 22:28:21 +00:00
latest = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).first()
thing = latest.id
for key, value in request.form.items():
if key not in AWARDS:
continue
if value:
2021-07-30 16:44:39 +00:00
if int(value) > 0:
notify_awards[key] = int(value)
2021-07-27 22:28:21 +00:00
for x in range(int(value)):
thing += 1
awards.append(AwardRelationship(
id=thing,
user_id=u.id,
kind=key
))
g.db.bulk_save_objects(awards)
2021-07-30 16:44:39 +00:00
text = "You were given the following awards:\n\n"
for key, value in notify_awards.items():
text += f" - **{value}** {AWARDS[key]['title']} {'Awards' if value != 1 else 'Award'}\n"
2021-07-30 16:44:54 +00:00
send_notification(1046, u, text)
2021-07-27 22:28:21 +00:00
2021-07-30 15:42:02 +00:00
return render_template("admin/user_award.html", awards=list(AWARDS.values()), v=v)