system_setting.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // SystemSettingAdditionalStyleName is the key type of additional style.
  17. SystemSettingAdditionalStyleName SystemSettingName = "additionalStyle"
  18. // SystemSettingAdditionalScriptName is the key type of additional script.
  19. SystemSettingAdditionalScriptName SystemSettingName = "additionalScript"
  20. // SystemSettingCustomizedProfileName is the key type of customized server profile.
  21. SystemSettingCustomizedProfileName SystemSettingName = "customizedProfile"
  22. )
  23. // CustomizedProfile is the struct definition for SystemSettingCustomizedProfileName system setting item.
  24. type CustomizedProfile struct {
  25. // Name is the server name, default is `memos`
  26. Name string `json:"name"`
  27. // LogoURL is the url of logo image.
  28. LogoURL string `json:"logoUrl"`
  29. // Description is the server description.
  30. Description string `json:"description"`
  31. // Locale is the server default locale.
  32. Locale string `json:"locale"`
  33. // Appearance is the server default appearance.
  34. Appearance string `json:"appearance"`
  35. // ExternalURL is the external url of server. e.g. https://usermemos.com
  36. ExternalURL string `json:"externalUrl"`
  37. }
  38. func (key SystemSettingName) String() string {
  39. switch key {
  40. case SystemSettingServerID:
  41. return "serverId"
  42. case SystemSettingSecretSessionName:
  43. return "secretSessionName"
  44. case SystemSettingAllowSignUpName:
  45. return "allowSignUp"
  46. case SystemSettingAdditionalStyleName:
  47. return "additionalStyle"
  48. case SystemSettingAdditionalScriptName:
  49. return "additionalScript"
  50. case SystemSettingCustomizedProfileName:
  51. return "customizedProfile"
  52. }
  53. return ""
  54. }
  55. var (
  56. SystemSettingAllowSignUpValue = []bool{true, false}
  57. )
  58. type SystemSetting struct {
  59. Name SystemSettingName
  60. // Value is a JSON string with basic value.
  61. Value string
  62. Description string
  63. }
  64. type SystemSettingUpsert struct {
  65. Name SystemSettingName `json:"name"`
  66. Value string `json:"value"`
  67. Description string `json:"description"`
  68. }
  69. func (upsert SystemSettingUpsert) Validate() error {
  70. if upsert.Name == SystemSettingServerID {
  71. return errors.New("update server id is not allowed")
  72. } else if upsert.Name == SystemSettingAllowSignUpName {
  73. value := false
  74. err := json.Unmarshal([]byte(upsert.Value), &value)
  75. if err != nil {
  76. return fmt.Errorf("failed to unmarshal system setting allow signup value")
  77. }
  78. invalid := true
  79. for _, v := range SystemSettingAllowSignUpValue {
  80. if value == v {
  81. invalid = false
  82. break
  83. }
  84. }
  85. if invalid {
  86. return fmt.Errorf("invalid system setting allow signup value")
  87. }
  88. } else if upsert.Name == SystemSettingAdditionalStyleName {
  89. value := ""
  90. err := json.Unmarshal([]byte(upsert.Value), &value)
  91. if err != nil {
  92. return fmt.Errorf("failed to unmarshal system setting additional style value")
  93. }
  94. } else if upsert.Name == SystemSettingAdditionalScriptName {
  95. value := ""
  96. err := json.Unmarshal([]byte(upsert.Value), &value)
  97. if err != nil {
  98. return fmt.Errorf("failed to unmarshal system setting additional script value")
  99. }
  100. } else if upsert.Name == SystemSettingCustomizedProfileName {
  101. customizedProfile := CustomizedProfile{
  102. Name: "memos",
  103. LogoURL: "",
  104. Description: "",
  105. Locale: "en",
  106. Appearance: "system",
  107. ExternalURL: "",
  108. }
  109. err := json.Unmarshal([]byte(upsert.Value), &customizedProfile)
  110. if err != nil {
  111. return fmt.Errorf("failed to unmarshal system setting customized profile value")
  112. }
  113. if !slices.Contains(UserSettingLocaleValue, customizedProfile.Locale) {
  114. return fmt.Errorf("invalid locale value")
  115. }
  116. if !slices.Contains(UserSettingAppearanceValue, customizedProfile.Appearance) {
  117. return fmt.Errorf("invalid appearance value")
  118. }
  119. } else {
  120. return fmt.Errorf("invalid system setting name")
  121. }
  122. return nil
  123. }
  124. type SystemSettingFind struct {
  125. Name *SystemSettingName `json:"name"`
  126. }