2022-11-03 21:47:36 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-01-03 20:05:37 +08:00
|
|
|
"errors"
|
2022-11-03 21:47:36 +08:00
|
|
|
"fmt"
|
2022-12-22 19:48:44 +08:00
|
|
|
|
|
|
|
"golang.org/x/exp/slices"
|
2022-11-03 21:47:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type SystemSettingName string
|
|
|
|
|
|
|
|
const (
|
2023-04-03 09:53:36 +08:00
|
|
|
// SystemSettingServerIDName is the name of server id.
|
2023-04-03 09:36:34 +08:00
|
|
|
SystemSettingServerIDName SystemSettingName = "server-id"
|
|
|
|
// SystemSettingSecretSessionName is the name of secret session.
|
|
|
|
SystemSettingSecretSessionName SystemSettingName = "secret-session"
|
|
|
|
// SystemSettingAllowSignUpName is the name of allow signup setting.
|
|
|
|
SystemSettingAllowSignUpName SystemSettingName = "allow-signup"
|
2023-04-08 18:13:51 +08:00
|
|
|
// SystemSettingIgnoreUpgradeName is the name of ignore upgrade.
|
|
|
|
SystemSettingIgnoreUpgradeName SystemSettingName = "ignore-upgrade"
|
2023-04-03 09:36:34 +08:00
|
|
|
// SystemSettingDisablePublicMemosName is the name of disable public memos setting.
|
|
|
|
SystemSettingDisablePublicMemosName SystemSettingName = "disable-public-memos"
|
|
|
|
// SystemSettingAdditionalStyleName is the name of additional style.
|
|
|
|
SystemSettingAdditionalStyleName SystemSettingName = "additional-style"
|
|
|
|
// SystemSettingAdditionalScriptName is the name of additional script.
|
|
|
|
SystemSettingAdditionalScriptName SystemSettingName = "additional-script"
|
|
|
|
// SystemSettingCustomizedProfileName is the name of customized server profile.
|
|
|
|
SystemSettingCustomizedProfileName SystemSettingName = "customized-profile"
|
|
|
|
// SystemSettingStorageServiceIDName is the name of storage service ID.
|
|
|
|
SystemSettingStorageServiceIDName SystemSettingName = "storage-service-id"
|
|
|
|
// SystemSettingLocalStoragePathName is the name of local storage path.
|
|
|
|
SystemSettingLocalStoragePathName SystemSettingName = "local-storage-path"
|
|
|
|
// SystemSettingOpenAIConfigName is the name of OpenAI config.
|
|
|
|
SystemSettingOpenAIConfigName SystemSettingName = "openai-config"
|
2022-11-03 21:47:36 +08:00
|
|
|
)
|
|
|
|
|
2022-12-18 21:18:30 +08:00
|
|
|
// CustomizedProfile is the struct definition for SystemSettingCustomizedProfileName system setting item.
|
|
|
|
type CustomizedProfile struct {
|
|
|
|
// Name is the server name, default is `memos`
|
|
|
|
Name string `json:"name"`
|
2022-12-23 00:21:53 +08:00
|
|
|
// LogoURL is the url of logo image.
|
|
|
|
LogoURL string `json:"logoUrl"`
|
2022-12-22 19:48:44 +08:00
|
|
|
// Description is the server description.
|
|
|
|
Description string `json:"description"`
|
|
|
|
// Locale is the server default locale.
|
|
|
|
Locale string `json:"locale"`
|
|
|
|
// Appearance is the server default appearance.
|
|
|
|
Appearance string `json:"appearance"`
|
2022-12-18 21:18:30 +08:00
|
|
|
// ExternalURL is the external url of server. e.g. https://usermemos.com
|
|
|
|
ExternalURL string `json:"externalUrl"`
|
|
|
|
}
|
|
|
|
|
2023-03-11 12:26:40 +08:00
|
|
|
type OpenAIConfig struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
}
|
|
|
|
|
2022-11-03 21:47:36 +08:00
|
|
|
func (key SystemSettingName) String() string {
|
|
|
|
switch key {
|
2023-04-03 09:36:34 +08:00
|
|
|
case SystemSettingServerIDName:
|
|
|
|
return "server-id"
|
2023-01-03 23:05:42 +08:00
|
|
|
case SystemSettingSecretSessionName:
|
2023-04-03 09:36:34 +08:00
|
|
|
return "secret-session"
|
2022-11-03 21:47:36 +08:00
|
|
|
case SystemSettingAllowSignUpName:
|
2023-04-03 09:36:34 +08:00
|
|
|
return "allow-signup"
|
2023-04-08 18:13:51 +08:00
|
|
|
case SystemSettingIgnoreUpgradeName:
|
|
|
|
return "ignore-upgrade"
|
2023-02-14 00:07:31 +08:00
|
|
|
case SystemSettingDisablePublicMemosName:
|
2023-04-03 09:36:34 +08:00
|
|
|
return "disable-public-memos"
|
2022-11-11 23:42:44 +08:00
|
|
|
case SystemSettingAdditionalStyleName:
|
2023-04-03 09:36:34 +08:00
|
|
|
return "additional-style"
|
2022-11-14 22:21:19 +08:00
|
|
|
case SystemSettingAdditionalScriptName:
|
2023-04-03 09:36:34 +08:00
|
|
|
return "additional-script"
|
2022-12-18 21:18:30 +08:00
|
|
|
case SystemSettingCustomizedProfileName:
|
2023-04-03 09:36:34 +08:00
|
|
|
return "customized-profile"
|
2023-02-15 22:54:46 +08:00
|
|
|
case SystemSettingStorageServiceIDName:
|
2023-04-03 09:36:34 +08:00
|
|
|
return "storage-service-id"
|
2023-03-19 19:37:57 +08:00
|
|
|
case SystemSettingLocalStoragePathName:
|
2023-04-03 09:36:34 +08:00
|
|
|
return "local-storage-path"
|
2023-03-11 12:26:40 +08:00
|
|
|
case SystemSettingOpenAIConfigName:
|
2023-04-03 09:36:34 +08:00
|
|
|
return "openai-config"
|
2022-11-03 21:47:36 +08:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
type SystemSetting struct {
|
2023-03-11 12:26:40 +08:00
|
|
|
Name SystemSettingName `json:"name"`
|
2023-01-03 20:05:37 +08:00
|
|
|
// Value is a JSON string with basic value.
|
2023-03-11 12:26:40 +08:00
|
|
|
Value string `json:"value"`
|
|
|
|
Description string `json:"description"`
|
2022-11-03 21:47:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type SystemSettingUpsert struct {
|
|
|
|
Name SystemSettingName `json:"name"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (upsert SystemSettingUpsert) Validate() error {
|
2023-04-03 09:36:34 +08:00
|
|
|
if upsert.Name == SystemSettingServerIDName {
|
2023-01-03 20:05:37 +08:00
|
|
|
return errors.New("update server id is not allowed")
|
|
|
|
} else if upsert.Name == SystemSettingAllowSignUpName {
|
2022-11-03 21:47:36 +08:00
|
|
|
value := false
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal system setting allow signup value")
|
|
|
|
}
|
2023-04-08 18:13:51 +08:00
|
|
|
} else if upsert.Name == SystemSettingIgnoreUpgradeName {
|
|
|
|
value := false
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal system setting ignore upgrade value")
|
|
|
|
}
|
2023-02-14 00:07:31 +08:00
|
|
|
} else if upsert.Name == SystemSettingDisablePublicMemosName {
|
|
|
|
value := false
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal system setting disable public memos value")
|
|
|
|
}
|
2022-11-11 23:42:44 +08:00
|
|
|
} else if upsert.Name == SystemSettingAdditionalStyleName {
|
|
|
|
value := ""
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal system setting additional style value")
|
|
|
|
}
|
2022-11-14 22:21:19 +08:00
|
|
|
} else if upsert.Name == SystemSettingAdditionalScriptName {
|
|
|
|
value := ""
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal system setting additional script value")
|
|
|
|
}
|
2022-12-18 21:18:30 +08:00
|
|
|
} else if upsert.Name == SystemSettingCustomizedProfileName {
|
2022-12-22 19:48:44 +08:00
|
|
|
customizedProfile := CustomizedProfile{
|
2022-12-18 21:18:30 +08:00
|
|
|
Name: "memos",
|
2022-12-23 00:21:53 +08:00
|
|
|
LogoURL: "",
|
2022-12-22 19:48:44 +08:00
|
|
|
Description: "",
|
|
|
|
Locale: "en",
|
|
|
|
Appearance: "system",
|
2022-12-18 21:18:30 +08:00
|
|
|
ExternalURL: "",
|
|
|
|
}
|
2022-12-22 19:48:44 +08:00
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &customizedProfile)
|
2022-12-18 21:18:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal system setting customized profile value")
|
|
|
|
}
|
2022-12-22 19:48:44 +08:00
|
|
|
if !slices.Contains(UserSettingLocaleValue, customizedProfile.Locale) {
|
|
|
|
return fmt.Errorf("invalid locale value")
|
|
|
|
}
|
|
|
|
if !slices.Contains(UserSettingAppearanceValue, customizedProfile.Appearance) {
|
|
|
|
return fmt.Errorf("invalid appearance value")
|
|
|
|
}
|
2023-02-15 22:54:46 +08:00
|
|
|
} else if upsert.Name == SystemSettingStorageServiceIDName {
|
2023-04-03 14:13:22 +08:00
|
|
|
value := DatabaseStorage
|
2023-02-15 22:54:46 +08:00
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal system setting storage service id value")
|
|
|
|
}
|
2023-02-13 19:36:48 +08:00
|
|
|
return nil
|
2023-03-19 19:37:57 +08:00
|
|
|
} else if upsert.Name == SystemSettingLocalStoragePathName {
|
|
|
|
value := ""
|
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal system setting local storage path value")
|
|
|
|
}
|
2023-03-11 12:26:40 +08:00
|
|
|
} else if upsert.Name == SystemSettingOpenAIConfigName {
|
|
|
|
value := OpenAIConfig{}
|
2023-03-06 20:10:53 +08:00
|
|
|
err := json.Unmarshal([]byte(upsert.Value), &value)
|
|
|
|
if err != nil {
|
2023-03-11 12:26:40 +08:00
|
|
|
return fmt.Errorf("failed to unmarshal system setting openai api config value")
|
2023-03-06 20:10:53 +08:00
|
|
|
}
|
2022-11-03 21:47:36 +08:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid system setting name")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SystemSettingFind struct {
|
2023-02-15 22:54:46 +08:00
|
|
|
Name SystemSettingName `json:"name"`
|
2022-11-03 21:47:36 +08:00
|
|
|
}
|