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.

160 lines
3.8 KiB

  1. package resolvers
  2. // This file will be automatically regenerated based on the schema, any resolver implementations
  3. // will be copied through when generating and any unknown code will be moved to the end.
  4. import (
  5. "context"
  6. "errors"
  7. "git.aiterp.net/stufflog/server/graph/graphcore"
  8. "git.aiterp.net/stufflog/server/internal/slerrors"
  9. "git.aiterp.net/stufflog/server/models"
  10. )
  11. func (r *queryResolver) Issue(ctx context.Context, id string) (*models.Issue, error) {
  12. user := r.Auth.UserFromContext(ctx)
  13. if user == nil {
  14. return nil, slerrors.PermissionDenied
  15. }
  16. issue, err := r.Database.Issues().Find(ctx, id)
  17. if err != nil {
  18. return nil, err
  19. }
  20. _, err = r.Auth.IssuePermission(ctx, *issue)
  21. if err != nil {
  22. return nil, err
  23. }
  24. return issue, nil
  25. }
  26. func (r *queryResolver) Issues(ctx context.Context, filter *models.IssueFilter) ([]*models.Issue, error) {
  27. user := r.Auth.UserFromContext(ctx)
  28. if user == nil {
  29. return nil, slerrors.PermissionDenied
  30. }
  31. if filter == nil {
  32. filter = &models.IssueFilter{}
  33. }
  34. issues, err := r.Database.Issues().List(ctx, *filter)
  35. if err != nil {
  36. return nil, err
  37. }
  38. deleteList := make([]int, 0, len(issues))
  39. for i, issue := range issues {
  40. _, err := r.Auth.IssuePermission(ctx, *issue)
  41. if err != nil {
  42. deleteList = append(deleteList, i-len(deleteList))
  43. }
  44. }
  45. for _, index := range deleteList {
  46. issues = append(issues[:index], issues[index+1:]...)
  47. }
  48. return issues, nil
  49. }
  50. func (r *queryResolver) Item(ctx context.Context, id string) (*models.Item, error) {
  51. user := r.Auth.UserFromContext(ctx)
  52. if user == nil {
  53. return nil, slerrors.PermissionDenied
  54. }
  55. return r.Database.Items().Find(ctx, id)
  56. }
  57. func (r *queryResolver) Items(ctx context.Context, filter *models.ItemFilter) ([]*models.Item, error) {
  58. user := r.Auth.UserFromContext(ctx)
  59. if user == nil {
  60. return nil, slerrors.PermissionDenied
  61. }
  62. if filter == nil {
  63. filter = &models.ItemFilter{}
  64. }
  65. return r.Database.Items().List(ctx, *filter)
  66. }
  67. func (r *queryResolver) ItemTags(ctx context.Context) ([]string, error) {
  68. user := r.Auth.UserFromContext(ctx)
  69. if user == nil {
  70. return nil, slerrors.PermissionDenied
  71. }
  72. return r.Database.Items().GetTags(ctx)
  73. }
  74. func (r *queryResolver) Project(ctx context.Context, id string) (*models.Project, error) {
  75. user := r.Auth.UserFromContext(ctx)
  76. if user == nil {
  77. return nil, slerrors.PermissionDenied
  78. }
  79. project, err := r.Database.Projects().Find(ctx, id)
  80. if err != nil {
  81. return nil, err
  82. }
  83. _, err = r.Auth.ProjectPermission(ctx, *project)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return project, nil
  88. }
  89. func (r *queryResolver) Projects(ctx context.Context, filter *models.ProjectFilter) ([]*models.Project, error) {
  90. user := r.Auth.UserFromContext(ctx)
  91. if user == nil {
  92. return nil, slerrors.PermissionDenied
  93. }
  94. skipCheck := false
  95. if filter == nil {
  96. filter = &models.ProjectFilter{}
  97. }
  98. if filter.Permission == nil {
  99. skipCheck = true
  100. filter.Permission = &models.ProjectFilterPermission{
  101. UserID: user.ID,
  102. MinLevel: models.ProjectPermissionLevelObserver,
  103. }
  104. }
  105. projects, err := r.Database.Projects().List(ctx, *filter)
  106. if err != nil {
  107. return nil, err
  108. }
  109. if !skipCheck && len(projects) > 0 {
  110. deleteList := make([]int, 0, 4)
  111. for i, project := range projects {
  112. if _, err := r.Auth.ProjectPermission(ctx, *project); err != nil {
  113. deleteList = append(deleteList, i-len(deleteList))
  114. }
  115. }
  116. for _, di := range deleteList {
  117. projects = append(projects[:di], projects[di:]...)
  118. }
  119. }
  120. return projects, nil
  121. }
  122. func (r *queryResolver) Session(ctx context.Context) (*models.User, error) {
  123. user := r.Auth.UserFromContext(ctx)
  124. if user == nil {
  125. return nil, errors.New("not logged in")
  126. }
  127. return user, nil
  128. }
  129. // Query returns graphcore.QueryResolver implementation.
  130. func (r *Resolver) Query() graphcore.QueryResolver { return &queryResolver{r} }
  131. type queryResolver struct{ *Resolver }