memos/api/user_setting.go

111 lines
3.4 KiB
Go
Raw Normal View History

package api
2022-08-20 16:51:28 +03:00
import (
"encoding/json"
"fmt"
2022-12-22 14:48:44 +03:00
"golang.org/x/exp/slices"
2022-08-20 16:51:28 +03:00
)
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"
2022-08-24 16:53:12 +03:00
// UserSettingMemoVisibilityKey is the key type for user preference memo default visibility.
2022-08-19 16:56:22 +03:00
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:
2022-08-19 16:56:22 +03:00
return "memoVisibility"
case UserSettingResourceVisibilityKey:
return "resourceVisibility"
}
return ""
}
2022-08-20 16:51:28 +03:00
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}
2022-08-20 16:51:28 +03:00
)
type UserSetting struct {
UserID int
Key UserSettingKey `json:"key"`
// Value is a JSON string with basic value
Value string `json:"value"`
}
type UserSettingUpsert struct {
2022-12-28 15:22:52 +03:00
UserID int `json:"-"`
Key UserSettingKey `json:"key"`
Value string `json:"value"`
}
2022-08-20 16:51:28 +03:00
func (upsert UserSettingUpsert) Validate() error {
if upsert.Key == UserSettingLocaleKey {
localeValue := "en"
2022-08-20 16:51:28 +03:00
err := json.Unmarshal([]byte(upsert.Value), &localeValue)
if err != nil {
return fmt.Errorf("failed to unmarshal user setting locale value")
}
2022-12-22 14:48:44 +03:00
if !slices.Contains(UserSettingLocaleValue, localeValue) {
2022-08-20 16:51:28 +03:00
return fmt.Errorf("invalid user setting locale value")
}
} else if upsert.Key == UserSettingAppearanceKey {
2022-12-22 14:48:44 +03:00
appearanceValue := "system"
err := json.Unmarshal([]byte(upsert.Value), &appearanceValue)
if err != nil {
return fmt.Errorf("failed to unmarshal user setting appearance value")
}
2022-12-22 14:48:44 +03:00
if !slices.Contains(UserSettingAppearanceValue, appearanceValue) {
return fmt.Errorf("invalid user setting appearance value")
}
2022-08-20 16:51:28 +03:00
} else if upsert.Key == UserSettingMemoVisibilityKey {
2022-11-26 09:23:29 +03:00
memoVisibilityValue := Private
2022-08-20 16:51:28 +03:00
err := json.Unmarshal([]byte(upsert.Value), &memoVisibilityValue)
if err != nil {
return fmt.Errorf("failed to unmarshal user setting memo visibility value")
}
2022-12-22 14:48:44 +03:00
if !slices.Contains(UserSettingMemoVisibilityValue, memoVisibilityValue) {
2022-08-20 16:51:28 +03:00
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")
}
2022-08-20 16:51:28 +03:00
} else {
return fmt.Errorf("invalid user setting key")
}
return nil
}
type UserSettingFind struct {
UserID int
Key UserSettingKey `json:"key"`
}
2022-11-06 07:21:58 +03:00
type UserSettingDelete struct {
UserID int
}