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.

277 lines
6.3 KiB

5 years ago
  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/loaders"
  8. "git.aiterp.net/stufflog/server/graph/graphcore"
  9. "git.aiterp.net/stufflog/server/internal/slerrors"
  10. "git.aiterp.net/stufflog/server/models"
  11. )
  12. func (r *queryResolver) Issue(ctx context.Context, id string) (*models.Issue, error) {
  13. user := r.Auth.UserFromContext(ctx)
  14. if user == nil {
  15. return nil, slerrors.PermissionDenied
  16. }
  17. issue, err := loaders.IssueLoaderFromContext(ctx).Load(id)
  18. if err != nil {
  19. return nil, err
  20. }
  21. _, err = r.Auth.IssuePermission(ctx, *issue)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return issue, nil
  26. }
  27. func (r *queryResolver) Issues(ctx context.Context, filter *models.IssueFilter) ([]*models.Issue, error) {
  28. user := r.Auth.UserFromContext(ctx)
  29. if user == nil {
  30. return nil, slerrors.PermissionDenied
  31. }
  32. if filter == nil {
  33. filter = &models.IssueFilter{}
  34. }
  35. issues, err := r.Database.Issues().List(ctx, *filter)
  36. if err != nil {
  37. return nil, err
  38. }
  39. loader := loaders.IssueLoaderFromContext(ctx)
  40. for _, issue := range issues {
  41. loader.Prime(issue.ID, issue)
  42. }
  43. deleteList := make([]int, 0, len(issues))
  44. for i, issue := range issues {
  45. _, err := r.Auth.IssuePermission(ctx, *issue)
  46. if err != nil {
  47. deleteList = append(deleteList, i-len(deleteList))
  48. }
  49. }
  50. for _, index := range deleteList {
  51. issues = append(issues[:index], issues[index+1:]...)
  52. }
  53. return issues, nil
  54. }
  55. func (r *queryResolver) Item(ctx context.Context, id string) (*models.Item, error) {
  56. user := r.Auth.UserFromContext(ctx)
  57. if user == nil {
  58. return nil, slerrors.PermissionDenied
  59. }
  60. return r.Database.Items().Find(ctx, id)
  61. }
  62. func (r *queryResolver) Items(ctx context.Context, filter *models.ItemFilter) ([]*models.Item, error) {
  63. user := r.Auth.UserFromContext(ctx)
  64. if user == nil {
  65. return nil, slerrors.PermissionDenied
  66. }
  67. if filter == nil {
  68. filter = &models.ItemFilter{}
  69. }
  70. return r.Database.Items().List(ctx, *filter)
  71. }
  72. func (r *queryResolver) ItemTags(ctx context.Context) ([]string, error) {
  73. user := r.Auth.UserFromContext(ctx)
  74. if user == nil {
  75. return nil, slerrors.PermissionDenied
  76. }
  77. return r.Database.Items().GetTags(ctx)
  78. }
  79. func (r *queryResolver) IssueItem(ctx context.Context, id string) (*models.IssueItem, error) {
  80. user := r.Auth.UserFromContext(ctx)
  81. if user == nil {
  82. return nil, slerrors.PermissionDenied
  83. }
  84. item, err := r.Database.IssueItems().Find(ctx, id)
  85. if err != nil {
  86. return nil, err
  87. }
  88. issue, err := loaders.IssueLoaderFromContext(ctx).Load(id)
  89. if err != nil {
  90. return nil, err
  91. }
  92. _, err = r.Auth.IssuePermission(ctx, *issue)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return item, nil
  97. }
  98. func (r *queryResolver) IssueItems(ctx context.Context, filter *models.IssueItemFilter) ([]*models.IssueItem, error) {
  99. user := r.Auth.UserFromContext(ctx)
  100. if user == nil {
  101. return nil, slerrors.PermissionDenied
  102. }
  103. if filter == nil {
  104. filter = &models.IssueItemFilter{}
  105. }
  106. items, err := r.Database.IssueItems().List(ctx, *filter)
  107. if err != nil {
  108. return nil, err
  109. }
  110. accessMap := make(map[string]bool)
  111. deleteList := make([]int, 0, len(items))
  112. for i, item := range items {
  113. if access, ok := accessMap[item.IssueID]; ok && access {
  114. continue
  115. } else if ok && !access {
  116. deleteList = append(deleteList, i-len(deleteList))
  117. continue
  118. }
  119. issue, err := loaders.IssueLoaderFromContext(ctx).Load(item.IssueID)
  120. if err != nil {
  121. deleteList = append(deleteList, i-len(deleteList))
  122. accessMap[item.IssueID] = true
  123. continue
  124. }
  125. _, err = r.Auth.IssuePermission(ctx, *issue)
  126. if err != nil {
  127. deleteList = append(deleteList, i-len(deleteList))
  128. }
  129. accessMap[issue.ID] = err != nil
  130. }
  131. for _, index := range deleteList {
  132. items = append(items[:index], items[index+1:]...)
  133. }
  134. return items, nil
  135. }
  136. func (r *queryResolver) Project(ctx context.Context, id string) (*models.Project, error) {
  137. user := r.Auth.UserFromContext(ctx)
  138. if user == nil {
  139. return nil, slerrors.PermissionDenied
  140. }
  141. project, err := r.Database.Projects().Find(ctx, id)
  142. if err != nil {
  143. return nil, err
  144. }
  145. _, err = r.Auth.ProjectPermission(ctx, project.ID)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return project, nil
  150. }
  151. func (r *queryResolver) Projects(ctx context.Context, filter *models.ProjectFilter) ([]*models.Project, error) {
  152. user := r.Auth.UserFromContext(ctx)
  153. if user == nil {
  154. return nil, slerrors.PermissionDenied
  155. }
  156. skipCheck := false
  157. if filter == nil {
  158. filter = &models.ProjectFilter{}
  159. }
  160. if filter.Permission == nil {
  161. skipCheck = true
  162. filter.Permission = &models.ProjectFilterPermission{
  163. UserID: user.ID,
  164. MinLevel: models.ProjectPermissionLevelObserver,
  165. }
  166. }
  167. projects, err := r.Database.Projects().List(ctx, *filter)
  168. if err != nil {
  169. return nil, err
  170. }
  171. if !skipCheck && len(projects) > 0 {
  172. deleteList := make([]int, 0, 4)
  173. for i, project := range projects {
  174. if _, err := r.Auth.ProjectPermission(ctx, project.ID); err != nil {
  175. deleteList = append(deleteList, i-len(deleteList))
  176. }
  177. }
  178. for _, di := range deleteList {
  179. projects = append(projects[:di], projects[di:]...)
  180. }
  181. }
  182. return projects, nil
  183. }
  184. func (r *queryResolver) Log(ctx context.Context, id string) (*models.Log, error) {
  185. user := r.Auth.UserFromContext(ctx)
  186. if user == nil {
  187. return nil, slerrors.PermissionDenied
  188. }
  189. log, err := r.Database.Logs().Find(ctx, id)
  190. if err != nil {
  191. return nil, err
  192. }
  193. r.Auth.FilterLog(ctx, log)
  194. if log.Empty() {
  195. return nil, slerrors.NotFound("Log")
  196. }
  197. return log, nil
  198. }
  199. func (r *queryResolver) Logs(ctx context.Context, filter *models.LogFilter) ([]*models.Log, error) {
  200. user := r.Auth.UserFromContext(ctx)
  201. if user == nil {
  202. return nil, slerrors.PermissionDenied
  203. }
  204. if filter == nil {
  205. filter = &models.LogFilter{
  206. UserIDs: []string{user.ID},
  207. }
  208. }
  209. logs, err := r.Database.Logs().List(ctx, *filter)
  210. if err != nil {
  211. return nil, err
  212. }
  213. r.Auth.FilterLogList(ctx, &logs)
  214. return logs, nil
  215. }
  216. func (r *queryResolver) Session(ctx context.Context) (*models.User, error) {
  217. user := r.Auth.UserFromContext(ctx)
  218. if user == nil {
  219. return nil, errors.New("not logged in")
  220. }
  221. return user, nil
  222. }
  223. // Query returns graphcore.QueryResolver implementation.
  224. func (r *Resolver) Query() graphcore.QueryResolver { return &queryResolver{r} }
  225. type queryResolver struct{ *Resolver }