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: 1 addition & 0 deletions internal/api/edit_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (s *editTestRunner) testVotePermissionsPromotion() {

func (s *editTestRunner) verifyUserRolePromotion(user *models.User) {
assert.Eventually(s.t, func() bool {
s.newRequest()
roles, _ := s.resolver.User().Roles(s.ctx, user)
for _, role := range roles {
if role == models.RoleEnumVote {
Expand Down
8 changes: 7 additions & 1 deletion internal/api/resolver_model_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/stashapp/stash-box/internal/auth"
"github.com/stashapp/stash-box/internal/converter"
"github.com/stashapp/stash-box/internal/dataloader"
"github.com/stashapp/stash-box/internal/models"
)
Expand All @@ -22,7 +23,12 @@ func (r *userResolver) Roles(ctx context.Context, user *models.User) ([]models.R
}
}

return r.services.User().GetRoles(ctx, user.ID)
roleStrings, err := dataloader.For(ctx).UserRolesByID.Load(user.ID)
if err != nil {
return nil, err
}

return converter.StringsToRoleEnums(roleStrings), nil
}

func (r *userResolver) VoteCount(ctx context.Context, obj *models.User) (*models.UserVoteCount, error) {
Expand Down
9 changes: 9 additions & 0 deletions internal/dataloader/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Loaders struct {
SceneEditsByID EditsLoader
EditCommentByID EditCommentLoader
UserByID UserLoader
UserRolesByID StringsLoader
}

func Middleware(fac service.Factory) func(next http.Handler) http.Handler {
Expand Down Expand Up @@ -299,6 +300,14 @@ func GetLoaders(ctx context.Context, fac service.Factory) *Loaders {
return s.LoadIds(ctx, ids)
},
},
UserRolesByID: StringsLoader{
maxBatch: 1000,
wait: 1 * time.Millisecond,
fetch: func(ids []uuid.UUID) ([][]string, []error) {
s := fac.User()
return s.LoadRoles(ctx, ids)
},
},
SceneByID: SceneLoader{
maxBatch: 1000,
wait: 1 * time.Millisecond,
Expand Down
1 change: 1 addition & 0 deletions internal/queries/querier.go

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

3 changes: 3 additions & 0 deletions internal/queries/sql/user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ DELETE FROM user_roles WHERE user_id = $1;
-- name: GetUserRoles :many
SELECT role FROM user_roles WHERE user_id = $1;

-- name: GetUserRolesByUserIDs :many
SELECT user_id, role FROM user_roles WHERE user_id = ANY($1::UUID[]);

-- name: CountVotesByType :many
SELECT vote, COUNT(*) as count FROM edit_votes WHERE user_id = $1 GROUP BY vote;

Expand Down
24 changes: 24 additions & 0 deletions internal/queries/user.sql.go

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

20 changes: 20 additions & 0 deletions internal/service/user/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,26 @@ func (s *User) GetRoles(ctx context.Context, userID uuid.UUID) ([]models.RoleEnu
return converter.StringsToRoleEnums(roleStrings), nil
}

// LoadRoles fetches roles for multiple users in one query.
func (s *User) LoadRoles(ctx context.Context, userIDs []uuid.UUID) ([][]string, []error) {
rows, err := s.queries.GetUserRolesByUserIDs(ctx, userIDs)
if err != nil {
return nil, errutil.DuplicateError(err, len(userIDs))
}

roleMap := make(map[uuid.UUID][]string, len(userIDs))
for _, row := range rows {
roleMap[row.UserID] = append(roleMap[row.UserID], row.Role)
}

roles := make([][]string, len(userIDs))
for i, userID := range userIDs {
roles[i] = roleMap[userID]
}

return roles, make([]error, len(userIDs))
}

// NewUser registers a new user. It returns the activation key only if
// email verification is not required, otherwise it returns nil.
func (s *User) NewUser(ctx context.Context, emailAddr string, inviteKey *uuid.UUID) (*uuid.UUID, error) {
Expand Down
Loading