system_setting.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "golang.org/x/exp/slices"
  6. )
  7. type SystemSettingName string
  8. const (
  9. // SystemSettingAllowSignUpName is the key type of allow signup setting.
  10. SystemSettingAllowSignUpName SystemSettingName = "allowSignUp"
  11. // SystemSettingAdditionalStyleName is the key type of additional style.
  12. SystemSettingAdditionalStyleName SystemSettingName = "additionalStyle"
  13. // SystemSettingAdditionalScriptName is the key type of additional script.
  14. SystemSettingAdditionalScriptName SystemSettingName = "additionalScript"
  15. // SystemSettingCustomizedProfileName is the key type of customized server profile.
  16. SystemSettingCustomizedProfileName SystemSettingName = "customizedProfile"
  17. )
  18. // CustomizedProfile is the struct definition for SystemSettingCustomizedProfileName system setting item.
  19. type CustomizedProfile struct {
  20. // Name is the server name, default is `memos`
  21. Name string `json:"name"`
  22. // LogoURL is the url of logo image.
  23. LogoURL string `json:"logoUrl"`
  24. // Description is the server description.
  25. Description string `json:"description"`
  26. // Locale is the server default locale.
  27. Locale string `json:"locale"`
  28. // Appearance is the server default appearance.
  29. Appearance string `json:"appearance"`
  30. // ExternalURL is the external url of server. e.g. https://usermemos.com
  31. ExternalURL string `json:"externalUrl"`
  32. }
  33. func (key SystemSettingName) String() string {
  34. switch key {
  35. case SystemSettingAllowSignUpName:
  36. return "allowSignUp"
  37. case SystemSettingAdditionalStyleName:
  38. return "additionalStyle"
  39. case SystemSettingAdditionalScriptName:
  40. return "additionalScript"
  41. case SystemSettingCustomizedProfileName:
  42. return "customizedProfile"
  43. }
  44. return ""
  45. }
  46. var (
  47. SystemSettingAllowSignUpValue = []bool{true, false}
  48. )
  49. type SystemSetting struct {
  50. Name SystemSettingName
  51. // Value is a JSON string with basic value
  52. Value string
  53. Description string
  54. }
  55. type SystemSettingUpsert struct {
  56. Name SystemSettingName `json:"name"`
  57. Value string `json:"value"`
  58. Description string `json:"description"`
  59. }
  60. func (upsert SystemSettingUpsert) Validate() error {
  61. if upsert.Name == SystemSettingAllowSignUpName {
  62. value := false
  63. err := json.Unmarshal([]byte(upsert.Value), &value)
  64. if err != nil {
  65. return fmt.Errorf("failed to unmarshal system setting allow signup value")
  66. }
  67. invalid := true
  68. for _, v := range SystemSettingAllowSignUpValue {
  69. if value == v {
  70. invalid = false
  71. break
  72. }
  73. }
  74. if invalid {
  75. return fmt.Errorf("invalid system setting allow signup value")
  76. }
  77. } else if upsert.Name == SystemSettingAdditionalStyleName {
  78. value := ""
  79. err := json.Unmarshal([]byte(upsert.Value), &value)
  80. if err != nil {
  81. return fmt.Errorf("failed to unmarshal system setting additional style value")
  82. }
  83. } else if upsert.Name == SystemSettingAdditionalScriptName {
  84. value := ""
  85. err := json.Unmarshal([]byte(upsert.Value), &value)
  86. if err != nil {
  87. return fmt.Errorf("failed to unmarshal system setting additional script value")
  88. }
  89. } else if upsert.Name == SystemSettingCustomizedProfileName {
  90. customizedProfile := CustomizedProfile{
  91. Name: "memos",
  92. LogoURL: "",
  93. Description: "",
  94. Locale: "en",
  95. Appearance: "system",
  96. ExternalURL: "",
  97. }
  98. err := json.Unmarshal([]byte(upsert.Value), &customizedProfile)
  99. if err != nil {
  100. return fmt.Errorf("failed to unmarshal system setting customized profile value")
  101. }
  102. if !slices.Contains(UserSettingLocaleValue, customizedProfile.Locale) {
  103. return fmt.Errorf("invalid locale value")
  104. }
  105. if !slices.Contains(UserSettingAppearanceValue, customizedProfile.Appearance) {
  106. return fmt.Errorf("invalid appearance value")
  107. }
  108. } else {
  109. return fmt.Errorf("invalid system setting name")
  110. }
  111. return nil
  112. }
  113. type SystemSettingFind struct {
  114. Name *SystemSettingName `json:"name"`
  115. }