From e3a3cbd358fb2aec4855248bfc4e7ca6f9aa505a Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Tue, 11 Oct 2022 18:41:09 +0200 Subject: [PATCH] add "hot" comment sorting --- files/classes/user.py | 2 +- files/helpers/sorting_and_time.py | 21 ++++++++++++++++----- files/routes/comments.py | 2 +- files/routes/front.py | 11 +---------- files/routes/posts.py | 2 +- files/routes/settings.py | 2 +- files/templates/settings_filters.html | 2 +- files/templates/submission.html | 2 ++ schema.sql | 5 ++--- seed-db.sql | 8 ++++---- 10 files changed, 30 insertions(+), 27 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index 715696546..23da23813 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -121,7 +121,7 @@ class User(Base): mfa_secret = deferred(Column(String)) is_private = Column(Boolean, default=False) stored_subscriber_count = Column(Integer, default=0) - defaultsortingcomments = Column(String, default="top") + defaultsortingcomments = Column(String, default="hot") defaultsorting = Column(String, default="hot") defaulttime = Column(String, default=DEFAULT_TIME_FILTER) is_nofollow = Column(Boolean, default=False) diff --git a/files/helpers/sorting_and_time.py b/files/helpers/sorting_and_time.py index 25baa621d..275ba4695 100644 --- a/files/helpers/sorting_and_time.py +++ b/files/helpers/sorting_and_time.py @@ -2,6 +2,7 @@ import time from files.classes.comment import Comment from files.classes.submission import Submission from files.helpers.const import * +from sqlalchemy.sql import func def apply_time_filter(t, objects, Class): now = int(time.time()) @@ -21,8 +22,12 @@ def apply_time_filter(t, objects, Class): return objects.filter(Class.created_utc >= cutoff) def sort_comments(sort, comments): - - if sort == 'new': + if sort == 'hot': + ti = int(time.time()) + 3600 + if SITE_NAME == 'rDrama': metric = Comment.realupvotes + else: metric = Comment.upvotes - Comment.downvotes + return comments.order_by(-1000000*(metric + 1)/(func.power(((ti - Comment.created_utc)/1000), 1.23)), Comment.created_utc.desc()) + elif sort == 'new': return comments.order_by(Comment.id.desc()) elif sort == 'old': return comments.order_by(Comment.id) @@ -30,13 +35,19 @@ def sort_comments(sort, comments): return comments.order_by((Comment.upvotes+1)/(Comment.downvotes+1) + (Comment.downvotes+1)/(Comment.upvotes+1), Comment.downvotes.desc(), Comment.id.desc()) elif sort == "bottom": return comments.order_by(Comment.upvotes - Comment.downvotes) - elif SITE_NAME == 'rDrama': - return comments.order_by(Comment.realupvotes.desc(), Comment.id.desc()) else: return comments.order_by(Comment.downvotes - Comment.upvotes, Comment.id.desc()) def sort_posts(sort, posts): - if sort == "new": + if sort == 'hot': + ti = int(time.time()) + 3600 + if SITE_NAME == 'rDrama': + return posts.order_by(-1000000*(Submission.realupvotes + 1 + Submission.comment_count/5)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc()) + else: + return posts.order_by(-1000000*(Submission.upvotes - Submission.downvotes + 1)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc()) + elif sort == "bump": + return posts.filter(Submission.comment_count > 1).order_by(Submission.bump_utc.desc(), Submission.created_utc.desc()) + elif sort == "new": return posts.order_by(Submission.created_utc.desc()) elif sort == "old": return posts.order_by(Submission.created_utc) diff --git a/files/routes/comments.py b/files/routes/comments.py index 74f5d1043..6b3f74b7a 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -69,7 +69,7 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None, sub=None): top_comment = c if v: defaultsortingcomments = v.defaultsortingcomments - else: defaultsortingcomments = "top" + else: defaultsortingcomments = "hot" sort=request.values.get("sort", defaultsortingcomments) if v: diff --git a/files/routes/front.py b/files/routes/front.py index fa1263b17..ac027cc13 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -115,16 +115,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) - if sort == 'hot': - ti = int(time.time()) + 3600 - if SITE_NAME == 'rDrama': - posts = posts.order_by(-1000000*(Submission.realupvotes + 1 + Submission.comment_count/5)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc()) - else: - posts = posts.order_by(-1000000*(Submission.upvotes - Submission.downvotes + 1)/(func.power(((ti - Submission.created_utc)/1000), 1.23)), Submission.created_utc.desc()) - elif sort == "bump": - posts = posts.filter(Submission.comment_count > 1).order_by(Submission.bump_utc.desc(), Submission.created_utc.desc()) - else: - posts = sort_posts(sort, posts) + posts = sort_posts(sort, posts) if v: size = v.frontsize or 0 else: size = 25 diff --git a/files/routes/posts.py b/files/routes/posts.py index 63470bcb4..ee49fc665 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 post.new or 'megathread' in post.title.lower(): defaultsortingcomments = 'new' elif v: defaultsortingcomments = v.defaultsortingcomments - else: defaultsortingcomments = "top" + else: defaultsortingcomments = "hot" sort = request.values.get("sort", defaultsortingcomments) if post.club and not (v and (v.paid_dues or v.id == post.author_id)): abort(403) diff --git a/files/routes/settings.py b/files/routes/settings.py index b764a3cee..5c731e485 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -240,7 +240,7 @@ def settings_profile_post(v): defaultsortingcomments = request.values.get("defaultsortingcomments") if defaultsortingcomments: - if defaultsortingcomments in {"new", "old", "controversial", "top", "bottom"}: + if defaultsortingcomments in {"new", "old", "controversial", "top", "hot", "bottom"}: v.defaultsortingcomments = defaultsortingcomments updated = True else: abort(400) diff --git a/files/templates/settings_filters.html b/files/templates/settings_filters.html index f4eb5209d..e32261994 100644 --- a/files/templates/settings_filters.html +++ b/files/templates/settings_filters.html @@ -71,7 +71,7 @@

Change the default sorting for comments.

diff --git a/files/templates/submission.html b/files/templates/submission.html index 4d6ffdb18..221a3cc98 100644 --- a/files/templates/submission.html +++ b/files/templates/submission.html @@ -952,6 +952,7 @@