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.

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