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
47 changes: 0 additions & 47 deletions api/dbv1/full_managers.go

This file was deleted.

32 changes: 15 additions & 17 deletions api/dbv1/get_grants.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions api/dbv1/managers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package dbv1

import (
"context"
)

type FullManagerGrant struct {
GetGrantsForUserIdRow
GranteeUserID *struct{} `json:"grantee_user_id,omitempty"`
}

type FullManagedUserGrant struct {
GetGrantsForGranteeUserIdRow
GranteeUserID *struct{} `json:"grantee_user_id,omitempty"`
}

type FullManager struct {
Manager FullUser `json:"manager"`
Grant FullManagerGrant `json:"grant"`
}

type FullManagedUser struct {
User FullUser `json:"user"`
Grant FullManagedUserGrant `json:"grant"`
}

func (q *Queries) FullManagers(ctx context.Context, params GetGrantsForUserIdParams) ([]FullManager, error) {

grants, err := q.GetGrantsForUserId(ctx, params)
if err != nil {
return nil, err
}

user_ids := make([]int32, len(grants))
for i, grant := range grants {
user_ids[i] = int32(grant.GranteeUserID)
}

users, err := q.FullUsersKeyed(ctx, GetUsersParams{
Ids: user_ids,
MyID: params.UserID,
})

if err != nil {
return nil, err
}

managers := make([]FullManager, len(grants))
for i, grant := range grants {
managers[i] = FullManager{
Manager: users[int32(grant.GranteeUserID)],
Grant: FullManagerGrant{GetGrantsForUserIdRow: grant},
}
}

return managers, nil
}

func (q *Queries) FullManagedUsers(ctx context.Context, params GetGrantsForGranteeUserIdParams) ([]FullManagedUser, error) {
grants, err := q.GetGrantsForGranteeUserId(ctx, GetGrantsForGranteeUserIdParams{
IsRevoked: params.IsRevoked,
IsApproved: params.IsApproved,
UserID: params.UserID,
})
if err != nil {
return nil, err
}

user_ids := make([]int32, len(grants))
for i, grant := range grants {
user_ids[i] = int32(grant.UserID)
}

users, err := q.FullUsersKeyed(ctx, GetUsersParams{
Ids: user_ids,
MyID: params.UserID,
})

if err != nil {
return nil, err
}

managedUsers := make([]FullManagedUser, len(grants))
for i, grant := range grants {
managedUsers[i] = FullManagedUser{
User: users[int32(grant.UserID)],
Grant: FullManagedUserGrant{GetGrantsForGranteeUserIdRow: grant},
}
}

return managedUsers, nil
}
14 changes: 6 additions & 8 deletions api/dbv1/queries/get_grants.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ JOIN users u ON u.wallet = g.grantee_address
WHERE g.user_id = @user_id::int
AND g.is_revoked = @is_revoked
AND g.is_current = true
AND sqlc.narg('is_approved')::boolean IS NULL OR g.is_approved = sqlc.narg('is_approved')
ORDER BY g.created_at DESC;
AND (sqlc.narg('is_approved')::boolean IS NULL OR g.is_approved = sqlc.narg('is_approved'));

-- name: GetGrantsForGranteeAddress :many
-- name: GetGrantsForGranteeUserId :many
SELECT
g.user_id,
g.grantee_address,
Expand All @@ -24,10 +23,9 @@ SELECT
g.created_at,
g.updated_at,
u.user_id as grantee_user_id
FROM grants g
JOIN users u ON u.wallet = g.grantee_address
WHERE g.grantee_address = @grantee_address
FROM users u
JOIN grants g ON g.grantee_address = u.wallet
WHERE u.user_id = @user_id::int
AND g.is_current = true
AND g.is_revoked = @is_revoked
AND sqlc.narg('is_approved')::boolean IS NULL OR g.is_approved = sqlc.narg('is_approved')
ORDER BY g.created_at DESC;
AND (sqlc.narg('is_approved')::boolean IS NULL OR g.is_approved = sqlc.narg('is_approved'));
20 changes: 20 additions & 0 deletions api/request_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package api

import (
"strconv"

"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5/pgtype"
)

func getOptionalBool(c *fiber.Ctx, key string) (pgtype.Bool, error) {
var value *bool
if valueStr := c.Query(key); valueStr != "" {
parsed, err := strconv.ParseBool(valueStr)
if err != nil {
return pgtype.Bool{}, err
}
value = &parsed
}
return pgtype.Bool{Bool: value != nil && *value, Valid: value != nil}, nil
}
Copy link
Copy Markdown
Contributor

@stereosteve stereosteve May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be written more simple like:

func getOptionalBool(c *fiber.Ctx, key string) (pgtype.Bool, error) {
	if valueStr := c.Query(key); valueStr != "" {
		parsed, err := strconv.ParseBool(c.Query(key))
		if err != nil {
			return pgtype.Bool{}, err
		}
		return pgtype.Bool{Bool: parsed, Valid: true}, nil
	}
	return pgtype.Bool{}, nil
}

8 changes: 8 additions & 0 deletions api/resolve_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func (app *ApiServer) getMyId(c *fiber.Ctx) int32 {
return int32(myId.(int))
}

func (app *ApiServer) getUserId(c *fiber.Ctx) int32 {
userId := c.Locals("userId")
if userId == nil {
return 0
}
return int32(userId.(int))
}

func (app *ApiServer) requireUserIdMiddleware(c *fiber.Ctx) error {
userId, err := trashid.DecodeHashId(c.Params("userId"))
if err != nil || userId == 0 {
Expand Down
1 change: 1 addition & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func NewApiServer(config config.Config) *ApiServer {
g.Get("/users/:userId/library/tracks", app.v1UsersLibraryTracks)
g.Get("/users/:userId/library/:playlistType", app.v1UsersLibraryPlaylists)
g.Get("/users/:userId/managers", app.v1UsersManagers)
g.Get("/users/:userId/managed_users", app.v1UsersManagedUsers)
g.Get("/users/:userId/mutuals", app.v1UsersMutuals)
g.Get("/users/:userId/reposts", app.v1UsersReposts)
g.Get("/users/:userId/related", app.v1UsersRelated)
Expand Down
1 change: 1 addition & 0 deletions api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func Test200UnAuthed(t *testing.T) {
"/v1/full/users/7eP5n/library/albums?type=purchase&sort_method=saves",

"/v1/full/users/7eP5n/managers",
"/v1/full/users/7eP5n/managed_users",
"/v1/full/users/7eP5n/mutuals",
"/v1/full/users/7eP5n/reposts",
"/v1/full/users/7eP5n/related",
Expand Down
3 changes: 3 additions & 0 deletions api/testdata/grants_fixtures.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ user_id,grantee_address,is_approved,is_revoked
1,0xc451c1f8943b575158310552b41230c61844a1c1,false,true
1,0x1234567890abcdef,true,true
1,0x681c616ae836ceca1effe00bd07f2fdbf9a082bc,false,false
2,0x681c616ae836ceca1effe00bd07f2fdbf9a082bc,true,false
3,0x681c616ae836ceca1effe00bd07f2fdbf9a082bc,true,true
4,0x681c616ae836ceca1effe00bd07f2fdbf9a082bc,false,true
36 changes: 36 additions & 0 deletions api/v1_users_managed_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package api

import (
"strconv"

"bridgerton.audius.co/api/dbv1"
"github.com/gofiber/fiber/v2"
)

func (app *ApiServer) v1UsersManagedUsers(c *fiber.Ctx) error {
// Behavior of this field is a little odd. We only want to filter by it
// if it is passed, but otherwise not use a default value for either.
isApproved, err := getOptionalBool(c, "is_approved")
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid value for is_approved")
}

isRevoked, err := strconv.ParseBool(c.Query("is_revoked", "false"))
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid value for is_revoked")
}
params := dbv1.GetGrantsForGranteeUserIdParams{
UserID: app.getUserId(c),
IsApproved: isApproved,
IsRevoked: isRevoked,
}

managedUsers, err := app.queries.FullManagedUsers(c.Context(), params)
if err != nil {
return err
}

return c.JSON(fiber.Map{
"data": managedUsers,
})
}
Loading
Loading