system_setting.go 5.9 KB

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