memos/store/user_setting.go

100 lines
2.8 KiB
Go
Raw Normal View History

package store
import (
"context"
2023-09-17 22:55:13 +08:00
storepb "github.com/usememos/memos/proto/gen/store"
)
2023-06-29 22:55:03 +08:00
type FindUserSetting struct {
2023-09-14 20:16:17 +08:00
UserID *int32
Key storepb.UserSettingKey
}
2023-12-16 12:18:53 +08:00
func (s *Store) UpsertUserSetting(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
userSettingMessage, err := s.driver.UpsertUserSetting(ctx, upsert)
if err != nil {
2023-09-14 20:16:17 +08:00
return nil, err
}
2023-09-15 08:18:30 +08:00
s.userSettingCache.Store(getUserSettingV1CacheKey(userSettingMessage.UserId, userSettingMessage.Key.String()), userSettingMessage)
2023-09-14 20:16:17 +08:00
return userSettingMessage, nil
}
2023-12-16 12:18:53 +08:00
func (s *Store) ListUserSettings(ctx context.Context, find *FindUserSetting) ([]*storepb.UserSetting, error) {
userSettingList, err := s.driver.ListUserSettings(ctx, find)
2023-09-14 20:16:17 +08:00
if err != nil {
return nil, err
}
for _, userSetting := range userSettingList {
2023-09-15 08:18:30 +08:00
s.userSettingCache.Store(getUserSettingV1CacheKey(userSetting.UserId, userSetting.Key.String()), userSetting)
2023-09-14 20:16:17 +08:00
}
return userSettingList, nil
}
2023-12-16 12:18:53 +08:00
func (s *Store) GetUserSetting(ctx context.Context, find *FindUserSetting) (*storepb.UserSetting, error) {
2023-09-14 20:16:17 +08:00
if find.UserID != nil {
2023-09-15 08:18:30 +08:00
if cache, ok := s.userSettingCache.Load(getUserSettingV1CacheKey(*find.UserID, find.Key.String())); ok {
2023-09-14 20:16:17 +08:00
return cache.(*storepb.UserSetting), nil
}
}
2023-12-16 12:18:53 +08:00
list, err := s.ListUserSettings(ctx, find)
2023-09-14 20:16:17 +08:00
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
userSetting := list[0]
2023-09-15 08:18:30 +08:00
s.userSettingCache.Store(getUserSettingV1CacheKey(userSetting.UserId, userSetting.Key.String()), userSetting)
2023-09-14 20:16:17 +08:00
return userSetting, nil
}
// GetUserAccessTokens returns the access tokens of the user.
func (s *Store) GetUserAccessTokens(ctx context.Context, userID int32) ([]*storepb.AccessTokensUserSetting_AccessToken, error) {
2023-12-16 12:18:53 +08:00
userSetting, err := s.GetUserSetting(ctx, &FindUserSetting{
2023-09-14 20:16:17 +08:00
UserID: &userID,
Key: storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS,
})
if err != nil {
return nil, err
}
if userSetting == nil {
return []*storepb.AccessTokensUserSetting_AccessToken{}, nil
}
accessTokensUserSetting := userSetting.GetAccessTokens()
return accessTokensUserSetting.AccessTokens, nil
}
// RemoveUserAccessToken remove the access token of the user.
func (s *Store) RemoveUserAccessToken(ctx context.Context, userID int32, token string) error {
oldAccessTokens, err := s.GetUserAccessTokens(ctx, userID)
if err != nil {
return err
}
newAccessTokens := make([]*storepb.AccessTokensUserSetting_AccessToken, 0, len(oldAccessTokens))
for _, t := range oldAccessTokens {
if token != t.AccessToken {
newAccessTokens = append(newAccessTokens, t)
}
}
2023-12-16 12:18:53 +08:00
_, err = s.UpsertUserSetting(ctx, &storepb.UserSetting{
UserId: userID,
Key: storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS,
Value: &storepb.UserSetting_AccessTokens{
AccessTokens: &storepb.AccessTokensUserSetting{
AccessTokens: newAccessTokens,
},
},
})
return err
}