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
8 changes: 8 additions & 0 deletions api/dbv1/get_playlists.sql.go

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

26 changes: 13 additions & 13 deletions api/dbv1/models.go

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

10 changes: 5 additions & 5 deletions api/dbv1/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ type ParallelParams struct {
type ParallelResult struct {
UserMap map[int32]User
TrackMap map[int32]Track
PlaylistMap map[int32]FullPlaylist
PlaylistMap map[int32]Playlist
}

func (q *Queries) Parallel(ctx context.Context, arg ParallelParams) (*ParallelResult, error) {
g, ctx := errgroup.WithContext(ctx)

var userMap map[int32]User
var trackMap map[int32]Track
var playlistMap map[int32]FullPlaylist
var playlistMap map[int32]Playlist

if len(arg.UserIds) > 0 {
g.Go(func() error {
Expand Down Expand Up @@ -53,7 +53,7 @@ func (q *Queries) Parallel(ctx context.Context, arg ParallelParams) (*ParallelRe
if len(arg.PlaylistIds) > 0 {
g.Go(func() error {
var err error
playlistMap, err = q.FullPlaylistsKeyed(ctx, FullPlaylistsParams{
playlistMap, err = q.PlaylistsKeyed(ctx, PlaylistsParams{
GetPlaylistsParams: GetPlaylistsParams{
Ids: arg.PlaylistIds,
MyID: arg.MyID,
Expand Down Expand Up @@ -93,8 +93,8 @@ func (r *ParallelResult) TrackList() []Track {
return trackList
}

func (r *ParallelResult) PlaylistList() []FullPlaylist {
playlistList := make([]FullPlaylist, 0, len(r.PlaylistMap))
func (r *ParallelResult) PlaylistList() []Playlist {
playlistList := make([]Playlist, 0, len(r.PlaylistMap))
for _, p := range r.PlaylistMap {
playlistList = append(playlistList, p)
}
Expand Down
96 changes: 18 additions & 78 deletions api/dbv1/full_playlists.go → api/dbv1/playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"fmt"

"api.audius.co/trashid"
"github.com/jackc/pgx/v5/pgtype"
)

type FullPlaylistsParams struct {
type PlaylistsParams struct {
GetPlaylistsParams
OmitTracks bool
TrackLimit int // 0 means use default (200), positive values set the limit
}

type FullPlaylist struct {
type Playlist struct {
GetPlaylistsRow

ID string `json:"id"`
Expand All @@ -28,17 +27,17 @@ type FullPlaylist struct {

FolloweeReposts []*FolloweeRepost `json:"followee_reposts"`
FolloweeFavorites []*FolloweeFavorite `json:"followee_favorites"`
PlaylistContents []FullPlaylistContentsItem `json:"playlist_contents"`
AddedTimestamps []FullPlaylistContentsItem `json:"added_timestamps"`
PlaylistContents []PlaylistContentsItem `json:"playlist_contents"`
AddedTimestamps []PlaylistContentsItem `json:"added_timestamps"`
}

type FullPlaylistContentsItem struct {
type PlaylistContentsItem struct {
Time float64 `json:"timestamp"`
TrackId string `json:"track_id"`
MetadataTime float64 `json:"metadata_timestamp"`
}

func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg FullPlaylistsParams) (map[int32]FullPlaylist, error) {
func (q *Queries) PlaylistsKeyed(ctx context.Context, arg PlaylistsParams) (map[int32]Playlist, error) {
rawPlaylists, err := q.GetPlaylists(ctx, arg.GetPlaylistsParams)
if err != nil {
return nil, err
Expand Down Expand Up @@ -77,7 +76,7 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg FullPlaylistsParam
return nil, err
}

playlistMap := map[int32]FullPlaylist{}
playlistMap := map[int32]Playlist{}
for _, playlist := range rawPlaylists {
id, _ := trashid.EncodeHashId(int(playlist.PlaylistID))
user, ok := loaded.UserMap[playlist.PlaylistOwnerID]
Expand All @@ -97,10 +96,10 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg FullPlaylistsParam
}

// slightly change playlist_contents
fullPlaylistContents := []FullPlaylistContentsItem{}
playlistContents := []PlaylistContentsItem{}
for _, item := range playlist.PlaylistContents.TrackIDs {
trackId, _ := trashid.EncodeHashId(int(item.Track))
fullPlaylistContents = append(fullPlaylistContents, FullPlaylistContentsItem{
playlistContents = append(playlistContents, PlaylistContentsItem{
Time: item.Time,
MetadataTime: item.MetadataTime,
TrackId: trackId,
Expand All @@ -123,7 +122,7 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg FullPlaylistsParam
playlistType = "playlist"
}

playlistMap[playlist.PlaylistID] = FullPlaylist{
playlistMap[playlist.PlaylistID] = Playlist{
GetPlaylistsRow: playlist,
ID: id,
Artwork: squareImageStruct(playlist.Artwork),
Expand All @@ -133,9 +132,9 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg FullPlaylistsParam
TrackCount: int32(len(playlist.PlaylistContents.TrackIDs)),
FolloweeFavorites: fullFolloweeFavorites(playlist.FolloweeFavorites),
FolloweeReposts: fullFolloweeReposts(playlist.FolloweeReposts),
PlaylistContents: fullPlaylistContents,
PlaylistContents: playlistContents,
Permalink: fmt.Sprintf("/%s/%s/%s", user.Handle.String, playlistType, playlist.Slug.String),
AddedTimestamps: fullPlaylistContents,
AddedTimestamps: playlistContents,
Access: Access{
Stream: streamAccess,
Download: downloadAccess,
Expand All @@ -146,79 +145,20 @@ func (q *Queries) FullPlaylistsKeyed(ctx context.Context, arg FullPlaylistsParam
return playlistMap, nil
}

func (q *Queries) FullPlaylists(ctx context.Context, arg FullPlaylistsParams) ([]FullPlaylist, error) {
playlistMap, err := q.FullPlaylistsKeyed(ctx, arg)
func (q *Queries) Playlists(ctx context.Context, arg PlaylistsParams) ([]Playlist, error) {
playlistMap, err := q.PlaylistsKeyed(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{}
list := make([]Playlist, 0, len(arg.Ids))
for _, id := range arg.Ids {
if t, found := playlistMap[id]; found {
fullPlaylists = append(fullPlaylists, t)
if p, found := playlistMap[id]; found {
list = append(list, p)
}
}

return fullPlaylists, nil
}

type MinPlaylist struct {
ID string `json:"id"`
PlaylistName pgtype.Text `json:"playlist_name"`
Artwork *SquareImage `json:"artwork"`
Access Access `json:"access"`
Description string `json:"description"`
IsImageAutogenerated bool `json:"is_image_autogenerated"`
Upc string `json:"upc"`
DdexApp string `json:"ddex_app"`
PlaylistContents interface{} `json:"playlist_contents"`
TrackCount int32 `json:"track_count"`
TotalPlayCount int64 `json:"total_play_count"`
IsAlbum bool `json:"is_album"`
FavoriteCount int32 `json:"favorite_count"`
RepostCount int32 `json:"repost_count"`
User User `json:"user"`
Permalink string `json:"permalink"`
}

func ToMinPlaylist(fullPlaylist FullPlaylist) MinPlaylist {
minTracks := make([]Track, len(fullPlaylist.Tracks))
for i, track := range fullPlaylist.Tracks {
minTracks[i] = track
}

return MinPlaylist{
ID: fullPlaylist.ID,
PlaylistName: fullPlaylist.PlaylistName,
Artwork: fullPlaylist.Artwork,
Access: fullPlaylist.Access,
Upc: fullPlaylist.Upc.String,
DdexApp: fullPlaylist.DdexApp.String,
PlaylistContents: fullPlaylist.PlaylistContents,
Description: fullPlaylist.Description.String,
IsImageAutogenerated: fullPlaylist.IsImageAutogenerated,
IsAlbum: fullPlaylist.IsAlbum,
TrackCount: int32(len(fullPlaylist.Tracks)),
TotalPlayCount: func() int64 {
var total int64
for _, track := range fullPlaylist.Tracks {
total += track.PlayCount
}
return total
}(),
FavoriteCount: int32(fullPlaylist.FavoriteCount.Int32),
RepostCount: int32(fullPlaylist.RepostCount.Int32),
User: fullPlaylist.User,
Permalink: fullPlaylist.Permalink,
}
}

func ToMinPlaylists(fullPlaylists []FullPlaylist) []MinPlaylist {
result := make([]MinPlaylist, len(fullPlaylists))
for i, playlist := range fullPlaylists {
result[i] = ToMinPlaylist(playlist)
}
return result
return list, nil
}
6 changes: 6 additions & 0 deletions api/dbv1/queries/get_playlists.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ SELECT
updated_at,
release_date,

(
SELECT COALESCE(SUM(ap.count), 0)::bigint
FROM jsonb_array_elements(COALESCE(p.playlist_contents->'track_ids', '[]'::jsonb)) AS e(item)
LEFT JOIN aggregate_plays ap ON ap.play_item_id = (e.item->>'track')::int
) AS total_play_count,

(
SELECT count(*) > 0
FROM reposts
Expand Down
19 changes: 5 additions & 14 deletions api/response_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,16 @@ func v1UsersResponse(c *fiber.Ctx, users []dbv1.User) error {

// Note: playlist response returned an array even though it's a single playlist
// Done for backwards compatibility. Would be nice to get rid of this.
func v1PlaylistResponse(c *fiber.Ctx, playlist dbv1.FullPlaylist) error {
if c.Locals("isFull").(bool) {
return c.JSON(fiber.Map{
"data": []dbv1.FullPlaylist{playlist},
})
}
// Default and full both return full playlist shape (min-collection parity with full).
func v1PlaylistResponse(c *fiber.Ctx, playlist dbv1.Playlist) error {
return c.JSON(fiber.Map{
"data": []dbv1.MinPlaylist{dbv1.ToMinPlaylist(playlist)},
"data": []dbv1.Playlist{playlist},
})
}

func v1PlaylistsResponse(c *fiber.Ctx, playlists []dbv1.FullPlaylist) error {
if c.Locals("isFull").(bool) {
return c.JSON(fiber.Map{
"data": playlists,
})
}
func v1PlaylistsResponse(c *fiber.Ctx, playlists []dbv1.Playlist) error {
return c.JSON(fiber.Map{
"data": dbv1.ToMinPlaylists(playlists),
"data": playlists,
})
}

Expand Down
Loading