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.

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