system_setting.go 5.5 KB

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