diff --git a/api/v1_users_history.go b/api/v1_users_history.go index dc836000..f7e97aca 100644 --- a/api/v1_users_history.go +++ b/api/v1_users_history.go @@ -1,6 +1,7 @@ package api import ( + "strings" "time" "bridgerton.audius.co/api/dbv1" @@ -61,6 +62,20 @@ func (app *ApiServer) v1UsersHistory(c *fiber.Ctx) error { class = "track_activity_full" } + filters := []string{} + if params.Query != "" { + filters = append(filters, "tracks.title ILIKE '%' || @query || '%' OR users.name ILIKE '%' || @query || '%'") + } + + if myId == 0 || myId != userId { + filters = append(filters, "tracks.is_unlisted = false") + } + + filterString := "" + if len(filters) > 0 { + filterString = "WHERE " + strings.Join(filters, " AND ") + } + sql := ` WITH history AS ( SELECT @@ -79,8 +94,7 @@ func (app *ApiServer) v1UsersHistory(c *fiber.Ctx) error { LEFT JOIN aggregate_track ON tracks.track_id = aggregate_track.track_id LEFT JOIN users ON tracks.owner_id = users.user_id LEFT JOIN aggregate_plays ON history.track_id = aggregate_plays.play_item_id - WHERE tracks.title ILIKE '%' || @query || '%' - OR users.name ILIKE '%' || @query || '%' + ` + filterString + ` ` + orderBy + ` LIMIT @limit OFFSET @offset ; diff --git a/api/v1_users_history_test.go b/api/v1_users_history_test.go index ec7d4687..c85f280e 100644 --- a/api/v1_users_history_test.go +++ b/api/v1_users_history_test.go @@ -3,6 +3,8 @@ package api import ( "testing" + "bridgerton.audius.co/database" + "bridgerton.audius.co/trashid" "github.com/stretchr/testify/assert" ) @@ -64,3 +66,76 @@ func TestUserListeningHistory(t *testing.T) { "data.0.item.title": "Trending Gated Jazz Track 1", }) } + +func TestUserListeningHistoryUnlisted(t *testing.T) { + app := emptyTestApp(t) + + fixtures := database.FixtureMap{ + "users": []map[string]any{ + { + "user_id": 1, + "handle": "user1", + }, + { + "user_id": 2, + "handle": "user2", + "wallet": "0x7d273271690538cf855e5b3002a0dd8c154bb060", + }, + }, + "tracks": []map[string]any{ + { + "track_id": 1, + "title": "Public Track", + "owner_id": 1, + "is_unlisted": false, + "created_at": parseTime(t, "2024-01-01"), + }, + { + "track_id": 2, + "title": "Unlisted Track", + "owner_id": 1, + "is_unlisted": true, + "created_at": parseTime(t, "2024-01-01"), + }, + }, + "user_listening_history": []map[string]any{ + { + "user_id": 2, + "listening_history": []map[string]any{ + { + "track_id": 1, + "play_count": 1, + "timestamp": parseTime(t, "2024-01-01"), + }, + { + "track_id": 2, + "play_count": 1, + "timestamp": parseTime(t, "2024-01-01"), + }, + }, + }, + }, + } + + database.Seed(app.pool.Replicas[0], fixtures) + user2Id := trashid.MustEncodeHashID(2) + + { + status, body := testGetWithWallet(t, app, "/v1/full/users/"+user2Id+"/history/tracks?user_id="+user2Id, "0x7d273271690538cf855e5b3002a0dd8c154bb060") + assert.Equal(t, 200, status) + jsonAssert(t, body, map[string]any{ + "data.#": 2, + "data.0.item_id": 1, + "data.1.item_id": 2, + }) + } + + { + status, body := testGet(t, app, "/v1/full/users/"+user2Id+"/history/tracks") + assert.Equal(t, 200, status) + jsonAssert(t, body, map[string]any{ + "data.#": 1, + "data.0.item_id": 1, + }) + } +}