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.

241 lines
5.8 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. return strings.Compare(characters[i].ID, characters[j].ID) < 0
  56. })
  57. return characters, nil
  58. }
  59. go s.refreshLogs()
  60. return s.characters.List(ctx, filter)
  61. }
  62. func (s *CharacterService) Create(ctx context.Context, nick, name, shortName, author, description string) (*models.Character, error) {
  63. token := s.authService.TokenFromContext(ctx)
  64. if token == nil {
  65. return nil, ErrUnauthenticated
  66. }
  67. if name == "" {
  68. return nil, errors.New("name cannot be empty")
  69. }
  70. // Insert nick into existing if character already exists.
  71. if character, err := s.characters.FindName(ctx, name); err == nil && character.Name == name {
  72. return s.AddNick(ctx, character.ID, nick)
  73. }
  74. if author == "" {
  75. author = token.UserID
  76. }
  77. if shortName == "" {
  78. split := strings.SplitN(name, " ", 2)
  79. shortName = split[0]
  80. }
  81. character := &models.Character{
  82. Name: name,
  83. ShortName: shortName,
  84. Author: author,
  85. Nicks: []string{nick},
  86. Description: description,
  87. }
  88. err := s.authService.CheckPermission(ctx, "add", character)
  89. if err != nil {
  90. return nil, err
  91. }
  92. character, err = s.characters.Insert(ctx, *character)
  93. if err != nil {
  94. return nil, err
  95. }
  96. s.changeService.Submit(ctx, "Character", "add", true, changekeys.Listed(character), character)
  97. go s.refreshLogs()
  98. return character, nil
  99. }
  100. func (s *CharacterService) Update(ctx context.Context, id string, name, shortName, description *string) (*models.Character, error) {
  101. character, err := s.characters.Find(ctx, id)
  102. if err != nil {
  103. return nil, err
  104. }
  105. err = s.authService.CheckPermission(ctx, "edit", character)
  106. if err != nil {
  107. return nil, err
  108. }
  109. character, err = s.characters.Update(ctx, *character, models.CharacterUpdate{
  110. Name: name,
  111. ShortName: shortName,
  112. Description: description,
  113. })
  114. if err != nil {
  115. return nil, err
  116. }
  117. s.loader.Clear(character.ID)
  118. s.loader.Prime(character.ID, character)
  119. s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
  120. return character, nil
  121. }
  122. func (s *CharacterService) AddNick(ctx context.Context, id string, nick string) (*models.Character, error) {
  123. character, err := s.characters.Find(ctx, id)
  124. if err != nil {
  125. return nil, err
  126. }
  127. err = s.authService.CheckPermission(ctx, "edit", character)
  128. if err != nil {
  129. return nil, err
  130. }
  131. character, err = s.characters.AddNick(ctx, *character, nick)
  132. if err != nil {
  133. return nil, err
  134. }
  135. s.loader.Clear(character.ID)
  136. s.loader.Prime(character.ID, character)
  137. s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
  138. go s.refreshLogs()
  139. return character, nil
  140. }
  141. func (s *CharacterService) RemoveNick(ctx context.Context, id string, nick string) (*models.Character, error) {
  142. character, err := s.characters.Find(ctx, id)
  143. if err != nil {
  144. return nil, err
  145. }
  146. err = s.authService.CheckPermission(ctx, "edit", character)
  147. if err != nil {
  148. return nil, err
  149. }
  150. character, err = s.characters.RemoveNick(ctx, *character, nick)
  151. if err != nil {
  152. return nil, err
  153. }
  154. s.loader.Clear(character.ID)
  155. s.loader.Prime(character.ID, character)
  156. s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
  157. go s.refreshLogs()
  158. return character, nil
  159. }
  160. func (s *CharacterService) Delete(ctx context.Context, id string) (*models.Character, error) {
  161. character, err := s.characters.Find(ctx, id)
  162. if err != nil {
  163. return nil, err
  164. }
  165. err = s.authService.CheckPermission(ctx, "edit", character)
  166. if err != nil {
  167. return nil, err
  168. }
  169. err = s.characters.Delete(ctx, *character)
  170. if err != nil {
  171. return nil, err
  172. }
  173. s.loader.Clear(character.ID)
  174. s.changeService.Submit(ctx, "Character", "remove", true, changekeys.Listed(character), character)
  175. go s.refreshLogs()
  176. return character, nil
  177. }
  178. func (s *CharacterService) refreshLogs() {
  179. if !atomic.CompareAndSwapUint32(&s.refreshSoftLock, 0, 1) {
  180. return
  181. }
  182. time.Sleep(time.Second * 60)
  183. atomic.StoreUint32(&s.refreshSoftLock, 0)
  184. err := s.logService.RefreshAllLogCharacters(context.Background())
  185. if err != nil {
  186. log.Println("Failed to refersh log characters:", err)
  187. return
  188. }
  189. }