Skip to content

Commit c6f1b56

Browse files
authored
remix_of structure (#23)
* remix_of structure to match v1 api * separate RemixOf and FullRemixOf * remove ineffective sqlc config
1 parent 5b6f1b6 commit c6f1b56

6 files changed

Lines changed: 99 additions & 9 deletions

File tree

api/dbv1/full_tracks.go

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

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67

78
"bridgerton.audius.co/trashid"
@@ -24,6 +25,7 @@ type FullTrack struct {
2425

2526
FolloweeReposts []*FolloweeRepost `json:"followee_reposts"`
2627
FolloweeFavorites []*FolloweeFavorite `json:"followee_favorites"`
28+
RemixOf FullRemixOf `json:"remix_of"`
2729
}
2830

2931
func (q *Queries) FullTracksKeyed(ctx context.Context, arg GetTracksParams) (map[int32]FullTrack, error) {
@@ -35,6 +37,12 @@ func (q *Queries) FullTracksKeyed(ctx context.Context, arg GetTracksParams) (map
3537
userIds := []int32{}
3638
for _, track := range rawTracks {
3739
userIds = append(userIds, track.UserID)
40+
41+
var remixOf RemixOf
42+
json.Unmarshal(track.RemixOf, &remixOf)
43+
for _, r := range remixOf.Tracks {
44+
userIds = append(userIds, r.ParentUserId)
45+
}
3846
}
3947

4048
userMap, err := q.FullUsersKeyed(ctx, GetUsersParams{
@@ -76,6 +84,23 @@ func (q *Queries) FullTracksKeyed(ctx context.Context, arg GetTracksParams) (map
7684
track.FieldVisibility = []byte(`{}`)
7785
}
7886

87+
// remix_of
88+
var remixOf RemixOf
89+
var fullRemixOf FullRemixOf
90+
json.Unmarshal(track.RemixOf, &remixOf)
91+
fullRemixOf = FullRemixOf{
92+
Tracks: make([]FullRemixOfTrack, len(remixOf.Tracks)),
93+
}
94+
for idx, r := range remixOf.Tracks {
95+
trackId, _ := trashid.EncodeHashId(int(r.ParentTrackId))
96+
fullRemixOf.Tracks[idx] = FullRemixOfTrack{
97+
HasRemixAuthorReposted: r.HasRemixAuthorReposted,
98+
HasRemixAuthorSaved: r.HasRemixAuthorSaved,
99+
ParentTrackId: trackId,
100+
User: userMap[r.ParentUserId],
101+
}
102+
}
103+
79104
fullTrack := FullTrack{
80105
GetTracksRow: track,
81106
IsStreamable: !track.IsDelete && !user.IsDeactivated,
@@ -88,6 +113,7 @@ func (q *Queries) FullTracksKeyed(ctx context.Context, arg GetTracksParams) (map
88113
UserID: user.ID,
89114
FolloweeFavorites: fullFolloweeFavorites(track.FolloweeFavorites),
90115
FolloweeReposts: fullFolloweeReposts(track.FolloweeReposts),
116+
RemixOf: fullRemixOf,
91117
}
92118
trackMap[track.TrackID] = fullTrack
93119
}

api/dbv1/get_tracks.sql.go

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

api/dbv1/jsonb_types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,23 @@ type FolloweeFavorite struct {
4646
UserId string `json:"user_id"`
4747
CreatedAt string `json:"created_at"`
4848
}
49+
50+
type RemixOf struct {
51+
Tracks []struct {
52+
HasRemixAuthorReposted bool `json:"has_remix_author_reposted"`
53+
HasRemixAuthorSaved bool `json:"has_remix_author_saved"`
54+
ParentTrackId int32 `json:"parent_track_id"`
55+
ParentUserId int32 `json:"parent_user_id"`
56+
}
57+
}
58+
59+
type FullRemixOfTrack struct {
60+
HasRemixAuthorReposted bool `json:"has_remix_author_reposted"`
61+
HasRemixAuthorSaved bool `json:"has_remix_author_saved"`
62+
ParentTrackId string `json:"parent_track_id"`
63+
User FullUser `json:"user"`
64+
}
65+
66+
type FullRemixOf struct {
67+
Tracks []FullRemixOfTrack `json:"tracks"`
68+
}

api/dbv1/models.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/dbv1/queries/get_tracks.sql

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ SELECT
1111
is_original_available,
1212
mood,
1313
release_date,
14-
remix_of,
1514
repost_count,
1615
save_count as favorite_count,
1716
comment_count,
@@ -103,6 +102,31 @@ SELECT
103102
) r
104103
)::jsonb as followee_favorites,
105104

105+
(
106+
SELECT json_build_object(
107+
'tracks', json_agg(
108+
json_build_object(
109+
'has_remix_author_reposted', repost_item_id is not null,
110+
'has_remix_author_saved', save_item_id is not null,
111+
'parent_track_id', r.parent_track_id,
112+
'parent_user_id', r.parent_owner_id
113+
)
114+
)
115+
)
116+
FROM (
117+
SELECT
118+
track_id as parent_track_id,
119+
owner_id as parent_owner_id,
120+
repost_item_id,
121+
save_item_id
122+
FROM remixes
123+
JOIN tracks parent_track ON parent_track_id = parent_track.track_id AND child_track_id = t.track_id
124+
LEFT JOIN reposts ON repost_type = 'track' AND repost_item_id = t.track_id AND reposts.user_id = parent_track.owner_id AND reposts.is_delete = false
125+
LEFT JOIN saves ON save_type = 'track' AND save_item_id = t.track_id AND saves.user_id = parent_track.owner_id AND saves.is_delete = false
126+
LIMIT 10
127+
) r
128+
)::jsonb as remix_of,
129+
106130

107131
-- followee_favorites,
108132
-- route_id,

sqlc.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ sql:
3737
import: "encoding/json"
3838
type: "RawMessage"
3939

40-
- column: "tracks.remix_of"
41-
go_type:
42-
import: "encoding/json"
43-
type: "RawMessage"
4440
- column: "tracks.copyright_line"
4541
go_type:
4642
import: "encoding/json"

0 commit comments

Comments
 (0)