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.

194 lines
5.6 KiB

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