mirror of
https://github.com/usememos/memos.git
synced 2025-04-03 04:11:20 +02:00
chore: fix user setting store
This commit is contained in:
parent
2964cf93ab
commit
c68bfcc3b9
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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";
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user