From 24651590fe5ea0bcdce0108209062b16181d7766 Mon Sep 17 00:00:00 2001 From: Deimos Date: Wed, 12 Sep 2018 20:13:51 -0600 Subject: [PATCH] Move viewer into CommentTree object Just a little simpler instead of needing to pass the viewer separately to different methods. --- tildes/tildes/models/comment/comment_tree.py | 16 +++++++++++----- tildes/tildes/views/topic.py | 6 +++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/tildes/tildes/models/comment/comment_tree.py b/tildes/tildes/models/comment/comment_tree.py index ddbfa21..86d0808 100644 --- a/tildes/tildes/models/comment/comment_tree.py +++ b/tildes/tildes/models/comment/comment_tree.py @@ -18,10 +18,16 @@ from .comment import Comment class CommentTree: """Class representing the tree of comments on a particular topic.""" - def __init__(self, comments: Sequence[Comment], sort: CommentSortOption) -> None: + def __init__( + self, + comments: Sequence[Comment], + sort: CommentSortOption, + viewer: Optional[User] = None, + ) -> None: """Create a sorted CommentTree from a flat list of Comments.""" self.tree: List[CommentInTree] = [] self.sort = sort + self.viewer = viewer # sort the comments by date, since replies will always be posted later this will # ensure that parent comments are always processed first @@ -164,17 +170,17 @@ class CommentTree: order=self.sort.name, ) - def collapse_from_tags(self, viewer: Optional[User]) -> None: + def collapse_from_tags(self) -> 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: + if comment.user == self.viewer: continue if comment.tag_counts["noise"] >= 2: comment.collapsed_state = "full" - def uncollapse_new_comments(self, viewer: User, threshold: datetime) -> None: + def uncollapse_new_comments(self, threshold: datetime) -> None: """Mark comments newer than the threshold (and parents) to stay uncollapsed.""" for comment in reversed(self.comments): # as soon as we reach an old comment, we can stop @@ -189,7 +195,7 @@ class CommentTree: continue # don't apply to the viewer's own comments - if comment.user == viewer: + if comment.user == self.viewer: continue # uncollapse the comment diff --git a/tildes/tildes/views/topic.py b/tildes/tildes/views/topic.py index 4ae6310..d7257e4 100644 --- a/tildes/tildes/views/topic.py +++ b/tildes/tildes/views/topic.py @@ -258,7 +258,7 @@ def get_topic(request: Request, comment_order: CommentSortOption) -> dict: .order_by(Comment.created_time) .all() ) - tree = CommentTree(comments, comment_order) + tree = CommentTree(comments, comment_order, request.user) # check if there are any items in the log to show visible_events = ( @@ -277,7 +277,7 @@ def get_topic(request: Request, comment_order: CommentSortOption) -> dict: .all() ) - tree.collapse_from_tags(request.user) + tree.collapse_from_tags() # if the user has the "mark new comments" feature enabled if request.user and request.user.track_comment_visits: @@ -289,7 +289,7 @@ def get_topic(request: Request, comment_order: CommentSortOption) -> dict: # collapse old comments if the user has a previous visit to the topic # (and doesn't have that behavior disabled) if topic.last_visit_time and request.user.collapse_old_comments: - tree.uncollapse_new_comments(request.user, topic.last_visit_time) + tree.uncollapse_new_comments(topic.last_visit_time) tree.finalize_collapsing_maximized() return {