2022-08-13 09:35:33 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-17 17:55:13 +03:00
|
|
|
|
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
2022-08-13 09:35:33 +03:00
|
|
|
)
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
type UserSetting struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
UserID int32
|
2023-06-26 18:06:53 +03:00
|
|
|
Key string
|
2022-08-13 09:35:33 +03:00
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
type FindUserSetting struct {
|
2023-08-04 16:55:07 +03:00
|
|
|
UserID *int32
|
2023-06-26 18:06:53 +03:00
|
|
|
Key string
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
func (s *Store) UpsertUserSetting(ctx context.Context, upsert *UserSetting) (*UserSetting, error) {
|
2023-09-26 14:02:48 +03:00
|
|
|
userSetting, err := s.driver.UpsertUserSetting(ctx, upsert)
|
|
|
|
if err != nil {
|
2022-08-13 09:35:33 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserID, userSetting.Key), userSetting)
|
2023-06-29 17:55:03 +03:00
|
|
|
return userSetting, nil
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:55:03 +03:00
|
|
|
func (s *Store) ListUserSettings(ctx context.Context, find *FindUserSetting) ([]*UserSetting, error) {
|
2023-09-26 14:02:48 +03:00
|
|
|
userSettingList, err := s.driver.ListUserSettings(ctx, find)
|
2022-08-13 09:35:33 +03:00
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return nil, err
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
for _, userSetting := range userSettingList {
|
|
|
|
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserID, userSetting.Key), userSetting)
|
|
|
|
}
|
2023-06-29 17:55:03 +03:00
|
|
|
return userSettingList, nil
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
2022-11-06 07:21:58 +03:00
|
|
|
|
2023-07-20 18:15:56 +03:00
|
|
|
func (s *Store) GetUserSetting(ctx context.Context, find *FindUserSetting) (*UserSetting, error) {
|
|
|
|
if find.UserID != nil {
|
|
|
|
if cache, ok := s.userSettingCache.Load(getUserSettingCacheKey(*find.UserID, find.Key)); ok {
|
|
|
|
return cache.(*UserSetting), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := s.ListUserSettings(ctx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
userSetting := list[0]
|
|
|
|
s.userSettingCache.Store(getUserSettingCacheKey(userSetting.UserID, userSetting.Key), userSetting)
|
|
|
|
return userSetting, nil
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:16:17 +03:00
|
|
|
type FindUserSettingV1 struct {
|
|
|
|
UserID *int32
|
|
|
|
Key storepb.UserSettingKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSetting) (*storepb.UserSetting, error) {
|
2023-09-26 14:02:48 +03:00
|
|
|
userSettingMessage, err := s.driver.UpsertUserSettingV1(ctx, upsert)
|
|
|
|
if err != nil {
|
2023-09-14 15:16:17 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-15 03:18:30 +03:00
|
|
|
s.userSettingCache.Store(getUserSettingV1CacheKey(userSettingMessage.UserId, userSettingMessage.Key.String()), userSettingMessage)
|
2023-09-14 15:16:17 +03:00
|
|
|
return userSettingMessage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ListUserSettingsV1(ctx context.Context, find *FindUserSettingV1) ([]*storepb.UserSetting, error) {
|
2023-09-26 14:02:48 +03:00
|
|
|
userSettingList, err := s.driver.ListUserSettingsV1(ctx, find)
|
2023-09-14 15:16:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, userSetting := range userSettingList {
|
2023-09-15 03:18:30 +03:00
|
|
|
s.userSettingCache.Store(getUserSettingV1CacheKey(userSetting.UserId, userSetting.Key.String()), userSetting)
|
2023-09-14 15:16:17 +03:00
|
|
|
}
|
|
|
|
return userSettingList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) GetUserSettingV1(ctx context.Context, find *FindUserSettingV1) (*storepb.UserSetting, error) {
|
|
|
|
if find.UserID != nil {
|
2023-09-15 03:18:30 +03:00
|
|
|
if cache, ok := s.userSettingCache.Load(getUserSettingV1CacheKey(*find.UserID, find.Key.String())); ok {
|
2023-09-14 15:16:17 +03:00
|
|
|
return cache.(*storepb.UserSetting), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := s.ListUserSettingsV1(ctx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
userSetting := list[0]
|
2023-09-15 03:18:30 +03:00
|
|
|
s.userSettingCache.Store(getUserSettingV1CacheKey(userSetting.UserId, userSetting.Key.String()), userSetting)
|
2023-09-14 15:16:17 +03: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) {
|
|
|
|
userSetting, err := s.GetUserSettingV1(ctx, &FindUserSettingV1{
|
|
|
|
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
|
|
|
|
}
|