stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

56 lines
1.4 KiB

package loaders
import (
"context"
"git.aiterp.net/stufflog/server/database/repositories"
"git.aiterp.net/stufflog/server/models"
"time"
)
var projectPermissionCtxKey = "ctx.stufflog.ProjectPermissionLoader"
func ProjectPermissionLoaderFromContext(ctx context.Context) *ProjectPermissionLoader {
return ctx.Value(projectPermissionCtxKey).(*ProjectPermissionLoader)
}
func NewProjectPermissionLoader(ctx context.Context, user *models.User, projectRepo repositories.ProjectRepository) *ProjectPermissionLoader {
return &ProjectPermissionLoader{
wait: time.Millisecond / 2,
maxBatch: 16,
fetch: func(keys []string) ([]*models.ProjectPermission, []error) {
errors := make([]error, len(keys))
results := make([]*models.ProjectPermission, len(keys))
if user == nil {
for i, key := range keys {
results[i] = &models.ProjectPermission{
ProjectID: key,
UserID: "",
Level: models.ProjectPermissionLevelNoAccess,
}
}
return results, errors
}
indices := make(map[string]int)
for i, key := range keys {
indices[key] = i
}
permissions, err := projectRepo.GetUserPermissions(ctx, *user, keys)
if err != nil {
for i := range errors {
errors[i] = err
}
return results, errors
}
for _, permission := range permissions {
results[indices[permission.ProjectID]] = permission
}
return results, errors
},
}
}