Browse Source

Move viewer into CommentTree object

Just a little simpler instead of needing to pass the viewer separately
to different methods.
merge-requests/37/head
Deimos 6 years ago
parent
commit
24651590fe
  1. 16
      tildes/tildes/models/comment/comment_tree.py
  2. 6
      tildes/tildes/views/topic.py

16
tildes/tildes/models/comment/comment_tree.py

@ -18,10 +18,16 @@ from .comment import Comment
class CommentTree: class CommentTree:
"""Class representing the tree of comments on a particular topic.""" """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.""" """Create a sorted CommentTree from a flat list of Comments."""
self.tree: List[CommentInTree] = [] self.tree: List[CommentInTree] = []
self.sort = sort self.sort = sort
self.viewer = viewer
# sort the comments by date, since replies will always be posted later this will # sort the comments by date, since replies will always be posted later this will
# ensure that parent comments are always processed first # ensure that parent comments are always processed first
@ -164,17 +170,17 @@ class CommentTree:
order=self.sort.name, 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.""" """Collapse comments based on how they've been tagged."""
for comment in self.comments: for comment in self.comments:
# never affect the viewer's own comments # never affect the viewer's own comments
if viewer and comment.user == viewer:
if comment.user == self.viewer:
continue continue
if comment.tag_counts["noise"] >= 2: if comment.tag_counts["noise"] >= 2:
comment.collapsed_state = "full" 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.""" """Mark comments newer than the threshold (and parents) to stay uncollapsed."""
for comment in reversed(self.comments): for comment in reversed(self.comments):
# as soon as we reach an old comment, we can stop # as soon as we reach an old comment, we can stop
@ -189,7 +195,7 @@ class CommentTree:
continue continue
# don't apply to the viewer's own comments # don't apply to the viewer's own comments
if comment.user == viewer:
if comment.user == self.viewer:
continue continue
# uncollapse the comment # uncollapse the comment

6
tildes/tildes/views/topic.py

@ -258,7 +258,7 @@ def get_topic(request: Request, comment_order: CommentSortOption) -> dict:
.order_by(Comment.created_time) .order_by(Comment.created_time)
.all() .all()
) )
tree = CommentTree(comments, comment_order)
tree = CommentTree(comments, comment_order, request.user)
# check if there are any items in the log to show # check if there are any items in the log to show
visible_events = ( visible_events = (
@ -277,7 +277,7 @@ def get_topic(request: Request, comment_order: CommentSortOption) -> dict:
.all() .all()
) )
tree.collapse_from_tags(request.user)
tree.collapse_from_tags()
# 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:
@ -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 # collapse old comments if the user has a previous visit to the topic
# (and doesn't have that behavior disabled) # (and doesn't have that behavior disabled)
if topic.last_visit_time and request.user.collapse_old_comments: 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() tree.finalize_collapsing_maximized()
return { return {

Loading…
Cancel
Save