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.

151 lines
3.8 KiB

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