Browse Source

Collapse comments tagged "noise"

merge-requests/37/head
Deimos 6 years ago
parent
commit
1750a0e067
  1. 28
      tildes/tildes/models/comment/comment_tree.py
  2. 2
      tildes/tildes/views/topic.py

28
tildes/tildes/models/comment/comment_tree.py

@ -10,6 +10,7 @@ from prometheus_client import Histogram
from tildes.enums import CommentSortOption from tildes.enums import CommentSortOption
from tildes.metrics import get_histogram from tildes.metrics import get_histogram
from tildes.models.user import User
from .comment import Comment from .comment import Comment
@ -175,6 +176,16 @@ class CommentTree:
order=self.sort.name, 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: def collapse_old_comments(self, threshold: datetime) -> None:
"""Collapse old comments in the tree. """Collapse old comments in the tree.
@ -191,6 +202,10 @@ class CommentTree:
if comment.is_deleted or comment.is_removed: if comment.is_deleted or comment.is_removed:
continue continue
# don't override any other collapsing decisions
if comment.collapsed_state:
continue
# uncollapse the comment # uncollapse the comment
comment.collapsed_state = "uncollapsed" comment.collapsed_state = "uncollapsed"
@ -214,8 +229,8 @@ class CommentTree:
@staticmethod @staticmethod
def _recursively_collapse(comment: Comment) -> None: def _recursively_collapse(comment: Comment) -> None:
"""Recursively collapse a comment and its replies as much as possible.""" """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 return
# if it doesn't have any uncollapsed descendants, collapse the whole branch # if it doesn't have any uncollapsed descendants, collapse the whole branch
@ -232,9 +247,10 @@ class CommentTree:
def finalize_collapsing_maximized(self) -> None: def finalize_collapsing_maximized(self) -> None:
"""Finish collapsing comments, collapsing as much as possible.""" """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: for comment in self.tree:
self._recursively_collapse(comment) 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

2
tildes/tildes/views/topic.py

@ -277,6 +277,8 @@ def get_topic(request: Request, comment_order: CommentSortOption) -> dict:
.all() .all()
) )
tree.collapse_from_tags(request.user)
# if the user has the "mark new comments" feature enabled # if the user has the "mark new comments" feature enabled
if request.user and request.user.track_comment_visits: if request.user and request.user.track_comment_visits:
# update their last visit time for this topic # update their last visit time for this topic

Loading…
Cancel
Save