system_setting.go 7.4 KB

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