2022-08-13 14:35:33 +08:00
|
|
|
package api
|
|
|
|
|
2022-08-20 21:51:28 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-12-22 19:48:44 +08:00
|
|
|
|
|
|
|
"golang.org/x/exp/slices"
|
2022-08-20 21:51:28 +08:00
|
|
|
)
|
|
|
|
|
2022-08-13 14:35:33 +08:00
|
|
|
type UserSettingKey string
|
|
|
|
|
|
|
|
const (
|
2022-08-19 00:00:47 +08:00
|
|
|
// UserSettingLocaleKey is the key type for user locale.
|
2022-08-13 14:35:33 +08:00
|
|
|
UserSettingLocaleKey UserSettingKey = "locale"
|
2022-12-02 20:00:34 +08:00
|
|
|
// UserSettingAppearanceKey is the key type for user appearance.
|
|
|
|
UserSettingAppearanceKey UserSettingKey = "appearance"
|
2022-08-24 21:53:12 +08:00
|
|
|
// UserSettingMemoVisibilityKey is the key type for user preference memo default visibility.
|
2022-08-19 21:56:22 +08:00
|
|
|
UserSettingMemoVisibilityKey UserSettingKey = "memoVisibility"
|
2022-10-21 20:26:00 +08:00
|
|
|
// UserSettingMemoDisplayTsOptionKey is the key type for memo display ts option.
|
2022-10-21 23:06:15 +08:00
|
|
|
UserSettingMemoDisplayTsOptionKey UserSettingKey = "memoDisplayTsOption"
|
2022-08-13 14:35:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns the string format of UserSettingKey type.
|
|
|
|
func (key UserSettingKey) String() string {
|
|
|
|
switch key {
|
|
|
|
case UserSettingLocaleKey:
|
|
|
|
return "locale"
|
2022-12-02 20:00:34 +08:00
|
|
|
case UserSettingAppearanceKey:
|
|
|
|
return "appearance"
|
2022-08-19 00:00:47 +08:00
|
|
|
case UserSettingMemoVisibilityKey:
|
2022-08-19 21:56:22 +08:00
|
|
|
return "memoVisibility"
|
2022-10-21 20:26:00 +08:00
|
|
|
case UserSettingMemoDisplayTsOptionKey:
|
|
|
|
return "memoDisplayTsOption"
|
2022-08-13 14:35:33 +08:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-08-20 21:51:28 +08:00
|
|
|
var (
|
2023-01-29 09:41:56 +08:00
|
|
|
UserSettingLocaleValue = []string{"en", "zh", "vi", "fr", "nl", "sv", "de", "es", "uk", "ru", "it", "hant"}
|
2022-12-04 12:23:29 +08:00
|
|
|
UserSettingAppearanceValue = []string{"system", "light", "dark"}
|
2022-11-26 14:23:29 +08:00
|
|
|
UserSettingMemoVisibilityValue = []Visibility{Private, Protected, Public}
|
2022-10-21 20:26:00 +08:00
|
|
|
UserSettingMemoDisplayTsOptionKeyValue = []string{"created_ts", "updated_ts"}
|
2022-08-20 21:51:28 +08:00
|
|
|
)
|
|
|
|
|
2022-08-13 14:35:33 +08: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 20:22:52 +08:00
|
|
|
UserID int `json:"-"`
|
2022-08-13 14:35:33 +08:00
|
|
|
Key UserSettingKey `json:"key"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
2022-08-20 21:51:28 +08:00
|
|
|
func (upsert UserSettingUpsert) Validate() error {
|
|
|
|
if upsert.Key == UserSettingLocaleKey {
|
2022-08-25 20:44:32 +08:00
|
|
|
localeValue := "en"
|
2022-08-20 21:51:28 +08:00
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &localeValue)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal user setting locale value")
|
|
|
|
}
|
2022-12-22 19:48:44 +08:00
|
|
|
if !slices.Contains(UserSettingLocaleValue, localeValue) {
|
2022-08-20 21:51:28 +08:00
|
|
|
return fmt.Errorf("invalid user setting locale value")
|
|
|
|
}
|
2022-12-02 20:00:34 +08:00
|
|
|
} else if upsert.Key == UserSettingAppearanceKey {
|
2022-12-22 19:48:44 +08:00
|
|
|
appearanceValue := "system"
|
2022-12-02 20:00:34 +08:00
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &appearanceValue)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal user setting appearance value")
|
|
|
|
}
|
2022-12-22 19:48:44 +08:00
|
|
|
if !slices.Contains(UserSettingAppearanceValue, appearanceValue) {
|
2022-12-02 20:00:34 +08:00
|
|
|
return fmt.Errorf("invalid user setting appearance value")
|
|
|
|
}
|
2022-08-20 21:51:28 +08:00
|
|
|
} else if upsert.Key == UserSettingMemoVisibilityKey {
|
2022-11-26 14:23:29 +08:00
|
|
|
memoVisibilityValue := Private
|
2022-08-20 21:51:28 +08: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 19:48:44 +08:00
|
|
|
if !slices.Contains(UserSettingMemoVisibilityValue, memoVisibilityValue) {
|
2022-08-20 21:51:28 +08:00
|
|
|
return fmt.Errorf("invalid user setting memo visibility value")
|
|
|
|
}
|
2022-10-21 20:26:00 +08:00
|
|
|
} else if upsert.Key == UserSettingMemoDisplayTsOptionKey {
|
|
|
|
memoDisplayTsOption := "created_ts"
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &memoDisplayTsOption)
|
2022-10-19 21:00:34 +08:00
|
|
|
if err != nil {
|
2022-10-21 20:26:00 +08:00
|
|
|
return fmt.Errorf("failed to unmarshal user setting memo display ts option")
|
2022-10-19 21:00:34 +08:00
|
|
|
}
|
2022-12-22 19:48:44 +08:00
|
|
|
if !slices.Contains(UserSettingMemoDisplayTsOptionKeyValue, memoDisplayTsOption) {
|
2022-10-21 20:26:00 +08:00
|
|
|
return fmt.Errorf("invalid user setting memo display ts option value")
|
2022-10-19 21:00:34 +08:00
|
|
|
}
|
2022-08-20 21:51:28 +08:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid user setting key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-13 14:35:33 +08:00
|
|
|
type UserSettingFind struct {
|
|
|
|
UserID int
|
2022-08-19 00:00:47 +08:00
|
|
|
|
|
|
|
Key *UserSettingKey `json:"key"`
|
2022-08-13 14:35:33 +08:00
|
|
|
}
|
2022-11-06 12:21:58 +08:00
|
|
|
|
|
|
|
type UserSettingDelete struct {
|
|
|
|
UserID int
|
|
|
|
}
|