Skip to content

Commit 6968b73

Browse files
[API-95] Add resolve endpoint, improve permalink matching
1 parent c6bfb4b commit 6968b73

23 files changed

Lines changed: 412 additions & 22 deletions

api/dbv1/get_playlist_ids_by_permalink.sql.go

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

api/dbv1/get_user_for_handle.sql.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- name: GetPlaylistIdsByPermalink :many
2+
SELECT playlist_id
3+
FROM playlist_routes
4+
JOIN users ON users.user_id = playlist_routes.owner_id
5+
WHERE handle_lc = ANY(@handles::text[])
6+
AND slug = ANY(@slugs::text[])
7+
AND (
8+
CONCAT(handle_lc, '/playlist/', slug) = ANY(@permalinks::text[]) -- in case of conflicts across users
9+
OR CONCAT(handle_lc, '/album/', slug) = ANY(@permalinks::text[]) -- in case of conflicts across users
10+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- name: GetUserForHandle :one
2+
SELECT user_id FROM users
3+
WHERE
4+
handle_lc = lower(@handle)
5+
AND is_current = true
6+
ORDER BY created_at ASC
7+
LIMIT 1;

api/fixture_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,30 @@ var (
161161
"updated_at": time.Now(),
162162
"txhash": "tx123",
163163
}
164+
165+
trackRouteBaseRow = map[string]any{
166+
"slug": nil,
167+
"title_slug": nil,
168+
"collision_id": nil,
169+
"owner_id": nil,
170+
"track_id": nil,
171+
"is_current": true,
172+
"blockhash": "block_abc123",
173+
"blocknumber": 101,
174+
"txhash": "tx123",
175+
}
176+
177+
playlistRouteBaseRow = map[string]any{
178+
"slug": nil,
179+
"title_slug": nil,
180+
"collision_id": nil,
181+
"owner_id": nil,
182+
"playlist_id": nil,
183+
"is_current": true,
184+
"blockhash": "block_abc123",
185+
"blocknumber": 101,
186+
"txhash": "tx123",
187+
}
164188
)
165189

166190
func insertFixtures(table string, baseRow map[string]any, csvFile string) {

api/resolve_middleware.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ func (app *ApiServer) isFullMiddleware(c *fiber.Ctx) error {
1414
return c.Next()
1515
}
1616

17+
func (app *ApiServer) getIsFull(c *fiber.Ctx) bool {
18+
return c.Locals("isFull").(bool)
19+
}
20+
1721
// will set myId if valid, defaults to 0
1822
func (app *ApiServer) resolveMyIdMiddleware(c *fiber.Ctx) error {
1923
myId, _ := trashid.DecodeHashId(c.Query("user_id"))

api/server.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ func NewApiServer(config config.Config) *ApiServer {
244244

245245
// Rewards
246246
g.Get("/rewards/claim", app.v1ClaimRewards)
247+
248+
// Resolve
249+
g.Get("/resolve", app.v1Resolve)
247250
}
248251

249252
app.Static("/", "./static")
@@ -308,11 +311,9 @@ func (app *ApiServer) resolveUserHandleToId(handle string) (int32, error) {
308311
if hit, ok := app.resolveHandleCache.Get(handle); ok {
309312
return hit, nil
310313
}
311-
var userId int32
312-
sql := `select user_id from users where handle_lc = lower($1)`
313-
err := app.pool.QueryRow(context.Background(), sql, handle).Scan(&userId)
314-
app.resolveHandleCache.Set(handle, userId)
315-
return userId, err
314+
user_id, err := app.queries.GetUserForHandle(context.Background(), handle)
315+
app.resolveHandleCache.Set(handle, int32(user_id))
316+
return int32(user_id), err
316317
}
317318

318319
func (as *ApiServer) Serve() {

api/server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ func TestMain(m *testing.M) {
7777
insertFixtures("associated_wallets", connectedWalletsBaseRow, "testdata/connected_wallets_fixtures.csv")
7878
insertFixtures("aggregate_user_tips", aggregateUserTipsBaseRow, "testdata/aggregate_user_tips_fixtures.csv")
7979
insertFixtures("usdc_purchases", usdcPurchaseBaseRow, "testdata/usdc_purchases_fixtures.csv")
80-
insertFixtures("track_routes", map[string]any{}, "testdata/track_routes_fixtures.csv")
80+
insertFixtures("track_routes", trackRouteBaseRow, "testdata/track_routes_fixtures.csv")
81+
insertFixtures("playlist_routes", playlistRouteBaseRow, "testdata/playlist_routes_fixtures.csv")
8182
insertFixtures("grants", grantBaseRow, "testdata/grants_fixtures.csv")
8283

8384
// index to es / os

api/testdata/playlist_fixtures.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ playlist_id,playlist_name,playlist_owner_id,is_album,playlist_contents,stream_co
33
2,Follow Gated Stream,3,t,"{}","{""follow_user_id"": 3}"
44
3,SecondAlbum,1,t,"{""track_ids"": [{""time"": 1722451644, ""track"": 200, ""metadata_time"": 1722451644},{""time"": 1722451644, ""track"": -1, ""metadata_time"": 1722451644},{""time"": 1722451644, ""track"": 300, ""metadata_time"": 1722451644}]}",
55
4,Purchase Gated Stream,3,t,"{}","{""usdc_purchase"": {""price"": 135, ""splits"": [{""user_id"": 3, ""percentage"": 100.0}]}}"
6+
500,playlist by permalink,7,f,,
7+
501,album by permalink,7,t,,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
slug,title_slug,collision_id,owner_id,playlist_id
2+
playlist-by-permalink,playlist-by-permalink,0,7,500
3+
album-by-permalink,album-by-permalink,0,8,501

0 commit comments

Comments
 (0)