From 820d8387f908b941f30f7c0c80a354c483a15a09 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Mon, 7 Feb 2022 13:39:26 +0200 Subject: [PATCH] xv --- files/classes/comment.py | 46 +++++++++++++++++++++--------- files/classes/submission.py | 37 ++++++++++++++++++------ files/helpers/const.py | 5 +++- files/routes/comments.py | 34 ++++++++++++++++++++-- files/routes/login.py | 4 +-- files/routes/oauth.py | 14 ++------- files/routes/posts.py | 34 +++++++++++++++++++--- files/routes/votes.py | 53 ++++++++++++++++++++++++----------- files/templates/comments.html | 2 +- files/templates/default.html | 2 +- files/templates/header.html | 4 +-- seed-db.sql | 4 +++ 12 files changed, 177 insertions(+), 62 deletions(-) diff --git a/files/classes/comment.py b/files/classes/comment.py index a2d9fba08..fab02df45 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -11,6 +11,7 @@ from files.helpers.const import * from files.helpers.lazy import lazy from .flags import CommentFlag from random import randint +from .votes import CommentVote class Comment(Base): @@ -79,22 +80,31 @@ class Comment(Base): @lazy def poll_voted(self, v): if v: - vote = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=self.id).one_or_none() - if vote: return vote.vote_type - else: return None - else: return None + vote = g.db.query(CommentVote.vote_type).filter_by(user_id=v.id, comment_id=self.id).one_or_none() + if vote: return vote[0] + return None @property @lazy def options(self): return [x for x in self.child_comments if x.author_id == AUTOPOLLER_ID] + @property + @lazy + def choices(self): + return [x for x in self.child_comments if x.author_id == AUTOCHOICE_ID] + def total_poll_voted(self, v): if v: for option in self.options: if option.poll_voted(v): return True return False + def total_choice_voted(self, v): + if v: + return g.db.query(CommentVote).filter(CommentVote.user_id == v.id, CommentVote.comment_id.in_(tuple(x.id for x in self.choices))).all() + return False + @property @lazy def controversial(self): @@ -198,12 +208,12 @@ class Comment(Base): @property def replies(self): if self.replies2 != None: return [x for x in self.replies2 if not x.author.shadowbanned] - return sorted((x for x in self.child_comments if x.author and not x.author.shadowbanned and x.author_id not in (AUTOPOLLER_ID, AUTOBETTER_ID)), key=lambda x: x.realupvotes, reverse=True) + return sorted((x for x in self.child_comments if x.author and not x.author.shadowbanned and x.author_id not in (AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), key=lambda x: x.realupvotes, reverse=True) @property def replies3(self): if self.replies2 != None: return self.replies2 - return sorted((x for x in self.child_comments if x.author_id not in (AUTOPOLLER_ID, AUTOBETTER_ID)), key=lambda x: x.realupvotes, reverse=True) + return sorted((x for x in self.child_comments if x.author_id not in (AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), key=lambda x: x.realupvotes, reverse=True) @property def replies2(self): @@ -361,15 +371,25 @@ class Comment(Base): g.db.add(self.author) g.db.commit() - for o in self.options: - body += f'
' + body += f'"> - {c.upvotes} votes' + curr = self.total_choice_voted(v) + if curr: + body += f'' + + for c in self.choices: + body += f'''
' if self.author.sig_html and not self.ghost and (self.author_id == MOOSE_ID or not (v and v.sigs_disabled)): body += f"
{self.author.sig_html}" diff --git a/files/classes/submission.py b/files/classes/submission.py index 1c6797b45..6b266df41 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -13,6 +13,7 @@ from .flags import Flag from .comment import Comment from flask import g from .sub import * +from .votes import CommentVote class Submission(Base): __tablename__ = "submissions" @@ -68,7 +69,7 @@ class Submission(Base): @property @lazy def comments2(self): - return g.db.query(Comment.author_id, Comment.created_utc, Comment.id).filter(Comment.parent_submission == self.id, Comment.author_id.notin_((AUTOPOLLER_ID,AUTOBETTER_ID))).all() + return g.db.query(Comment.author_id, Comment.created_utc, Comment.id).filter(Comment.parent_submission == self.id, Comment.author_id.notin_((AUTOPOLLER_ID,AUTOBETTER_ID, AUTOCHOICE_ID))).all() @property @lazy @@ -86,6 +87,11 @@ class Submission(Base): def options(self): return g.db.query(Comment).filter_by(parent_submission = self.id, author_id = AUTOPOLLER_ID, level=1) + @property + @lazy + def choices(self): + return g.db.query(Comment).filter_by(parent_submission = self.id, author_id = AUTOCHOICE_ID, level=1) + @property @lazy def bet_options(self): @@ -97,6 +103,11 @@ class Submission(Base): if option.poll_voted(v): return True return False + def total_choice_voted(self, v): + if v: + return g.db.query(CommentVote).filter(CommentVote.user_id == v.id, CommentVote.comment_id.in_(tuple(x.id for x in self.choices))).all() + return False + def total_bet_voted(self, v): if "closed" in self.body.lower(): return True if v: @@ -373,15 +384,25 @@ class Submission(Base): g.db.add(self.author) g.db.commit() - for o in self.options: - body += f'
' + body += f'"> - {c.upvotes} votes' + curr = self.total_choice_voted(v) + if curr: + body += f'' + + for c in self.choices: + body += f'''
' for c in self.bet_options: body += f'''
[^<\s+]|[^>\s+]<', body_html, re.A))): return {"error":"You can only type marseys!"}, 403 diff --git a/files/routes/login.py b/files/routes/login.py index 7c49ddfee..cf8c13f41 100644 --- a/files/routes/login.py +++ b/files/routes/login.py @@ -315,9 +315,9 @@ def sign_up_post(v): g.db.add(new_badge) - id_1 = g.db.query(User.id).filter_by(id=8).count() + id_1 = g.db.query(User.id).filter_by(id=9).count() users_count = g.db.query(User.id).count() - if id_1 == 0 and users_count == 7: admin_level=3 + if id_1 == 0 and users_count == 8: admin_level=3 else: admin_level=0 new_user = User( diff --git a/files/routes/oauth.py b/files/routes/oauth.py index e9bc3972d..fe635949c 100644 --- a/files/routes/oauth.py +++ b/files/routes/oauth.py @@ -5,7 +5,6 @@ from files.helpers.const import * from files.classes import * from flask import * from files.__main__ import app, limiter -from sqlalchemy.orm import joinedload @app.get("/authorize") @auth_required @@ -190,13 +189,9 @@ def admin_app_id(v, aid): aid=aid - oauth = g.db.query(OauthApp).options( - joinedload( - OauthApp.author)).filter_by( - id=aid).one_or_none() + oauth = g.db.query(OauthApp).filter_by(id=aid).one_or_none() - pids=oauth.idlist(page=int(request.values.get("page",1)), - ) + pids=oauth.idlist(page=int(request.values.get("page",1))) next_exists=len(pids)==101 pids=pids[:100] @@ -216,10 +211,7 @@ def admin_app_id_comments(v, aid): aid=aid - oauth = g.db.query(OauthApp).options( - joinedload( - OauthApp.author)).filter_by( - id=aid).one_or_none() + oauth = g.db.query(OauthApp).filter_by(id=aid).one_or_none() cids=oauth.comments_idlist(page=int(request.values.get("page",1)), ) diff --git a/files/routes/posts.py b/files/routes/posts.py index 12516d9b9..be0e9845d 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -148,7 +148,7 @@ def post_id(pid, anything=None, v=None, sub=None): if not (v and v.shadowbanned) and not (v and v.admin_level > 1): comments = comments.join(User, User.id == Comment.author_id).filter(User.shadowbanned == None) - comments=comments.filter(Comment.parent_submission == post.id, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID))).join( + comments=comments.filter(Comment.parent_submission == post.id, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID))).join( votes, votes.c.comment_id == Comment.id, isouter=True @@ -191,7 +191,7 @@ def post_id(pid, anything=None, v=None, sub=None): else: pinned = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.is_pinned != None).all() - comments = g.db.query(Comment).join(User, User.id == Comment.author_id).filter(User.shadowbanned == None, Comment.parent_submission == post.id, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID)), Comment.level == 1, Comment.is_pinned == None) + comments = g.db.query(Comment).join(User, User.id == Comment.author_id).filter(User.shadowbanned == None, Comment.parent_submission == post.id, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), Comment.level == 1, Comment.is_pinned == None) if sort == "new": comments = comments.order_by(Comment.created_utc.desc()) @@ -271,7 +271,7 @@ def viewmore(v, pid, sort, offset): votes.c.vote_type, blocking.c.id, blocked.c.id, - ).filter(Comment.parent_submission == pid, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID)), Comment.is_pinned == None, Comment.id.notin_(ids)) + ).filter(Comment.parent_submission == pid, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), Comment.is_pinned == None, Comment.id.notin_(ids)) if not (v and v.shadowbanned) and not (v and v.admin_level > 1): comments = comments.join(User, User.id == Comment.author_id).filter(User.shadowbanned == None) @@ -315,7 +315,7 @@ def viewmore(v, pid, sort, offset): second = [c[0] for c in comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None), func.length(Comment.body) <= 50).all()] comments = first + second else: - comments = g.db.query(Comment).join(User, User.id == Comment.author_id).filter(User.shadowbanned == None, Comment.parent_submission == pid, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID)), Comment.level == 1, Comment.is_pinned == None, Comment.id.notin_(ids)) + comments = g.db.query(Comment).join(User, User.id == Comment.author_id).filter(User.shadowbanned == None, Comment.parent_submission == pid, Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID)), Comment.level == 1, Comment.is_pinned == None, Comment.id.notin_(ids)) if sort == "new": comments = comments.order_by(Comment.created_utc.desc()) @@ -474,6 +474,18 @@ def edit_post(pid, v): ) g.db.add(c) + if not p.choices.count(): + for i in re.finditer('\s*##([^\$\n]+)##\s*', body, re.A): + body = body.replace(i.group(0), "") + c = Comment(author_id=AUTOCHOICE_ID, + parent_submission=p.id, + level=1, + body_html=filter_emojis_only(i.group(1)), + upvotes=0, + is_bot=True + ) + g.db.add(c) + body_html = sanitize(body, edit=True) bans = filter_comment_html(body_html) @@ -960,6 +972,11 @@ def submit_post(v, sub=None): options.append(i.group(1)) body = body.replace(i.group(0), "") + choices = [] + for i in re.finditer('\s*##([^\$\n]+)##\s*', body, re.A): + choices.append(i.group(1)) + body = body.replace(i.group(0), "") + if v.agendaposter and not v.marseyawarded: body = torture_ap(body, v.username) if request.files.get("file2") and request.headers.get("cf-ipcountry") != "T1": @@ -1047,7 +1064,16 @@ def submit_post(v, sub=None): upvotes=0, is_bot=True ) + g.db.add(c) + for choice in choices: + c = Comment(author_id=AUTOCHOICE_ID, + parent_submission=new_post.id, + level=1, + body_html=filter_emojis_only(choice), + upvotes=0, + is_bot=True + ) g.db.add(c) vote = Vote(user_id=v.id, diff --git a/files/routes/votes.py b/files/routes/votes.py index 9d464896d..8ad1e63e1 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -4,7 +4,6 @@ from files.helpers.const import * from files.classes import * from flask import * from files.__main__ import app, limiter, cache -from sqlalchemy.orm import joinedload from os import environ @app.get("/votes") @@ -29,30 +28,18 @@ def admin_vote_info_get(v): thing_id = g.db.query(Submission.id).filter_by(upvotes=thing.upvotes, downvotes=thing.downvotes).order_by(Submission.id).first()[0] else: thing_id = thing.id - ups = g.db.query(Vote - ).options(joinedload(Vote.user) - ).filter_by(submission_id=thing_id, vote_type=1 - ).order_by(Vote.id).all() + ups = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=1).order_by(Vote.id).all() - downs = g.db.query(Vote - ).options(joinedload(Vote.user) - ).filter_by(submission_id=thing_id, vote_type=-1 - ).order_by(Vote.id).all() + downs = g.db.query(Vote).filter_by(submission_id=thing_id, vote_type=-1).order_by(Vote.id).all() elif isinstance(thing, Comment): if thing.author.shadowbanned and not (v and v.admin_level): thing_id = g.db.query(Comment.id).filter_by(upvotes=thing.upvotes, downvotes=thing.downvotes).order_by(Comment.id).first()[0] else: thing_id = thing.id - ups = g.db.query(CommentVote - ).options(joinedload(CommentVote.user) - ).filter_by(comment_id=thing_id, vote_type=1 - ).order_by(CommentVote.id).all() + ups = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=1).order_by(CommentVote.id).all() - downs = g.db.query(CommentVote - ).options(joinedload(CommentVote.user) - ).filter_by(comment_id=thing_id, vote_type=-1 - ).order_by(CommentVote.id).all() + downs = g.db.query(CommentVote).filter_by(comment_id=thing_id, vote_type=-1 ).order_by(CommentVote.id).all() else: abort(400) @@ -248,4 +235,36 @@ def bet(comment_id, v): g.db.add(autobetter) g.db.commit() + return "", 204 + +@app.post("/vote/choice/") +@auth_required +def api_vote_choice(comment_id, v): + + comment_id = int(comment_id) + comment = get_comment(comment_id) + + existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).one_or_none() + + if existing and existing.vote_type == 1: return "", 204 + + if existing: + existing.vote_type = 1 + g.db.add(existing) + else: + vote = CommentVote(user_id=v.id, vote_type=1, comment_id=comment.id) + g.db.add(vote) + + if comment.parent_comment: parent = comment.parent_comment + else: parent = comment.post + + for vote in parent.total_choice_voted(v): + g.db.delete(vote) + + try: + g.db.flush() + comment.upvotes = g.db.query(CommentVote.id).filter_by(comment_id=comment.id, vote_type=1).count() + g.db.add(comment) + g.db.commit() + except: g.db.rollback() return "", 204 \ No newline at end of file diff --git a/files/templates/comments.html b/files/templates/comments.html index 789aaf15f..671ed96a9 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -908,7 +908,7 @@ {% if v %} - + {% endif %} diff --git a/files/templates/default.html b/files/templates/default.html index 6295f2c14..180b59c69 100644 --- a/files/templates/default.html +++ b/files/templates/default.html @@ -272,7 +272,7 @@ {% endblock %}
- {% if request.path in ('/', '/logged_out', '/logged_out/') or SITE_NAME == '2Much4You' %} + {% if request.path in ('/', '/logged_out', '/logged_out/') or sub %} {% block sidebar %} {% include "sidebar_" + SITE_NAME + ".html" %} {% endblock %} diff --git a/files/templates/header.html b/files/templates/header.html index 51480d203..9809fff65 100644 --- a/files/templates/header.html +++ b/files/templates/header.html @@ -1,6 +1,6 @@