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
|
|
|
|
}
|
2024-05-11 21:14:21 +08:00
|
|
|
if userSetting == nil {
|
|
|
|
return nil, errors.New("unexpected nil user setting")
|
|
|
|
}
|
2024-04-17 08:56:52 +08:00
|
|
|
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserId, userSetting.Key.String()), userSetting)
|
2024-04-13 02:08:35 +08:00
|
|
|
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
|
|
|
|
}
|
2024-05-11 21:14:21 +08:00
|
|
|
if userSetting == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-17 08:56:52 +08:00
|
|
|
s.userSettingCache.Store(getUserSettingCacheKey(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 {
|
2024-04-17 08:56:52 +08:00
|
|
|
if cache, ok := s.userSettingCache.Load(getUserSettingCacheKey(*find.UserID, find.Key.String())); ok {
|
2024-05-12 13:19:31 +08:00
|
|
|
userSetting, ok := cache.(*storepb.UserSetting)
|
|
|
|
if ok {
|
|
|
|
return userSetting, nil
|
|
|
|
}
|
2023-09-14 20:16:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]
|
2024-04-17 08:56:52 +08:00
|
|
|
s.userSettingCache.Store(getUserSettingCacheKey(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,
|
2024-05-13 20:03:04 +08:00
|
|
|
Key: storepb.UserSettingKey_ACCESS_TOKENS,
|
2023-09-14 20:16:17 +08:00
|
|
|
})
|
|
|
|
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,
|
2024-05-13 20:03:04 +08:00
|
|
|
Key: storepb.UserSettingKey_ACCESS_TOKENS,
|
2023-11-22 23:20:45 +08:00
|
|
|
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 {
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.UserSettingKey_ACCESS_TOKENS:
|
2024-04-13 02:08:35 +08:00
|
|
|
accessTokensUserSetting := &storepb.AccessTokensUserSetting{}
|
|
|
|
if err := protojsonUnmarshaler.Unmarshal([]byte(raw.Value), accessTokensUserSetting); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSetting.Value = &storepb.UserSetting_AccessTokens{AccessTokens: accessTokensUserSetting}
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.UserSettingKey_LOCALE:
|
2024-04-13 02:08:35 +08:00
|
|
|
userSetting.Value = &storepb.UserSetting_Locale{Locale: raw.Value}
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.UserSettingKey_APPEARANCE:
|
2024-04-13 02:08:35 +08:00
|
|
|
userSetting.Value = &storepb.UserSetting_Appearance{Appearance: raw.Value}
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.UserSettingKey_MEMO_VISIBILITY:
|
2024-04-13 02:08:35 +08:00
|
|
|
userSetting.Value = &storepb.UserSetting_MemoVisibility{MemoVisibility: raw.Value}
|
|
|
|
default:
|
2024-05-11 21:14:21 +08:00
|
|
|
return nil, nil
|
2024-04-13 02:08:35 +08:00
|
|
|
}
|
|
|
|
return userSetting, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertUserSettingToRaw(userSetting *storepb.UserSetting) (*UserSetting, error) {
|
|
|
|
raw := &UserSetting{
|
|
|
|
UserID: userSetting.UserId,
|
|
|
|
Key: userSetting.Key,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch userSetting.Key {
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.UserSettingKey_ACCESS_TOKENS:
|
2024-04-13 02:08:35 +08:00
|
|
|
accessTokensUserSetting := userSetting.GetAccessTokens()
|
|
|
|
value, err := protojson.Marshal(accessTokensUserSetting)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
raw.Value = string(value)
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.UserSettingKey_LOCALE:
|
2024-04-13 02:08:35 +08:00
|
|
|
raw.Value = userSetting.GetLocale()
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.UserSettingKey_APPEARANCE:
|
2024-04-13 02:08:35 +08:00
|
|
|
raw.Value = userSetting.GetAppearance()
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.UserSettingKey_MEMO_VISIBILITY:
|
2024-04-13 02:08:35 +08:00
|
|
|
raw.Value = userSetting.GetMemoVisibility()
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unsupported user setting key: %v", userSetting.Key)
|
|
|
|
}
|
|
|
|
return raw, nil
|
|
|
|
}
|