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
12 changes: 12 additions & 0 deletions api/auth_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,22 @@ func (app *ApiServer) isAuthorizedRequest(ctx context.Context, userId int32, aut
return isAuthorized
}

// get the wallet that signed the request
func (app *ApiServer) getAuthedWallet(c *fiber.Ctx) string {
return c.Locals("authedWallet").(string)
}

// get the wallet that signed the request, or "" if not set
func (app *ApiServer) tryGetAuthedWallet(c *fiber.Ctx) string {
if c == nil {
return ""
}
if w, ok := c.Locals("authedWallet").(string); ok {
return w
}
return ""
}

// validateOAuthJWTTokenToUserId validates the OAuth JWT and returns the userId from the payload.
func (app *ApiServer) validateOAuthJWTTokenToUserId(ctx context.Context, token string) (trashid.HashId, error) {
tokenParts := strings.Split(token, ".")
Expand Down
12 changes: 10 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.

15 changes: 9 additions & 6 deletions api/dbv1/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

type ParallelParams struct {
UserIds []int32
TrackIds []int32
PlaylistIds []int32
MyID int32
UserIds []int32
TrackIds []int32
PlaylistIds []int32
MyID int32
AuthedWallet string
}

type ParallelResult struct {
Expand Down Expand Up @@ -42,8 +43,9 @@ func (q *Queries) Parallel(ctx context.Context, arg ParallelParams) (*ParallelRe
var err error
trackMap, err = q.TracksKeyed(ctx, TracksParams{
GetTracksParams: GetTracksParams{
Ids: arg.TrackIds,
MyID: arg.MyID,
Ids: arg.TrackIds,
MyID: arg.MyID,
AuthedWallet: arg.AuthedWallet,
},
})
return err
Expand All @@ -58,6 +60,7 @@ func (q *Queries) Parallel(ctx context.Context, arg ParallelParams) (*ParallelRe
Ids: arg.PlaylistIds,
MyID: arg.MyID,
},
AuthedWallet: arg.AuthedWallet,
})
return err
})
Expand Down
12 changes: 7 additions & 5 deletions api/dbv1/playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

type PlaylistsParams struct {
GetPlaylistsParams
OmitTracks bool
TrackLimit int // 0 means use default (200), positive values set the limit
OmitTracks bool
TrackLimit int // 0 means use default (200), positive values set the limit
AuthedWallet string // wallet that signed the request; tracks with matching access_authorities are shown
}

type Playlist struct {
Expand Down Expand Up @@ -68,9 +69,10 @@ func (q *Queries) PlaylistsKeyed(ctx context.Context, arg PlaylistsParams) (map[

// fetch users + tracks in parallel
loaded, err := q.Parallel(ctx, ParallelParams{
UserIds: userIds,
TrackIds: trackIds,
MyID: arg.MyID.(int32),
UserIds: userIds,
TrackIds: trackIds,
MyID: arg.MyID.(int32),
AuthedWallet: arg.AuthedWallet,
})
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion api/dbv1/queries/get_tracks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ LEFT JOIN aggregate_plays on play_item_id = t.track_id
LEFT JOIN track_routes on t.track_id = track_routes.track_id and track_routes.is_current = true
WHERE (is_unlisted = false OR t.owner_id = @my_id OR @include_unlisted::bool = TRUE)
AND t.track_id = ANY(@ids::int[])
AND t.access_authorities IS NULL
AND (t.access_authorities IS NULL
OR (COALESCE(@authed_wallet, '') <> ''
AND EXISTS (SELECT 1 FROM unnest(t.access_authorities) aa WHERE lower(aa) = lower(@authed_wallet))))
ORDER BY t.track_id
;
29 changes: 18 additions & 11 deletions api/request_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/ethereum/go-ethereum/crypto"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)

Expand Down Expand Up @@ -139,22 +138,30 @@ func (app *ApiServer) getSignerFromApiAccessKey(ctx context.Context, apiAccessKe
JOIN api_keys ak ON LOWER(ak.api_key) = LOWER(aak.api_key)
WHERE aak.api_access_key = $1 AND aak.is_active = true
`, apiAccessKey).Scan(&parentApiKey, &apiSecret)
if err == pgx.ErrNoRows || err != nil || apiSecret == "" {
return nil
if err == nil && apiSecret != "" {
privateKey, keyErr := crypto.HexToECDSA(strings.TrimPrefix(apiSecret, "0x"))
if keyErr != nil {
return nil
}
parentApiKeyLower := strings.ToLower(parentApiKey)
app.apiAccessKeySignerCache.Set(apiAccessKey, apiAccessKeySignerEntry{
ApiKey: parentApiKeyLower,
ApiSecret: apiSecret,
})
return &Signer{
Address: parentApiKeyLower,
PrivateKey: privateKey,
}
}

parentApiKeyLower := strings.ToLower(parentApiKey)
app.apiAccessKeySignerCache.Set(apiAccessKey, apiAccessKeySignerEntry{
ApiKey: parentApiKeyLower,
ApiSecret: apiSecret,
})

privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(apiSecret, "0x"))
// Fallback: use apiAccessKey as raw private key when no api_secret is found
privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(apiAccessKey, "0x"))
if err != nil {
return nil
}
address := crypto.PubkeyToAddress(privateKey.PublicKey)
return &Signer{
Address: parentApiKeyLower,
Address: address.Hex(),
PrivateKey: privateKey,
}
}
7 changes: 4 additions & 3 deletions api/v1_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ func (app *ApiServer) queryFullComments(
}
}
related, err := app.queries.Parallel(c.Context(), dbv1.ParallelParams{
UserIds: userIds,
TrackIds: trackIds,
MyID: app.getMyId(c),
UserIds: userIds,
TrackIds: trackIds,
MyID: app.getMyId(c),
AuthedWallet: app.tryGetAuthedWallet(c),
})
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions api/v1_explore_best_selling.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ func (app *ApiServer) v1ExploreBestSelling(c *fiber.Ctx) error {
}
}
related, err := app.queries.Parallel(c.Context(), dbv1.ParallelParams{
PlaylistIds: playlistIds,
TrackIds: trackIds,
MyID: app.getMyId(c),
PlaylistIds: playlistIds,
TrackIds: trackIds,
MyID: app.getMyId(c),
AuthedWallet: app.tryGetAuthedWallet(c),
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions api/v1_playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (app *ApiServer) v1Playlist(c *fiber.Ctx) error {
MyID: myId,
Ids: []int32{int32(playlistId)},
},
AuthedWallet: app.tryGetAuthedWallet(c),
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions api/v1_playlist_by_permalink.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (app *ApiServer) v1PlaylistByPermalink(c *fiber.Ctx) error {
MyID: myId,
Ids: ids,
},
AuthedWallet: app.tryGetAuthedWallet(c),
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions api/v1_playlist_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (app *ApiServer) v1PlaylistStream(c *fiber.Ctx) error {
MyID: myId,
Ids: []int32{int32(playlistId)},
},
AuthedWallet: app.tryGetAuthedWallet(c),
})
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions api/v1_playlist_tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ func (app *ApiServer) v1PlaylistTracks(c *fiber.Ctx) error {

tracks, err := app.queries.Tracks(c.Context(), dbv1.TracksParams{
GetTracksParams: dbv1.GetTracksParams{
Ids: trackIds,
MyID: myId,
Ids: trackIds,
MyID: myId,
AuthedWallet: app.tryGetAuthedWallet(c),
},
})

Expand Down
3 changes: 2 additions & 1 deletion api/v1_playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func (app *ApiServer) v1Playlists(c *fiber.Ctx) error {
MyID: myId,
Ids: ids,
},
OmitTracks: !withTracks,
OmitTracks: !withTracks,
AuthedWallet: app.tryGetAuthedWallet(c),
})
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion api/v1_playlists_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func (app *ApiServer) v1PlaylistsTop(c *fiber.Ctx) error {
Ids: playlistsIds,
MyID: myId,
},
OmitTracks: true,
OmitTracks: true,
AuthedWallet: app.tryGetAuthedWallet(c),
})

return v1PlaylistsResponse(c, playlists)
Expand Down
14 changes: 9 additions & 5 deletions api/v1_playlists_trending.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func (app *ApiServer) v1PlaylistsTrending(c *fiber.Ctx) error {
AND t.is_delete = false
AND t.is_current = true
AND t.stem_of IS NULL
AND t.access_authorities IS NULL
AND (t.access_authorities IS NULL
OR (COALESCE(@authed_wallet, '') <> ''
AND EXISTS (SELECT 1 FROM unnest(t.access_authorities) aa WHERE lower(aa) = lower(@authed_wallet))))
)
SELECT
playlist_id
Expand Down Expand Up @@ -95,9 +97,10 @@ func (app *ApiServer) v1PlaylistsTrending(c *fiber.Ctx) error {
`

rows, err := app.pool.Query(c.Context(), sql, pgx.NamedArgs{
"limit": params.Limit,
"offset": params.Offset,
"time": params.Time,
"limit": params.Limit,
"offset": params.Offset,
"time": params.Time,
"authed_wallet": app.tryGetAuthedWallet(c),
})
if err != nil {
return err
Expand All @@ -113,7 +116,8 @@ func (app *ApiServer) v1PlaylistsTrending(c *fiber.Ctx) error {
Ids: ids,
MyID: myId,
},
OmitTracks: params.OmitTracks,
OmitTracks: params.OmitTracks,
AuthedWallet: app.tryGetAuthedWallet(c),
// Limit these to 5 items to prevent slow load times
TrackLimit: 5,
})
Expand Down
11 changes: 7 additions & 4 deletions api/v1_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ func (app *ApiServer) searchTracks(c *fiber.Ctx) ([]dbv1.Track, error) {

tracks, err := app.queries.Tracks(c.Context(), dbv1.TracksParams{
GetTracksParams: dbv1.GetTracksParams{
Ids: tracksIds,
MyID: myId,
Ids: tracksIds,
MyID: myId,
AuthedWallet: app.tryGetAuthedWallet(c),
},
})
return tracks, err
Expand Down Expand Up @@ -214,7 +215,8 @@ func (app *ApiServer) searchPlaylists(c *fiber.Ctx) ([]dbv1.Playlist, error) {
Ids: playlistsIds,
MyID: myId,
},
OmitTracks: true,
OmitTracks: true,
AuthedWallet: app.tryGetAuthedWallet(c),
})
return playlists, err
}
Expand Down Expand Up @@ -256,7 +258,8 @@ func (app *ApiServer) searchAlbums(c *fiber.Ctx) ([]dbv1.Playlist, error) {
Ids: playlistsIds,
MyID: myId,
},
OmitTracks: true,
OmitTracks: true,
AuthedWallet: app.tryGetAuthedWallet(c),
})
return playlists, err
}
5 changes: 3 additions & 2 deletions api/v1_track.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ func (app *ApiServer) v1Track(c *fiber.Ctx) error {

tracks, err := app.queries.Tracks(c.Context(), dbv1.TracksParams{
GetTracksParams: dbv1.GetTracksParams{
MyID: myId,
Ids: []int32{int32(trackId)},
MyID: myId,
Ids: []int32{int32(trackId)},
AuthedWallet: app.tryGetAuthedWallet(c),
},
})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions api/v1_track_access_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (app *ApiServer) v1TrackAccessInfo(c *fiber.Ctx) error {
GetTracksParams: dbv1.GetTracksParams{
MyID: myId,
Ids: []int32{int32(trackId)},
AuthedWallet: app.tryGetAuthedWallet(c),
IncludeUnlisted: true,
},
})
Expand Down
6 changes: 5 additions & 1 deletion api/v1_track_comment_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ func (app *ApiServer) v1TrackCommentCount(c *fiber.Ctx) error {
track AS (
SELECT track_id, owner_id
FROM tracks
WHERE track_id = @trackId AND access_authorities IS NULL
WHERE track_id = @trackId
AND (access_authorities IS NULL
OR (COALESCE(@authed_wallet, '') <> ''
AND EXISTS (SELECT 1 FROM unnest(access_authorities) aa WHERE lower(aa) = lower(@authed_wallet))))
),

-- Users muted by high-karma users
Expand Down Expand Up @@ -107,6 +110,7 @@ func (app *ApiServer) v1TrackCommentCount(c *fiber.Ctx) error {
"myId": myId,
"trackId": trackId,
"karmaCommentCountThreshold": karmaCommentCountThreshold,
"authed_wallet": app.tryGetAuthedWallet(c),
})
if err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion api/v1_track_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ func (app *ApiServer) v1TrackComments(c *fiber.Ctx) error {
track AS (
SELECT track_id, owner_id
FROM tracks
WHERE track_id = @track_id AND access_authorities IS NULL
WHERE track_id = @track_id
AND (access_authorities IS NULL
OR (COALESCE(@authed_wallet, '') <> ''
AND EXISTS (SELECT 1 FROM unnest(access_authorities) aa WHERE lower(aa) = lower(@authed_wallet))))
),

-- Users muted by high-karma users
Expand Down Expand Up @@ -107,6 +110,7 @@ func (app *ApiServer) v1TrackComments(c *fiber.Ctx) error {
"myId": myId,
"track_id": trackId,
"karmaCommentCountThreshold": karmaCommentCountThreshold,
"authed_wallet": app.tryGetAuthedWallet(c),
}

return app.queryFullComments(c, sql, args, true)
Expand Down
5 changes: 3 additions & 2 deletions api/v1_track_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func (app *ApiServer) v1TrackDownload(c *fiber.Ctx) error {

tracks, err := app.queries.Tracks(c.Context(), dbv1.TracksParams{
GetTracksParams: dbv1.GetTracksParams{
MyID: myId,
Ids: []int32{int32(trackId)},
MyID: myId,
Ids: []int32{int32(trackId)},
AuthedWallet: app.tryGetAuthedWallet(c),
},
})
if err != nil {
Expand Down
Loading