rDrama/files/routes/votes.py

148 lines
3.8 KiB
Python
Raw Normal View History

2021-08-04 15:35:10 +00:00
from files.helpers.wrappers import *
from files.helpers.get import *
from files.classes import *
2021-07-21 01:12:26 +00:00
from flask import *
2021-08-04 15:35:10 +00:00
from files.__main__ import app
2021-07-21 01:12:26 +00:00
2021-07-25 15:05:14 +00:00
2021-07-27 22:31:28 +00:00
@app.get("/votes")
2021-07-25 15:05:14 +00:00
@auth_desired
def admin_vote_info_get(v):
if v and v.is_banned and not v.unban_utc: return render_template("seized.html")
link = request.args.get("link")
2021-07-30 11:05:20 +00:00
if not link: return render_template("votes.html", v=v)
2021-07-25 15:05:14 +00:00
2021-08-03 06:19:33 +00:00
try:
if "t2_" in link: thing = get_post(int(link.split("t2_")[1]), v=v)
elif "t3_" in link: thing = get_comment(int(link.split("t3_")[1]), v=v)
else: abort(400)
except: abort(400)
2021-07-25 15:05:14 +00:00
if isinstance(thing, Submission):
ups = g.db.query(Vote
).options(joinedload(Vote.user)
).filter_by(submission_id=thing.id, vote_type=1
).all()
downs = g.db.query(Vote
).options(joinedload(Vote.user)
).filter_by(submission_id=thing.id, vote_type=-1
).all()
elif isinstance(thing, Comment):
ups = g.db.query(CommentVote
).options(joinedload(CommentVote.user)
).filter_by(comment_id=thing.id, vote_type=1
).all()
downs = g.db.query(CommentVote
).options(joinedload(CommentVote.user)
).filter_by(comment_id=thing.id, vote_type=-1
).all()
else:
abort(400)
2021-07-30 11:05:20 +00:00
return render_template("votes.html",
2021-07-25 15:05:14 +00:00
v=v,
thing=thing,
ups=ups,
downs=downs,)
2021-07-31 04:48:47 +00:00
@app.post("/vote/post/<post_id>/<new>")
2021-07-21 01:12:26 +00:00
@is_not_banned
@validate_formkey
2021-07-26 19:40:00 +00:00
def api_vote_post(post_id, new, v):
2021-07-21 01:12:26 +00:00
2021-07-26 19:40:00 +00:00
if new not in ["-1", "0", "1"]: abort(400)
2021-07-21 01:12:26 +00:00
# disallow bots
2021-07-24 12:26:28 +00:00
if request.headers.get("X-User-Type","") == "Bot": abort(403)
2021-07-21 01:12:26 +00:00
2021-07-26 19:40:00 +00:00
new = int(new)
2021-07-21 01:12:26 +00:00
post = get_post(post_id)
# check for existing vote
2021-07-24 12:26:28 +00:00
existing = g.db.query(Vote).filter_by(user_id=v.id, submission_id=post.id).first()
2021-07-21 01:12:26 +00:00
if existing:
2021-07-26 19:43:14 +00:00
if existing.vote_type == 0 and new != 0:
2021-08-04 16:00:57 +00:00
post.author.coins += 1
2021-07-26 00:33:02 +00:00
g.db.add(post.author)
2021-07-26 19:43:14 +00:00
elif existing.vote_type != 0 and new == 0:
2021-08-04 16:00:57 +00:00
post.author.coins -= 1
2021-07-26 00:33:02 +00:00
g.db.add(post.author)
2021-07-26 19:40:00 +00:00
existing.vote_type = new
2021-07-21 01:12:26 +00:00
g.db.add(existing)
else:
2021-07-26 19:40:00 +00:00
if new != 0:
2021-08-04 16:00:57 +00:00
post.author.coins += 1
2021-07-26 00:33:02 +00:00
g.db.add(post.author)
2021-07-21 01:12:26 +00:00
vote = Vote(user_id=v.id,
2021-07-26 19:40:00 +00:00
vote_type=new,
2021-07-30 05:31:38 +00:00
submission_id=post_id,
2021-08-03 17:33:13 +00:00
app_id=v.client.application.id if v.client else None
2021-07-21 01:12:26 +00:00
)
g.db.add(vote)
2021-07-26 20:04:23 +00:00
2021-08-01 08:26:44 +00:00
try: g.db.flush()
except: g.db.rollback()
2021-07-26 17:00:29 +00:00
post.upvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=1).count()
post.downvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=-1).count()
2021-07-21 01:12:26 +00:00
g.db.add(post)
return "", 204
2021-07-31 04:48:47 +00:00
@app.post("/vote/comment/<comment_id>/<new>")
2021-07-21 01:12:26 +00:00
@is_not_banned
@validate_formkey
2021-07-26 19:40:00 +00:00
def api_vote_comment(comment_id, new, v):
2021-07-21 01:12:26 +00:00
2021-07-30 05:52:32 +00:00
if new not in ["-1", "0", "1"]: abort(400)
2021-07-21 01:12:26 +00:00
2021-07-30 05:52:32 +00:00
if request.headers.get("X-User-Type","") == "Bot": abort(403)
2021-07-21 01:12:26 +00:00
2021-07-26 19:40:00 +00:00
new = int(new)
2021-07-21 01:12:26 +00:00
2021-07-30 06:17:38 +00:00
try: comment_id = int(comment_id)
2021-07-31 06:59:18 +00:00
except:
try: comment_id = int(comment_id, 36)
except: abort(404)
2021-07-21 01:12:26 +00:00
comment = get_comment(comment_id)
# check for existing vote
2021-07-26 19:43:14 +00:00
existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).first()
2021-07-21 01:12:26 +00:00
if existing:
2021-07-26 19:43:14 +00:00
if existing.vote_type == 0 and new != 0:
2021-08-04 16:00:57 +00:00
comment.author.coins += 1
2021-07-26 00:34:07 +00:00
g.db.add(comment.author)
2021-07-26 19:43:14 +00:00
elif existing.vote_type != 0 and new == 0:
2021-08-04 16:00:57 +00:00
comment.author.coins -= 1
2021-07-26 00:34:07 +00:00
g.db.add(comment.author)
2021-07-26 19:58:24 +00:00
existing.vote_type = new
2021-07-21 01:12:26 +00:00
g.db.add(existing)
else:
2021-07-26 19:40:00 +00:00
if new != 0:
2021-08-04 16:00:57 +00:00
comment.author.coins += 1
2021-07-26 00:34:07 +00:00
g.db.add(comment.author)
2021-07-21 01:12:26 +00:00
vote = CommentVote(user_id=v.id,
2021-07-26 19:47:56 +00:00
vote_type=new,
2021-07-30 05:31:38 +00:00
comment_id=comment_id,
2021-08-03 17:33:13 +00:00
app_id=v.client.application.id if v.client else None
2021-07-21 01:12:26 +00:00
)
g.db.add(vote)
2021-07-26 20:04:23 +00:00
2021-07-28 05:04:30 +00:00
try: g.db.flush()
except: g.db.rollback()
2021-07-26 16:45:06 +00:00
comment.upvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=1).count()
comment.downvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=-1).count()
2021-07-21 01:12:26 +00:00
g.db.add(comment)
2021-07-26 20:01:48 +00:00
return make_response(""), 204