user.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package store
  2. import (
  3. "context"
  4. )
  5. // Role is the type of a role.
  6. type Role string
  7. const (
  8. // RoleHost is the HOST role.
  9. RoleHost Role = "HOST"
  10. // RoleAdmin is the ADMIN role.
  11. RoleAdmin Role = "ADMIN"
  12. // RoleUser is the USER role.
  13. RoleUser Role = "USER"
  14. )
  15. func (e Role) String() string {
  16. switch e {
  17. case RoleHost:
  18. return "HOST"
  19. case RoleAdmin:
  20. return "ADMIN"
  21. case RoleUser:
  22. return "USER"
  23. }
  24. return "USER"
  25. }
  26. const (
  27. SystemBotID int32 = 0
  28. )
  29. var (
  30. SystemBot = &User{
  31. ID: SystemBotID,
  32. Username: "system_bot",
  33. Role: RoleAdmin,
  34. Email: "",
  35. Nickname: "Bot",
  36. }
  37. )
  38. type User struct {
  39. ID int32
  40. // Standard fields
  41. RowStatus RowStatus
  42. CreatedTs int64
  43. UpdatedTs int64
  44. // Domain specific fields
  45. Username string
  46. Role Role
  47. Email string
  48. Nickname string
  49. PasswordHash string
  50. AvatarURL string
  51. Description string
  52. }
  53. type UpdateUser struct {
  54. ID int32
  55. UpdatedTs *int64
  56. RowStatus *RowStatus
  57. Username *string
  58. Role *Role
  59. Email *string
  60. Nickname *string
  61. Password *string
  62. AvatarURL *string
  63. PasswordHash *string
  64. Description *string
  65. }
  66. type FindUser struct {
  67. ID *int32
  68. RowStatus *RowStatus
  69. Username *string
  70. Role *Role
  71. Email *string
  72. Nickname *string
  73. // Random and limit are used in list users.
  74. // Whether to return random users.
  75. Random bool
  76. // The maximum number of users to return.
  77. Limit *int
  78. }
  79. type DeleteUser struct {
  80. ID int32
  81. }
  82. func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) {
  83. user, err := s.driver.CreateUser(ctx, create)
  84. if err != nil {
  85. return nil, err
  86. }
  87. s.userCache.Store(user.ID, user)
  88. return user, nil
  89. }
  90. func (s *Store) UpdateUser(ctx context.Context, update *UpdateUser) (*User, error) {
  91. user, err := s.driver.UpdateUser(ctx, update)
  92. if err != nil {
  93. return nil, err
  94. }
  95. s.userCache.Store(user.ID, user)
  96. return user, nil
  97. }
  98. func (s *Store) ListUsers(ctx context.Context, find *FindUser) ([]*User, error) {
  99. list, err := s.driver.ListUsers(ctx, find)
  100. if err != nil {
  101. return nil, err
  102. }
  103. for _, user := range list {
  104. s.userCache.Store(user.ID, user)
  105. }
  106. return list, nil
  107. }
  108. func (s *Store) GetUser(ctx context.Context, find *FindUser) (*User, error) {
  109. if find.ID != nil {
  110. if *find.ID == SystemBotID {
  111. return SystemBot, nil
  112. }
  113. if cache, ok := s.userCache.Load(*find.ID); ok {
  114. user, ok := cache.(*User)
  115. if ok {
  116. return user, nil
  117. }
  118. }
  119. }
  120. list, err := s.ListUsers(ctx, find)
  121. if err != nil {
  122. return nil, err
  123. }
  124. if len(list) == 0 {
  125. return nil, nil
  126. }
  127. user := list[0]
  128. s.userCache.Store(user.ID, user)
  129. return user, nil
  130. }
  131. func (s *Store) DeleteUser(ctx context.Context, delete *DeleteUser) error {
  132. err := s.driver.DeleteUser(ctx, delete)
  133. if err != nil {
  134. return err
  135. }
  136. s.userCache.Delete(delete.ID)
  137. return nil
  138. }