system_setting.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  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. // 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. )
  33. // CustomizedProfile is the struct definition for SystemSettingCustomizedProfileName system setting item.
  34. type CustomizedProfile struct {
  35. // Name is the server name, default is `memos`
  36. Name string `json:"name"`
  37. // LogoURL is the url of logo image.
  38. LogoURL string `json:"logoUrl"`
  39. // Description is the server description.
  40. Description string `json:"description"`
  41. // Locale is the server default locale.
  42. Locale string `json:"locale"`
  43. // Appearance is the server default appearance.
  44. Appearance string `json:"appearance"`
  45. // ExternalURL is the external url of server. e.g. https://usermemos.com
  46. ExternalURL string `json:"externalUrl"`
  47. }
  48. type OpenAIConfig struct {
  49. Key string `json:"key"`
  50. Host string `json:"host"`
  51. }
  52. func (key SystemSettingName) String() string {
  53. switch key {
  54. case SystemSettingServerIDName:
  55. return "server-id"
  56. case SystemSettingSecretSessionName:
  57. return "secret-session"
  58. case SystemSettingAllowSignUpName:
  59. return "allow-signup"
  60. case SystemSettingIgnoreUpgradeName:
  61. return "ignore-upgrade"
  62. case SystemSettingDisablePublicMemosName:
  63. return "disable-public-memos"
  64. case SystemSettingAdditionalStyleName:
  65. return "additional-style"
  66. case SystemSettingAdditionalScriptName:
  67. return "additional-script"
  68. case SystemSettingCustomizedProfileName:
  69. return "customized-profile"
  70. case SystemSettingStorageServiceIDName:
  71. return "storage-service-id"
  72. case SystemSettingLocalStoragePathName:
  73. return "local-storage-path"
  74. case SystemSettingOpenAIConfigName:
  75. return "openai-config"
  76. }
  77. return ""
  78. }
  79. type SystemSetting struct {
  80. Name SystemSettingName `json:"name"`
  81. // Value is a JSON string with basic value.
  82. Value string `json:"value"`
  83. Description string `json:"description"`
  84. }
  85. type SystemSettingUpsert struct {
  86. Name SystemSettingName `json:"name"`
  87. Value string `json:"value"`
  88. Description string `json:"description"`
  89. }
  90. func (upsert SystemSettingUpsert) Validate() error {
  91. if upsert.Name == SystemSettingServerIDName {
  92. return errors.New("update server id is not allowed")
  93. } else if upsert.Name == SystemSettingAllowSignUpName {
  94. value := false
  95. err := json.Unmarshal([]byte(upsert.Value), &value)
  96. if err != nil {
  97. return fmt.Errorf("failed to unmarshal system setting allow signup value")
  98. }
  99. } else if upsert.Name == SystemSettingIgnoreUpgradeName {
  100. value := false
  101. err := json.Unmarshal([]byte(upsert.Value), &value)
  102. if err != nil {
  103. return fmt.Errorf("failed to unmarshal system setting ignore upgrade value")
  104. }
  105. } else if upsert.Name == SystemSettingDisablePublicMemosName {
  106. value := false
  107. err := json.Unmarshal([]byte(upsert.Value), &value)
  108. if err != nil {
  109. return fmt.Errorf("failed to unmarshal system setting disable public memos value")
  110. }
  111. } else if upsert.Name == SystemSettingAdditionalStyleName {
  112. value := ""
  113. err := json.Unmarshal([]byte(upsert.Value), &value)
  114. if err != nil {
  115. return fmt.Errorf("failed to unmarshal system setting additional style value")
  116. }
  117. } else if upsert.Name == SystemSettingAdditionalScriptName {
  118. value := ""
  119. err := json.Unmarshal([]byte(upsert.Value), &value)
  120. if err != nil {
  121. return fmt.Errorf("failed to unmarshal system setting additional script value")
  122. }
  123. } else if upsert.Name == SystemSettingCustomizedProfileName {
  124. customizedProfile := CustomizedProfile{
  125. Name: "memos",
  126. LogoURL: "",
  127. Description: "",
  128. Locale: "en",
  129. Appearance: "system",
  130. ExternalURL: "",
  131. }
  132. err := json.Unmarshal([]byte(upsert.Value), &customizedProfile)
  133. if err != nil {
  134. return fmt.Errorf("failed to unmarshal system setting customized profile value")
  135. }
  136. if !slices.Contains(UserSettingLocaleValue, customizedProfile.Locale) {
  137. return fmt.Errorf("invalid locale value")
  138. }
  139. if !slices.Contains(UserSettingAppearanceValue, customizedProfile.Appearance) {
  140. return fmt.Errorf("invalid appearance value")
  141. }
  142. } else if upsert.Name == SystemSettingStorageServiceIDName {
  143. value := DatabaseStorage
  144. err := json.Unmarshal([]byte(upsert.Value), &value)
  145. if err != nil {
  146. return fmt.Errorf("failed to unmarshal system setting storage service id value")
  147. }
  148. return nil
  149. } else if upsert.Name == SystemSettingLocalStoragePathName {
  150. value := ""
  151. err := json.Unmarshal([]byte(upsert.Value), &value)
  152. if err != nil {
  153. return fmt.Errorf("failed to unmarshal system setting local storage path value")
  154. }
  155. } else if upsert.Name == SystemSettingOpenAIConfigName {
  156. value := OpenAIConfig{}
  157. err := json.Unmarshal([]byte(upsert.Value), &value)
  158. if err != nil {
  159. return fmt.Errorf("failed to unmarshal system setting openai api config value")
  160. }
  161. } else {
  162. return fmt.Errorf("invalid system setting name")
  163. }
  164. return nil
  165. }
  166. type SystemSettingFind struct {
  167. Name SystemSettingName `json:"name"`
  168. }