Skip to content

Commit f0c401b

Browse files
Make playlists the same as v1 (#42)
* Add missing playlist fields * Fix test * Remove comment
1 parent 1608034 commit f0c401b

5 files changed

Lines changed: 84 additions & 48 deletions

File tree

api/dbv1/full_playlists.go

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dbv1
22

33
import (
44
"context"
5+
"fmt"
56

67
"bridgerton.audius.co/trashid"
78
"github.com/jackc/pgx/v5/pgtype"
@@ -10,12 +11,13 @@ import (
1011
type FullPlaylist struct {
1112
GetPlaylistsRow
1213

13-
ID string `json:"id"`
14-
Artwork *SquareImage `json:"artwork"`
15-
UserID trashid.HashId `json:"user_id"`
16-
User FullUser `json:"user"`
17-
Tracks []FullTrack `json:"tracks"`
18-
Access Access `json:"access"`
14+
ID string `json:"id"`
15+
Artwork *SquareImage `json:"artwork"`
16+
UserID trashid.HashId `json:"user_id"`
17+
User FullUser `json:"user"`
18+
Tracks []FullTrack `json:"tracks"`
19+
Access Access `json:"access"`
20+
Permalink string `json:"permalink"`
1921

2022
FolloweeReposts []*FolloweeRepost `json:"followee_reposts"`
2123
FolloweeFavorites []*FolloweeFavorite `json:"followee_favorites"`
@@ -89,6 +91,13 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg GetPlaylistsParams
8991
streamAccess := q.GetPlaylistAccess(ctx, arg.MyID.(int32), playlist.StreamConditions, &playlist, &user)
9092
downloadAccess := streamAccess
9193

94+
var playlistType string
95+
if playlist.IsAlbum {
96+
playlistType = "album"
97+
} else {
98+
playlistType = "playlist"
99+
}
100+
92101
playlistMap[playlist.PlaylistID] = FullPlaylist{
93102
GetPlaylistsRow: playlist,
94103
ID: id,
@@ -99,6 +108,7 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg GetPlaylistsParams
99108
FolloweeFavorites: fullFolloweeFavorites(playlist.FolloweeFavorites),
100109
FolloweeReposts: fullFolloweeReposts(playlist.FolloweeReposts),
101110
PlaylistContents: fullPlaylistContents,
111+
Permalink: fmt.Sprintf("/%s/%s/%s", user.Handle.String, playlistType, playlist.Slug.String),
102112
AddedTimestamps: fullPlaylistContents,
103113
Access: Access{
104114
Stream: streamAccess,
@@ -129,20 +139,22 @@ func (q *Queries) FullPlaylists(ctx context.Context, arg GetPlaylistsParams) ([]
129139
}
130140

131141
type MinPlaylist struct {
132-
ID string `json:"id"`
133-
PlaylistName pgtype.Text `json:"playlist_name"`
134-
PlaylistOwnerID int32 `json:"playlist_owner_id"`
135-
PlaylistID int32 `json:"playlist_id"`
136-
Artwork *SquareImage `json:"artwork"`
137-
Description *string `json:"description"`
138-
PlaylistContents interface{} `json:"playlist_contents"`
139-
IsAlbum bool `json:"is_album"`
140-
IsPrivate bool `json:"is_private"`
141-
FavoriteCount int32 `json:"favorite_count"`
142-
RepostCount int32 `json:"repost_count"`
143-
UserID trashid.HashId `json:"user_id"`
144-
User MinUser `json:"user"`
145-
Tracks []MinTrack `json:"tracks"`
142+
ID string `json:"id"`
143+
PlaylistName pgtype.Text `json:"playlist_name"`
144+
Artwork *SquareImage `json:"artwork"`
145+
Access Access `json:"access"`
146+
Description string `json:"description"`
147+
IsImageAutogenerated bool `json:"is_image_autogenerated"`
148+
Upc string `json:"upc"`
149+
DdexApp string `json:"ddex_app"`
150+
PlaylistContents interface{} `json:"playlist_contents"`
151+
TrackCount int32 `json:"track_count"`
152+
TotalPlayCount int64 `json:"total_play_count"`
153+
IsAlbum bool `json:"is_album"`
154+
FavoriteCount int32 `json:"favorite_count"`
155+
RepostCount int32 `json:"repost_count"`
156+
User MinUser `json:"user"`
157+
Permalink string `json:"permalink"`
146158
}
147159

148160
func ToMinPlaylist(fullPlaylist FullPlaylist) MinPlaylist {
@@ -152,20 +164,30 @@ func ToMinPlaylist(fullPlaylist FullPlaylist) MinPlaylist {
152164
}
153165

154166
return MinPlaylist{
155-
ID: fullPlaylist.ID,
156-
PlaylistName: fullPlaylist.PlaylistName,
157-
PlaylistOwnerID: fullPlaylist.PlaylistOwnerID,
158-
PlaylistID: fullPlaylist.PlaylistID,
159-
Artwork: fullPlaylist.Artwork,
160-
PlaylistContents: fullPlaylist.PlaylistContents,
161-
Description: nil,
162-
IsAlbum: false,
163-
IsPrivate: false,
164-
FavoriteCount: 0,
165-
RepostCount: 0,
166-
UserID: fullPlaylist.UserID,
167-
User: ToMinUser(fullPlaylist.User),
168-
Tracks: minTracks,
167+
ID: fullPlaylist.ID,
168+
PlaylistName: fullPlaylist.PlaylistName,
169+
Artwork: fullPlaylist.Artwork,
170+
Access: fullPlaylist.Access,
171+
Upc: fullPlaylist.Upc.String,
172+
DdexApp: fullPlaylist.DdexApp.String,
173+
PlaylistContents: fullPlaylist.PlaylistContents,
174+
Description: fullPlaylist.Description.String,
175+
IsImageAutogenerated: fullPlaylist.IsImageAutogenerated,
176+
IsAlbum: fullPlaylist.IsAlbum,
177+
TrackCount: int32(len(fullPlaylist.Tracks)),
178+
TotalPlayCount: func() int64 {
179+
var total int64
180+
for _, track := range fullPlaylist.Tracks {
181+
if track.PlayCount.Valid {
182+
total += track.PlayCount.Int64
183+
}
184+
}
185+
return total
186+
}(),
187+
FavoriteCount: int32(fullPlaylist.FavoriteCount.Int32),
188+
RepostCount: int32(fullPlaylist.RepostCount.Int32),
189+
User: ToMinUser(fullPlaylist.User),
190+
Permalink: fullPlaylist.Permalink,
169191
}
170192
}
171193

api/dbv1/get_playlists.sql.go

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

api/dbv1/queries/get_playlists.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
SELECT
33
-- artwork
44
p.description,
5-
-- permalink
65
-- id
76
p.is_album,
87
p.is_delete,
@@ -11,6 +10,8 @@ SELECT
1110
p.is_scheduled_release,
1211
p.is_stream_gated,
1312
p.stream_conditions,
13+
p.upc,
14+
p.ddex_app,
1415
-- is_streamable,
1516

1617
coalesce(playlist_image_sizes_multihash, playlist_image_multihash) as artwork,
@@ -20,6 +21,7 @@ SELECT
2021
p.playlist_id,
2122
p.playlist_owner_id,
2223
p.playlist_contents,
24+
playlist_routes.slug as slug,
2325

2426
p.blocknumber,
2527

@@ -96,6 +98,7 @@ SELECT
9698

9799
FROM playlists p
98100
JOIN aggregate_playlist using (playlist_id)
101+
LEFT JOIN playlist_routes on p.playlist_id = playlist_routes.playlist_id and playlist_routes.is_current = true
99102
WHERE is_delete = false
100-
and playlist_id = ANY(@ids::int[])
103+
and p.playlist_id = ANY(@ids::int[])
101104
;

api/response_helpers.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ func v1UsersResponse(c *fiber.Ctx, users []dbv1.FullUser) error {
2727
})
2828
}
2929

30+
// Note: playlist response returned an array even though it's a single playlist
31+
// Done for backwards compatibility. Would be nice to get rid of this.
3032
func v1PlaylistResponse(c *fiber.Ctx, playlist dbv1.FullPlaylist) error {
3133
if c.Locals("isFull").(bool) {
3234
return c.JSON(fiber.Map{
33-
"data": playlist,
35+
"data": []dbv1.FullPlaylist{playlist},
3436
})
3537
}
3638
return c.JSON(fiber.Map{
37-
"data": dbv1.ToMinPlaylist(playlist),
39+
"data": []dbv1.MinPlaylist{dbv1.ToMinPlaylist(playlist)},
3840
})
3941
}
4042

api/v1_playlist_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@ import (
99

1010
func TestGetPlaylist(t *testing.T) {
1111
var playlistResponse struct {
12-
Data dbv1.FullPlaylist
12+
Data []dbv1.FullPlaylist
1313
}
1414

1515
status, body := testGet(t, "/v1/full/playlists/7eP5n", &playlistResponse)
1616
assert.Equal(t, 200, status)
1717

1818
jsonAssert(t, body, map[string]string{
19-
"data.id": "7eP5n",
20-
"data.playlist_name": "First",
19+
"data.0.id": "7eP5n",
20+
"data.0.playlist_name": "First",
2121
})
2222
}
2323

2424
func TestGetPlaylistFollowDownloadAccess(t *testing.T) {
2525
var playlistResponse struct {
26-
Data dbv1.FullPlaylist
26+
Data []dbv1.FullPlaylist
2727
}
2828
// No access
2929
_, body1 := testGet(t, "/v1/full/playlists/ML51L", &playlistResponse)
3030
jsonAssert(t, body1, map[string]string{
31-
"data.playlist_name": "Follow Gated Stream",
32-
"data.access": `{"stream":false,"download":false}`,
31+
"data.0.playlist_name": "Follow Gated Stream",
32+
"data.0.access": `{"stream":false,"download":false}`,
3333
})
3434

3535
// With access
3636
_, body2 := testGet(t, "/v1/full/playlists/ML51L?user_id=ELKzn", &playlistResponse)
3737
jsonAssert(t, body2, map[string]string{
38-
"data.playlist_name": "Follow Gated Stream",
39-
"data.access": `{"stream":true,"download":true}`,
38+
"data.0.playlist_name": "Follow Gated Stream",
39+
"data.0.access": `{"stream":true,"download":true}`,
4040
})
4141
}

0 commit comments

Comments
 (0)