rDrama/drama/routes/votes.py

153 lines
3.8 KiB
Python
Raw Normal View History

2021-07-22 19:19:49 +00:00
from drama.helpers.wrappers import *
from drama.helpers.get import *
from drama.classes import *
2021-07-21 01:12:26 +00:00
from flask import *
2021-07-22 19:19:49 +00:00
from drama.__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
ids = re.search("://[^/]+\w+/post/(\w+)/[^/]+(/(\w+))?", link)
try:
post_id = ids.group(1)
comment_id = ids.group(3)
except: abort(400)
if comment_id:
thing = get_comment(int(comment_id), v=v)
else:
thing = get_post(int(post_id), v=v)
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-07-26 00:33:02 +00:00
post.author.dramacoins += 1
g.db.add(post.author)
2021-07-26 19:43:14 +00:00
elif existing.vote_type != 0 and new == 0:
2021-07-26 00:33:02 +00:00
post.author.dramacoins -= 1
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-07-26 00:33:02 +00:00
post.author.dramacoins += 1
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-07-21 01:12:26 +00:00
app_id=v.client.application.id if v.client else None
)
g.db.add(vote)
2021-07-26 20:04:23 +00:00
g.db.flush()
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)
except: comment_id = int(comment_id, 36)
2021-07-30 05:54:18 +00:00
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-07-26 00:34:07 +00:00
comment.author.dramacoins += 1
g.db.add(comment.author)
2021-07-26 19:43:14 +00:00
elif existing.vote_type != 0 and new == 0:
2021-07-26 00:34:07 +00:00
comment.author.dramacoins -= 1
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-07-26 00:34:07 +00:00
comment.author.dramacoins += 1
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-07-21 01:12:26 +00:00
app_id=v.client.application.id if v.client else None
)
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