user_setting.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "golang.org/x/exp/slices"
  7. )
  8. type UserSettingKey string
  9. const (
  10. // UserSettingLocaleKey is the key type for user locale.
  11. UserSettingLocaleKey UserSettingKey = "locale"
  12. // UserSettingAppearanceKey is the key type for user appearance.
  13. UserSettingAppearanceKey UserSettingKey = "appearance"
  14. // UserSettingMemoVisibilityKey is the key type for user preference memo default visibility.
  15. UserSettingMemoVisibilityKey UserSettingKey = "memo-visibility"
  16. // UserSettingTelegramUserID is the key type for telegram UserID of memos user.
  17. UserSettingTelegramUserIDKey UserSettingKey = "telegram-user-id"
  18. )
  19. // String returns the string format of UserSettingKey type.
  20. func (key UserSettingKey) String() string {
  21. switch key {
  22. case UserSettingLocaleKey:
  23. return "locale"
  24. case UserSettingAppearanceKey:
  25. return "appearance"
  26. case UserSettingMemoVisibilityKey:
  27. return "memo-visibility"
  28. case UserSettingTelegramUserIDKey:
  29. return "telegram-user-id"
  30. }
  31. return ""
  32. }
  33. var (
  34. UserSettingLocaleValue = []string{
  35. "de",
  36. "en",
  37. "es",
  38. "fr",
  39. "hr",
  40. "it",
  41. "ja",
  42. "ko",
  43. "nl",
  44. "pl",
  45. "pt-BR",
  46. "ru",
  47. "sl",
  48. "sv",
  49. "tr",
  50. "uk",
  51. "vi",
  52. "zh-Hans",
  53. "zh-Hant",
  54. }
  55. UserSettingAppearanceValue = []string{"system", "light", "dark"}
  56. UserSettingMemoVisibilityValue = []Visibility{Private, Protected, Public}
  57. )
  58. type UserSetting struct {
  59. UserID int
  60. Key UserSettingKey `json:"key"`
  61. // Value is a JSON string with basic value
  62. Value string `json:"value"`
  63. }
  64. type UserSettingUpsert struct {
  65. UserID int `json:"-"`
  66. Key UserSettingKey `json:"key"`
  67. Value string `json:"value"`
  68. }
  69. func (upsert UserSettingUpsert) Validate() error {
  70. if upsert.Key == UserSettingLocaleKey {
  71. localeValue := "en"
  72. err := json.Unmarshal([]byte(upsert.Value), &localeValue)
  73. if err != nil {
  74. return fmt.Errorf("failed to unmarshal user setting locale value")
  75. }
  76. if !slices.Contains(UserSettingLocaleValue, localeValue) {
  77. return fmt.Errorf("invalid user setting locale value")
  78. }
  79. } else if upsert.Key == UserSettingAppearanceKey {
  80. appearanceValue := "system"
  81. err := json.Unmarshal([]byte(upsert.Value), &appearanceValue)
  82. if err != nil {
  83. return fmt.Errorf("failed to unmarshal user setting appearance value")
  84. }
  85. if !slices.Contains(UserSettingAppearanceValue, appearanceValue) {
  86. return fmt.Errorf("invalid user setting appearance value")
  87. }
  88. } else if upsert.Key == UserSettingMemoVisibilityKey {
  89. memoVisibilityValue := Private
  90. err := json.Unmarshal([]byte(upsert.Value), &memoVisibilityValue)
  91. if err != nil {
  92. return fmt.Errorf("failed to unmarshal user setting memo visibility value")
  93. }
  94. if !slices.Contains(UserSettingMemoVisibilityValue, memoVisibilityValue) {
  95. return fmt.Errorf("invalid user setting memo visibility value")
  96. }
  97. } else if upsert.Key == UserSettingTelegramUserIDKey {
  98. var s string
  99. err := json.Unmarshal([]byte(upsert.Value), &s)
  100. if err != nil {
  101. return fmt.Errorf("invalid user setting telegram user id value")
  102. }
  103. if s == "" {
  104. return nil
  105. }
  106. if _, err := strconv.Atoi(s); err != nil {
  107. return fmt.Errorf("invalid user setting telegram user id value")
  108. }
  109. } else {
  110. return fmt.Errorf("invalid user setting key")
  111. }
  112. return nil
  113. }
  114. type UserSettingFind struct {
  115. UserID *int
  116. Key UserSettingKey `json:"key"`
  117. }
  118. type UserSettingDelete struct {
  119. UserID int
  120. }