mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update workspace setting service
This commit is contained in:
@@ -2,6 +2,10 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
storepb "github.com/usememos/memos/proto/gen/store"
|
||||
)
|
||||
|
||||
type WorkspaceSetting struct {
|
||||
@@ -25,14 +29,14 @@ func (s *Store) ListWorkspaceSettings(ctx context.Context, find *FindWorkspaceSe
|
||||
}
|
||||
|
||||
for _, systemSettingMessage := range list {
|
||||
s.systemSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
|
||||
s.workspaceSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetWorkspaceSetting(ctx context.Context, find *FindWorkspaceSetting) (*WorkspaceSetting, error) {
|
||||
if find.Name != "" {
|
||||
if cache, ok := s.systemSettingCache.Load(find.Name); ok {
|
||||
if cache, ok := s.workspaceSettingCache.Load(find.Name); ok {
|
||||
return cache.(*WorkspaceSetting), nil
|
||||
}
|
||||
}
|
||||
@@ -47,15 +51,51 @@ func (s *Store) GetWorkspaceSetting(ctx context.Context, find *FindWorkspaceSett
|
||||
}
|
||||
|
||||
systemSettingMessage := list[0]
|
||||
s.systemSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
|
||||
s.workspaceSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
|
||||
return systemSettingMessage, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetWorkspaceSettingWithDefaultValue(ctx context.Context, settingName string, defaultValue string) string {
|
||||
if setting, err := s.GetWorkspaceSetting(ctx, &FindWorkspaceSetting{
|
||||
Name: settingName,
|
||||
}); err == nil && setting != nil {
|
||||
return setting.Value
|
||||
}
|
||||
return defaultValue
|
||||
type FindWorkspaceSettingV1 struct {
|
||||
Key storepb.WorkspaceSettingKey
|
||||
}
|
||||
|
||||
func (s *Store) UpsertWorkspaceSettingV1(ctx context.Context, upsert *storepb.WorkspaceSetting) (*storepb.WorkspaceSetting, error) {
|
||||
workspaceSetting, err := s.driver.UpsertWorkspaceSettingV1(ctx, upsert)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Failed to upsert workspace setting")
|
||||
}
|
||||
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
||||
return workspaceSetting, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListWorkspaceSettingsV1(ctx context.Context, find *FindWorkspaceSettingV1) ([]*storepb.WorkspaceSetting, error) {
|
||||
list, err := s.driver.ListWorkspaceSettingsV1(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, workspaceSetting := range list {
|
||||
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *Store) GetWorkspaceSettingV1(ctx context.Context, find *FindWorkspaceSettingV1) (*storepb.WorkspaceSetting, error) {
|
||||
if find.Key != storepb.WorkspaceSettingKey_WORKSPACE_SETTING_KEY_UNSPECIFIED {
|
||||
if cache, ok := s.workspaceSettingV1Cache.Load(find.Key.String()); ok {
|
||||
return cache.(*storepb.WorkspaceSetting), nil
|
||||
}
|
||||
}
|
||||
|
||||
list, err := s.ListWorkspaceSettingsV1(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
workspaceSetting := list[0]
|
||||
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
||||
return workspaceSetting, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user