From 1750a0e06789583f3e35b46de8dcd9edbf533b05 Mon Sep 17 00:00:00 2001 From: Deimos Date: Tue, 11 Sep 2018 12:05:32 -0600 Subject: [PATCH] Collapse comments tagged "noise" --- tildes/tildes/models/comment/comment_tree.py | 28 +++++++++++++++----- tildes/tildes/views/topic.py | 2 ++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/tildes/tildes/models/comment/comment_tree.py b/tildes/tildes/models/comment/comment_tree.py index 10eda1b..72d60dc 100644 --- a/tildes/tildes/models/comment/comment_tree.py +++ b/tildes/tildes/models/comment/comment_tree.py @@ -10,6 +10,7 @@ from prometheus_client import Histogram from tildes.enums import CommentSortOption from tildes.metrics import get_histogram +from tildes.models.user import User from .comment import Comment @@ -175,6 +176,16 @@ class CommentTree: order=self.sort.name, ) + def collapse_from_tags(self, viewer: Optional[User]) -> None: + """Collapse comments based on how they've been tagged.""" + for comment in self.comments: + # never affect the viewer's own comments + if viewer and comment.user == viewer: + continue + + if comment.tag_counts["noise"] >= 2: + comment.collapsed_state = "full" + def collapse_old_comments(self, threshold: datetime) -> None: """Collapse old comments in the tree. @@ -191,6 +202,10 @@ class CommentTree: if comment.is_deleted or comment.is_removed: continue + # don't override any other collapsing decisions + if comment.collapsed_state: + continue + # uncollapse the comment comment.collapsed_state = "uncollapsed" @@ -214,8 +229,8 @@ class CommentTree: @staticmethod def _recursively_collapse(comment: Comment) -> None: """Recursively collapse a comment and its replies as much as possible.""" - # stop processing the branch as soon as we hit an uncollapsed comment - if comment.collapsed_state == "uncollapsed": + # stop processing this branch if we hit an uncollapsed/fully-collapsed comment + if comment.collapsed_state in ("uncollapsed", "full"): return # if it doesn't have any uncollapsed descendants, collapse the whole branch @@ -232,9 +247,10 @@ class CommentTree: def finalize_collapsing_maximized(self) -> None: """Finish collapsing comments, collapsing as much as possible.""" - # if no comments have their state defined, leave them all uncollapsed - if all([comment.collapsed_state is None for comment in self.comments]): - return - for comment in self.tree: self._recursively_collapse(comment) + + # if all the top-level comments end up fully collapsed, uncollapse instead + if all([comment.collapsed_state == "full" for comment in self.tree]): + for comment in self.tree: + comment.collapsed_state = None diff --git a/tildes/tildes/views/topic.py b/tildes/tildes/views/topic.py index 95488de..a27f6dd 100644 --- a/tildes/tildes/views/topic.py +++ b/tildes/tildes/views/topic.py @@ -277,6 +277,8 @@ def get_topic(request: Request, comment_order: CommentSortOption) -> dict: .all() ) + tree.collapse_from_tags(request.user) + # if the user has the "mark new comments" feature enabled if request.user and request.user.track_comment_visits: # update their last visit time for this topic