Skip to content

Commit 05c7f79

Browse files
dylanjeffersclaude
andcommitted
Include private playlists/albums in count for own profile
The album_count and playlist_count fields in aggregate_user only counted public items. This caused the mobile profile to briefly flash "no albums" before hidden collections loaded, since the count was 0 but the list endpoint correctly returned private items to the owner. Adds total_playlist_count and total_album_count columns that include private items, and returns them when viewing your own profile (same pattern as track_count/total_track_count). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0247f66 commit 05c7f79

6 files changed

Lines changed: 90 additions & 8 deletions

File tree

api/dbv1/get_users.sql.go

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/dbv1/models.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/dbv1/queries/get_users.sql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
-- name: GetUsers :many
22
SELECT
3-
album_count,
3+
-- Use total_album_count when viewing own profile, otherwise use album_count
4+
(CASE
5+
WHEN u.user_id = @my_id::int THEN total_album_count
6+
ELSE album_count
7+
END)::bigint as album_count,
48
artist_pick_track_id,
59
bio,
610

@@ -24,7 +28,11 @@ SELECT
2428
donation,
2529
location,
2630
name,
27-
playlist_count,
31+
-- Use total_playlist_count when viewing own profile, otherwise use playlist_count
32+
(CASE
33+
WHEN u.user_id = @my_id::int THEN total_playlist_count
34+
ELSE playlist_count
35+
END)::bigint as playlist_count,
2836
profile_type,
2937

3038
-- todo: this can sometimes be a Qm cid

ddl/functions/handle_playlist.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@ begin
4646
where user_id = new.playlist_owner_id;
4747
end if;
4848
end if;
49+
50+
-- Update total counts (includes private items)
51+
declare
52+
total_delta int := 0;
53+
begin
54+
if (new.is_delete = true and new.is_current = true) and (old_row.is_delete = false) then
55+
total_delta := -1;
56+
end if;
57+
58+
if (old_row is null) then
59+
total_delta := 1;
60+
end if;
61+
62+
if total_delta != 0 then
63+
if new.is_album then
64+
update aggregate_user
65+
set total_album_count = total_album_count + total_delta
66+
where user_id = new.playlist_owner_id;
67+
else
68+
update aggregate_user
69+
set total_playlist_count = total_playlist_count + total_delta
70+
where user_id = new.playlist_owner_id;
71+
end if;
72+
end if;
73+
end;
74+
4975
-- Create playlist notification
5076
begin
5177
if new.is_private = FALSE AND
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
begin;
2+
3+
alter table aggregate_user add column if not exists total_playlist_count int default 0;
4+
alter table aggregate_user add column if not exists total_album_count int default 0;
5+
6+
UPDATE aggregate_user au
7+
SET total_playlist_count = playlist_stats.total_count
8+
FROM (
9+
SELECT
10+
p.playlist_owner_id AS user_id,
11+
COUNT(*) AS total_count
12+
FROM playlists p
13+
WHERE
14+
p.is_current IS TRUE
15+
AND p.is_delete IS FALSE
16+
AND p.is_album IS FALSE
17+
GROUP BY p.playlist_owner_id
18+
) AS playlist_stats
19+
WHERE au.user_id = playlist_stats.user_id;
20+
21+
UPDATE aggregate_user au
22+
SET total_album_count = album_stats.total_count
23+
FROM (
24+
SELECT
25+
p.playlist_owner_id AS user_id,
26+
COUNT(*) AS total_count
27+
FROM playlists p
28+
WHERE
29+
p.is_current IS TRUE
30+
AND p.is_delete IS FALSE
31+
AND p.is_album IS TRUE
32+
GROUP BY p.playlist_owner_id
33+
) AS album_stats
34+
WHERE au.user_id = album_stats.user_id;
35+
36+
commit;

sql/01_schema.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5825,7 +5825,9 @@ CREATE TABLE public.aggregate_user (
58255825
dominant_genre_count integer DEFAULT 0,
58265826
score integer DEFAULT 0,
58275827
total_track_count integer DEFAULT 0,
5828-
track_share_count integer DEFAULT 0
5828+
track_share_count integer DEFAULT 0,
5829+
total_playlist_count integer DEFAULT 0,
5830+
total_album_count integer DEFAULT 0
58295831
);
58305832

58315833

0 commit comments

Comments
 (0)