2022-08-13 14:35:33 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2024-04-13 02:08:35 +08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2024-04-13 02:57:32 +08:00
|
|
|
|
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
2022-08-13 14:35:33 +08:00
|
|
|
)
|
|
|
|
|
2024-04-13 02:08:35 +08:00
|
|
|
type UserSetting struct {
|
|
|
|
UserID int32
|
|
|
|
Key storepb.UserSettingKey
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
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) {
|
2024-04-13 02:08:35 +08:00
|
|
|
userSettingRaw, err := convertUserSettingToRaw(upsert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSettingRaw, err = s.driver.UpsertUserSetting(ctx, userSettingRaw)
|
2023-09-26 19:02:48 +08:00
|
|
|
if err != nil {
|
2023-09-14 20:16:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-13 02:08:35 +08:00
|
|
|
userSetting, err := convertUserSettingFromRaw(userSettingRaw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.userSettingCache.Store(getUserSettingV1CacheKey(userSetting.UserId, userSetting.Key.String()), userSetting)
|
|
|
|
return userSetting, nil
|
2023-09-14 20:16:17 +08:00
|
|
|
}
|
|
|
|
|
2023-12-16 12:18:53 +08:00
|
|
|
func (s *Store) ListUserSettings(ctx context.Context, find *FindUserSetting) ([]*storepb.UserSetting, error) {
|
2024-04-13 02:08:35 +08:00
|
|
|
userSettingRawList, err := s.driver.ListUserSettings(ctx, find)
|
2023-09-14 20:16:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-13 02:08:35 +08:00
|
|
|
userSettings := []*storepb.UserSetting{}
|
|
|
|
for _, userSettingRaw := range userSettingRawList {
|
|
|
|
userSetting, err := convertUserSettingFromRaw(userSettingRaw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-15 08:18:30 +08:00
|
|
|
s.userSettingCache.Store(getUserSettingV1CacheKey(userSetting.UserId, userSetting.Key.String()), userSetting)
|
2024-04-13 02:08:35 +08:00
|
|
|
userSettings = append(userSettings, userSetting)
|
2023-09-14 20:16:17 +08:00
|
|
|
}
|
2024-04-13 02:08:35 +08:00
|
|
|
return userSettings, nil
|
2023-09-14 20:16:17 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2024-04-13 02:08:35 +08:00
|
|
|
if len(list) > 1 {
|
|
|
|
return nil, errors.Errorf("expected 1 user setting, but got %d", len(list))
|
|
|
|
}
|
2023-09-14 20:16:17 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-11-22 23:20:45 +08:00
|
|
|
|
|
|
|
// 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{
|
2023-11-22 23:20:45 +08:00
|
|
|
UserId: userID,
|
|
|
|
Key: storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS,
|
|
|
|
Value: &storepb.UserSetting_AccessTokens{
|
|
|
|
AccessTokens: &storepb.AccessTokensUserSetting{
|
|
|
|
AccessTokens: newAccessTokens,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2024-04-13 02:08:35 +08:00
|
|
|
|
|
|
|
func convertUserSettingFromRaw(raw *UserSetting) (*storepb.UserSetting, error) {
|
|
|
|
userSetting := &storepb.UserSetting{
|
|
|
|
UserId: raw.UserID,
|
|
|
|
Key: raw.Key,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch raw.Key {
|
|
|
|
case storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS:
|
|
|
|
accessTokensUserSetting := &storepb.AccessTokensUserSetting{}
|
|
|
|
if err := protojsonUnmarshaler.Unmarshal([]byte(raw.Value), accessTokensUserSetting); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSetting.Value = &storepb.UserSetting_AccessTokens{AccessTokens: accessTokensUserSetting}
|
|
|
|
case storepb.UserSettingKey_USER_SETTING_LOCALE:
|
|
|
|
userSetting.Value = &storepb.UserSetting_Locale{Locale: raw.Value}
|
|
|
|
case storepb.UserSettingKey_USER_SETTING_APPEARANCE:
|
|
|
|
userSetting.Value = &storepb.UserSetting_Appearance{Appearance: raw.Value}
|
|
|
|
case storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY:
|
|
|
|
userSetting.Value = &storepb.UserSetting_MemoVisibility{MemoVisibility: raw.Value}
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unsupported user setting key: %v", raw.Key)
|
|
|
|
}
|
|
|
|
return userSetting, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertUserSettingToRaw(userSetting *storepb.UserSetting) (*UserSetting, error) {
|
|
|
|
raw := &UserSetting{
|
|
|
|
UserID: userSetting.UserId,
|
|
|
|
Key: userSetting.Key,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch userSetting.Key {
|
|
|
|
case storepb.UserSettingKey_USER_SETTING_ACCESS_TOKENS:
|
|
|
|
accessTokensUserSetting := userSetting.GetAccessTokens()
|
|
|
|
value, err := protojson.Marshal(accessTokensUserSetting)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
raw.Value = string(value)
|
|
|
|
case storepb.UserSettingKey_USER_SETTING_LOCALE:
|
|
|
|
raw.Value = userSetting.GetLocale()
|
|
|
|
case storepb.UserSettingKey_USER_SETTING_APPEARANCE:
|
|
|
|
raw.Value = userSetting.GetAppearance()
|
|
|
|
case storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY:
|
|
|
|
raw.Value = userSetting.GetMemoVisibility()
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unsupported user setting key: %v", userSetting.Key)
|
|
|
|
}
|
|
|
|
return raw, nil
|
|
|
|
}
|