Skip to content

Commit

Permalink
[QA-1824] Don't filter own playlists in collection library queries (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
schottra authored Nov 14, 2024
1 parent ea4205f commit 6af8381
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ def populate_entries(db):
"release_date": datetime(2019, 6, 15),
"created_at": datetime(2019, 6, 15),
},
{
"track_id": 4,
"title": "track 4",
"owner_id": 1,
"is_unlisted": True,
"release_date": datetime(2019, 6, 15),
"created_at": datetime(2019, 6, 15),
},
{
"track_id": 5,
"title": "track 5",
"owner_id": 2,
"is_unlisted": True,
"release_date": datetime(2019, 6, 15),
"created_at": datetime(2019, 6, 15),
},
],
"playlists": [
# playlists -----
Expand All @@ -45,6 +61,44 @@ def populate_entries(db):
"created_at": datetime(2019, 6, 17),
"playlist_contents": {"track_ids": [{"track": 1}]},
},
# user 1 hidden playlist
{
"playlist_id": 5,
"playlist_owner_id": 1,
"is_album": False,
"is_private": True,
"created_at": datetime(2019, 6, 18),
"playlist_name": "asdf",
"playlist_contents": {"track_ids": [{"track": 1}, {"track": 2}]},
},
# user 1 playlist of all hidden tracks
{
"playlist_id": 6,
"playlist_owner_id": 1,
"is_album": False,
"created_at": datetime(2019, 6, 19),
"playlist_name": "asdf",
"playlist_contents": {"track_ids": [{"track": 4}]},
},
# user 2 hidden playlist
{
"playlist_id": 7,
"playlist_owner_id": 2,
"is_album": False,
"is_private": True,
"created_at": datetime(2019, 6, 17),
"playlist_name": "asdf",
"playlist_contents": {"track_ids": [{"track": 1}, {"track": 2}]},
},
# user 2 playlist of all hidden tracks
{
"playlist_id": 8,
"playlist_owner_id": 2,
"is_album": False,
"created_at": datetime(2019, 6, 18),
"playlist_name": "asdf",
"playlist_contents": {"track_ids": [{"track": 5}]},
},
# reposted
{
"playlist_id": 3,
Expand Down Expand Up @@ -94,7 +148,21 @@ def populate_entries(db):
"save_type": "album",
"user_id": 1,
"created_at": datetime(2019, 6, 16),
}
},
# saved hidden playlist
{
"save_item_id": 7,
"save_type": "playlist",
"user_id": 1,
"created_at": datetime(2019, 6, 17),
},
# saved playlist of all hidden tracks
{
"save_item_id": 8,
"save_type": "playlist",
"user_id": 1,
"created_at": datetime(2019, 6, 18),
},
],
}
populate_mock_db(db, test_entities)
Expand Down Expand Up @@ -134,9 +202,18 @@ def test_distinguishes_albums_vs_favorites(session):

args["collection_type"] = CollectionType.playlist
res = _get_collection_library(args, session)
assert len(res) == 2
assert_correct_collection(res, 0, 1, "should only playlists")
assert_correct_collection(res, 1, 3, "should only playlists")
assert len(res) == 4
assert_correct_collection(
res,
0,
6,
"should return playlists of all hidden tracks belonging to current user",
)
assert_correct_collection(
res, 1, 5, "should return hidden playlists belonging to user"
)
assert_correct_collection(res, 2, 1, "should get only playlists")
assert_correct_collection(res, 3, 3, "should get only playlists")


@with_collection_library_setup
Expand Down Expand Up @@ -196,7 +273,7 @@ def test_sort_orders(session):
args["sort_method"] = SortMethod.reposts
args["collection_type"] = CollectionType.playlist
res = _get_collection_library(args, session)
assert len(res) == 2
assert len(res) == 4
assert_correct_collection(res, 0, 3, "sort by desc repost")
assert_correct_collection(res, 1, 1, "sort by desc repost")

Expand Down
17 changes: 13 additions & 4 deletions packages/discovery-provider/src/queries/get_collection_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def _get_collection_library(args: GetCollectionLibraryArgs, session):
if not query_results:
return []

playlists, activity_timestamp = zip(*query_results)
playlists = helpers.query_result_to_list(playlists)
playlist_results, activity_timestamp = zip(*query_results)
playlists = helpers.query_result_to_list(playlist_results)
playlist_ids = list(map(lambda playlist: playlist["playlist_id"], playlists))

# Populate playlists
Expand Down Expand Up @@ -222,9 +222,15 @@ def _get_collection_library(args: GetCollectionLibraryArgs, session):
)
album_purchases = set([p[0] for p in album_purchases])

# We don't want to filter any of the user's own playlists in the logic below
own_playlist_ids = {
p["playlist_id"] for p in playlists if p["playlist_owner_id"] == user_id
}

playlists = list(
filter(
lambda playlist: not playlist["is_private"]
lambda playlist: playlist["playlist_id"] in own_playlist_ids
or not playlist["is_private"]
or playlist["playlist_id"] in album_purchases,
playlists,
)
Expand All @@ -242,7 +248,10 @@ def _get_collection_library(args: GetCollectionLibraryArgs, session):

# exclude playlists with only hidden tracks and empty playlists
playlists = filter_playlists_with_only_hidden_tracks(
session, playlists, playlist_track_ids, ignore_ids=list(album_purchases)
session,
playlists,
playlist_track_ids,
ignore_ids=list(album_purchases | own_playlist_ids),
)

# attach users
Expand Down

0 comments on commit 6af8381

Please sign in to comment.