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-09-28 19:45:17 +00:00
|
|
|
from sqlalchemy.orm import joinedload
|
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):
|
2021-08-23 17:48:55 +00:00
|
|
|
|
2021-07-25 15:05:14 +00:00
|
|
|
|
2021-09-19 13:11:34 +00:00
|
|
|
link = request.values.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-08-22 20:31:12 +00:00
|
|
|
@auth_required
|
2021-07-21 01:12:26 +00:00
|
|
|
@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-09-18 19:56:11 +00:00
|
|
|
existing = g.db.query(Vote).options(lazyload('*')).filter_by(user_id=v.id, submission_id=post.id).first()
|
2021-07-24 12:26:28 +00:00
|
|
|
|
2021-08-19 05:51:42 +00:00
|
|
|
if existing and existing.vote_type == new: return "", 204
|
2021-08-19 05:14:52 +00:00
|
|
|
|
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-09-13 18:28:20 +00:00
|
|
|
post.author.truecoins += 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-09-13 18:28:20 +00:00
|
|
|
post.author.truecoins -= 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-09-13 18:28:20 +00:00
|
|
|
post.author.truecoins += 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-09-20 19:39:42 +00:00
|
|
|
try:
|
|
|
|
g.db.flush()
|
2021-09-27 21:46:35 +00:00
|
|
|
post.upvotes = g.db.query(Vote.id).options(lazyload('*')).filter_by(submission_id=post.id, vote_type=1).count()
|
|
|
|
post.downvotes = g.db.query(Vote.id).options(lazyload('*')).filter_by(submission_id=post.id, vote_type=-1).count()
|
2021-09-20 19:39:42 +00:00
|
|
|
g.db.add(post)
|
|
|
|
g.db.commit()
|
|
|
|
except: g.db.rollback()
|
2021-07-21 01:12:26 +00:00
|
|
|
return "", 204
|
|
|
|
|
2021-07-31 04:48:47 +00:00
|
|
|
@app.post("/vote/comment/<comment_id>/<new>")
|
2021-08-22 20:31:12 +00:00
|
|
|
@auth_required
|
2021-07-21 01:12:26 +00:00
|
|
|
@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-09-18 19:56:11 +00:00
|
|
|
existing = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id).first()
|
2021-07-26 19:43:14 +00:00
|
|
|
|
2021-08-19 05:51:42 +00:00
|
|
|
if existing and existing.vote_type == new: return "", 204
|
2021-08-19 05:14:52 +00:00
|
|
|
|
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-09-13 18:28:20 +00:00
|
|
|
comment.author.truecoins += 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-09-13 18:28:20 +00:00
|
|
|
comment.author.truecoins -= 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-09-13 18:28:20 +00:00
|
|
|
comment.author.truecoins += 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-09-18 19:56:44 +00:00
|
|
|
|
2021-09-18 21:57:16 +00:00
|
|
|
try:
|
|
|
|
g.db.flush()
|
2021-09-27 21:46:35 +00:00
|
|
|
comment.upvotes = g.db.query(CommentVote.id).options(lazyload('*')).filter_by(comment_id=comment.id, vote_type=1).count()
|
|
|
|
comment.downvotes = g.db.query(CommentVote.id).options(lazyload('*')).filter_by(comment_id=comment.id, vote_type=-1).count()
|
2021-09-18 21:57:16 +00:00
|
|
|
g.db.add(comment)
|
|
|
|
g.db.commit()
|
2021-09-19 12:46:30 +00:00
|
|
|
except: g.db.rollback()
|
2021-10-05 23:11:17 +00:00
|
|
|
return "", 204
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/vote/poll/<comment_id>")
|
|
|
|
@auth_required
|
|
|
|
def api_vote_poll(comment_id, v):
|
|
|
|
|
|
|
|
vote = request.values.get("vote")
|
|
|
|
if vote == "true": new = 1
|
|
|
|
elif vote == "false": new = 0
|
|
|
|
else: abort(400)
|
|
|
|
|
|
|
|
comment_id = int(comment_id)
|
|
|
|
|
2021-10-05 23:16:31 +00:00
|
|
|
existing = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment_id).first()
|
2021-10-05 23:11:17 +00:00
|
|
|
|
|
|
|
if existing and existing.vote_type == vote: return "", 204
|
|
|
|
|
|
|
|
if existing:
|
|
|
|
existing.vote_type = new
|
|
|
|
g.db.add(existing)
|
|
|
|
else:
|
|
|
|
vote = CommentVote(user_id=v.id, vote_type=new, comment_id=comment_id)
|
|
|
|
g.db.add(vote)
|
|
|
|
|
|
|
|
g.db.flush()
|
|
|
|
comment.upvotes = g.db.query(CommentVote.id).options(lazyload('*')).filter_by(comment_id=comment.id, vote_type=1).count()
|
|
|
|
g.db.add(comment)
|
|
|
|
g.db.commit()
|
2021-08-19 05:14:52 +00:00
|
|
|
return "", 204
|