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.

248 lines
6.0 KiB

  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/models"
  6. "git.aiterp.net/rpdata/api/models/changekeys"
  7. "git.aiterp.net/rpdata/api/repositories"
  8. "git.aiterp.net/rpdata/api/services/loaders"
  9. "log"
  10. "sort"
  11. "strings"
  12. "sync/atomic"
  13. "time"
  14. )
  15. type CharacterService struct {
  16. characters repositories.CharacterRepository
  17. loader *loaders.CharacterLoader
  18. changeService *ChangeService
  19. authService *AuthService
  20. logService *LogService
  21. refreshSoftLock uint32
  22. }
  23. // Find uses the loader to find the character by the ID.
  24. func (s *CharacterService) Find(ctx context.Context, id string) (*models.Character, error) {
  25. return s.loader.Load(id)
  26. }
  27. // Find uses the loader to find the character by the ID.
  28. func (s *CharacterService) FindNick(ctx context.Context, nick string) (*models.Character, error) {
  29. return s.characters.FindNick(ctx, nick)
  30. }
  31. // List lists the characters. If the only filter active is `IDs`, the loader is used to batch together requests.
  32. func (s *CharacterService) List(ctx context.Context, filter models.CharacterFilter) ([]*models.Character, error) {
  33. if len(filter.IDs) > 0 && len(filter.Names) == 0 && len(filter.Nicks) == 0 && filter.Author == nil && filter.Search == nil {
  34. characters, errs := s.loader.LoadAll(filter.IDs)
  35. if len(characters) == 0 && len(errs) > 0 {
  36. if errs[0] == repositories.ErrNotFound {
  37. return []*models.Character{}, nil
  38. } else {
  39. return nil, errs[0]
  40. }
  41. }
  42. if err := ctx.Err(); err != nil {
  43. return nil, err
  44. }
  45. var badIndices []int
  46. for i, character := range characters {
  47. if character == nil {
  48. badIndices = append(badIndices, i-len(badIndices))
  49. }
  50. }
  51. for _, index := range badIndices {
  52. characters = append(characters[:index], characters[index+1:]...)
  53. }
  54. sort.Slice(characters, func(i, j int) bool {
  55. if len(characters[i].ID) > len(characters[j].ID) {
  56. return true
  57. }
  58. if len(characters[i].ID) < len(characters[j].ID) {
  59. return false
  60. }
  61. return strings.Compare(characters[i].ID, characters[j].ID) < 0
  62. })
  63. return characters, nil
  64. }
  65. go s.refreshLogs()
  66. return s.characters.List(ctx, filter)
  67. }
  68. func (s *CharacterService) Create(ctx context.Context, nick, name, shortName, author, description string) (*models.Character, error) {
  69. token := s.authService.TokenFromContext(ctx)
  70. if token == nil {
  71. return nil, ErrUnauthenticated
  72. }
  73. if name == "" {
  74. return nil, errors.New("name cannot be empty")
  75. }
  76. // Insert nick into existing if character already exists.
  77. if character, err := s.characters.FindName(ctx, name); err == nil && character.Name == name {
  78. return s.AddNick(ctx, character.ID, nick)
  79. }
  80. if author == "" {
  81. author = token.UserID
  82. }
  83. if shortName == "" {
  84. split := strings.SplitN(name, " ", 2)
  85. shortName = split[0]
  86. }
  87. character := &models.Character{
  88. Name: name,
  89. ShortName: shortName,
  90. Author: author,
  91. Nicks: []string{nick},
  92. Description: description,
  93. }
  94. err := s.authService.CheckPermission(ctx, "add", character)
  95. if err != nil {
  96. return nil, err
  97. }
  98. character, err = s.characters.Insert(ctx, *character)
  99. if err != nil {
  100. return nil, err
  101. }
  102. s.changeService.Submit(ctx, "Character", "add", true, changekeys.Listed(character), character)
  103. go s.refreshLogs()
  104. return character, nil
  105. }
  106. func (s *CharacterService) Update(ctx context.Context, id string, name, shortName, description *string) (*models.Character, error) {
  107. character, err := s.characters.Find(ctx, id)
  108. if err != nil {
  109. return nil, err
  110. }
  111. err = s.authService.CheckPermission(ctx, "edit", character)
  112. if err != nil {
  113. return nil, err
  114. }
  115. character, err = s.characters.Update(ctx, *character, models.CharacterUpdate{
  116. Name: name,
  117. ShortName: shortName,
  118. Description: description,
  119. })
  120. if err != nil {
  121. return nil, err
  122. }
  123. s.loader.Clear(character.ID)
  124. s.loader.Prime(character.ID, character)
  125. s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
  126. return character, nil
  127. }
  128. func (s *CharacterService) AddNick(ctx context.Context, id string, nick string) (*models.Character, error) {
  129. character, err := s.characters.Find(ctx, id)
  130. if err != nil {
  131. return nil, err
  132. }
  133. err = s.authService.CheckPermission(ctx, "edit", character)
  134. if err != nil {
  135. return nil, err
  136. }
  137. character, err = s.characters.AddNick(ctx, *character, nick)
  138. if err != nil {
  139. return nil, err
  140. }
  141. s.loader.Clear(character.ID)
  142. s.loader.Prime(character.ID, character)
  143. s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
  144. go s.refreshLogs()
  145. return character, nil
  146. }
  147. func (s *CharacterService) RemoveNick(ctx context.Context, id string, nick string) (*models.Character, error) {
  148. character, err := s.characters.Find(ctx, id)
  149. if err != nil {
  150. return nil, err
  151. }
  152. err = s.authService.CheckPermission(ctx, "edit", character)
  153. if err != nil {
  154. return nil, err
  155. }
  156. character, err = s.characters.RemoveNick(ctx, *character, nick)
  157. if err != nil {
  158. return nil, err
  159. }
  160. s.loader.Clear(character.ID)
  161. s.loader.Prime(character.ID, character)
  162. s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
  163. go s.refreshLogs()
  164. return character, nil
  165. }
  166. func (s *CharacterService) Delete(ctx context.Context, id string) (*models.Character, error) {
  167. character, err := s.characters.Find(ctx, id)
  168. if err != nil {
  169. return nil, err
  170. }
  171. err = s.authService.CheckPermission(ctx, "edit", character)
  172. if err != nil {
  173. return nil, err
  174. }
  175. err = s.characters.Delete(ctx, *character)
  176. if err != nil {
  177. return nil, err
  178. }
  179. s.loader.Clear(character.ID)
  180. s.changeService.Submit(ctx, "Character", "remove", true, changekeys.Listed(character), character)
  181. go s.refreshLogs()
  182. return character, nil
  183. }
  184. func (s *CharacterService) refreshLogs() {
  185. if !atomic.CompareAndSwapUint32(&s.refreshSoftLock, 0, 1) {
  186. return
  187. }
  188. time.Sleep(time.Second * 60)
  189. atomic.StoreUint32(&s.refreshSoftLock, 0)
  190. err := s.logService.RefreshAllLogCharacters(context.Background())
  191. if err != nil {
  192. log.Println("Failed to refersh log characters:", err)
  193. return
  194. }
  195. }