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

  1. package loaders
  2. import (
  3. "context"
  4. "git.aiterp.net/stufflog/server/database/repositories"
  5. "git.aiterp.net/stufflog/server/models"
  6. "time"
  7. )
  8. var projectPermissionCtxKey = "ctx.stufflog.ProjectPermissionLoader"
  9. func ProjectPermissionLoaderFromContext(ctx context.Context) *ProjectPermissionLoader {
  10. return ctx.Value(projectPermissionCtxKey).(*ProjectPermissionLoader)
  11. }
  12. func NewProjectPermissionLoader(ctx context.Context, user *models.User, projectRepo repositories.ProjectRepository) *ProjectPermissionLoader {
  13. return &ProjectPermissionLoader{
  14. wait: time.Millisecond / 2,
  15. maxBatch: 16,
  16. fetch: func(keys []string) ([]*models.ProjectPermission, []error) {
  17. errors := make([]error, len(keys))
  18. results := make([]*models.ProjectPermission, len(keys))
  19. if user == nil {
  20. for i, key := range keys {
  21. results[i] = &models.ProjectPermission{
  22. ProjectID: key,
  23. UserID: "",
  24. Level: models.ProjectPermissionLevelNoAccess,
  25. }
  26. }
  27. return results, errors
  28. }
  29. indices := make(map[string]int)
  30. for i, key := range keys {
  31. indices[key] = i
  32. }
  33. permissions, err := projectRepo.GetUserPermissions(ctx, *user, keys)
  34. if err != nil {
  35. for i := range errors {
  36. errors[i] = err
  37. }
  38. return results, errors
  39. }
  40. for _, permission := range permissions {
  41. results[indices[permission.ProjectID]] = permission
  42. }
  43. return results, errors
  44. },
  45. }
  46. }