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.

180 lines
4.8 KiB

  1. package queries
  2. import (
  3. "context"
  4. "errors"
  5. "log"
  6. "time"
  7. "git.aiterp.net/rpdata/api/models/stories"
  8. "git.aiterp.net/rpdata/api/models/characters"
  9. "git.aiterp.net/rpdata/api/models/changekeys"
  10. "git.aiterp.net/rpdata/api/models/changes"
  11. "git.aiterp.net/rpdata/api/graph2/input"
  12. "git.aiterp.net/rpdata/api/internal/auth"
  13. "git.aiterp.net/rpdata/api/models/chapters"
  14. "git.aiterp.net/rpdata/api/models/comments"
  15. "git.aiterp.net/rpdata/api/models"
  16. )
  17. // Queries
  18. func (r *resolver) Comment(ctx context.Context, id string) (*models.Comment, error) {
  19. comment, err := comments.Find(id)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return &comment, nil
  24. }
  25. // Mutations
  26. func (r *mutationResolver) AddComment(ctx context.Context, input input.CommentAddInput) (*models.Comment, error) {
  27. chapter, err := chapters.FindID(input.ChapterID)
  28. if err != nil {
  29. return nil, errors.New("Chapter not found")
  30. }
  31. token := auth.TokenFromContext(ctx)
  32. if !token.Permitted("member", "story.edit") {
  33. return nil, errors.New("Unauthorized")
  34. }
  35. if !chapter.CanComment() {
  36. return nil, errors.New("Comments are disabled or locked")
  37. }
  38. var characterPtr *models.Character
  39. if input.CharacterID != nil {
  40. character, err := characters.FindID(*input.CharacterID)
  41. if err != nil {
  42. return nil, errors.New("Character not found")
  43. } else if character.Author != token.UserID {
  44. return nil, errors.New("That is not your character")
  45. }
  46. characterPtr = &character
  47. }
  48. fictionalDate := time.Time{}
  49. if input.FictionalDate != nil {
  50. fictionalDate = *input.FictionalDate
  51. }
  52. subject := ""
  53. if input.Subject != nil {
  54. subject = *input.Subject
  55. }
  56. comment, err := comments.Add(chapter, subject, token.UserID, input.Source, input.CharacterName, characterPtr, time.Now(), fictionalDate)
  57. if err != nil {
  58. return nil, errors.New("Failed to add comment: " + err.Error())
  59. }
  60. go func() {
  61. story, err := stories.FindID(chapter.StoryID)
  62. if err != nil {
  63. log.Println("WARNING: Couldn't log comment change:", err)
  64. return
  65. }
  66. changes.Submit("Comment", "add", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
  67. }()
  68. return &comment, nil
  69. }
  70. func (r *mutationResolver) EditComment(ctx context.Context, input input.CommentEditInput) (*models.Comment, error) {
  71. comment, err := comments.Find(input.CommentID)
  72. if err != nil {
  73. return nil, errors.New("Comment not found")
  74. }
  75. token := auth.TokenFromContext(ctx)
  76. if !token.PermittedUser(comment.Author, "member", "story.edit") {
  77. return nil, errors.New("You cannot edit this comment")
  78. }
  79. chapter, err := chapters.FindID(comment.ChapterID)
  80. if err != nil {
  81. return nil, errors.New("Comment's chapter not found")
  82. }
  83. if !chapter.CanComment() {
  84. return nil, errors.New("Comments are disabled or locked")
  85. }
  86. if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true {
  87. input.FictionalDate = &time.Time{}
  88. }
  89. if input.CharacterID != nil && *input.CharacterID != "" {
  90. character, err := characters.FindID(*input.CharacterID)
  91. if err != nil {
  92. return nil, errors.New("Character not found")
  93. } else if character.Author != token.UserID {
  94. return nil, errors.New("That is not your character")
  95. }
  96. }
  97. comment, err = comments.Edit(comment, input.Source, input.CharacterName, input.CharacterID, input.Subject, input.FictionalDate)
  98. if err != nil {
  99. return nil, errors.New("Could not post comment: " + err.Error())
  100. }
  101. go func() {
  102. story, err := stories.FindID(chapter.StoryID)
  103. if err != nil {
  104. log.Println("WARNING: Couldn't log comment change:", err)
  105. return
  106. }
  107. changes.Submit("Comment", "edit", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
  108. }()
  109. return &comment, nil
  110. }
  111. func (r *mutationResolver) RemoveComment(ctx context.Context, input input.CommentRemoveInput) (*models.Comment, error) {
  112. comment, err := comments.Find(input.CommentID)
  113. if err != nil {
  114. return nil, errors.New("Comment not found")
  115. }
  116. token := auth.TokenFromContext(ctx)
  117. if !token.PermittedUser(comment.Author, "member", "story.edit") {
  118. return nil, errors.New("You cannot remove this comment")
  119. }
  120. chapter, err := chapters.FindID(comment.ChapterID)
  121. if err != nil {
  122. return nil, errors.New("Comment's chapter not found")
  123. }
  124. if !chapter.CanComment() {
  125. return nil, errors.New("Comments are disabled or locked")
  126. }
  127. err = comments.Remove(comment)
  128. if err != nil {
  129. return nil, errors.New("Failed to remove comment: " + err.Error())
  130. }
  131. go func() {
  132. story, err := stories.FindID(chapter.StoryID)
  133. if err != nil {
  134. log.Println("WARNING: Couldn't log comment change:", err)
  135. return
  136. }
  137. changes.Submit("Comment", "remove", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
  138. }()
  139. return &comment, nil
  140. }