mirror of
https://github.com/usememos/memos.git
synced 2024-12-21 02:01:55 +03:00
e062c9b4a7
* Created tr.json for Turkish Translation * updated file for trLocale * Updated for Turkish Locale * Update i18n.ts * Update i18n.ts * Update package.json * Update package.json * Update i18n.d.ts * Update user_setting.go * Update package.json * Update web/src/components/LocaleSelect.tsx Co-authored-by: boojack <stevenlgtm@gmail.com> * Update package.json * Update LocaleSelect.tsx * Update LocaleSelect.tsx * Update i18n.ts * Update i18n.ts --------- Co-authored-by: boojack <stevenlgtm@gmail.com>
111 lines
3.4 KiB
Go
111 lines
3.4 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"golang.org/x/exp/slices"
|
|
)
|
|
|
|
type UserSettingKey string
|
|
|
|
const (
|
|
// UserSettingLocaleKey is the key type for user locale.
|
|
UserSettingLocaleKey UserSettingKey = "locale"
|
|
// UserSettingAppearanceKey is the key type for user appearance.
|
|
UserSettingAppearanceKey UserSettingKey = "appearance"
|
|
// UserSettingMemoVisibilityKey is the key type for user preference memo default visibility.
|
|
UserSettingMemoVisibilityKey UserSettingKey = "memoVisibility"
|
|
// UserSettingResourceVisibilityKey is the key type for user preference resource default visibility.
|
|
UserSettingResourceVisibilityKey UserSettingKey = "resourceVisibility"
|
|
)
|
|
|
|
// String returns the string format of UserSettingKey type.
|
|
func (key UserSettingKey) String() string {
|
|
switch key {
|
|
case UserSettingLocaleKey:
|
|
return "locale"
|
|
case UserSettingAppearanceKey:
|
|
return "appearance"
|
|
case UserSettingMemoVisibilityKey:
|
|
return "memoVisibility"
|
|
case UserSettingResourceVisibilityKey:
|
|
return "resourceVisibility"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
var (
|
|
UserSettingLocaleValue = []string{"en", "zh", "vi", "fr", "nl", "sv", "de", "es", "uk", "ru", "it", "hant", "tr", "ko"}
|
|
UserSettingAppearanceValue = []string{"system", "light", "dark"}
|
|
UserSettingMemoVisibilityValue = []Visibility{Private, Protected, Public}
|
|
UserSettingResourceVisibilityValue = []Visibility{Private, Protected, Public}
|
|
)
|
|
|
|
type UserSetting struct {
|
|
UserID int
|
|
Key UserSettingKey `json:"key"`
|
|
// Value is a JSON string with basic value
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
type UserSettingUpsert struct {
|
|
UserID int `json:"-"`
|
|
Key UserSettingKey `json:"key"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
func (upsert UserSettingUpsert) Validate() error {
|
|
if upsert.Key == UserSettingLocaleKey {
|
|
localeValue := "en"
|
|
err := json.Unmarshal([]byte(upsert.Value), &localeValue)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to unmarshal user setting locale value")
|
|
}
|
|
if !slices.Contains(UserSettingLocaleValue, localeValue) {
|
|
return fmt.Errorf("invalid user setting locale value")
|
|
}
|
|
} else if upsert.Key == UserSettingAppearanceKey {
|
|
appearanceValue := "system"
|
|
err := json.Unmarshal([]byte(upsert.Value), &appearanceValue)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to unmarshal user setting appearance value")
|
|
}
|
|
if !slices.Contains(UserSettingAppearanceValue, appearanceValue) {
|
|
return fmt.Errorf("invalid user setting appearance value")
|
|
}
|
|
} else if upsert.Key == UserSettingMemoVisibilityKey {
|
|
memoVisibilityValue := Private
|
|
err := json.Unmarshal([]byte(upsert.Value), &memoVisibilityValue)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to unmarshal user setting memo visibility value")
|
|
}
|
|
if !slices.Contains(UserSettingMemoVisibilityValue, memoVisibilityValue) {
|
|
return fmt.Errorf("invalid user setting memo visibility value")
|
|
}
|
|
} else if upsert.Key == UserSettingResourceVisibilityKey {
|
|
resourceVisibilityValue := Private
|
|
err := json.Unmarshal([]byte(upsert.Value), &resourceVisibilityValue)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to unmarshal user setting resource visibility value")
|
|
}
|
|
if !slices.Contains(UserSettingResourceVisibilityValue, resourceVisibilityValue) {
|
|
return fmt.Errorf("invalid user setting resource visibility value")
|
|
}
|
|
} else {
|
|
return fmt.Errorf("invalid user setting key")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type UserSettingFind struct {
|
|
UserID int
|
|
|
|
Key UserSettingKey `json:"key"`
|
|
}
|
|
|
|
type UserSettingDelete struct {
|
|
UserID int
|
|
}
|