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.

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