From aeb3f76e7b9806df8dfd2a796aff07e90bdf22ca Mon Sep 17 00:00:00 2001 From: Aevann Date: Mon, 4 Dec 2023 15:41:42 +0200 Subject: [PATCH] fix issue that made it so when u unpin a child comment, if theres another child comment in the same chain, it gets effectively unpinned as well --- files/classes/comment.py | 15 +++++++++------ .../20231204-better-pinned-children-code.sql | 5 +++++ 2 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 migrations/20231204-better-pinned-children-code.sql diff --git a/files/classes/comment.py b/files/classes/comment.py index e8939eb70..0f037a222 100644 --- a/files/classes/comment.py +++ b/files/classes/comment.py @@ -187,7 +187,7 @@ class Comment(Base): is_bot = Column(Boolean, default=False) stickied = Column(String) stickied_utc = Column(Integer) - stickied_child_id = Column(Integer) + num_of_pinned_children = Column(Integer, default=0) sentto = Column(Integer, ForeignKey("users.id")) app_id = Column(Integer, ForeignKey("oauth_apps.id")) upvotes = Column(Integer, default=1) @@ -279,7 +279,7 @@ class Comment(Base): if self.replies2 != None: return self.replies2 - replies = g.db.query(Comment).filter_by(parent_comment_id=self.id).order_by(Comment.stickied, Comment.stickied_child_id) + replies = g.db.query(Comment).filter_by(parent_comment_id=self.id).order_by(Comment.stickied, Comment.num_of_pinned_children.desc()) if not self.parent_post: sort='old' return sort_objects(sort, replies, Comment).all() @@ -520,11 +520,14 @@ class Comment(Base): c = self while c.level > 2: c = c.parent_comment - c.stickied_child_id = self.id + c.num_of_pinned_children += 1 g.db.add(c) def unpin_parents(self): - cleanup = g.db.query(Comment).filter_by(stickied_child_id=self.id).all() - for c in cleanup: - c.stickied_child_id = None + c = self + while c.level > 2: + c = c.parent_comment + c.num_of_pinned_children -= 1 + if c.num_of_pinned_children < 0: + c.num_of_pinned_children = 0 g.db.add(c) diff --git a/migrations/20231204-better-pinned-children-code.sql b/migrations/20231204-better-pinned-children-code.sql new file mode 100644 index 000000000..30c6cfc76 --- /dev/null +++ b/migrations/20231204-better-pinned-children-code.sql @@ -0,0 +1,5 @@ +alter table comments add column num_of_pinned_children int default 0 not null; +update comments set num_of_pinned_children=1 where stickied_child_id is not null; + +alter table comments alter column num_of_pinned_children drop default; +alter table comments drop column stickied_child_id;