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.

125 lines
3.1 KiB

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