refactor sorting

master
Aevann1 2022-10-12 08:10:11 +02:00
parent 07043873b9
commit b1abd33835
6 changed files with 24 additions and 41 deletions

View File

@ -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()

View File

@ -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())

View File

@ -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]

View File

@ -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()

View File

@ -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()

View File

@ -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]