55
66 "bridgerton.audius.co/trashid"
77 "github.com/jackc/pgx/v5/pgtype"
8- "golang.org/x/sync/errgroup"
98)
109
1110type FullPlaylist struct {
@@ -17,8 +16,15 @@ type FullPlaylist struct {
1716 User FullUser `json:"user"`
1817 Tracks []FullTrack `json:"tracks"`
1918
20- FolloweeReposts []* FolloweeRepost `json:"followee_reposts"`
21- FolloweeFavorites []* FolloweeFavorite `json:"followee_favorites"`
19+ FolloweeReposts []* FolloweeRepost `json:"followee_reposts"`
20+ FolloweeFavorites []* FolloweeFavorite `json:"followee_favorites"`
21+ PlaylistContents []FullPlaylistContentsItem `json:"playlist_contents"`
22+ }
23+
24+ type FullPlaylistContentsItem struct {
25+ Time int64 `json:"timestamp"`
26+ TrackId string `json:"track_id"`
27+ MetadataTime int64 `json:"metadata_timestamp"`
2228}
2329
2430func (q * Queries ) FullPlaylistsKeyed (ctx context.Context , arg GetPlaylistsParams ) (map [int32 ]FullPlaylist , error ) {
@@ -38,38 +44,19 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg GetPlaylistsParams
3844 }
3945
4046 // fetch users + tracks in parallel
41- g , ctx := errgroup .WithContext (ctx )
42- userMap := map [int32 ]FullUser {}
43- trackMap := map [int32 ]FullTrack {}
44-
45- // fetch users
46- g .Go (func () error {
47- var err error
48- userMap , err = q .FullUsersKeyed (ctx , GetUsersParams {
49- MyID : arg .MyID ,
50- Ids : userIds ,
51- })
52- return err
53- })
54-
55- // fetch tracks
56- g .Go (func () error {
57- var err error
58- trackMap , err = q .FullTracksKeyed (ctx , GetTracksParams {
59- MyID : arg .MyID ,
60- Ids : trackIds ,
61- })
62- return err
47+ loaded , err := q .Parallel (ctx , ParallelParams {
48+ UserIds : userIds ,
49+ TrackIds : trackIds ,
50+ MyID : arg .MyID ,
6351 })
64-
65- if err := g .Wait (); err != nil {
52+ if err != nil {
6653 return nil , err
6754 }
6855
6956 playlistMap := map [int32 ]FullPlaylist {}
7057 for _ , playlist := range rawPlaylists {
7158 id , _ := trashid .EncodeHashId (int (playlist .PlaylistID ))
72- user , ok := userMap [playlist .PlaylistOwnerID ]
59+ user , ok := loaded . UserMap [playlist .PlaylistOwnerID ]
7360
7461 // GetUser will omit deactivated users
7562 // so skip tracks if user doesn't come back.
@@ -80,11 +67,22 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg GetPlaylistsParams
8067
8168 var tracks = make ([]FullTrack , 0 , len (playlist .PlaylistContents .TrackIDs ))
8269 for _ , t := range playlist .PlaylistContents .TrackIDs {
83- if track , ok := trackMap [int32 (t .Track )]; ok {
70+ if track , ok := loaded . TrackMap [int32 (t .Track )]; ok {
8471 tracks = append (tracks , track )
8572 }
8673 }
8774
75+ // slightly change playlist_contents
76+ fullPlaylistContents := []FullPlaylistContentsItem {}
77+ for _ , item := range playlist .PlaylistContents .TrackIDs {
78+ trackId , _ := trashid .EncodeHashId (int (item .Track ))
79+ fullPlaylistContents = append (fullPlaylistContents , FullPlaylistContentsItem {
80+ Time : item .Time ,
81+ MetadataTime : item .MetadataTime ,
82+ TrackId : trackId ,
83+ })
84+ }
85+
8886 playlistMap [playlist .PlaylistID ] = FullPlaylist {
8987 GetPlaylistsRow : playlist ,
9088 ID : id ,
@@ -94,6 +92,7 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg GetPlaylistsParams
9492 Tracks : tracks ,
9593 FolloweeFavorites : fullFolloweeFavorites (playlist .FolloweeFavorites ),
9694 FolloweeReposts : fullFolloweeReposts (playlist .FolloweeReposts ),
95+ PlaylistContents : fullPlaylistContents ,
9796 }
9897 }
9998
0 commit comments