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
34 changes: 14 additions & 20 deletions api/dbv1/full_tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbv1

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

"bridgerton.audius.co/trashid"
Expand All @@ -17,12 +18,8 @@ type FullTrack struct {
UserID string `json:"user_id"`
User FullUser `json:"user"`

// hide these GetTrackRow fields
FolloweeFavoriteIds any `json:"followee_favorite_ids,omitempty"`
FolloweeRepostIds any `json:"followee_repost_ids,omitempty"`

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

func (q *Queries) FullTracks(ctx context.Context, arg GetTracksParams) ([]FullTrack, error) {
Expand All @@ -34,12 +31,6 @@ func (q *Queries) FullTracks(ctx context.Context, arg GetTracksParams) ([]FullTr
userIds := []int32{}
for _, track := range rawTracks {
userIds = append(userIds, track.UserID)
for _, id := range track.FolloweeFavoriteIds {
userIds = append(userIds, id)
}
for _, id := range track.FolloweeRepostIds {
userIds = append(userIds, id)
}
}

users, err := q.FullUsers(ctx, GetUsersParams{
Expand Down Expand Up @@ -67,17 +58,20 @@ func (q *Queries) FullTracks(ctx context.Context, arg GetTracksParams) ([]FullTr
continue
}

followeeFavorites := []FullUser{}
for _, id := range track.FolloweeFavoriteIds {
if user, ok := userMap[id]; ok {
followeeFavorites = append(followeeFavorites, user)
// 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)
}
}

followeeReposts := []FullUser{}
for _, id := range track.FolloweeFavoriteIds {
if user, ok := userMap[id]; ok {
followeeReposts = append(followeeReposts, user)
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)
}
}

Expand Down
77 changes: 47 additions & 30 deletions api/dbv1/get_tracks.sql.go

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

14 changes: 14 additions & 0 deletions api/dbv1/jsonb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ type PlaylistContents struct {
MetadataTime int64 `json:"metadata_time"`
} `json:"track_ids"`
}

type FolloweeRepost struct {
RepostItemId string `json:"repost_item_id"`
RepostType string `json:"repost_type"`
UserId string `json:"user_id"`
CreatedAt string `json:"created_at"`
}

type FolloweeFavorite struct {
FavoriteItemId string `json:"favorite_item_id"`
FavoriteType string `json:"favorite_type"`
UserId string `json:"user_id"`
CreatedAt string `json:"created_at"`
}
24 changes: 12 additions & 12 deletions api/dbv1/models.go

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

69 changes: 43 additions & 26 deletions api/dbv1/queries/get_tracks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,50 @@ SELECT
is_scheduled_release,
is_unlisted,

ARRAY(
SELECT user_id
FROM saves
JOIN follows ON followee_user_id = saves.user_id AND follower_user_id = @my_id
JOIN aggregate_user USING (user_id)
-- todo: join users, filter out deactivated
WHERE @my_id > 0
AND save_item_id = t.track_id
AND save_type = 'track'
AND saves.is_delete = false
ORDER BY follower_count DESC
LIMIT 10
)::int[] as followee_favorite_ids,
(
SELECT json_agg(
json_build_object(
'user_id', r.user_id::text,
'repost_item_id', repost_item_id::text, -- this is redundant
'repost_type', 'RepostType.track', -- some sqlalchemy bs
'created_at', r.created_at -- this is not actually present in python response?
)
)
FROM (
SELECT user_id, repost_item_id, reposts.created_at
FROM reposts
JOIN follows ON followee_user_id = reposts.user_id AND follower_user_id = @my_id
JOIN aggregate_user USING (user_id)
WHERE repost_item_id = t.track_id
AND repost_type = 'track'
AND reposts.is_delete = false
ORDER BY follower_count DESC
LIMIT 3
) r
)::jsonb as followee_reposts,

(
SELECT json_agg(
json_build_object(
'user_id', r.user_id::text,
'favorite_item_id', r.save_item_id::text, -- this is redundant
'favorite_type', 'SaveType.track', -- some sqlalchemy bs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love these comments LOOOL

'created_at', r.created_at -- this is not actually present in python response?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting.. maybe it gets marshalled away?

)
)
FROM (
SELECT user_id, save_item_id, saves.created_at
FROM saves
JOIN follows ON followee_user_id = saves.user_id AND follower_user_id = @my_id
JOIN aggregate_user USING (user_id)
WHERE save_item_id = t.track_id
AND save_type = 'track'
AND saves.is_delete = false
ORDER BY follower_count DESC
LIMIT 3
) r
)::jsonb as followee_favorites,

ARRAY(
SELECT user_id
FROM reposts
JOIN follows ON followee_user_id = reposts.user_id AND follower_user_id = @my_id
JOIN aggregate_user USING (user_id)
-- todo: join users, filter out deactivated
WHERE @my_id > 0
AND repost_item_id = t.track_id
AND repost_type = 'track'
AND reposts.is_delete = false
ORDER BY follower_count DESC
LIMIT 10
)::int[] as followee_repost_ids,

-- followee_favorites,
-- route_id,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/joho/godotenv v1.5.1
github.com/speps/go-hashids/v2 v2.0.1
github.com/stretchr/testify v1.10.0
github.com/valyala/fasthttp v1.59.0
go.uber.org/zap v1.27.0
golang.org/x/sync v0.12.0
google.golang.org/protobuf v1.36.6
Expand Down Expand Up @@ -50,7 +51,6 @@ require (
github.com/sasha-s/go-deadlock v0.3.5 // indirect
github.com/supranational/blst v0.3.14 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.59.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ sql:
import: "encoding/json"
type: "RawMessage"

- db_type: "jsonb"
go_type:
import: "encoding/json"
type: "RawMessage"

- column: "tracks.copyright_line"
go_type:
import: "encoding/json"
Expand Down
Loading
Loading