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.

146 lines
4.2 KiB

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