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.

136 lines
3.8 KiB

  1. package queries
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  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/chapters"
  10. "git.aiterp.net/rpdata/api/models/stories"
  11. )
  12. func (r *resolver) Story(ctx context.Context, id string) (models.Story, error) {
  13. return stories.FindID(id)
  14. }
  15. func (r *resolver) Stories(ctx context.Context, filter *stories.Filter) ([]models.Story, error) {
  16. if filter != nil {
  17. if filter.Unlisted != nil && *filter.Unlisted == true {
  18. token := auth.TokenFromContext(ctx)
  19. if !token.Authenticated() {
  20. return nil, errors.New("You are not permitted to view unlisted stories")
  21. }
  22. if !token.Permitted("story.unlisted") {
  23. filter.Author = &token.UserID
  24. }
  25. }
  26. }
  27. return stories.List(filter)
  28. }
  29. // Mutations
  30. func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInput) (models.Story, error) {
  31. token := auth.TokenFromContext(ctx)
  32. if token == nil || !token.Permitted("member", "story.add") {
  33. return models.Story{}, errors.New("Permission denied")
  34. }
  35. author := token.UserID
  36. if input.Author != nil && *input.Author != author {
  37. if !token.Permitted("story.add") {
  38. return models.Story{}, errors.New("You are not permitted to add a story in another author's name")
  39. }
  40. author = *input.Author
  41. }
  42. fictionalDate := time.Time{}
  43. if input.FictionalDate != nil {
  44. fictionalDate = *input.FictionalDate
  45. }
  46. listed := input.Listed != nil && *input.Listed
  47. open := input.Open != nil && *input.Open
  48. return stories.Add(input.Name, author, input.Category, listed, open, input.Tags, time.Now(), fictionalDate)
  49. }
  50. func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTagAddInput) (models.Story, error) {
  51. token := auth.TokenFromContext(ctx)
  52. story, err := stories.FindID(input.ID)
  53. if err != nil {
  54. return models.Story{}, errors.New("Story not found")
  55. }
  56. if !token.PermittedUser(story.Author, "member", "story.edit") {
  57. return models.Story{}, errors.New("You are not permitted to edit this story")
  58. }
  59. return stories.AddTag(story, input.Tag)
  60. }
  61. func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.StoryTagRemoveInput) (models.Story, error) {
  62. token := auth.TokenFromContext(ctx)
  63. story, err := stories.FindID(input.ID)
  64. if err != nil {
  65. return models.Story{}, errors.New("Story not found")
  66. }
  67. if !token.PermittedUser(story.Author, "member", "story.edit") {
  68. return models.Story{}, errors.New("You are not permitted to edit this story")
  69. }
  70. return stories.RemoveTag(story, input.Tag)
  71. }
  72. func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditInput) (models.Story, error) {
  73. token := auth.TokenFromContext(ctx)
  74. story, err := stories.FindID(input.ID)
  75. if err != nil {
  76. return models.Story{}, errors.New("Story not found")
  77. }
  78. if !token.PermittedUser(story.Author, "member", "story.edit") {
  79. return models.Story{}, errors.New("You are not permitted to remove this story")
  80. }
  81. if input.ClearFictionalDate != nil && *input.ClearFictionalDate {
  82. input.FictionalDate = &time.Time{}
  83. }
  84. return stories.Edit(story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate)
  85. }
  86. func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRemoveInput) (models.Story, error) {
  87. token := auth.TokenFromContext(ctx)
  88. story, err := stories.FindID(input.ID)
  89. if err != nil {
  90. return models.Story{}, errors.New("Story not found")
  91. }
  92. if !token.PermittedUser(story.Author, "member", "story.remove") {
  93. return models.Story{}, errors.New("You are not permitted to remove this story")
  94. }
  95. story, err = stories.Remove(story)
  96. if err != nil {
  97. return models.Story{}, err
  98. }
  99. err = chapters.RemoveStory(story)
  100. if err != nil {
  101. return models.Story{}, errors.New("Failed to remove chapters, but story is removed: " + err.Error())
  102. }
  103. return story, nil
  104. }