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.

169 lines
4.9 KiB

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