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.

167 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. post, err := posts.FindID(id)
  16. if err != nil {
  17. return nil, err
  18. }
  19. return &post, nil
  20. }
  21. func (r *resolver) 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. return posts.List(filter)
  37. }
  38. // Mutation
  39. func (r *mutationResolver) AddPost(ctx context.Context, input input.PostAddInput) (*models.Post, error) {
  40. token := auth.TokenFromContext(ctx)
  41. if !token.Authenticated() || !token.Permitted("post.add") {
  42. return nil, errors.New("You are not permitted to edit logs")
  43. }
  44. log, err := logs.FindID(input.LogID)
  45. if err != nil {
  46. return nil, err
  47. }
  48. post, err := posts.Add(log, input.Time, input.Kind, input.Nick, input.Text)
  49. if err != nil {
  50. return nil, err
  51. }
  52. go logs.UpdateCharacters(log, nil)
  53. go changes.Submit("Post", "add", token.UserID, true, changekeys.Many(log, post), post)
  54. return &post, nil
  55. }
  56. func (r *mutationResolver) EditPost(ctx context.Context, input input.PostEditInput) (*models.Post, error) {
  57. token := auth.TokenFromContext(ctx)
  58. if !token.Authenticated() || !token.Permitted("post.edit") {
  59. return nil, errors.New("You are not permitted to edit logs")
  60. }
  61. post, err := posts.FindID(input.ID)
  62. if err != nil {
  63. return nil, errors.New("Post not found")
  64. }
  65. if input.Nick != nil {
  66. go func() {
  67. log, err := logs.FindID(post.LogID)
  68. if err != nil {
  69. return
  70. }
  71. logs.UpdateCharacters(log, nil)
  72. }()
  73. }
  74. post, err = posts.Edit(post, input.Time, input.Kind, input.Nick, input.Text)
  75. if err != nil {
  76. return nil, errors.New("Adding post failed: " + err.Error())
  77. }
  78. go func() {
  79. log, err := logs.FindID(post.LogID)
  80. if err != nil {
  81. log = models.Log{ID: post.LogID}
  82. }
  83. changes.Submit("Post", "edit", token.UserID, true, changekeys.Many(log, post), post)
  84. }()
  85. return &post, nil
  86. }
  87. func (r *mutationResolver) MovePost(ctx context.Context, input input.PostMoveInput) ([]models.Post, error) {
  88. token := auth.TokenFromContext(ctx)
  89. if !token.Authenticated() || !token.Permitted("post.move") {
  90. return nil, errors.New("You are not permitted to edit logs")
  91. }
  92. post, err := posts.FindID(input.ID)
  93. if err != nil {
  94. return nil, errors.New("Post not found")
  95. }
  96. posts, err := posts.Move(post, input.ToPosition)
  97. if err != nil {
  98. return nil, errors.New("Moving posts failed: " + err.Error())
  99. }
  100. go func() {
  101. log, err := logs.FindID(post.LogID)
  102. if err != nil {
  103. log = models.Log{ID: post.LogID}
  104. }
  105. changes.Submit("Post", "move", token.UserID, true, changekeys.Many(log, posts), posts)
  106. }()
  107. return posts, nil
  108. }
  109. func (r *mutationResolver) RemovePost(ctx context.Context, input input.PostRemoveInput) (*models.Post, error) {
  110. token := auth.TokenFromContext(ctx)
  111. if !token.Authenticated() || !token.Permitted("post.remove") {
  112. return nil, errors.New("You are not permitted to edit logs")
  113. }
  114. post, err := posts.FindID(input.ID)
  115. if err != nil {
  116. return nil, errors.New("Post not found (before removing, of course)")
  117. }
  118. post, err = posts.Remove(post)
  119. if err != nil {
  120. return nil, errors.New("Could not remove post: " + err.Error())
  121. }
  122. go func() {
  123. log, err := logs.FindID(post.LogID)
  124. if err != nil {
  125. return
  126. }
  127. logs.UpdateCharacters(log, nil)
  128. changes.Submit("Post", "remove", token.UserID, true, changekeys.Many(log, post), post)
  129. }()
  130. return &post, nil
  131. }