diff --git a/files/classes/comment.py b/files/classes/comment.py index f3388ba5de..d16b8e0fea 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 @@ -37,7 +38,7 @@ class Comment(Base): notifiedto=Column(Integer) app_id = Column(Integer, ForeignKey("oauth_apps.id")) oauth_app = relationship("OauthApp", viewonly=True) - upvotes = Column(Integer, default=1) + upvotes = Column(Integer, default=0) downvotes = Column(Integer, default=0) body = deferred(Column(String(10000))) body_html = deferred(Column(String(20000))) @@ -61,6 +62,13 @@ class Comment(Base): return f"" + def poll_voted(self, v): + if 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 + else: return None + @property @lazy def created_datetime(self): diff --git a/files/classes/submission.py b/files/classes/submission.py index f41e3a4965..a201c56db5 100644 --- a/files/classes/submission.py +++ b/files/classes/submission.py @@ -4,9 +4,9 @@ from sqlalchemy.orm import relationship, deferred import re, random from urllib.parse import urlparse from files.helpers.lazy import lazy -from files.helpers.const import SLURS +from files.helpers.const import SLURS, AUTOPOLLER_ACCOUNT 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 = AUTOPOLLER_ACCOUNT) + + @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/helpers/const.py b/files/helpers/const.py index 06947ce038..2d0a026cc6 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -165,6 +165,7 @@ NOTIFICATIONS_ACCOUNT = 1046 if site == "pcmemes.net": AUTOJANNY_ACCOUNT = 1050 else: AUTOJANNY_ACCOUNT = 2360 LONGPOSTBOT_ACCOUNT = 1832 +AUTOPOLLER_ACCOUNT = 6176 PUSHER_INSTANCE_ID = '02ddcc80-b8db-42be-9022-44c546b4dce6' PUSHER_KEY = environ.get("PUSHER_KEY", "").strip() diff --git a/files/routes/comments.py b/files/routes/comments.py index 9133283c1d..56a0fe78ab 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -286,6 +286,7 @@ def api_comment(v): body=body[:10000] ) + c.upvotes = 1 g.db.add(c) g.db.flush() @@ -562,13 +563,14 @@ def api_comment(v): ) g.db.add(vote) + cache.delete_memoized(comment_idlist) v.comment_count = g.db.query(Comment.id).options(lazyload('*')).filter(Comment.author_id == v.id, Comment.parent_submission != None).filter_by(is_banned=False, deleted_utc=0).count() g.db.add(v) - parent_post.comment_count = g.db.query(Comment.id).options(lazyload('*')).filter_by(parent_submission=parent_post.id).count() + parent_post.comment_count += 1 g.db.add(parent_post) c.voted = 1 diff --git a/files/routes/posts.py b/files/routes/posts.py index cc91612fb6..2f4086fb65 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 != AUTOPOLLER_ACCOUNT, ).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 != AUTOPOLLER_ACCOUNT, 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 = 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=AUTOPOLLER_ACCOUNT, + 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 8aba1347dc..aa76f21ce0 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -160,4 +160,34 @@ 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) + comment = get_comment(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 diff --git a/files/templates/comments.html b/files/templates/comments.html index 012e955b60..c8d8e9a960 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -33,14 +33,14 @@ {% if v %} {% include "award_modal.html" %} - + {% endif %} {% if v and v.admin_level == 6 %} - + {% endif %} +