From 2aa281e8c8a83d7f308366b5ebfce0a860aed0e4 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Wed, 12 Oct 2022 09:03:28 +0200 Subject: [PATCH] refactor sorting again (untested) --- files/classes/comment.py | 54 +++++++++++++++++++------------ files/classes/user.py | 3 +- files/helpers/get.py | 3 -- files/helpers/sorting_and_time.py | 22 ------------- files/routes/front.py | 5 +-- files/routes/notifications.py | 3 -- files/routes/posts.py | 11 ++++--- files/routes/search.py | 5 +-- files/routes/users.py | 5 +-- files/templates/comments.html | 6 +--- 10 files changed, 51 insertions(+), 66 deletions(-) diff --git a/files/classes/comment.py b/files/classes/comment.py index e8b6d78c1..a4303f370 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -9,7 +9,6 @@ from files.__main__ import Base from files.classes.votes import CommentVote from files.helpers.const import * from files.helpers.regex import * -from files.helpers.regex import * from files.helpers.lazy import lazy from .flags import CommentFlag from .votes import CommentVote @@ -18,6 +17,31 @@ from random import randint from math import floor +def sort_objects(sort, objects, Class, v): + if not (v and v.can_see_shadowbanned): + objects = objects.join(Class.author).filter(User.shadowbanned == None) + + if sort == 'hot': + ti = int(time.time()) + 3600 + if SITE_NAME == 'rDrama': metric = Class.realupvotes + else: metric = Class.upvotes - Class.downvotes + if Class.__name__ == "Submission": metric += Class.comment_count/5 + return objects.order_by(-1000000*(metric + 1)/(func.power(((ti - Class.created_utc)/1000), 1.23)), Class.created_utc.desc()) + elif sort == "bump" and Class.__name__ == "Submission": + return objects.filter(Class.comment_count > 1).order_by(Class.bump_utc.desc(), Class.created_utc.desc()) + elif sort == "comments" and Class.__name__ == "Submission": + return objects.order_by(Class.comment_count.desc(), Class.created_utc.desc()) + elif sort == "new": + return objects.order_by(Class.created_utc.desc()) + elif sort == "old": + return objects.order_by(Class.created_utc) + elif sort == "controversial": + return objects.order_by((Class.upvotes+1)/(Class.downvotes+1) + (Class.downvotes+1)/(Class.upvotes+1), Class.downvotes.desc(), Class.created_utc.desc()) + elif sort == "bottom": + return objects.order_by(Class.upvotes - Class.downvotes, Class.created_utc.desc()) + else: + return objects.order_by(Class.downvotes - Class.upvotes, Class.created_utc.desc()) + def normalize_urls_runtime(body, v): if not v: return body @@ -69,8 +93,7 @@ class Comment(Base): post = relationship("Submission", back_populates="comments") author = relationship("User", primaryjoin="User.id==Comment.author_id") senttouser = relationship("User", primaryjoin="User.id==Comment.sentto") - parent_comment = relationship("Comment", remote_side=[id], back_populates="child_comments") - child_comments = relationship("Comment", order_by="Comment.stickied, Comment.realupvotes.desc()", remote_side=[parent_comment_id], back_populates="parent_comment") + parent_comment = relationship("Comment", remote_side=[id]) awards = relationship("AwardRelationship", order_by="AwardRelationship.awarded_utc.desc()", back_populates="comment") flags = relationship("CommentFlag", order_by="CommentFlag.created_utc") options = relationship("CommentOption", order_by="CommentOption.id") @@ -207,26 +230,15 @@ class Comment(Base): elif self.parent_submission: return f"p_{self.parent_submission}" @lazy - def replies(self, sort=None): - if self.replies2 != None: - replies = self.replies2 - elif not self.parent_submission: - replies = g.db.query(Comment).filter_by(parent_comment_id=self.id).order_by(Comment.id).all() - else: - replies = self.child_comments - - return [x for x in replies if not x.author.shadowbanned] - - - @lazy - def replies3(self, sort): + def replies(self, sort, v): if self.replies2 != None: return self.replies2 - - if not self.parent_submission: - return g.db.query(Comment).filter_by(parent_comment_id=self.id).order_by(Comment.id).all() - return self.child_comments + replies = g.db.query(Comment).filter_by(parent_comment_id=self.id).order_by(Comment.stickied) + + if not self.parent_submission: sort='old' + + return sort_objects(sort, replies, Comment, v) @property @@ -311,7 +323,7 @@ class Comment(Base): 'is_bot': self.is_bot, 'flags': flags, 'author': '👻' if self.ghost else self.author.json, - 'replies': [x.json for x in self.replies()] + 'replies': [x.json for x in self.replies(sort="old", v=None)] } if self.level >= 2: data['parent_comment_id'] = self.parent_comment_id diff --git a/files/classes/user.py b/files/classes/user.py index 15970039f..4a0021275 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -7,6 +7,7 @@ from files.helpers.media import * from files.helpers.const import * from files.classes.casino_game import Casino_Game from files.helpers.sorting_and_time import * +from files.classes.comment import sort_objects from .alts import Alt from .saves import * from .notifications import Notification @@ -470,7 +471,7 @@ class User(Base): posts = apply_time_filter(t, posts, Submission) - posts = sort_objects(sort, posts, Submission) + posts = sort_objects(sort, posts, Submission, v) posts = posts.offset(25 * (page - 1)).limit(26).all() diff --git a/files/helpers/get.py b/files/helpers/get.py index 52aacc21e..d5b82e0fc 100644 --- a/files/helpers/get.py +++ b/files/helpers/get.py @@ -264,9 +264,6 @@ def get_comments(cids, v=None, load_parent=False): blocked.c.target_id, ).filter(Comment.id.in_(cids)) - if not (v and v.can_see_shadowbanned): - comments = comments.join(Comment.author).filter(User.shadowbanned == None) - comments = comments.join( votes, votes.c.comment_id == Comment.id, diff --git a/files/helpers/sorting_and_time.py b/files/helpers/sorting_and_time.py index 7d2f2eace..4d7a4e9b0 100644 --- a/files/helpers/sorting_and_time.py +++ b/files/helpers/sorting_and_time.py @@ -20,25 +20,3 @@ def apply_time_filter(t, objects, Class): cutoff = 0 return objects.filter(Class.created_utc >= cutoff) - -def sort_objects(sort, objects, Class): - if sort == 'hot': - ti = int(time.time()) + 3600 - if SITE_NAME == 'rDrama': metric = Class.realupvotes - else: metric = Class.upvotes - Class.downvotes - if Class == Submission: metric += Class.comment_count/5 - return objects.order_by(-1000000*(metric + 1)/(func.power(((ti - Class.created_utc)/1000), 1.23)), Class.created_utc.desc()) - elif sort == "bump" and Class == Submission: - return objects.filter(Class.comment_count > 1).order_by(Class.bump_utc.desc(), Class.created_utc.desc()) - elif sort == "comments" and Class == Submission: - return objects.order_by(Class.comment_count.desc(), Class.created_utc.desc()) - elif sort == "new": - return objects.order_by(Class.created_utc.desc()) - elif sort == "old": - return objects.order_by(Class.created_utc) - elif sort == "controversial": - return objects.order_by((Class.upvotes+1)/(Class.downvotes+1) + (Class.downvotes+1)/(Class.upvotes+1), Class.downvotes.desc(), Class.created_utc.desc()) - elif sort == "bottom": - return objects.order_by(Class.upvotes - Class.downvotes, Class.created_utc.desc()) - else: - return objects.order_by(Class.downvotes - Class.upvotes, Class.created_utc.desc()) diff --git a/files/routes/front.py b/files/routes/front.py index 1b10d4787..13dae7f5e 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -3,6 +3,7 @@ from files.helpers.get import * from files.helpers.discord import * from files.helpers.const import * from files.helpers.sorting_and_time import * +from files.classes.comment import sort_objects from files.__main__ import app, cache, limiter from files.classes.submission import Submission from files.helpers.awards import award_timers @@ -115,7 +116,7 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false" if not (v and v.shadowbanned): posts = posts.join(Submission.author).filter(User.shadowbanned == None) - posts = sort_objects(sort, posts, Submission) + posts = sort_objects(sort, posts, Submission, v) if v: size = v.frontsize or 0 else: size = 25 @@ -247,7 +248,7 @@ def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", gt=0, lt=0, if not gt and not lt: comments = apply_time_filter(t, comments, Comment) - comments = sort_objects(sort, comments, Comment) + comments = sort_objects(sort, comments, Comment, v) comments = comments.offset(25 * (page - 1)).limit(26).all() return [x[0] for x in comments] diff --git a/files/routes/notifications.py b/files/routes/notifications.py index 207bbe136..ca64f7380 100644 --- a/files/routes/notifications.py +++ b/files/routes/notifications.py @@ -258,9 +258,6 @@ def notifications(v): or_(Comment.sentto == None, Comment.sentto == 2), ).order_by(Notification.created_utc.desc()) - if not (v and v.can_see_shadowbanned): - comments = comments.join(Comment.author).filter(User.shadowbanned == None) - comments = comments.offset(25 * (page - 1)).limit(26).all() next_exists = (len(comments) > 25) diff --git a/files/routes/posts.py b/files/routes/posts.py index 797d29202..ff4b9a699 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -11,6 +11,7 @@ from files.helpers.slots import * from files.helpers.get import * from files.helpers.actions import * from files.helpers.sorting_and_time import * +from files.classes.comment import sort_objects from files.classes import * from flask import * from io import BytesIO @@ -196,7 +197,7 @@ def post_id(pid, anything=None, v=None, sub=None): comments = comments.filter(Comment.level == 1, Comment.stickied == None) - comments = sort_objects(sort, comments, Comment) + comments = sort_objects(sort, comments, Comment, v) comments = [c[0] for c in comments.all()] else: @@ -204,7 +205,7 @@ def post_id(pid, anything=None, v=None, sub=None): comments = g.db.query(Comment).join(Comment.author).filter(User.shadowbanned == None, Comment.parent_submission == post.id, Comment.level == 1, Comment.stickied == None) - comments = sort_objects(sort, comments, Comment) + comments = sort_objects(sort, comments, Comment, v) comments = comments.all() @@ -316,13 +317,13 @@ def viewmore(v, pid, sort, offset): comments = comments.filter(Comment.level == 1) - comments = sort_objects(sort, comments, Comment) + comments = sort_objects(sort, comments, Comment, v) comments = [c[0] for c in comments.all()] else: comments = g.db.query(Comment).join(Comment.author).filter(User.shadowbanned == None, Comment.parent_submission == pid, Comment.level == 1, Comment.stickied == None, Comment.id.notin_(ids)) - comments = sort_objects(sort, comments, Comment) + comments = sort_objects(sort, comments, Comment, v) comments = comments.offset(offset).all() @@ -395,7 +396,7 @@ def morecomments(v, cid): comments = output else: c = get_comment(cid) - comments = c.replies(None) + comments = c.replies(sort=request.values.get('sort'), v=v) if comments: p = comments[0].post else: p = None diff --git a/files/routes/search.py b/files/routes/search.py index eb716b39a..49163eee3 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -5,6 +5,7 @@ from flask import * from files.__main__ import app from files.helpers.regex import * from files.helpers.sorting_and_time import * +from files.classes.comment import sort_objects import time from calendar import timegm @@ -145,7 +146,7 @@ def searchposts(v): posts = apply_time_filter(t, posts, Submission) - posts = sort_objects(sort, posts, Submission) + posts = sort_objects(sort, posts, Submission, v) total = posts.count() @@ -248,7 +249,7 @@ def searchcomments(v): except: abort(400) comments = comments.filter(Comment.created_utc < before) - comments = sort_objects(sort, comments, Comment) + comments = sort_objects(sort, comments, Comment, v) total = comments.count() diff --git a/files/routes/users.py b/files/routes/users.py index 1460ae542..25968707f 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -8,6 +8,7 @@ from files.helpers.alerts import * from files.helpers.sanitize import * from files.helpers.const import * from files.helpers.sorting_and_time import * +from files.classes.comment import sort_objects from files.mail import * from flask import * from files.__main__ import app, limiter, db_session @@ -650,7 +651,7 @@ def messagereply(v): notif = Notification(comment_id=c.id, user_id=admin) g.db.add(notif) - ids = [c.top_comment.id] + [x.id for x in c.top_comment.replies(None)] + ids = [c.top_comment.id] + [x.id for x in c.top_comment.replies(sort="old", v=v)] notifications = g.db.query(Notification).filter(Notification.comment_id.in_(ids), Notification.user_id.in_(admins)) for n in notifications: g.db.delete(n) @@ -909,7 +910,7 @@ def u_username_comments(username, v=None): comments = apply_time_filter(t, comments, Comment) - comments = sort_objects(sort, comments, Comment) + comments = sort_objects(sort, comments, Comment, v) 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 f5825a042..203863c14 100644 --- a/files/templates/comments.html +++ b/files/templates/comments.html @@ -21,11 +21,7 @@ {% set score=ups-downs %} {% if render_replies %} - {% if v and v.can_see_shadowbanned %} - {% set replies=c.replies3(sort) %} - {% else %} - {% set replies=c.replies(sort) %} - {% endif %} + {% set replies=c.replies(sort=sort, v=v) %} {% endif %} {% if c.is_blocking and not c.ghost or (c.is_banned or c.deleted_utc) and not (v and v.admin_level >= PERMS['POST_COMMENT_MODERATION']) and not (v and v.id==c.author_id) %}