forked from rDrama/rDrama
1
0
Fork 0
rDrama/drama/routes/votes.py

172 lines
4.3 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
@app.route("/votes", methods=["GET"])
@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")
if not link: return render_template("admin/votes.html", v=v)
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)
return render_template("admin/votes.html",
v=v,
thing=thing,
ups=ups,
downs=downs,)
2021-07-26 19:40:00 +00:00
@app.route("/api/v1/vote/post/<post_id>/<new>", methods=["POST"])
@app.route("/api/vote/post/<post_id>/<new>", methods=["POST"])
2021-07-21 01:12:26 +00:00
@is_not_banned
@api("vote")
@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
2021-07-26 19:40:00 +00:00
if new==-1:
2021-07-21 01:12:26 +00:00
count=g.db.query(Vote).filter(
Vote.user_id.in_(
tuple(
[v.id]+[x.id for x in v.alts]
)
),
Vote.created_utc > (int(time.time())-3600),
Vote.vote_type==-1
).count()
if count >=15 and v.admin_level==0:
return jsonify({"error": "You're doing that too much. Try again later."}), 403
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)
2021-07-26 20:01:39 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
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-21 01:12:26 +00:00
submission_id=base36decode(post_id),
app_id=v.client.application.id if v.client else None
)
g.db.add(vote)
2021-07-26 20:01:39 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
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-26 19:40:00 +00:00
@app.route("/api/v1/vote/comment/<comment_id>/<new>", methods=["POST"])
@app.route("/api/vote/comment/<comment_id>/<new>", methods=["POST"])
2021-07-21 01:12:26 +00:00
@is_not_banned
@api("vote")
@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-26 19:40:00 +00:00
if new not in ["-1", "0", "1"]:
2021-07-21 01:12:26 +00:00
abort(400)
# disallow bots
if request.headers.get("X-User-Type","") == "Bot":
abort(403)
2021-07-26 19:40:00 +00:00
new = int(new)
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)
2021-07-26 20:01:39 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
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-21 01:12:26 +00:00
comment_id=base36decode(comment_id),
app_id=v.client.application.id if v.client else None
)
g.db.add(vote)
2021-07-26 20:01:39 +00:00
g.db.commit()
2021-07-21 01:12:26 +00:00
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)
return make_response(""), 204