From e066a7c91e66093b157d7235e2f885ae52e27e71 Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Wed, 12 Oct 2022 22:16:08 +0200 Subject: [PATCH] Show hidden comments if the logged in user replied to it in the chain --- main.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index e27c6e8..e7d1ed8 100644 --- a/main.py +++ b/main.py @@ -656,15 +656,28 @@ def create_comment_tree(comments, user): # Collect comments first, then build the tree in case we encounter a child before a parent comment_map = { v[0]: Comment(*v) for v in comments } root = [] + # We should keep showing hidden comments if the user replied to them, directly or indirectly. + # To do that, keep track of user comments, then walk up the tree and insert hidden comments. + user_comments = [] # Build tree - for comment in comment_map.values(): - if comment.hidden and not user.is_moderator(): - continue + def insert(comment): parent = comment_map.get(comment.parent_id) if parent is not None: parent.children.append(comment) else: root.append(comment) + for comment in comment_map.values(): + if comment.hidden and (not user or not user.is_moderator()): + continue + insert(comment) + if user and (comment.author_id == user.id and not user.is_moderator()): + user_comments.append(comment) + # Insert replied-to hidden comments + for c in user_comments: + while c is not None: + if c.hidden: + insert(c) + c = comment_map.get(c.parent_id) # Sort each comment based on create time def sort_time(l): l.sort(key=lambda c: c.modify_time, reverse=True)