2021-12-08 23:43:14 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2022-08-07 09:23:46 +08:00
|
|
|
"context"
|
2021-12-08 23:43:14 +08:00
|
|
|
)
|
|
|
|
|
2023-06-17 22:35:17 +08:00
|
|
|
// Role is the type of a role.
|
|
|
|
type Role string
|
|
|
|
|
|
|
|
const (
|
2023-07-02 18:56:25 +08:00
|
|
|
// RoleHost is the HOST role.
|
|
|
|
RoleHost Role = "HOST"
|
|
|
|
// RoleAdmin is the ADMIN role.
|
|
|
|
RoleAdmin Role = "ADMIN"
|
|
|
|
// RoleUser is the USER role.
|
|
|
|
RoleUser Role = "USER"
|
2023-06-17 22:35:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (e Role) String() string {
|
|
|
|
switch e {
|
2023-07-02 18:56:25 +08:00
|
|
|
case RoleHost:
|
2023-06-17 22:35:17 +08:00
|
|
|
return "HOST"
|
2023-07-02 18:56:25 +08:00
|
|
|
case RoleAdmin:
|
2023-06-17 22:35:17 +08:00
|
|
|
return "ADMIN"
|
2023-07-02 18:56:25 +08:00
|
|
|
case RoleUser:
|
2023-06-17 22:35:17 +08:00
|
|
|
return "USER"
|
|
|
|
}
|
|
|
|
return "USER"
|
|
|
|
}
|
|
|
|
|
2023-11-06 20:49:02 +08:00
|
|
|
const (
|
|
|
|
SystemBotID int32 = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
SystemBot = &User{
|
|
|
|
ID: SystemBotID,
|
|
|
|
Username: "system_bot",
|
|
|
|
Role: RoleAdmin,
|
|
|
|
Email: "",
|
|
|
|
Nickname: "Bot",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-07-02 14:27:23 +08:00
|
|
|
type User struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID int32
|
2023-06-17 22:35:17 +08:00
|
|
|
|
|
|
|
// Standard fields
|
|
|
|
RowStatus RowStatus
|
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs int64
|
|
|
|
|
|
|
|
// Domain specific fields
|
|
|
|
Username string
|
|
|
|
Role Role
|
|
|
|
Email string
|
|
|
|
Nickname string
|
|
|
|
PasswordHash string
|
|
|
|
AvatarURL string
|
2024-03-13 21:24:16 +08:00
|
|
|
Description string
|
2023-06-17 22:35:17 +08:00
|
|
|
}
|
|
|
|
|
2023-07-02 14:27:23 +08:00
|
|
|
type UpdateUser struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID int32
|
2023-07-02 14:27:23 +08:00
|
|
|
|
|
|
|
UpdatedTs *int64
|
|
|
|
RowStatus *RowStatus
|
2023-09-10 18:56:24 +08:00
|
|
|
Username *string
|
|
|
|
Role *Role
|
|
|
|
Email *string
|
|
|
|
Nickname *string
|
|
|
|
Password *string
|
|
|
|
AvatarURL *string
|
2023-07-02 14:27:23 +08:00
|
|
|
PasswordHash *string
|
2024-03-13 21:24:16 +08:00
|
|
|
Description *string
|
2023-07-02 14:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type FindUser struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID *int32
|
2023-06-17 22:35:17 +08:00
|
|
|
RowStatus *RowStatus
|
2023-07-06 22:53:38 +08:00
|
|
|
Username *string
|
|
|
|
Role *Role
|
|
|
|
Email *string
|
|
|
|
Nickname *string
|
2024-03-29 00:01:45 +08:00
|
|
|
|
|
|
|
// Random and limit are used in list users.
|
|
|
|
// Whether to return random users.
|
|
|
|
Random bool
|
|
|
|
// The maximum number of users to return.
|
|
|
|
Limit *int
|
2023-06-17 22:35:17 +08:00
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
type DeleteUser struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID int32
|
2023-07-02 18:56:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) {
|
2023-09-26 18:23:45 +08:00
|
|
|
user, err := s.driver.CreateUser(ctx, create)
|
|
|
|
if err != nil {
|
2023-07-02 14:27:23 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-06 21:56:42 +08:00
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
s.userCache.Store(user.ID, user)
|
2023-07-02 14:27:23 +08:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) UpdateUser(ctx context.Context, update *UpdateUser) (*User, error) {
|
2023-09-26 18:23:45 +08:00
|
|
|
user, err := s.driver.UpdateUser(ctx, update)
|
|
|
|
if err != nil {
|
2023-07-02 14:27:23 +08:00
|
|
|
return nil, err
|
2023-06-17 22:35:17 +08:00
|
|
|
}
|
2023-07-02 14:27:23 +08:00
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
s.userCache.Store(user.ID, user)
|
2023-07-02 14:27:23 +08:00
|
|
|
return user, nil
|
2023-06-17 22:35:17 +08:00
|
|
|
}
|
|
|
|
|
2023-07-02 14:27:23 +08:00
|
|
|
func (s *Store) ListUsers(ctx context.Context, find *FindUser) ([]*User, error) {
|
2023-09-26 18:23:45 +08:00
|
|
|
list, err := s.driver.ListUsers(ctx, find)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
2023-07-02 18:56:25 +08:00
|
|
|
return nil, err
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-09-03 18:54:22 +08:00
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
for _, user := range list {
|
|
|
|
s.userCache.Store(user.ID, user)
|
|
|
|
}
|
2023-07-02 18:56:25 +08:00
|
|
|
return list, nil
|
2022-07-26 21:41:20 +08:00
|
|
|
}
|
2023-07-20 23:15:56 +08:00
|
|
|
|
|
|
|
func (s *Store) GetUser(ctx context.Context, find *FindUser) (*User, error) {
|
|
|
|
if find.ID != nil {
|
2023-11-06 20:49:02 +08:00
|
|
|
if *find.ID == SystemBotID {
|
|
|
|
return SystemBot, nil
|
|
|
|
}
|
2023-07-20 23:15:56 +08:00
|
|
|
if cache, ok := s.userCache.Load(*find.ID); ok {
|
2024-05-12 13:19:31 +08:00
|
|
|
user, ok := cache.(*User)
|
|
|
|
if ok {
|
|
|
|
return user, nil
|
|
|
|
}
|
2023-07-20 23:15:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := s.ListUsers(ctx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
user := list[0]
|
|
|
|
s.userCache.Store(user.ID, user)
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) DeleteUser(ctx context.Context, delete *DeleteUser) error {
|
2023-09-26 18:23:45 +08:00
|
|
|
err := s.driver.DeleteUser(ctx, delete)
|
2023-07-20 23:15:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-26 18:23:45 +08:00
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
s.userCache.Delete(delete.ID)
|
|
|
|
return nil
|
|
|
|
}
|