diff --git a/packages/discovery-provider/integration_tests/queries/test_get_comments.py b/packages/discovery-provider/integration_tests/queries/test_get_comments.py index 12cf895bcab..dfff90ae797 100644 --- a/packages/discovery-provider/integration_tests/queries/test_get_comments.py +++ b/packages/discovery-provider/integration_tests/queries/test_get_comments.py @@ -188,6 +188,95 @@ def test_get_muted_comments(app): assert comments[0]["is_muted"] == True +def test_get_comments_from_muted_user_by_track_owner(app): + entities = { + "comments": [ + { + "comment_id": 1, + "user_id": 1, + "entity_id": 1, + "entity_type": "Track", + "created_at": datetime(2022, 1, 1), + "track_timestamp_s": 1, + }, + ], + "muted_users": [ + { + "muted_user_id": 1, + "user_id": 10, # the owner of this track + } + ], + "tracks": [{"track_id": 1, "owner_id": 10}], + } + + with app.app_context(): + db = get_db() + populate_mock_db(db, entities) + + # to a third party user, there should be no comments + # because the track owner muted the user + comments = get_track_comments({}, 1, current_user_id=2) + assert len(comments) == 0 + # the muted user should see their own comment + comments = get_track_comments({}, 1, current_user_id=1) + assert len(comments) == 1 + # the person who muted the user should not see the comment + comments = get_track_comments({}, 1, current_user_id=10) + assert len(comments) == 0 + + +def test_get_comment_replies_from_muted_user_by_track_owner(app): + entities = { + "comments": [ + { + "comment_id": 1, + "user_id": 1, + "entity_id": 1, + "entity_type": "Track", + "created_at": datetime(2022, 1, 1), + "track_timestamp_s": 1, + }, + { + "comment_id": 2, + "user_id": 1, + "entity_id": 1, + "entity_type": "Track", + "created_at": datetime(2022, 1, 2), + "track_timestamp_s": 2, + }, + ], + "comment_threads": [{"parent_comment_id": 1, "comment_id": 2}], + "muted_users": [ + { + "muted_user_id": 1, + "user_id": 10, # the owner of this track + } + ], + "tracks": [{"track_id": 1, "owner_id": 10}], + } + + with app.app_context(): + db = get_db() + populate_mock_db(db, entities) + + # to a third party user, there should be no comment replies + # because the track owner muted the user + comments = get_paginated_replies( + {"limit": 10, "offset": 0, "sort_method": "newest"}, 1, current_user_id=2 + ) + assert len(comments) == 0 + # the muted user should see their own comment + comments = get_paginated_replies( + {"limit": 10, "offset": 0, "sort_method": "newest"}, 1, current_user_id=1 + ) + assert len(comments) == 1 + # the person who muted the user should not see the comment + comments = get_paginated_replies( + {"limit": 10, "offset": 0, "sort_method": "newest"}, 1, current_user_id=10 + ) + assert len(comments) == 0 + + def test_get_deleted_comments(app): entities = { "comments": [ diff --git a/packages/discovery-provider/src/queries/get_comments.py b/packages/discovery-provider/src/queries/get_comments.py index 17e16b30a20..7213f5dec3a 100644 --- a/packages/discovery-provider/src/queries/get_comments.py +++ b/packages/discovery-provider/src/queries/get_comments.py @@ -99,9 +99,9 @@ def get_replies( MutedUser.muted_user_id == Comment.user_id, or_( MutedUser.user_id == current_user_id, + MutedUser.muted_user_id == current_user_id, MutedUser.muted_user_id.in_(muted_by_karma), ), - current_user_id != Comment.user_id, ), ) .outerjoin(CommentReport, Comment.comment_id == CommentReport.comment_id) @@ -109,10 +109,6 @@ def get_replies( .filter( CommentThread.parent_comment_id == parent_comment_id, Comment.is_delete == False, - or_( - MutedUser.muted_user_id == None, - MutedUser.is_delete == True, - ), # Exclude muted users' comments or_( CommentReport.comment_id == None, and_( @@ -121,6 +117,19 @@ def get_replies( ), CommentReport.is_delete == True, ), + or_( + # Include comments where muting was undone + MutedUser.is_delete == True, + # Include comments where the current user is muted + # (Even if I am muted, I can still see my own comments) + MutedUser.muted_user_id == current_user_id, + # Include comments where the artist has not muted the commenter + and_( + MutedUser.muted_user_id == Comment.user_id, + MutedUser.user_id != artist_id, + artist_id == current_user_id, + ), + ), ) .order_by(asc(Comment.created_at)) .offset(offset) @@ -281,9 +290,9 @@ def get_track_comments(args, track_id, current_user_id=None): MutedUser.muted_user_id == Comment.user_id, or_( MutedUser.user_id == current_user_id, + MutedUser.muted_user_id == current_user_id, MutedUser.muted_user_id.in_(muted_by_karma), ), - current_user_id != Comment.user_id, # show comment to comment owner ), ) .outerjoin( @@ -314,9 +323,18 @@ def get_track_comments(args, track_id, current_user_id=None): CommentReport.is_delete == True, ), or_( - MutedUser.muted_user_id == None, + # Include comments where muting was undone MutedUser.is_delete == True, - ), # Exclude muted users' comments + # Include comments where the current user is muted + # (Even if I am muted, I can still see my own comments) + MutedUser.muted_user_id == current_user_id, + # Include comments where the artist has not muted the commenter + and_( + MutedUser.muted_user_id == Comment.user_id, + MutedUser.user_id != artist_id, + artist_id == current_user_id, + ), + ), ) .having( (func.count(ReplyCountAlias.comment_id) > 0)