2022-08-13 09:35:33 +03:00
|
|
|
package api
|
|
|
|
|
2022-08-20 16:51:28 +03:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2022-08-13 09:35:33 +03:00
|
|
|
type UserSettingKey string
|
|
|
|
|
|
|
|
const (
|
2022-08-18 19:00:47 +03:00
|
|
|
// UserSettingLocaleKey is the key type for user locale.
|
2022-08-13 09:35:33 +03:00
|
|
|
UserSettingLocaleKey UserSettingKey = "locale"
|
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"
|
2022-08-25 15:44:32 +03:00
|
|
|
// UserSettingEditorFontStyleKey is the key type for editor font style.
|
|
|
|
UserSettingEditorFontStyleKey UserSettingKey = "editorFontStyle"
|
2022-09-20 15:42:14 +03:00
|
|
|
// UserSettingEditorFontStyleKey is the key type for mobile editor style.
|
|
|
|
UserSettingMobileEditorStyleKey UserSettingKey = "mobileEditorStyle"
|
2022-10-21 15:26:00 +03:00
|
|
|
// UserSettingMemoDisplayTsOptionKey is the key type for memo display ts option.
|
2022-10-21 18:06:15 +03:00
|
|
|
UserSettingMemoDisplayTsOptionKey UserSettingKey = "memoDisplayTsOption"
|
2022-08-13 09:35:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns the string format of UserSettingKey type.
|
|
|
|
func (key UserSettingKey) String() string {
|
|
|
|
switch key {
|
|
|
|
case UserSettingLocaleKey:
|
|
|
|
return "locale"
|
2022-08-18 19:00:47 +03:00
|
|
|
case UserSettingMemoVisibilityKey:
|
2022-08-19 16:56:22 +03:00
|
|
|
return "memoVisibility"
|
2022-08-25 15:44:32 +03:00
|
|
|
case UserSettingEditorFontStyleKey:
|
|
|
|
return "editorFontFamily"
|
2022-09-20 15:42:14 +03:00
|
|
|
case UserSettingMobileEditorStyleKey:
|
|
|
|
return "mobileEditorStyle"
|
2022-10-21 15:26:00 +03:00
|
|
|
case UserSettingMemoDisplayTsOptionKey:
|
|
|
|
return "memoDisplayTsOption"
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-08-20 16:51:28 +03:00
|
|
|
var (
|
2022-10-21 15:26:00 +03:00
|
|
|
UserSettingLocaleValue = []string{"en", "zh", "vi"}
|
|
|
|
UserSettingMemoVisibilityValue = []Visibility{Privite, Protected, Public}
|
|
|
|
UserSettingEditorFontStyleValue = []string{"normal", "mono"}
|
|
|
|
UserSettingMobileEditorStyleValue = []string{"normal", "float"}
|
|
|
|
UserSettingMemoDisplayTsOptionKeyValue = []string{"created_ts", "updated_ts"}
|
2022-08-20 16:51:28 +03:00
|
|
|
)
|
|
|
|
|
2022-08-13 09:35:33 +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 {
|
|
|
|
UserID int
|
|
|
|
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 {
|
2022-08-25 15:44:32 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid := true
|
|
|
|
for _, value := range UserSettingLocaleValue {
|
|
|
|
if localeValue == value {
|
|
|
|
invalid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if invalid {
|
|
|
|
return fmt.Errorf("invalid user setting locale value")
|
|
|
|
}
|
|
|
|
} else if upsert.Key == UserSettingMemoVisibilityKey {
|
2022-08-25 15:44:32 +03:00
|
|
|
memoVisibilityValue := Privite
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid := true
|
|
|
|
for _, value := range UserSettingMemoVisibilityValue {
|
|
|
|
if memoVisibilityValue == value {
|
|
|
|
invalid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if invalid {
|
|
|
|
return fmt.Errorf("invalid user setting memo visibility value")
|
|
|
|
}
|
2022-08-25 15:44:32 +03:00
|
|
|
} else if upsert.Key == UserSettingEditorFontStyleKey {
|
|
|
|
editorFontStyleValue := "normal"
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &editorFontStyleValue)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal user setting editor font style")
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid := true
|
|
|
|
for _, value := range UserSettingEditorFontStyleValue {
|
|
|
|
if editorFontStyleValue == value {
|
|
|
|
invalid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if invalid {
|
|
|
|
return fmt.Errorf("invalid user setting editor font style value")
|
|
|
|
}
|
2022-09-20 15:42:14 +03:00
|
|
|
} else if upsert.Key == UserSettingMobileEditorStyleKey {
|
|
|
|
mobileEditorStyleValue := "normal"
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &mobileEditorStyleValue)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal user setting mobile editor style")
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid := true
|
|
|
|
for _, value := range UserSettingMobileEditorStyleValue {
|
|
|
|
if mobileEditorStyleValue == value {
|
|
|
|
invalid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if invalid {
|
|
|
|
return fmt.Errorf("invalid user setting mobile editor style value")
|
|
|
|
}
|
2022-10-21 15:26:00 +03:00
|
|
|
} else if upsert.Key == UserSettingMemoDisplayTsOptionKey {
|
|
|
|
memoDisplayTsOption := "created_ts"
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &memoDisplayTsOption)
|
2022-10-19 16:00:34 +03:00
|
|
|
if err != nil {
|
2022-10-21 15:26:00 +03:00
|
|
|
return fmt.Errorf("failed to unmarshal user setting memo display ts option")
|
2022-10-19 16:00:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
invalid := true
|
2022-10-21 15:26:00 +03:00
|
|
|
for _, value := range UserSettingMemoDisplayTsOptionKeyValue {
|
|
|
|
if memoDisplayTsOption == value {
|
2022-10-19 16:00:34 +03:00
|
|
|
invalid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if invalid {
|
2022-10-21 15:26:00 +03:00
|
|
|
return fmt.Errorf("invalid user setting memo display ts option value")
|
2022-10-19 16:00:34 +03:00
|
|
|
}
|
2022-08-20 16:51:28 +03:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid user setting key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-13 09:35:33 +03:00
|
|
|
type UserSettingFind struct {
|
|
|
|
UserID int
|
2022-08-18 19:00:47 +03:00
|
|
|
|
|
|
|
Key *UserSettingKey `json:"key"`
|
2022-08-13 09:35:33 +03:00
|
|
|
}
|