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.

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