Skip to content

Commit 0552250

Browse files
[API-95] Add resolve endpoint, improve permalink matching (#55)
New working URLs http://localhost:1323/v1/resolve?url=https://audius.co/jacknifemusic/jacknife-bladeboi-on-my-mind-remix-contest http://localhost:1323/v1/resolve?url=https://audius.co/sturmfrau/playlist/this-is-sturmfrau http://localhost:1323/v1/resolve?url=https://audius.co/oshimakesmusic Tested= ``` make test ```
1 parent d5357d0 commit 0552250

24 files changed

Lines changed: 457 additions & 39 deletions

api/dbv1/get_playlist_ids_by_permalink.sql.go

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

api/dbv1/get_track_ids_by_permalink.sql.go

Lines changed: 20 additions & 8 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: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- name: GetPlaylistIdsByPermalink :many
2+
WITH lower_handles AS (
3+
SELECT LOWER(h) AS handle
4+
FROM unnest(@handles::text[]) AS h
5+
),
6+
lower_permalinks AS (
7+
SELECT LOWER(p) AS permalink
8+
FROM unnest(@permalinks::text[]) AS p
9+
)
10+
SELECT pr.playlist_id
11+
FROM playlist_routes pr
12+
JOIN users u ON u.user_id = pr.owner_id
13+
JOIN lower_handles lh
14+
ON u.handle_lc = lh.handle
15+
WHERE pr.slug = ANY(@slugs::text[])
16+
-- in case of conflicts across users
17+
AND (
18+
CONCAT('/', u.handle_lc, '/playlist/', LOWER(pr.slug)) = ANY(
19+
SELECT permalink FROM lower_permalinks
20+
)
21+
OR CONCAT('/', u.handle_lc, '/album/', LOWER(pr.slug)) = ANY(
22+
SELECT permalink FROM lower_permalinks
23+
)
24+
);
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
-- name: GetTrackIdsByPermalink :many
2-
SELECT track_id
3-
FROM track_routes
4-
JOIN users ON users.user_id = track_routes.owner_id
5-
WHERE handle_lc = ANY(@handles::text[])
6-
AND slug = ANY(@slugs::text[])
7-
AND CONCAT(handle_lc, '/', slug) = ANY(@permalinks::text[]) -- in case of conflicts across users
8-
;
2+
WITH lower_handles AS (
3+
SELECT LOWER(h) AS handle
4+
FROM unnest(@handles::text[]) AS h
5+
),
6+
lower_permalinks AS (
7+
SELECT LOWER(p) AS permalink
8+
FROM unnest(@permalinks::text[]) AS p
9+
)
10+
SELECT tr.track_id
11+
FROM track_routes tr
12+
JOIN users u ON u.user_id = tr.owner_id
13+
JOIN lower_handles lh
14+
ON u.handle_lc = lh.handle
15+
WHERE tr.slug = ANY(@slugs::text[])
16+
-- in case of conflicts across usAers
17+
AND CONCAT('/', u.handle_lc, '/', LOWER(tr.slug)) = ANY(
18+
SELECT permalink FROM lower_permalinks
19+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- name: GetUserForHandle :one
2+
SELECT user_id FROM users
3+
WHERE
4+
handle_lc = lower(@handle)
5+
ORDER BY created_at ASC
6+
LIMIT 1;

api/fixture_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ var (
162162
"txhash": "tx123",
163163
}
164164

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+
}
188+
165189
commentBaseRow = map[string]any{
166190
"entity_type": "Track",
167191
"created_at": time.Now(),

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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ func NewApiServer(config config.Config) *ApiServer {
250250
// Rewards
251251
g.Get("/rewards/claim", app.v1ClaimRewards)
252252

253+
// Resolve
254+
g.Get("/resolve", app.v1Resolve)
255+
253256
// Comments
254257
g.Get("/comments/unclaimed_id", app.v1CommentsUnclaimedId)
255258
}
@@ -316,11 +319,12 @@ func (app *ApiServer) resolveUserHandleToId(handle string) (int32, error) {
316319
if hit, ok := app.resolveHandleCache.Get(handle); ok {
317320
return hit, nil
318321
}
319-
var userId int32
320-
sql := `select user_id from users where handle_lc = lower($1)`
321-
err := app.pool.QueryRow(context.Background(), sql, handle).Scan(&userId)
322-
app.resolveHandleCache.Set(handle, userId)
323-
return userId, err
322+
user_id, err := app.queries.GetUserForHandle(context.Background(), handle)
323+
if err != nil {
324+
return 0, err
325+
}
326+
app.resolveHandleCache.Set(handle, int32(user_id))
327+
return int32(user_id), nil
324328
}
325329

326330
func (as *ApiServer) Serve() {

api/server_test.go

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

0 commit comments

Comments
 (0)