chore: fix user setting store

This commit is contained in:
steven 2023-12-08 09:54:32 +08:00
parent 2964cf93ab
commit c68bfcc3b9
6 changed files with 89 additions and 11 deletions

View File

@ -187,6 +187,14 @@ func (s *APIV2Service) DeleteUser(ctx context.Context, request *apiv2pb.DeleteUs
return &apiv2pb.DeleteUserResponse{}, nil
}
func getDefaultUserSetting() *apiv2pb.UserSetting {
return &apiv2pb.UserSetting{
Locale: "en",
Appearance: "system",
MemoVisibility: "PRIVATE",
}
}
func (s *APIV2Service) GetUserSetting(ctx context.Context, _ *apiv2pb.GetUserSettingRequest) (*apiv2pb.GetUserSettingResponse, error) {
user, err := getCurrentUser(ctx, s.Store)
if err != nil {
@ -199,7 +207,7 @@ func (s *APIV2Service) GetUserSetting(ctx context.Context, _ *apiv2pb.GetUserSet
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list user settings: %v", err)
}
userSettingMessage := &apiv2pb.UserSetting{}
userSettingMessage := getDefaultUserSetting()
for _, setting := range userSettings {
if setting.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
userSettingMessage.Locale = setting.GetLocale()

View File

@ -67,8 +67,16 @@ func (d *DB) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSettin
return nil, err
}
valueString = string(valueBytes)
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
valueString = upsert.GetLocale()
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
valueString = upsert.GetAppearance()
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
valueString = upsert.GetMemoVisibility()
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
valueString = upsert.GetTelegramUserId()
} else {
return nil, errors.New("invalid user setting key")
return nil, errors.Errorf("unknown user setting key: %s", upsert.Key.String())
}
if _, err := d.db.ExecContext(ctx, stmt, upsert.UserId, upsert.Key.String(), valueString, valueString); err != nil {
@ -115,9 +123,24 @@ func (d *DB) ListUserSettingsV1(ctx context.Context, find *store.FindUserSetting
userSetting.Value = &storepb.UserSetting_AccessTokens{
AccessTokens: accessTokensUserSetting,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
userSetting.Value = &storepb.UserSetting_Locale{
Locale: valueString,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
userSetting.Value = &storepb.UserSetting_Appearance{
Appearance: valueString,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
userSetting.Value = &storepb.UserSetting_MemoVisibility{
MemoVisibility: valueString,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
userSetting.Value = &storepb.UserSetting_TelegramUserId{
TelegramUserId: valueString,
}
} else {
// Skip unknown user setting v1 key.
continue
return nil, errors.Errorf("unknown user setting key: %s", userSetting.Key.String())
}
userSettingList = append(userSettingList, userSetting)
}

View File

@ -88,8 +88,16 @@ func (d *DB) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSettin
return nil, err
}
valueString = string(valueBytes)
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
valueString = upsert.GetLocale()
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
valueString = upsert.GetAppearance()
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
valueString = upsert.GetMemoVisibility()
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
valueString = upsert.GetTelegramUserId()
} else {
return nil, errors.New("invalid user setting key")
return nil, errors.Errorf("unknown user setting key: %s", upsert.Key.String())
}
// Construct the query using Squirrel
@ -158,9 +166,24 @@ func (d *DB) ListUserSettingsV1(ctx context.Context, find *store.FindUserSetting
userSetting.Value = &storepb.UserSetting_AccessTokens{
AccessTokens: accessTokensUserSetting,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
userSetting.Value = &storepb.UserSetting_Locale{
Locale: valueString,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
userSetting.Value = &storepb.UserSetting_Appearance{
Appearance: valueString,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
userSetting.Value = &storepb.UserSetting_MemoVisibility{
MemoVisibility: valueString,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
userSetting.Value = &storepb.UserSetting_TelegramUserId{
TelegramUserId: valueString,
}
} else {
// Skip unknown user setting v1 key
continue
return nil, errors.Errorf("unknown user setting key: %s", userSetting.Key.String())
}
userSettingList = append(userSettingList, userSetting)
}

View File

@ -3,11 +3,11 @@ package sqlite
import (
"context"
"database/sql"
"errors"
"strings"
"google.golang.org/protobuf/encoding/protojson"
"github.com/pkg/errors"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
@ -87,8 +87,16 @@ func (d *DB) UpsertUserSettingV1(ctx context.Context, upsert *storepb.UserSettin
return nil, err
}
valueString = string(valueBytes)
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
valueString = upsert.GetLocale()
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
valueString = upsert.GetAppearance()
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
valueString = upsert.GetMemoVisibility()
} else if upsert.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
valueString = upsert.GetTelegramUserId()
} else {
return nil, errors.New("invalid user setting key")
return nil, errors.Errorf("unknown user setting key: %s", upsert.Key.String())
}
if _, err := d.db.ExecContext(ctx, stmt, upsert.UserId, upsert.Key.String(), valueString); err != nil {
@ -141,6 +149,22 @@ func (d *DB) ListUserSettingsV1(ctx context.Context, find *store.FindUserSetting
userSetting.Value = &storepb.UserSetting_AccessTokens{
AccessTokens: accessTokensUserSetting,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_LOCALE {
userSetting.Value = &storepb.UserSetting_Locale{
Locale: valueString,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_APPEARANCE {
userSetting.Value = &storepb.UserSetting_Appearance{
Appearance: valueString,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_MEMO_VISIBILITY {
userSetting.Value = &storepb.UserSetting_MemoVisibility{
MemoVisibility: valueString,
}
} else if userSetting.Key == storepb.UserSettingKey_USER_SETTING_TELEGRAM_USER_ID {
userSetting.Value = &storepb.UserSetting_TelegramUserId{
TelegramUserId: valueString,
}
} else {
// Skip unknown user setting v1 key.
continue

View File

@ -80,7 +80,7 @@ const MemoEditor = (props: Props) => {
}, []);
useEffect(() => {
let visibility = userSetting.memoVisibility || "PRIVATE";
let visibility = userSetting.memoVisibility;
if (systemStatus.disablePublicMemos && visibility === "PUBLIC") {
visibility = "PRIVATE";
}

View File

@ -85,7 +85,7 @@ const PreferencesSection = () => {
<span className="text-sm break-keep text-ellipsis overflow-hidden">{t("setting.preference-section.default-memo-visibility")}</span>
<Select
className="!min-w-fit"
value={setting.memoVisibility || "PRIVATE"}
value={setting.memoVisibility}
startDecorator={<VisibilityIcon visibility={setting.memoVisibility as Visibility} />}
onChange={(_, visibility) => {
if (visibility) {