Skip to content
Open
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
1 change: 0 additions & 1 deletion api/resolve_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (app *ApiServer) getUserId(c *fiber.Ctx) int32 {
}

func (app *ApiServer) requireUserIdMiddleware(c *fiber.Ctx) error {
// Allow /users/me/* routes to pass through without userId resolution
if c.Params("userId") == "me" {
return c.Next()
}
Expand Down
2 changes: 1 addition & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func NewApiServer(config config.Config) *ApiServer {
g.Get("/users/genre/top", app.v1UsersGenreTop)
g.Get("/users/account/:wallet", app.requireAuthMiddleware, app.v1UsersAccount)
g.Get("/users/verify_token", app.v1UsersVerifyToken)
g.Post("/users/me/ping", app.requireAuthMiddleware, app.postV1UsersPing)
g.Post("/users/me/ping", app.postV1UsersPing)

g.Use("/users/handle/:handle", app.requireHandleMiddleware)
g.Get("/users/handle/:handle", app.v1User)
Expand Down
9 changes: 6 additions & 3 deletions api/v1_users_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ func (app *ApiServer) postV1UsersPing(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusServiceUnavailable, "writes not available")
}

wallet := app.getAuthedWallet(c)
myId := app.getMyId(c)
if myId == 0 {
return fiber.NewError(fiber.StatusBadRequest, "user_id query param is required")
}

_, err := app.writePool.Exec(c.Context(), `
UPDATE users
SET last_active_at = now()
WHERE wallet = $1
WHERE user_id = $1
AND is_current = true
`, wallet)
`, myId)
if err != nil {
app.logger.Error("postV1UsersPing: failed to update last_active_at", zap.Error(err))
return fiber.NewError(fiber.StatusInternalServerError, "failed to record activity")
Expand Down
32 changes: 32 additions & 0 deletions api/v1_users_ping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package api

import (
"testing"

"api.audius.co/database"
"api.audius.co/api/testdata"
"github.com/stretchr/testify/assert"
)

func TestPostV1UsersPing(t *testing.T) {
app := emptyTestApp(t)
database.SeedTable(app.pool.Replicas[0], "users", testdata.UserFixtures)

// user 1 = wallet 0x7d273271690538cf855e5b3002a0dd8c154bb060, encoded = 7eP5n
wallet := "0x7d273271690538cf855e5b3002a0dd8c154bb060"

t.Run("authenticated request returns 200", func(t *testing.T) {
status, body := testPostWithWallet(t, app, "/v1/users/me/ping?user_id=7eP5n", wallet, nil, nil)
assert.Equal(t, 200, status, "body: %s", string(body))
})

t.Run("missing user_id returns 400", func(t *testing.T) {
status, _ := testPostWithWallet(t, app, "/v1/users/me/ping", wallet, nil, nil)
assert.Equal(t, 400, status)
})

t.Run("unauthenticated request with user_id returns 403", func(t *testing.T) {
status, _ := testPost(t, app, "/v1/users/me/ping?user_id=7eP5n", nil, nil)
assert.Equal(t, 403, status)
})
}
Loading