system_setting.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "golang.org/x/exp/slices"
  7. )
  8. type SystemSettingName string
  9. const (
  10. // SystemSettingServerIDName is the name of server id.
  11. SystemSettingServerIDName SystemSettingName = "server-id"
  12. // SystemSettingSecretSessionName is the name of secret session.
  13. SystemSettingSecretSessionName SystemSettingName = "secret-session"
  14. // SystemSettingAllowSignUpName is the name of allow signup setting.
  15. SystemSettingAllowSignUpName SystemSettingName = "allow-signup"
  16. // SystemSettingIgnoreUpgradeName is the name of ignore upgrade.
  17. SystemSettingIgnoreUpgradeName SystemSettingName = "ignore-upgrade"
  18. // SystemSettingDisablePublicMemosName is the name of disable public memos setting.
  19. SystemSettingDisablePublicMemosName SystemSettingName = "disable-public-memos"
  20. // SystemSettingMaxUploadSizeMiBName is the name of max upload size setting.
  21. SystemSettingMaxUploadSizeMiBName SystemSettingName = "max-upload-size-mib"
  22. // SystemSettingAdditionalStyleName is the name of additional style.
  23. SystemSettingAdditionalStyleName SystemSettingName = "additional-style"
  24. // SystemSettingAdditionalScriptName is the name of additional script.
  25. SystemSettingAdditionalScriptName SystemSettingName = "additional-script"
  26. // SystemSettingCustomizedProfileName is the name of customized server profile.
  27. SystemSettingCustomizedProfileName SystemSettingName = "customized-profile"
  28. // SystemSettingStorageServiceIDName is the name of storage service ID.
  29. SystemSettingStorageServiceIDName SystemSettingName = "storage-service-id"
  30. // SystemSettingLocalStoragePathName is the name of local storage path.
  31. SystemSettingLocalStoragePathName SystemSettingName = "local-storage-path"
  32. // SystemSettingOpenAIConfigName is the name of OpenAI config.
  33. SystemSettingOpenAIConfigName SystemSettingName = "openai-config"
  34. // SystemSettingTelegramRobotToken is the name of Telegram Robot Token.
  35. SystemSettingTelegramRobotTokenName SystemSettingName = "telegram-robot-token"
  36. )
  37. // CustomizedProfile is the struct definition for SystemSettingCustomizedProfileName system setting item.
  38. type CustomizedProfile struct {
  39. // Name is the server name, default is `memos`
  40. Name string `json:"name"`
  41. // LogoURL is the url of logo image.
  42. LogoURL string `json:"logoUrl"`
  43. // Description is the server description.
  44. Description string `json:"description"`
  45. // Locale is the server default locale.
  46. Locale string `json:"locale"`
  47. // Appearance is the server default appearance.
  48. Appearance string `json:"appearance"`
  49. // ExternalURL is the external url of server. e.g. https://usermemos.com
  50. ExternalURL string `json:"externalUrl"`
  51. }
  52. type OpenAIConfig struct {
  53. Key string `json:"key"`
  54. Host string `json:"host"`
  55. }
  56. func (key SystemSettingName) String() string {
  57. switch key {
  58. case SystemSettingServerIDName:
  59. return "server-id"
  60. case SystemSettingSecretSessionName:
  61. return "secret-session"
  62. case SystemSettingAllowSignUpName:
  63. return "allow-signup"
  64. case SystemSettingIgnoreUpgradeName:
  65. return "ignore-upgrade"
  66. case SystemSettingDisablePublicMemosName:
  67. return "disable-public-memos"
  68. case SystemSettingMaxUploadSizeMiBName:
  69. return "max-upload-size-mib"
  70. case SystemSettingAdditionalStyleName:
  71. return "additional-style"
  72. case SystemSettingAdditionalScriptName:
  73. return "additional-script"
  74. case SystemSettingCustomizedProfileName:
  75. return "customized-profile"
  76. case SystemSettingStorageServiceIDName:
  77. return "storage-service-id"
  78. case SystemSettingLocalStoragePathName:
  79. return "local-storage-path"
  80. case SystemSettingOpenAIConfigName:
  81. return "openai-config"
  82. case SystemSettingTelegramRobotTokenName:
  83. return "telegram-robot-token"
  84. }
  85. return ""
  86. }
  87. type SystemSetting struct {
  88. Name SystemSettingName `json:"name"`
  89. // Value is a JSON string with basic value.
  90. Value string `json:"value"`
  91. Description string `json:"description"`
  92. }
  93. type SystemSettingUpsert struct {
  94. Name SystemSettingName `json:"name"`
  95. Value string `json:"value"`
  96. Description string `json:"description"`
  97. }
  98. const systemSettingUnmarshalError = `failed to unmarshal value from system setting "%v"`
  99. func (upsert SystemSettingUpsert) Validate() error {
  100. switch settingName := upsert.Name; settingName {
  101. case SystemSettingServerIDName:
  102. return fmt.Errorf("updating %v is not allowed", settingName)
  103. case SystemSettingAllowSignUpName:
  104. var value bool
  105. if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
  106. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  107. }
  108. case SystemSettingIgnoreUpgradeName:
  109. var value bool
  110. if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
  111. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  112. }
  113. case SystemSettingDisablePublicMemosName:
  114. var value bool
  115. if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
  116. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  117. }
  118. case SystemSettingMaxUploadSizeMiBName:
  119. var value int
  120. if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
  121. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  122. }
  123. case SystemSettingAdditionalStyleName:
  124. var value string
  125. if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
  126. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  127. }
  128. case SystemSettingAdditionalScriptName:
  129. var value string
  130. if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
  131. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  132. }
  133. case SystemSettingCustomizedProfileName:
  134. customizedProfile := CustomizedProfile{
  135. Name: "memos",
  136. LogoURL: "",
  137. Description: "",
  138. Locale: "en",
  139. Appearance: "system",
  140. ExternalURL: "",
  141. }
  142. if err := json.Unmarshal([]byte(upsert.Value), &customizedProfile); err != nil {
  143. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  144. }
  145. if !slices.Contains(UserSettingLocaleValue, customizedProfile.Locale) {
  146. return fmt.Errorf(`invalid locale value for system setting "%v"`, settingName)
  147. }
  148. if !slices.Contains(UserSettingAppearanceValue, customizedProfile.Appearance) {
  149. return fmt.Errorf(`invalid appearance value for system setting "%v"`, settingName)
  150. }
  151. case SystemSettingStorageServiceIDName:
  152. value := DatabaseStorage
  153. if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
  154. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  155. }
  156. return nil
  157. case SystemSettingLocalStoragePathName:
  158. value := ""
  159. if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
  160. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  161. }
  162. case SystemSettingOpenAIConfigName:
  163. value := OpenAIConfig{}
  164. if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
  165. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  166. }
  167. case SystemSettingTelegramRobotTokenName:
  168. if upsert.Value == "" {
  169. return nil
  170. }
  171. fragments := strings.Split(upsert.Value, ":")
  172. if len(fragments) != 2 {
  173. return fmt.Errorf(systemSettingUnmarshalError, settingName)
  174. }
  175. default:
  176. return fmt.Errorf("invalid system setting name")
  177. }
  178. return nil
  179. }
  180. type SystemSettingFind struct {
  181. Name SystemSettingName `json:"name"`
  182. }