user_setting.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "golang.org/x/exp/slices"
  6. )
  7. type UserSettingKey string
  8. const (
  9. // UserSettingLocaleKey is the key type for user locale.
  10. UserSettingLocaleKey UserSettingKey = "locale"
  11. // UserSettingAppearanceKey is the key type for user appearance.
  12. UserSettingAppearanceKey UserSettingKey = "appearance"
  13. // UserSettingMemoVisibilityKey is the key type for user preference memo default visibility.
  14. UserSettingMemoVisibilityKey UserSettingKey = "memoVisibility"
  15. )
  16. // String returns the string format of UserSettingKey type.
  17. func (key UserSettingKey) String() string {
  18. switch key {
  19. case UserSettingLocaleKey:
  20. return "locale"
  21. case UserSettingAppearanceKey:
  22. return "appearance"
  23. case UserSettingMemoVisibilityKey:
  24. return "memoVisibility"
  25. }
  26. return ""
  27. }
  28. var (
  29. UserSettingLocaleValue = []string{"en", "zh", "vi", "fr", "nl", "sv", "de", "es", "uk", "ru", "it", "hant", "ko"}
  30. UserSettingAppearanceValue = []string{"system", "light", "dark"}
  31. UserSettingMemoVisibilityValue = []Visibility{Private, Protected, Public}
  32. )
  33. type UserSetting struct {
  34. UserID int
  35. Key UserSettingKey `json:"key"`
  36. // Value is a JSON string with basic value
  37. Value string `json:"value"`
  38. }
  39. type UserSettingUpsert struct {
  40. UserID int `json:"-"`
  41. Key UserSettingKey `json:"key"`
  42. Value string `json:"value"`
  43. }
  44. func (upsert UserSettingUpsert) Validate() error {
  45. if upsert.Key == UserSettingLocaleKey {
  46. localeValue := "en"
  47. err := json.Unmarshal([]byte(upsert.Value), &localeValue)
  48. if err != nil {
  49. return fmt.Errorf("failed to unmarshal user setting locale value")
  50. }
  51. if !slices.Contains(UserSettingLocaleValue, localeValue) {
  52. return fmt.Errorf("invalid user setting locale value")
  53. }
  54. } else if upsert.Key == UserSettingAppearanceKey {
  55. appearanceValue := "system"
  56. err := json.Unmarshal([]byte(upsert.Value), &appearanceValue)
  57. if err != nil {
  58. return fmt.Errorf("failed to unmarshal user setting appearance value")
  59. }
  60. if !slices.Contains(UserSettingAppearanceValue, appearanceValue) {
  61. return fmt.Errorf("invalid user setting appearance value")
  62. }
  63. } else if upsert.Key == UserSettingMemoVisibilityKey {
  64. memoVisibilityValue := Private
  65. err := json.Unmarshal([]byte(upsert.Value), &memoVisibilityValue)
  66. if err != nil {
  67. return fmt.Errorf("failed to unmarshal user setting memo visibility value")
  68. }
  69. if !slices.Contains(UserSettingMemoVisibilityValue, memoVisibilityValue) {
  70. return fmt.Errorf("invalid user setting memo visibility value")
  71. }
  72. } else {
  73. return fmt.Errorf("invalid user setting key")
  74. }
  75. return nil
  76. }
  77. type UserSettingFind struct {
  78. UserID int
  79. Key *UserSettingKey `json:"key"`
  80. }
  81. type UserSettingDelete struct {
  82. UserID int
  83. }