From 17fd08987061fd017e4bbe2a2daac59a2aad7abf Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Thu, 1 Sep 2022 22:31:08 +0200 Subject: [PATCH] refactor pinning child comments --- files/classes/comment.py | 3 ++- files/routes/posts.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/files/classes/comment.py b/files/classes/comment.py index 4f241a2847..6cb92acb46 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -69,8 +69,9 @@ 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") + top_comment = relationship("Comment", primaryjoin="Comment.id==Comment.top_comment_id") parent_comment = relationship("Comment", remote_side=[id], back_populates="child_comments") - child_comments = relationship("Comment", order_by="Comment.realupvotes.desc()", remote_side=[parent_comment_id], back_populates="parent_comment") + child_comments = relationship("Comment", order_by="Comment.stickied, Comment.realupvotes.desc()", remote_side=[parent_comment_id], back_populates="parent_comment") 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") diff --git a/files/routes/posts.py b/files/routes/posts.py index 82dcdd231a..42d1234612 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -171,6 +171,11 @@ def post_id(pid, anything=None, v=None, sub=None): output.append(comment) pinned = [c[0] for c in comments.filter(Comment.stickied != None).all()] + + for x in pinned: + if x.level > 1: + pinned.remove(x) + pinned.append(x.top_comment) comments = comments.filter(Comment.level == 1, Comment.stickied == None) @@ -182,6 +187,11 @@ def post_id(pid, anything=None, v=None, sub=None): else: pinned = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.stickied != None).all() + for x in pinned: + if x.level > 1: + pinned.remove(x) + pinned.append(x.top_comment) + 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)