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.

162 lines
4.1 KiB

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