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.

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