user_setting.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // UserSettingMemoDisplayTsOptionKey is the key type for memo display ts option.
  16. UserSettingMemoDisplayTsOptionKey UserSettingKey = "memoDisplayTsOption"
  17. )
  18. // String returns the string format of UserSettingKey type.
  19. func (key UserSettingKey) String() string {
  20. switch key {
  21. case UserSettingLocaleKey:
  22. return "locale"
  23. case UserSettingAppearanceKey:
  24. return "appearance"
  25. case UserSettingMemoVisibilityKey:
  26. return "memoVisibility"
  27. case UserSettingMemoDisplayTsOptionKey:
  28. return "memoDisplayTsOption"
  29. }
  30. return ""
  31. }
  32. var (
  33. UserSettingLocaleValue = []string{"en", "zh", "vi", "fr", "nl", "sv", "de", "es", "uk", "ru"}
  34. UserSettingAppearanceValue = []string{"system", "light", "dark"}
  35. UserSettingMemoVisibilityValue = []Visibility{Private, Protected, Public}
  36. UserSettingMemoDisplayTsOptionKeyValue = []string{"created_ts", "updated_ts"}
  37. )
  38. type UserSetting struct {
  39. UserID int
  40. Key UserSettingKey `json:"key"`
  41. // Value is a JSON string with basic value
  42. Value string `json:"value"`
  43. }
  44. type UserSettingUpsert struct {
  45. UserID int `json:"-"`
  46. Key UserSettingKey `json:"key"`
  47. Value string `json:"value"`
  48. }
  49. func (upsert UserSettingUpsert) Validate() error {
  50. if upsert.Key == UserSettingLocaleKey {
  51. localeValue := "en"
  52. err := json.Unmarshal([]byte(upsert.Value), &localeValue)
  53. if err != nil {
  54. return fmt.Errorf("failed to unmarshal user setting locale value")
  55. }
  56. if !slices.Contains(UserSettingLocaleValue, localeValue) {
  57. return fmt.Errorf("invalid user setting locale value")
  58. }
  59. } else if upsert.Key == UserSettingAppearanceKey {
  60. appearanceValue := "system"
  61. err := json.Unmarshal([]byte(upsert.Value), &appearanceValue)
  62. if err != nil {
  63. return fmt.Errorf("failed to unmarshal user setting appearance value")
  64. }
  65. if !slices.Contains(UserSettingAppearanceValue, appearanceValue) {
  66. return fmt.Errorf("invalid user setting appearance value")
  67. }
  68. } else if upsert.Key == UserSettingMemoVisibilityKey {
  69. memoVisibilityValue := Private
  70. err := json.Unmarshal([]byte(upsert.Value), &memoVisibilityValue)
  71. if err != nil {
  72. return fmt.Errorf("failed to unmarshal user setting memo visibility value")
  73. }
  74. if !slices.Contains(UserSettingMemoVisibilityValue, memoVisibilityValue) {
  75. return fmt.Errorf("invalid user setting memo visibility value")
  76. }
  77. } else if upsert.Key == UserSettingMemoDisplayTsOptionKey {
  78. memoDisplayTsOption := "created_ts"
  79. err := json.Unmarshal([]byte(upsert.Value), &memoDisplayTsOption)
  80. if err != nil {
  81. return fmt.Errorf("failed to unmarshal user setting memo display ts option")
  82. }
  83. if !slices.Contains(UserSettingMemoDisplayTsOptionKeyValue, memoDisplayTsOption) {
  84. return fmt.Errorf("invalid user setting memo display ts option value")
  85. }
  86. } else {
  87. return fmt.Errorf("invalid user setting key")
  88. }
  89. return nil
  90. }
  91. type UserSettingFind struct {
  92. UserID int
  93. Key *UserSettingKey `json:"key"`
  94. }
  95. type UserSettingDelete struct {
  96. UserID int
  97. }