From 8ecb4582d0667883d1b7a035b6bd988184969247 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Wed, 22 Jun 2022 21:50:20 +0200 Subject: [PATCH] refactor sorting comments --- files/classes/clients.py | 2 +- files/classes/comment.py | 34 +++++++++++++++++++++------ files/classes/user.py | 2 +- files/routes/front.py | 11 +-------- files/routes/posts.py | 44 ++++------------------------------- files/routes/search.py | 11 +-------- files/routes/users.py | 19 ++++----------- files/templates/comments.html | 4 ++-- 8 files changed, 42 insertions(+), 85 deletions(-) diff --git a/files/classes/clients.py b/files/classes/clients.py index 2df38f4ce..fc9ee3358 100644 --- a/files/classes/clients.py +++ b/files/classes/clients.py @@ -54,7 +54,7 @@ class OauthApp(Base): posts = g.db.query(Comment.id).filter_by(app_id=self.id) - posts=posts.order_by(Comment.created_utc.desc()) + posts=posts.order_by(Comment.id.desc()) posts=posts.offset(100*(page-1)).limit(101) diff --git a/files/classes/comment.py b/files/classes/comment.py index 72f866d54..e478d92ee 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -14,6 +14,22 @@ from random import randint from .votes import CommentVote from math import floor + +def sort_comments(sort, comments): + + if sort == 'new': + order = Comment.id.desc() + elif sort == 'old': + order = comment.id + elif sort == 'controversial': + order = (Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc() + elif sort == "bottom": + order = Comment.realupvotes + else: + order = Comment.realupvotes.desc() + + return comments.order_by(order) + class Comment(Base): __tablename__ = "comments" @@ -208,26 +224,30 @@ class Comment(Base): else: return g.db.get(Comment, self.parent_comment_id) - @property @lazy def parent_fullname(self): if self.parent_comment_id: return f"t3_{self.parent_comment_id}" elif self.parent_submission: return f"t2_{self.parent_submission}" - @property - def replies(self): + @lazy + def replies(self, sort): if self.replies2 != None: return [x for x in self.replies2 if not x.author.shadowbanned] if not self.parent_submission: return [x for x in self.child_comments.order_by(Comment.id) if not x.author.shadowbanned] - return [x for x in self.child_comments.filter(Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID))).order_by(Comment.realupvotes.desc()) if not x.author.shadowbanned] + + comments = self.child_comments.filter(Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID))) + comments = sort_comments(sort, comments) + return [x for x in comments if not x.author.shadowbanned] - @property - def replies3(self): + @lazy + def replies3(self, sort): if self.replies2 != None: return self.replies2 if not self.parent_submission: return self.child_comments.order_by(Comment.id).all() - return self.child_comments.filter(Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID))).order_by(Comment.realupvotes.desc()).all() + + comments = self.child_comments.filter(Comment.author_id.notin_((AUTOPOLLER_ID, AUTOBETTER_ID, AUTOCHOICE_ID))) + return sort_comments(sort, comments) @property diff --git a/files/classes/user.py b/files/classes/user.py index df97c3d41..8e4c4b876 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -718,7 +718,7 @@ class User(Base): if self.admin_level < 2: comments = comments.filter(Comment.author_id.notin_(self.userblocks)) - return [x[0] for x in comments.order_by(Comment.created_utc.desc()).offset(25 * (page - 1)).all()] + return [x[0] for x in comments.order_by(Comment.id.desc()).offset(25 * (page - 1)).all()] @property @lazy diff --git a/files/routes/front.py b/files/routes/front.py index 3c0f79081..0f7c192ac 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -519,16 +519,7 @@ def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", gt=0, lt=0, cutoff = 0 comments = comments.filter(Comment.created_utc >= cutoff) - if sort == "new": - comments = comments.order_by(Comment.created_utc.desc()) - elif sort == "old": - comments = comments.order_by(Comment.created_utc) - elif sort == "controversial": - comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc()) - elif sort == "top": - comments = comments.order_by(Comment.downvotes - Comment.upvotes) - elif sort == "bottom": - comments = comments.order_by(Comment.upvotes - Comment.downvotes) + comments = sort_comments(sort, comments) comments = comments.offset(25 * (page - 1)).limit(26).all() return [x[0] for x in comments] diff --git a/files/routes/posts.py b/files/routes/posts.py index 5879c8042..c3a1a5157 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -195,16 +195,7 @@ def post_id(pid, anything=None, v=None, sub=None): comments = comments.filter(Comment.level == 1, Comment.stickied == None) - if sort == "new": - comments = comments.order_by(Comment.created_utc.desc()) - elif sort == "old": - comments = comments.order_by(Comment.created_utc) - elif sort == "controversial": - comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc()) - elif sort == "top": - comments = comments.order_by(Comment.realupvotes.desc()) - elif sort == "bottom": - comments = comments.order_by(Comment.upvotes - Comment.downvotes) + comments = sort_comments(sort, comments) first = [c[0] for c in comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body_html) > 100)).all()] second = [c[0] for c in comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body_html) <= 100).all()] @@ -214,16 +205,7 @@ def post_id(pid, anything=None, v=None, sub=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.stickied == None) - if sort == "new": - comments = comments.order_by(Comment.created_utc.desc()) - elif sort == "old": - comments = comments.order_by(Comment.created_utc) - elif sort == "controversial": - comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc()) - elif sort == "top": - comments = comments.order_by(Comment.realupvotes.desc()) - elif sort == "bottom": - comments = comments.order_by(Comment.upvotes - Comment.downvotes) + comments = sort_comments(sort, comments) first = comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body_html) > 100)).all() second = comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body_html) <= 100).all() @@ -324,16 +306,7 @@ def viewmore(v, pid, sort, offset): comments = comments.filter(Comment.level == 1) - if sort == "new": - comments = comments.order_by(Comment.created_utc.desc()) - elif sort == "old": - comments = comments.order_by(Comment.created_utc) - elif sort == "controversial": - comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc()) - elif sort == "top": - comments = comments.order_by(Comment.realupvotes.desc()) - elif sort == "bottom": - comments = comments.order_by(Comment.upvotes - Comment.downvotes) + comments = sort_comments(sort, comments) first = [c[0] for c in comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body_html) > 100)).all()] second = [c[0] for c in comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body_html) <= 100).all()] @@ -341,16 +314,7 @@ def viewmore(v, pid, sort, offset): 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, AUTOCHOICE_ID)), Comment.level == 1, Comment.stickied == None, Comment.id.notin_(ids)) - if sort == "new": - comments = comments.order_by(Comment.created_utc.desc()) - elif sort == "old": - comments = comments.order_by(Comment.created_utc) - elif sort == "controversial": - comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc()) - elif sort == "top": - comments = comments.order_by(Comment.realupvotes.desc()) - elif sort == "bottom": - comments = comments.order_by(Comment.upvotes - Comment.downvotes) + comments = sort_comments(sort, comments) first = comments.filter(or_(and_(Comment.slots_result == None, Comment.blackjack_result == None, Comment.wordle_result == None), func.length(Comment.body_html) > 100)).all() second = comments.filter(or_(Comment.slots_result != None, Comment.blackjack_result != None, Comment.wordle_result != None), func.length(Comment.body_html) <= 100).all() diff --git a/files/routes/search.py b/files/routes/search.py index 461679ec4..71472740d 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -240,16 +240,7 @@ def searchcomments(v): comments = comments.filter(Comment.parent_submission.notin_(club)) - if sort == "new": - comments = comments.order_by(Comment.created_utc.desc()) - elif sort == "old": - comments = comments.order_by(Comment.created_utc) - elif sort == "controversial": - comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc()) - elif sort == "top": - comments = comments.order_by(Comment.downvotes - Comment.upvotes) - elif sort == "bottom": - comments = comments.order_by(Comment.upvotes - Comment.downvotes) + comments = sort_comments(sort, comments) total = comments.count() diff --git a/files/routes/users.py b/files/routes/users.py index 212f811ea..984c6982e 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -115,7 +115,7 @@ def upvoters_comments(v, username, uid): page = max(1, int(request.values.get("page", 1))) - listing = g.db.query(Comment).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost == False, Comment.is_banned == False, Comment.deleted_utc == 0, CommentVote.vote_type==1, Comment.author_id==id, CommentVote.user_id==uid).order_by(Comment.created_utc.desc()).offset(25 * (page - 1)).limit(26).all() + listing = g.db.query(Comment).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost == False, Comment.is_banned == False, Comment.deleted_utc == 0, CommentVote.vote_type==1, Comment.author_id==id, CommentVote.user_id==uid).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all() listing = [c.id for c in listing] next_exists = len(listing) > 25 @@ -157,7 +157,7 @@ def downvoters_comments(v, username, uid): page = max(1, int(request.values.get("page", 1))) - listing = g.db.query(Comment).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost == False, Comment.is_banned == False, Comment.deleted_utc == 0, CommentVote.vote_type==-1, Comment.author_id==id, CommentVote.user_id==uid).order_by(Comment.created_utc.desc()).offset(25 * (page - 1)).limit(26).all() + listing = g.db.query(Comment).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost == False, Comment.is_banned == False, Comment.deleted_utc == 0, CommentVote.vote_type==-1, Comment.author_id==id, CommentVote.user_id==uid).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all() listing = [c.id for c in listing] next_exists = len(listing) > 25 @@ -202,7 +202,7 @@ def upvoting_comments(v, username, uid): page = max(1, int(request.values.get("page", 1))) - listing = g.db.query(Comment).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost == False, Comment.is_banned == False, Comment.deleted_utc == 0, CommentVote.vote_type==1, CommentVote.user_id==id, Comment.author_id==uid).order_by(Comment.created_utc.desc()).offset(25 * (page - 1)).limit(26).all() + listing = g.db.query(Comment).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost == False, Comment.is_banned == False, Comment.deleted_utc == 0, CommentVote.vote_type==1, CommentVote.user_id==id, Comment.author_id==uid).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all() listing = [c.id for c in listing] next_exists = len(listing) > 25 @@ -244,7 +244,7 @@ def downvoting_comments(v, username, uid): page = max(1, int(request.values.get("page", 1))) - listing = g.db.query(Comment).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost == False, Comment.is_banned == False, Comment.deleted_utc == 0, CommentVote.vote_type==-1, CommentVote.user_id==id, Comment.author_id==uid).order_by(Comment.created_utc.desc()).offset(25 * (page - 1)).limit(26).all() + listing = g.db.query(Comment).join(CommentVote, CommentVote.comment_id==Comment.id).filter(Comment.ghost == False, Comment.is_banned == False, Comment.deleted_utc == 0, CommentVote.vote_type==-1, CommentVote.user_id==id, Comment.author_id==uid).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all() listing = [c.id for c in listing] next_exists = len(listing) > 25 @@ -1041,16 +1041,7 @@ def u_username_comments(username, v=None): cutoff = 0 comments = comments.filter(Comment.created_utc >= cutoff) - if sort == "new": - comments = comments.order_by(Comment.created_utc.desc()) - elif sort == "old": - comments = comments.order_by(Comment.created_utc) - elif sort == "controversial": - comments = comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc()) - elif sort == "top": - comments = comments.order_by(Comment.downvotes - Comment.upvotes) - elif sort == "bottom": - comments = comments.order_by(Comment.upvotes - Comment.downvotes) + comments = sort_comments(sort, comments) comments = comments.offset(25 * (page - 1)).limit(26).all() ids = [x.id for x in comments] diff --git a/files/templates/comments.html b/files/templates/comments.html index 0e5dec7c4..c8e359a4c 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -58,9 +58,9 @@ {% set score=ups-downs %} {% if v and (v.shadowbanned or v.admin_level >= 2) %} - {% set replies=c.replies3 %} + {% set replies=c.replies3(sort) %} {% else %} - {% set replies=c.replies %} + {% set replies=c.replies(sort) %} {% endif %} {% if c.is_blocking or (c.is_banned or c.deleted_utc) and not (v and v.admin_level > 1) and not (v and v.id==c.author_id) %}