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.

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