Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions api/dbv1/full_followee_actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dbv1

import (
"encoding/json"

"bridgerton.audius.co/trashid"
)

func fullFolloweeReposts(raw json.RawMessage) []*FolloweeRepost {
var followeeReposts []*FolloweeRepost
if err := json.Unmarshal(raw, &followeeReposts); err == nil {
for _, r := range followeeReposts {
r.RepostItemId = trashid.StringEncode(r.RepostItemId)
r.UserId = trashid.StringEncode(r.UserId)
}
}
return followeeReposts
}

func fullFolloweeFavorites(raw json.RawMessage) []*FolloweeFavorite {
var followeeFavorites []*FolloweeFavorite
if err := json.Unmarshal(raw, &followeeFavorites); err == nil {
for _, r := range followeeFavorites {
r.FavoriteItemId = trashid.StringEncode(r.FavoriteItemId)
r.UserId = trashid.StringEncode(r.UserId)
}
}
return followeeFavorites
}
49 changes: 34 additions & 15 deletions api/dbv1/full_playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ type FullPlaylist struct {
UserID string `json:"user_id"`
User FullUser `json:"user"`
Tracks []FullTrack `json:"tracks"`

FolloweeReposts []*FolloweeRepost `json:"followee_reposts"`
FolloweeFavorites []*FolloweeFavorite `json:"followee_favorites"`
}

func (q *Queries) FullPlaylists(ctx context.Context, arg GetPlaylistsParams) ([]FullPlaylist, error) {
func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg GetPlaylistsParams) (map[int32]FullPlaylist, error) {
rawPlaylists, err := q.GetPlaylists(ctx, arg)
if err != nil {
return nil, err
Expand All @@ -41,33 +44,29 @@ func (q *Queries) FullPlaylists(ctx context.Context, arg GetPlaylistsParams) ([]

// fetch users
g.Go(func() error {
users, err := q.FullUsers(ctx, GetUsersParams{
var err error
userMap, err = q.FullUsersKeyed(ctx, GetUsersParams{
MyID: arg.MyID,
Ids: userIds,
})
for _, user := range users {
userMap[user.UserID] = user
}
return err
})

// fetch tracks
g.Go(func() error {
tracks, err := q.FullTracks(ctx, GetTracksParams{
var err error
trackMap, err = q.FullTracksKeyed(ctx, GetTracksParams{
MyID: arg.MyID,
Ids: trackIds,
})
for _, track := range tracks {
trackMap[track.TrackID] = track
}
return err
})

if err := g.Wait(); err != nil {
return nil, err
}

fullPlaylists := make([]FullPlaylist, 0, len(rawPlaylists))
playlistMap := map[int32]FullPlaylist{}
for _, playlist := range rawPlaylists {
id, _ := trashid.EncodeHashId(int(playlist.PlaylistID))
user, ok := userMap[playlist.PlaylistOwnerID]
Expand All @@ -86,14 +85,34 @@ func (q *Queries) FullPlaylists(ctx context.Context, arg GetPlaylistsParams) ([]
}
}

fullPlaylists = append(fullPlaylists, FullPlaylist{
playlistMap[playlist.PlaylistID] = FullPlaylist{
GetPlaylistsRow: playlist,
ID: id,
// Artwork: squareImageStruct(track.CoverArtSizes, track.CoverArt),
User: user,
UserID: user.ID,
Tracks: tracks,
})
User: user,
UserID: user.ID,
Tracks: tracks,
FolloweeFavorites: fullFolloweeFavorites(playlist.FolloweeFavorites),
FolloweeReposts: fullFolloweeReposts(playlist.FolloweeReposts),
}
}

return playlistMap, nil
}

func (q *Queries) FullPlaylists(ctx context.Context, arg GetPlaylistsParams) ([]FullPlaylist, error) {
playlistMap, err := q.FullPlaylistsKeyed(ctx, arg)
if err != nil {
return nil, err
}

// return in same order as input list of ids
// some ids may be not found...
fullPlaylists := []FullPlaylist{}
for _, id := range arg.Ids {
if t, found := playlistMap[id]; found {
fullPlaylists = append(fullPlaylists, t)
}
}

return fullPlaylists, nil
Expand Down
40 changes: 13 additions & 27 deletions api/dbv1/full_tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dbv1

import (
"context"
"encoding/json"
"fmt"

"bridgerton.audius.co/trashid"
Expand All @@ -22,7 +21,7 @@ type FullTrack struct {
FolloweeFavorites []*FolloweeFavorite `json:"followee_favorites"`
}

func (q *Queries) FullTracks(ctx context.Context, arg GetTracksParams) ([]FullTrack, error) {
func (q *Queries) FullTracksKeyed(ctx context.Context, arg GetTracksParams) (map[int32]FullTrack, error) {
rawTracks, err := q.GetTracks(ctx, GetTracksParams(arg))
if err != nil {
return nil, err
Expand All @@ -33,19 +32,14 @@ func (q *Queries) FullTracks(ctx context.Context, arg GetTracksParams) ([]FullTr
userIds = append(userIds, track.UserID)
}

users, err := q.FullUsers(ctx, GetUsersParams{
userMap, err := q.FullUsersKeyed(ctx, GetUsersParams{
MyID: arg.MyID,
Ids: userIds,
})
if err != nil {
return nil, err
}

userMap := map[int32]FullUser{}
for _, user := range users {
userMap[user.UserID] = user
}

trackMap := map[int32]FullTrack{}
for _, track := range rawTracks {
track.ID, _ = trashid.EncodeHashId(int(track.TrackID))
Expand All @@ -58,34 +52,26 @@ func (q *Queries) FullTracks(ctx context.Context, arg GetTracksParams) ([]FullTr
continue
}

// re-encode ids for followee_favorites + followee_reposts
var followeeReposts []*FolloweeRepost
if err = json.Unmarshal(track.FolloweeReposts, &followeeReposts); err == nil {
for _, r := range followeeReposts {
r.RepostItemId = trashid.StringEncode(r.RepostItemId)
r.UserId = trashid.StringEncode(r.UserId)
}
}

var followeeFavorites []*FolloweeFavorite
if err = json.Unmarshal(track.FolloweeFavorites, &followeeFavorites); err == nil {
for _, r := range followeeFavorites {
r.FavoriteItemId = trashid.StringEncode(r.FavoriteItemId)
r.UserId = trashid.StringEncode(r.UserId)
}
}

fullTrack := FullTrack{
GetTracksRow: track,
Artwork: squareImageStruct(track.CoverArtSizes, track.CoverArt),
User: user,
UserID: user.ID,
FolloweeFavorites: followeeFavorites,
FolloweeReposts: followeeReposts,
FolloweeFavorites: fullFolloweeFavorites(track.FolloweeFavorites),
FolloweeReposts: fullFolloweeReposts(track.FolloweeReposts),
}
trackMap[track.TrackID] = fullTrack
}

return trackMap, nil
}

func (q *Queries) FullTracks(ctx context.Context, arg GetTracksParams) ([]FullTrack, error) {
trackMap, err := q.FullTracksKeyed(ctx, arg)
if err != nil {
return nil, err
}

// return in same order as input list of ids
// some ids may be not found...
fullTracks := []FullTrack{}
Expand Down
26 changes: 22 additions & 4 deletions api/dbv1/full_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ type FullUser struct {
CoverPhoto RectangleImage `json:"cover_photo"`
}

func (q *Queries) FullUsers(ctx context.Context, arg GetUsersParams) ([]FullUser, error) {
func (q *Queries) FullUsersKeyed(ctx context.Context, arg GetUsersParams) (map[int32]FullUser, error) {
rawUsers, err := q.GetUsers(ctx, arg)
if err != nil {
return nil, err
}

fullUsers := make([]FullUser, len(rawUsers))
for idx, user := range rawUsers {
userMap := map[int32]FullUser{}
for _, user := range rawUsers {

// playlist_library only populated for current user
if user.UserID != arg.MyID {
Expand Down Expand Up @@ -65,14 +65,32 @@ func (q *Queries) FullUsers(ctx context.Context, arg GetUsersParams) ([]FullUser
artistPickTrackID = &id
}

fullUsers[idx] = FullUser{
userMap[user.UserID] = FullUser{
GetUsersRow: user,
ArtistPickTrackID: artistPickTrackID,
CoverPhoto: coverPhoto,
ProfilePicture: profilePicture,
}
}

return userMap, nil
}

func (q *Queries) FullUsers(ctx context.Context, arg GetUsersParams) ([]FullUser, error) {
userMap, err := q.FullUsersKeyed(ctx, arg)
if err != nil {
return nil, err
}

fullUsers := []FullUser{}

// return in same order as input list of ids
for _, id := range arg.Ids {
if u, found := userMap[id]; found {
fullUsers = append(fullUsers, u)
}
}

return fullUsers, nil
}

Expand Down
52 changes: 51 additions & 1 deletion api/dbv1/get_playlists.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/dbv1/get_tracks.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions api/dbv1/get_users.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading