GraphQL API and utilities for the rpdata project
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.

181 lines
4.4 KiB

  1. package resolvers
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/graph2/graphcore"
  6. "git.aiterp.net/rpdata/api/internal/auth"
  7. "git.aiterp.net/rpdata/api/models"
  8. "git.aiterp.net/rpdata/api/models/changekeys"
  9. "git.aiterp.net/rpdata/api/models/changes"
  10. "git.aiterp.net/rpdata/api/models/logs"
  11. "git.aiterp.net/rpdata/api/models/posts"
  12. )
  13. // Queries
  14. func (r *queryResolver) Post(ctx context.Context, id string) (*models.Post, error) {
  15. post, err := posts.FindID(id)
  16. if err != nil {
  17. return nil, err
  18. }
  19. return &post, nil
  20. }
  21. func (r *queryResolver) Posts(ctx context.Context, filter *posts.Filter) ([]*models.Post, error) {
  22. // Some sanity checks to avoid querying an insame amount of posts.
  23. if filter == nil {
  24. filter = &posts.Filter{Limit: 256}
  25. } else {
  26. if (filter.Limit <= 0 || filter.Limit > 256) && filter.LogID == nil {
  27. return nil, errors.New("a limit of 0 (no limit) or >256 without a logId is not allowed")
  28. }
  29. if len(filter.Kind) > 32 {
  30. return nil, errors.New("You cannot specify more than 32 kinds")
  31. }
  32. if len(filter.ID) > 32 {
  33. return nil, errors.New("You cannot specify more than 32 IDs")
  34. }
  35. }
  36. posts, err := posts.List(filter)
  37. if err != nil {
  38. return nil, err
  39. }
  40. posts2 := make([]*models.Post, len(posts))
  41. for i := range posts {
  42. posts2[i] = &posts[i]
  43. }
  44. return posts2, nil
  45. }
  46. // Mutation
  47. func (r *mutationResolver) AddPost(ctx context.Context, input graphcore.PostAddInput) (*models.Post, error) {
  48. token := auth.TokenFromContext(ctx)
  49. if !token.Authenticated() || !token.Permitted("post.add") {
  50. return nil, errors.New("You are not permitted to edit logs")
  51. }
  52. log, err := logs.FindID(input.LogID)
  53. if err != nil {
  54. return nil, err
  55. }
  56. post, err := posts.Add(log, input.Time, input.Kind, input.Nick, input.Text)
  57. if err != nil {
  58. return nil, err
  59. }
  60. go logs.UpdateCharacters(log, nil)
  61. go changes.Submit("Post", "add", token.UserID, true, changekeys.Many(log, post), post)
  62. return &post, nil
  63. }
  64. func (r *mutationResolver) EditPost(ctx context.Context, input graphcore.PostEditInput) (*models.Post, error) {
  65. token := auth.TokenFromContext(ctx)
  66. if !token.Authenticated() || !token.Permitted("post.edit") {
  67. return nil, errors.New("You are not permitted to edit logs")
  68. }
  69. post, err := posts.FindID(input.ID)
  70. if err != nil {
  71. return nil, errors.New("Post not found")
  72. }
  73. if input.Nick != nil {
  74. go func() {
  75. log, err := logs.FindID(post.LogID)
  76. if err != nil {
  77. return
  78. }
  79. logs.UpdateCharacters(log, nil)
  80. }()
  81. }
  82. post, err = posts.Edit(post, input.Time, input.Kind, input.Nick, input.Text)
  83. if err != nil {
  84. return nil, errors.New("Adding post failed: " + err.Error())
  85. }
  86. go func() {
  87. log, err := logs.FindID(post.LogID)
  88. if err != nil {
  89. log = models.Log{ID: post.LogID}
  90. }
  91. changes.Submit("Post", "edit", token.UserID, true, changekeys.Many(log, post), post)
  92. }()
  93. return &post, nil
  94. }
  95. func (r *mutationResolver) MovePost(ctx context.Context, input graphcore.PostMoveInput) ([]*models.Post, error) {
  96. token := auth.TokenFromContext(ctx)
  97. if !token.Authenticated() || !token.Permitted("post.move") {
  98. return nil, errors.New("You are not permitted to edit logs")
  99. }
  100. post, err := posts.FindID(input.ID)
  101. if err != nil {
  102. return nil, errors.New("Post not found")
  103. }
  104. posts, err := posts.Move(post, input.ToPosition)
  105. if err != nil {
  106. return nil, errors.New("Moving posts failed: " + err.Error())
  107. }
  108. go func() {
  109. log, err := logs.FindID(post.LogID)
  110. if err != nil {
  111. log = models.Log{ID: post.LogID}
  112. }
  113. changes.Submit("Post", "move", token.UserID, true, changekeys.Many(log, posts), posts)
  114. }()
  115. posts2 := make([]*models.Post, len(posts))
  116. for i := range posts {
  117. posts2[i] = &posts[i]
  118. }
  119. return posts2, nil
  120. }
  121. func (r *mutationResolver) RemovePost(ctx context.Context, input graphcore.PostRemoveInput) (*models.Post, error) {
  122. token := auth.TokenFromContext(ctx)
  123. if !token.Authenticated() || !token.Permitted("post.remove") {
  124. return nil, errors.New("You are not permitted to edit logs")
  125. }
  126. post, err := posts.FindID(input.ID)
  127. if err != nil {
  128. return nil, errors.New("Post not found (before removing, of course)")
  129. }
  130. post, err = posts.Remove(post)
  131. if err != nil {
  132. return nil, errors.New("Could not remove post: " + err.Error())
  133. }
  134. go func() {
  135. log, err := logs.FindID(post.LogID)
  136. if err != nil {
  137. return
  138. }
  139. logs.UpdateCharacters(log, nil)
  140. changes.Submit("Post", "remove", token.UserID, true, changekeys.Many(log, post), post)
  141. }()
  142. return &post, nil
  143. }