diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 167b5471..06398eeb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -44,7 +44,7 @@ jobs: done - name: Run tests - run: go test -cover ./... + run: go test -count=1 -cover ./... - name: Shutdown diff --git a/api/dbv1/get_users.sql.go b/api/dbv1/get_users.sql.go index 5824eb07..e3ffa573 100644 --- a/api/dbv1/get_users.sql.go +++ b/api/dbv1/get_users.sql.go @@ -39,6 +39,7 @@ current_user_subscribed_targets AS ( FROM subscriptions s JOIN input_users i ON i.user_id = s.user_id WHERE $1 > 0 + AND s.entity_type = 'User' AND s.subscriber_id = $1 AND s.is_delete = false GROUP BY s.user_id diff --git a/api/dbv1/queries/get_users.sql b/api/dbv1/queries/get_users.sql index 468a76b8..2b9c1389 100644 --- a/api/dbv1/queries/get_users.sql +++ b/api/dbv1/queries/get_users.sql @@ -23,6 +23,7 @@ current_user_subscribed_targets AS ( FROM subscriptions s JOIN input_users i ON i.user_id = s.user_id WHERE @my_id > 0 + AND s.entity_type = 'User' AND s.subscriber_id = @my_id AND s.is_delete = false GROUP BY s.user_id diff --git a/api/v1_events_followers_test.go b/api/v1_events_followers_test.go index 6f5e937f..6d236b58 100644 --- a/api/v1_events_followers_test.go +++ b/api/v1_events_followers_test.go @@ -173,7 +173,7 @@ func TestEventFollowState_CountsOnlyLiveEventSubscriptions(t *testing.T) { // A legacy user-type subscription with matching numeric id — // must NOT be counted. { - "subscriber_id": 2, + "subscriber_id": 5, "user_id": 200, "entity_type": "User", "entity_id": nil, @@ -514,7 +514,12 @@ func TestEventsFollowers_ReturnsOnlyLiveEventSubscribers(t *testing.T) { app := emptyTestApp(t) database.Seed(app.pool.Replicas[0], database.FixtureMap{ - "users": testEventFollowersBaseUsers(), + "users": append(testEventFollowersBaseUsers(), map[string]any{ + "user_id": 4, + "handle": "legacyfan", + "handle_lc": "legacyfan", + "name": "Legacy Fan", + }), "tracks": { { "track_id": 1, @@ -561,7 +566,7 @@ func TestEventsFollowers_ReturnsOnlyLiveEventSubscribers(t *testing.T) { // Legacy user-type subscription with a colliding numeric id — // must NOT show up. { - "subscriber_id": 1, + "subscriber_id": 4, "user_id": 200, "entity_type": "User", "entity_id": nil, diff --git a/api/v1_users_subscribers.go b/api/v1_users_subscribers.go index 27116c82..78c53e4a 100644 --- a/api/v1_users_subscribers.go +++ b/api/v1_users_subscribers.go @@ -20,7 +20,8 @@ func (app *ApiServer) v1UsersSubscribers(c *fiber.Ctx) error { FROM subscriptions WHERE - user_id = @userId + entity_type = 'User' + AND user_id = @userId AND is_current = true AND is_delete = false ORDER BY diff --git a/api/v1_users_subscribers_test.go b/api/v1_users_subscribers_test.go index 3c8bf897..98e3a3b7 100644 --- a/api/v1_users_subscribers_test.go +++ b/api/v1_users_subscribers_test.go @@ -18,6 +18,7 @@ func TestUsersSubscribers(t *testing.T) { {"user_id": 3, "handle": "subscriber3", "name": "Subscriber 3"}, {"user_id": 4, "handle": "deletedsub", "name": "Deleted Sub"}, {"user_id": 5, "handle": "oldsub", "name": "Old Sub"}, + {"user_id": 6, "handle": "eventonlyfan", "name": "Event Only Fan"}, }, "aggregate_user": []map[string]any{ {"user_id": 1, "track_count": 1}, @@ -25,17 +26,19 @@ func TestUsersSubscribers(t *testing.T) { {"user_id": 3, "track_count": 1}, {"user_id": 4, "track_count": 1}, {"user_id": 5, "track_count": 1}, + {"user_id": 6, "track_count": 1}, }, } database.Seed(app.pool.Replicas[0], fixtures) _, err := app.pool.Exec(t.Context(), ` - INSERT INTO subscriptions (user_id, subscriber_id, is_current, is_delete, txhash) + INSERT INTO subscriptions (user_id, subscriber_id, is_current, is_delete, txhash, entity_type, entity_id) VALUES - (1, 3, TRUE, FALSE, 'tx-sub-3'), - (1, 2, TRUE, FALSE, 'tx-sub-2'), - (1, 4, TRUE, TRUE, 'tx-sub-deleted'), - (1, 5, FALSE, FALSE, 'tx-sub-not-current') + (1, 3, TRUE, FALSE, 'tx-sub-3', 'User', NULL), + (1, 2, TRUE, FALSE, 'tx-sub-2', 'User', NULL), + (1, 4, TRUE, TRUE, 'tx-sub-deleted', 'User', NULL), + (1, 5, FALSE, FALSE, 'tx-sub-not-current', 'User', NULL), + (1, 6, TRUE, FALSE, 'tx-sub-event-collision', 'Event', 1) `) assert.NoError(t, err) diff --git a/api/v1_users_test.go b/api/v1_users_test.go index 8b3cd320..ec5347b7 100644 --- a/api/v1_users_test.go +++ b/api/v1_users_test.go @@ -5,6 +5,7 @@ import ( "testing" "api.audius.co/api/dbv1" + "api.audius.co/database" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -87,6 +88,51 @@ func TestUserQuery(t *testing.T) { } } +func TestUserQuery_DoesCurrentUserSubscribeIgnoresEventSubscriptions(t *testing.T) { + app := emptyTestApp(t) + + database.Seed(app.pool.Replicas[0], database.FixtureMap{ + "users": []map[string]any{ + {"user_id": 1, "handle": "artist", "name": "Artist"}, + {"user_id": 2, "handle": "eventhost", "name": "Event Host"}, + {"user_id": 3, "handle": "viewer", "name": "Viewer"}, + }, + "subscriptions": []map[string]any{ + { + "subscriber_id": 3, + "user_id": 1, + "entity_type": "User", + "entity_id": nil, + "is_current": true, + "is_delete": false, + "txhash": "tx-user-sub", + }, + { + "subscriber_id": 3, + "user_id": 2, + "entity_type": "Event", + "entity_id": 2, + "is_current": true, + "is_delete": false, + "txhash": "tx-event-collision", + }, + }, + }) + + users, err := app.queries.Users(t.Context(), dbv1.GetUsersParams{ + MyID: 3, + Ids: []int32{1, 2}, + }) + assert.NoError(t, err) + require.Len(t, users, 2) + byID := map[int32]dbv1.User{} + for _, user := range users { + byID[user.UserID] = user + } + assert.True(t, byID[1].DoesCurrentUserSubscribe) + assert.False(t, byID[2].DoesCurrentUserSubscribe) +} + func TestGetUsers(t *testing.T) { app := testAppWithFixtures(t) var userResponse struct { diff --git a/ddl/functions/handle_playlist.sql b/ddl/functions/handle_playlist.sql index 6c844935..be452a8e 100644 --- a/ddl/functions/handle_playlist.sql +++ b/ddl/functions/handle_playlist.sql @@ -60,6 +60,7 @@ begin from subscriptions where is_current and not is_delete and + entity_type = 'User' and user_id=new.playlist_owner_id ) into subscriber_user_ids; if array_length(subscriber_user_ids, 1) > 0 then diff --git a/ddl/functions/handle_track.sql b/ddl/functions/handle_track.sql index 32b54790..0d28d855 100644 --- a/ddl/functions/handle_track.sql +++ b/ddl/functions/handle_track.sql @@ -52,6 +52,7 @@ begin from subscriptions where is_current and not is_delete and + entity_type = 'User' and user_id=new.owner_id ) into subscriber_user_ids; diff --git a/sql/01_schema.sql b/sql/01_schema.sql index 8ec65688..93751524 100644 --- a/sql/01_schema.sql +++ b/sql/01_schema.sql @@ -3537,6 +3537,7 @@ begin from subscriptions where is_current and not is_delete and + entity_type = 'User' and user_id=new.playlist_owner_id ) into subscriber_user_ids; if array_length(subscriber_user_ids, 1) > 0 then @@ -4784,6 +4785,7 @@ begin from subscriptions where is_current and not is_delete and + entity_type = 'User' and user_id=new.owner_id ) into subscriber_user_ids;