chore: update system setting name convention (#1448)

This commit is contained in:
boojack 2023-04-03 09:36:34 +08:00 committed by GitHub
parent 8a33290722
commit 4f2adfef7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 135 additions and 105 deletions

View file

@ -11,26 +11,26 @@ import (
type SystemSettingName string
const (
// SystemSettingServerID is the key type of server id.
SystemSettingServerID SystemSettingName = "serverId"
// SystemSettingSecretSessionName is the key type of secret session name.
SystemSettingSecretSessionName SystemSettingName = "secretSessionName"
// SystemSettingAllowSignUpName is the key type of allow signup setting.
SystemSettingAllowSignUpName SystemSettingName = "allowSignUp"
// SystemSettingDisablePublicMemosName is the key type of disable public memos setting.
SystemSettingDisablePublicMemosName SystemSettingName = "disablePublicMemos"
// SystemSettingAdditionalStyleName is the key type of additional style.
SystemSettingAdditionalStyleName SystemSettingName = "additionalStyle"
// SystemSettingAdditionalScriptName is the key type of additional script.
SystemSettingAdditionalScriptName SystemSettingName = "additionalScript"
// SystemSettingCustomizedProfileName is the key type of customized server profile.
SystemSettingCustomizedProfileName SystemSettingName = "customizedProfile"
// SystemSettingStorageServiceIDName is the key type of storage service ID.
SystemSettingStorageServiceIDName SystemSettingName = "storageServiceId"
// SystemSettingLocalStoragePathName is the key type of local storage path.
SystemSettingLocalStoragePathName SystemSettingName = "localStoragePath"
// SystemSettingOpenAIConfigName is the key type of OpenAI config.
SystemSettingOpenAIConfigName SystemSettingName = "openAIConfig"
// SystemSettingServerID is the name of server id.
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"
// 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"
)
// CustomizedProfile is the struct definition for SystemSettingCustomizedProfileName system setting item.
@ -56,26 +56,26 @@ type OpenAIConfig struct {
func (key SystemSettingName) String() string {
switch key {
case SystemSettingServerID:
return "serverId"
case SystemSettingServerIDName:
return "server-id"
case SystemSettingSecretSessionName:
return "secretSessionName"
return "secret-session"
case SystemSettingAllowSignUpName:
return "allowSignUp"
return "allow-signup"
case SystemSettingDisablePublicMemosName:
return "disablePublicMemos"
return "disable-public-memos"
case SystemSettingAdditionalStyleName:
return "additionalStyle"
return "additional-style"
case SystemSettingAdditionalScriptName:
return "additionalScript"
return "additional-script"
case SystemSettingCustomizedProfileName:
return "customizedProfile"
return "customized-profile"
case SystemSettingStorageServiceIDName:
return "storageServiceId"
return "storage-service-id"
case SystemSettingLocalStoragePathName:
return "localStoragePath"
return "local-storage-path"
case SystemSettingOpenAIConfigName:
return "openAIConfig"
return "openai-config"
}
return ""
}
@ -94,7 +94,7 @@ type SystemSettingUpsert struct {
}
func (upsert SystemSettingUpsert) Validate() error {
if upsert.Name == SystemSettingServerID {
if upsert.Name == SystemSettingServerIDName {
return errors.New("update server id is not allowed")
} else if upsert.Name == SystemSettingAllowSignUpName {
value := false

View file

@ -35,7 +35,7 @@ func (s *Server) registerRSSRoutes(g *echo.Group) {
}
baseURL := c.Scheme() + "://" + c.Request().Host
rss, err := generateRSSFromMemoList(memoList, baseURL, &systemCustomizedProfile)
rss, err := generateRSSFromMemoList(memoList, baseURL, systemCustomizedProfile)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
}
@ -72,7 +72,7 @@ func (s *Server) registerRSSRoutes(g *echo.Group) {
baseURL := c.Scheme() + "://" + c.Request().Host
rss, err := generateRSSFromMemoList(memoList, baseURL, &systemCustomizedProfile)
rss, err := generateRSSFromMemoList(memoList, baseURL, systemCustomizedProfile)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
}
@ -114,57 +114,27 @@ func generateRSSFromMemoList(memoList []*api.Memo, baseURL string, profile *api.
return rss, nil
}
func getSystemCustomizedProfile(ctx context.Context, s *Server) (api.CustomizedProfile, error) {
systemStatus := api.SystemStatus{
CustomizedProfile: api.CustomizedProfile{
Name: "memos",
LogoURL: "",
Description: "",
Locale: "en",
Appearance: "system",
ExternalURL: "",
},
func getSystemCustomizedProfile(ctx context.Context, s *Server) (*api.CustomizedProfile, error) {
customizedProfile := &api.CustomizedProfile{
Name: "memos",
LogoURL: "",
Description: "",
Locale: "en",
Appearance: "system",
ExternalURL: "",
}
systemSettingList, err := s.Store.FindSystemSettingList(ctx, &api.SystemSettingFind{})
systemSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
Name: api.SystemSettingCustomizedProfileName,
})
if err != nil {
return api.CustomizedProfile{}, err
return customizedProfile, err
}
for _, systemSetting := range systemSettingList {
if systemSetting.Name == api.SystemSettingServerID || systemSetting.Name == api.SystemSettingSecretSessionName {
continue
}
var value any
err := json.Unmarshal([]byte(systemSetting.Value), &value)
if err != nil {
return api.CustomizedProfile{}, err
}
if systemSetting.Name == api.SystemSettingCustomizedProfileName {
valueMap := value.(map[string]any)
systemStatus.CustomizedProfile = api.CustomizedProfile{}
if v := valueMap["name"]; v != nil {
systemStatus.CustomizedProfile.Name = v.(string)
}
if v := valueMap["logoUrl"]; v != nil {
systemStatus.CustomizedProfile.LogoURL = v.(string)
}
if v := valueMap["description"]; v != nil {
systemStatus.CustomizedProfile.Description = v.(string)
}
if v := valueMap["locale"]; v != nil {
systemStatus.CustomizedProfile.Locale = v.(string)
}
if v := valueMap["appearance"]; v != nil {
systemStatus.CustomizedProfile.Appearance = v.(string)
}
if v := valueMap["externalUrl"]; v != nil {
systemStatus.CustomizedProfile.ExternalURL = v.(string)
}
}
err = json.Unmarshal([]byte(systemSetting.Value), customizedProfile)
if err != nil {
return customizedProfile, err
}
return systemStatus.CustomizedProfile, nil
return customizedProfile, nil
}
func min(a, b int) int {

View file

@ -60,7 +60,7 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting list").SetInternal(err)
}
for _, systemSetting := range systemSettingList {
if systemSetting.Name == api.SystemSettingServerID || systemSetting.Name == api.SystemSettingSecretSessionName || systemSetting.Name == api.SystemSettingOpenAIConfigName {
if systemSetting.Name == api.SystemSettingServerIDName || systemSetting.Name == api.SystemSettingSecretSessionName || systemSetting.Name == api.SystemSettingOpenAIConfigName {
continue
}
@ -194,14 +194,14 @@ func (s *Server) registerSystemRoutes(g *echo.Group) {
func (s *Server) getSystemServerID(ctx context.Context) (string, error) {
serverIDValue, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
Name: api.SystemSettingServerID,
Name: api.SystemSettingServerIDName,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
return "", err
}
if serverIDValue == nil || serverIDValue.Value == "" {
serverIDValue, err = s.Store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: api.SystemSettingServerID,
Name: api.SystemSettingServerIDName,
Value: uuid.NewString(),
})
if err != nil {

View file

@ -1,15 +1,6 @@
INSERT INTO
user_setting (user_id, key, value)
SELECT
user_id,
'memo-visibility',
value
FROM
user_setting
WHERE
key = 'memoVisibility';
DELETE FROM
UPDATE
user_setting
SET
key = 'memo-visibility'
WHERE
key = 'memoVisibility';

View file

@ -0,0 +1,69 @@
UPDATE
system_setting
SET
key = 'server-id'
WHERE
key = 'serverId';
UPDATE
system_setting
SET
key = 'secret-session'
WHERE
key = 'secretSessionName';
UPDATE
system_setting
SET
key = 'allow-signup'
WHERE
key = 'allowSignUp';
UPDATE
system_setting
SET
key = 'disable-public-memos'
WHERE
key = 'disablePublicMemos';
UPDATE
system_setting
SET
key = 'additional-style'
WHERE
key = 'additionalStyle';
UPDATE
system_setting
SET
key = 'additional-script'
WHERE
key = 'additionalScript';
UPDATE
system_setting
SET
key = 'customized-profile'
WHERE
key = 'customizedProfile';
UPDATE
system_setting
SET
key = 'storage-service-id'
WHERE
key = 'storageServiceId';
UPDATE
system_setting
SET
key = 'local-storage-path'
WHERE
key = 'localStoragePath';
UPDATE
system_setting
SET
key = 'openai-config'
WHERE
key = 'openAIConfig';

View file

@ -1,4 +1,4 @@
INSERT INTO
system_setting (`name`, `value`, `description`)
VALUES
('allowSignUp', 'true', '');
('allow-signup', 'true', '');

View file

@ -29,7 +29,7 @@ const StorageSection = () => {
const handleActiveStorageServiceChanged = async (storageId: StorageId) => {
await api.upsertSystemSetting({
name: "storageServiceId",
name: "storage-service-id",
value: JSON.stringify(storageId),
});
await globalStore.fetchSystemStatus();

View file

@ -57,7 +57,7 @@ const SystemSection = () => {
useEffect(() => {
api.getSystemSetting().then(({ data: { data: systemSettings } }) => {
const openAIConfigSetting = systemSettings.find((setting) => setting.name === "openAIConfig");
const openAIConfigSetting = systemSettings.find((setting) => setting.name === "openai-config");
if (openAIConfigSetting) {
setOpenAIConfig(JSON.parse(openAIConfigSetting.value));
}
@ -70,7 +70,7 @@ const SystemSection = () => {
allowSignUp: value,
});
await api.upsertSystemSetting({
name: "allowSignUp",
name: "allow-signup",
value: JSON.stringify(value),
});
};
@ -100,7 +100,7 @@ const SystemSection = () => {
const handleSaveOpenAIConfig = async () => {
try {
await api.upsertSystemSetting({
name: "openAIConfig",
name: "openai-config",
value: JSON.stringify(openAIConfig),
});
} catch (error) {
@ -127,7 +127,7 @@ const SystemSection = () => {
const handleSaveAdditionalStyle = async () => {
try {
await api.upsertSystemSetting({
name: "additionalStyle",
name: "additional-style",
value: JSON.stringify(state.additionalStyle),
});
} catch (error) {
@ -147,7 +147,7 @@ const SystemSection = () => {
const handleSaveAdditionalScript = async () => {
try {
await api.upsertSystemSetting({
name: "additionalScript",
name: "additional-script",
value: JSON.stringify(state.additionalScript),
});
} catch (error) {
@ -164,7 +164,7 @@ const SystemSection = () => {
});
globalStore.setSystemStatus({ disablePublicMemos: value });
await api.upsertSystemSetting({
name: "disablePublicMemos",
name: "disable-public-memos",
value: JSON.stringify(value),
});
};

View file

@ -83,7 +83,7 @@ const UpdateCustomizedProfileDialog: React.FC<Props> = ({ destroy }: Props) => {
try {
await api.upsertSystemSetting({
name: "customizedProfile",
name: "customized-profile",
value: JSON.stringify(state),
});
await globalStore.fetchSystemStatus();

View file

@ -23,7 +23,7 @@ const UpdateLocalStorageDialog: React.FC<Props> = (props: Props) => {
const handleConfirmBtnClick = async () => {
try {
await api.upsertSystemSetting({
name: "localStoragePath",
name: "local-storage-path",
value: JSON.stringify(path),
});
await globalStore.fetchSystemStatus();