From b1abd33835084efa3f5487a165c9adba83ec4fd9 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Wed, 12 Oct 2022 08:10:11 +0200 Subject: [PATCH] refactor sorting --- files/classes/user.py | 2 +- files/helpers/sorting_and_time.py | 45 ++++++++++--------------------- files/routes/front.py | 4 +-- files/routes/posts.py | 8 +++--- files/routes/search.py | 4 +-- files/routes/users.py | 2 +- 6 files changed, 24 insertions(+), 41 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index 23da23813..15c24b7df 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -470,7 +470,7 @@ class User(Base): posts = apply_time_filter(t, posts, Submission) - posts = sort_posts(sort, posts) + posts = sort_objects(sort, posts, Submission) posts = posts.offset(25 * (page - 1)).limit(26).all() diff --git a/files/helpers/sorting_and_time.py b/files/helpers/sorting_and_time.py index 275ba4695..7d2f2eace 100644 --- a/files/helpers/sorting_and_time.py +++ b/files/helpers/sorting_and_time.py @@ -21,41 +21,24 @@ def apply_time_filter(t, objects, Class): return objects.filter(Class.created_utc >= cutoff) -def sort_comments(sort, comments): +def sort_objects(sort, objects, Class): 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) - elif sort == 'controversial': - 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) - else: - return comments.order_by(Comment.downvotes - Comment.upvotes, Comment.id.desc()) - -def sort_posts(sort, posts): - 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()) + 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 posts.order_by(Submission.created_utc.desc()) + return objects.order_by(Class.created_utc.desc()) elif sort == "old": - return posts.order_by(Submission.created_utc) + return objects.order_by(Class.created_utc) elif sort == "controversial": - return posts.order_by((Submission.upvotes+1)/(Submission.downvotes+1) + (Submission.downvotes+1)/(Submission.upvotes+1), Submission.downvotes.desc(), Submission.created_utc.desc()) + 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 posts.order_by(Submission.upvotes - Submission.downvotes, Submission.created_utc.desc()) - elif sort == "comments": - return posts.order_by(Submission.comment_count.desc(), Submission.created_utc.desc()) + return objects.order_by(Class.upvotes - Class.downvotes, Class.created_utc.desc()) else: - return posts.order_by(Submission.downvotes - Submission.upvotes, Submission.created_utc.desc()) + 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 ac027cc13..1b10d4787 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -115,7 +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) - posts = sort_posts(sort, posts) + posts = sort_objects(sort, posts, Submission) if v: size = v.frontsize or 0 else: size = 25 @@ -247,7 +247,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_comments(sort, comments) + comments = sort_objects(sort, comments, Comment) 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 186a0b85b..348132d7a 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -196,7 +196,7 @@ def post_id(pid, anything=None, v=None, sub=None): comments = comments.filter(Comment.level == 1, Comment.stickied == None) - comments = sort_comments(sort, comments) + comments = sort_objects(sort, comments, Comment) comments = [c[0] for c in comments.all()] else: @@ -204,7 +204,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_comments(sort, comments) + comments = sort_objects(sort, comments, Comment) comments = comments.all() @@ -316,13 +316,13 @@ def viewmore(v, pid, sort, offset): comments = comments.filter(Comment.level == 1) - comments = sort_comments(sort, comments) + comments = sort_objects(sort, comments, Comment) 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_comments(sort, comments) + comments = sort_objects(sort, comments, Comment) comments = comments.offset(offset).all() diff --git a/files/routes/search.py b/files/routes/search.py index addfb9c37..eb716b39a 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -145,7 +145,7 @@ def searchposts(v): posts = apply_time_filter(t, posts, Submission) - posts = sort_posts(sort, posts) + posts = sort_objects(sort, posts, Submission) total = posts.count() @@ -248,7 +248,7 @@ def searchcomments(v): except: abort(400) comments = comments.filter(Comment.created_utc < before) - comments = sort_comments(sort, comments) + comments = sort_objects(sort, comments, Comment) total = comments.count() diff --git a/files/routes/users.py b/files/routes/users.py index f6e5e7da2..1460ae542 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -909,7 +909,7 @@ def u_username_comments(username, v=None): comments = apply_time_filter(t, comments, Comment) - comments = sort_comments(sort, comments) + comments = sort_objects(sort, comments, Comment) comments = comments.offset(25 * (page - 1)).limit(26).all() ids = [x.id for x in comments]