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.

173 lines
5.3 KiB

  1. package queries
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "git.aiterp.net/rpdata/api/graph2/input"
  7. "git.aiterp.net/rpdata/api/internal/auth"
  8. "git.aiterp.net/rpdata/api/models"
  9. "git.aiterp.net/rpdata/api/models/changes"
  10. "git.aiterp.net/rpdata/api/models/characters"
  11. "git.aiterp.net/rpdata/api/models/logs"
  12. )
  13. // Queries
  14. func (r *resolver) Character(ctx context.Context, id *string, nick *string) (models.Character, error) {
  15. if id != nil {
  16. return characters.FindID(*id)
  17. } else if nick != nil {
  18. return characters.FindNick(*nick)
  19. } else {
  20. return models.Character{}, errors.New("You must specify either an ID or a nick")
  21. }
  22. }
  23. func (r *resolver) Characters(ctx context.Context, filter *characters.Filter) ([]models.Character, error) {
  24. return characters.List(filter)
  25. }
  26. // Mutations
  27. func (r *mutationResolver) AddCharacter(ctx context.Context, input input.CharacterAddInput) (models.Character, error) {
  28. token := auth.TokenFromContext(ctx)
  29. if !token.Permitted("member", "character.add") {
  30. return models.Character{}, errors.New("You are not permitted to add characters")
  31. }
  32. if len(input.Name) < 2 || len(input.Nick) < 2 {
  33. return models.Character{}, errors.New("You need to provide a name and a nick (min length: 2)")
  34. }
  35. shortName := ""
  36. if input.ShortName != nil {
  37. shortName = *input.ShortName
  38. } else {
  39. shortName = strings.SplitN(input.Name, " ", 2)[0]
  40. }
  41. description := ""
  42. if input.Description != nil {
  43. description = *input.Description
  44. }
  45. author := token.UserID
  46. if input.Author != nil && *input.Author != author {
  47. if !token.Permitted("character.add") {
  48. return models.Character{}, errors.New("You are only permitted to add your own characters")
  49. }
  50. author = *input.Author
  51. }
  52. logs.ScheduleFullUpdate()
  53. character, err := characters.Add(input.Nick, input.Name, shortName, author, description)
  54. if err != nil {
  55. return models.Character{}, errors.New("Adding character failed: " + err.Error())
  56. }
  57. go changes.Submit("Character", "add", token.UserID, character)
  58. return character, nil
  59. }
  60. func (r *mutationResolver) AddCharacterNick(ctx context.Context, input input.CharacterNickInput) (models.Character, error) {
  61. character, err := characters.FindID(input.ID)
  62. if err != nil {
  63. return models.Character{}, errors.New("Character not found")
  64. }
  65. if len(input.Nick) < 2 {
  66. return models.Character{}, errors.New("You need to provide a valid nick (min length: 2)")
  67. }
  68. token := auth.TokenFromContext(ctx)
  69. if !token.PermittedUser(character.Author, "member", "character.edit") {
  70. return models.Character{}, errors.New("You are not permitted to edit this character")
  71. }
  72. logs.ScheduleFullUpdate()
  73. character, err = characters.AddNick(character, input.Nick)
  74. if err != nil {
  75. return models.Character{}, errors.New("Failed to add nick: " + err.Error())
  76. }
  77. go logs.ScheduleFullUpdate()
  78. go changes.Submit("Character", "edit", token.UserID, character)
  79. return character, nil
  80. }
  81. func (r *mutationResolver) RemoveCharacterNick(ctx context.Context, input input.CharacterNickInput) (models.Character, error) {
  82. character, err := characters.FindID(input.ID)
  83. if err != nil {
  84. return models.Character{}, errors.New("Character not found")
  85. }
  86. token := auth.TokenFromContext(ctx)
  87. if !token.PermittedUser(character.Author, "member", "character.edit") {
  88. return models.Character{}, errors.New("You are not permitted to edit this character")
  89. }
  90. character, err = characters.RemoveNick(character, input.Nick)
  91. if err != nil {
  92. return models.Character{}, errors.New("Failed to remove nick: " + err.Error())
  93. }
  94. go logs.ScheduleFullUpdate()
  95. go changes.Submit("Character", "edit", token.UserID, character)
  96. return character, nil
  97. }
  98. func (r *mutationResolver) EditCharacter(ctx context.Context, input input.CharacterEditInput) (models.Character, error) {
  99. character, err := characters.FindID(input.ID)
  100. if err != nil {
  101. return models.Character{}, errors.New("Character not found")
  102. }
  103. if input.Name != nil && len(*input.Name) < 2 {
  104. return models.Character{}, errors.New("You need to provide a valid name (min length: 2)")
  105. }
  106. if input.ShortName != nil && len(*input.ShortName) < 2 {
  107. return models.Character{}, errors.New("You need to provide a valid short name (min length: 2)")
  108. }
  109. token := auth.TokenFromContext(ctx)
  110. if !token.PermittedUser(character.Author, "member", "character.edit") {
  111. return models.Character{}, errors.New("You are not permitted to edit this character")
  112. }
  113. character, err = characters.Edit(character, input.Name, input.ShortName, input.Description)
  114. if err != nil {
  115. return models.Character{}, errors.New("Failed to edit character: " + err.Error())
  116. }
  117. go changes.Submit("Character", "edit", token.UserID, character)
  118. return character, nil
  119. }
  120. func (r *mutationResolver) RemoveCharacter(ctx context.Context, input input.CharacterRemoveInput) (models.Character, error) {
  121. character, err := characters.FindID(input.ID)
  122. if err != nil {
  123. return models.Character{}, errors.New("Character not found")
  124. }
  125. token := auth.TokenFromContext(ctx)
  126. if !token.PermittedUser(character.Author, "member", "character.remove") {
  127. return models.Character{}, errors.New("You are not permitted to remove this character")
  128. }
  129. character, err = characters.Remove(character)
  130. if err != nil {
  131. return models.Character{}, errors.New("Failed to remove character: " + err.Error())
  132. }
  133. go changes.Submit("Character", "remove", token.UserID, character)
  134. return character, nil
  135. }