refactor sorting again (untested)

remotes/1693176582716663532/tmp_refs/heads/watchparty
Aevann1 2022-10-12 09:03:28 +02:00
parent 925ec12141
commit 2aa281e8c8
10 changed files with 51 additions and 66 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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) %}