From 2f708e34bdf71aac642cabede462aa544cb9f763 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Wed, 6 Oct 2021 01:11:17 +0200 Subject: [PATCH] d --- files/classes/comment.py | 8 +++++++- files/classes/submission.py | 18 ++++++++++++++---- files/routes/posts.py | 23 +++++++++++++++++++++-- files/routes/votes.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/files/classes/comment.py b/files/classes/comment.py index f3388ba5d..fc6f68921 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -2,7 +2,8 @@ import re from urllib.parse import urlencode, urlparse, parse_qs from flask import * from sqlalchemy import * -from sqlalchemy.orm import relationship, deferred +from sqlalchemy.orm import relationship, deferred, lazyload +from files.classes.votes import CommentVote from files.helpers.lazy import lazy from files.helpers.const import SLURS from files.__main__ import Base @@ -61,6 +62,11 @@ class Comment(Base): return f"" + def poll_voted(self, v): + vote = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=self.id).first() + if vote: return vote.vote_type + else: return None + @property @lazy def created_datetime(self): diff --git a/files/classes/submission.py b/files/classes/submission.py index f41e3a496..52fd00661 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -6,7 +6,7 @@ from urllib.parse import urlparse from files.helpers.lazy import lazy from files.helpers.const import SLURS from files.__main__ import Base -from .flags import * +from .flags import Flag from os import environ import time @@ -35,11 +35,8 @@ class Submission(Base): private = Column(Boolean, default=False) club = Column(Boolean, default=False) comment_count = Column(Integer, default=0) - comments = relationship("Comment", primaryjoin="Comment.parent_submission==Submission.id", viewonly=True) - flags = relationship("Flag", lazy="dynamic", viewonly=True) is_approved = Column(Integer, ForeignKey("users.id"), default=0) over_18 = Column(Boolean, default=False) - author = relationship("User", primaryjoin="Submission.author_id==User.id") is_bot = Column(Boolean, default=False) upvotes = Column(Integer, default=1) downvotes = Column(Integer, default=0) @@ -52,6 +49,9 @@ class Submission(Base): ban_reason = Column(String(128)) embed_url = Column(String(256)) + comments = relationship("Comment", lazy="dynamic", primaryjoin="Comment.parent_submission==Submission.id", viewonly=True) + flags = relationship("Flag", lazy="dynamic", viewonly=True) + author = relationship("User", primaryjoin="Submission.author_id==User.id") oauth_app = relationship("OauthApp", viewonly=True) approved_by = relationship("User", uselist=False, primaryjoin="Submission.is_approved==User.id", viewonly=True) awards = relationship("AwardRelationship", viewonly=True) @@ -71,6 +71,16 @@ class Submission(Base): return f"" + @property + @lazy + def options(self): + return self.comments.filter_by(author_id = 3369) + + @property + @lazy + def created_datetime(self): + return str(time.strftime("%d/%B/%Y %H:%M:%S UTC", time.gmtime(self.created_utc))) + @property @lazy def created_datetime(self): diff --git a/files/routes/posts.py b/files/routes/posts.py index cc91612fb..89caa84b9 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -123,7 +123,8 @@ def post_id(pid, anything=None, v=None): comments = comments.filter(Comment.author_id.notin_(shadowbanned)) comments=comments.filter( - Comment.parent_submission == post.id + Comment.parent_submission == post.id, + Comment.author_id != 3369, ).join( votes, votes.c.comment_id == Comment.id, @@ -161,7 +162,7 @@ def post_id(pid, anything=None, v=None): else: shadowbanned = [x[0] for x in g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned != None).all()] - comments = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.author_id.notin_(shadowbanned)) + comments = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.author_id != 3369, Comment.author_id.notin_(shadowbanned)) if sort == "new": comments = comments.order_by(Comment.created_utc.desc()) @@ -730,9 +731,17 @@ def submit_post(v): for i in re.finditer('^(https:\/\/.*\.(png|jpg|jpeg|gif|webp|PNG|JPG|JPEG|GIF|WEBP|9999))', body, re.MULTILINE): if "wikipedia" not in i.group(1): body = body.replace(i.group(1), f'![]({i.group(1)})') body = re.sub('([^\n])\n([^\n])', r'\1\n\n\2', body) + + options = [] + for i in re.finditer('\s*\$([^\$]+)\$\s*', body): + options.append(i.group(1)) + body.replace(i.group(0), "") + body_md = CustomRenderer().render(mistletoe.Document(body)) body_html = sanitize(body_md) + + if len(body_html) > 20000: abort(400) # Run safety filter @@ -806,6 +815,16 @@ def submit_post(v): g.db.add(new_post) g.db.flush() + for option in options: + c = Comment(author_id=3369, + parent_submission=new_post.id, + level=1, + body=option, + ) + + g.db.add(c) + g.db.flush() + vote = Vote(user_id=v.id, vote_type=1, submission_id=new_post.id diff --git a/files/routes/votes.py b/files/routes/votes.py index 8aba1347d..8dd9c99c7 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -160,4 +160,33 @@ def api_vote_comment(comment_id, new, v): g.db.add(comment) g.db.commit() except: g.db.rollback() + return "", 204 + + +@app.post("/vote/poll/") +@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) + + existing = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id).first() + + 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() return "", 204 \ No newline at end of file