Skip to content

Commit

Permalink
Feat/hide deleted comments (#1720)
Browse files Browse the repository at this point in the history
filter out soft deleted comments unless explicit filter is added to include them
include in filter serializer
only include deleted comments if they have non-deleted children
  • Loading branch information
lsabor authored Dec 16, 2024
1 parent 5f83d3c commit 2655b5f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions comments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class CommentFilterSerializer(serializers.Serializer):
sort = serializers.CharField(required=False, allow_null=True)
focus_comment_id = serializers.IntegerField(required=False, allow_null=True)
is_private = serializers.BooleanField(required=False, allow_null=True)
include_deleted = serializers.BooleanField(required=False, allow_null=True)

def validate_post(self, value: int):
try:
Expand Down
6 changes: 6 additions & 0 deletions comments/services/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def get_comments_feed(
sort=None,
is_private=None,
focus_comment_id: int = None,
include_deleted=False,
):
user = user if user and user.is_authenticated else None

Expand All @@ -27,6 +28,11 @@ def get_comments_feed(
else:
qs = qs.filter(is_private=False)

if not include_deleted:
qs = qs.filter(
Q(is_soft_deleted=False) | Q(child_comments__is_soft_deleted=False)
).distinct()

qs = qs.annotate_vote_score()

order_by_args = []
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/test_comments/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,17 @@ def test_get_comments_feed_permissions(user1, user2):
c2 = factory_comment(author=user2, on_post=post, is_private=True, text="Comment 2")
c3 = factory_comment(author=user2, on_post=post, text="Comment 3")

c_deleted = factory_comment(author=user2, on_post=post, is_soft_deleted=True)
factory_comment(author=user2, on_post=post, is_soft_deleted=True)

assert {c.pk for c in get_comments_feed(Comment.objects.all())} == {
c_deleted.pk,
c3.pk,
}
assert {c.pk for c in get_comments_feed(Comment.objects.all(), user=user1)} == {
c_deleted.pk,
c3.pk,
}
assert {c.pk for c in get_comments_feed(Comment.objects.all(), user=user2)} == {
c1.pk,
c3.pk,
c_deleted.pk,
}
assert {
c.pk
Expand Down

0 comments on commit 2655b5f

Please sign in to comment.