mirror of
https://github.com/usememos/memos.git
synced 2025-04-15 01:52:19 +02:00
chore: tweak workspace setting store
This commit is contained in:
parent
af954db473
commit
4c47e93fce
@ -18,8 +18,8 @@ func (s *APIV2Service) GetWorkspaceSetting(ctx context.Context, request *apiv2pb
|
|||||||
return nil, status.Errorf(codes.InvalidArgument, "invalid workspace setting name: %v", err)
|
return nil, status.Errorf(codes.InvalidArgument, "invalid workspace setting name: %v", err)
|
||||||
}
|
}
|
||||||
settingKey := storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[settingKeyString])
|
settingKey := storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[settingKeyString])
|
||||||
workspaceSetting, err := s.Store.GetWorkspaceSettingV1(ctx, &store.FindWorkspaceSettingV1{
|
workspaceSetting, err := s.Store.GetWorkspaceSettingV1(ctx, &store.FindWorkspaceSetting{
|
||||||
Key: settingKey,
|
Name: settingKey.String(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
|
return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
|
||||||
|
@ -2,13 +2,8 @@ package mysql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"google.golang.org/protobuf/encoding/protojson"
|
|
||||||
|
|
||||||
storepb "github.com/usememos/memos/proto/gen/store"
|
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -68,93 +63,3 @@ func (d *DB) DeleteWorkspaceSetting(ctx context.Context, delete *store.DeleteWor
|
|||||||
_, err := d.db.ExecContext(ctx, stmt, delete.Name)
|
_, err := d.db.ExecContext(ctx, stmt, delete.Name)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DB) UpsertWorkspaceSettingV1(ctx context.Context, upsert *storepb.WorkspaceSetting) (*storepb.WorkspaceSetting, error) {
|
|
||||||
stmt := `
|
|
||||||
INSERT INTO system_setting (name, value, description)
|
|
||||||
VALUES (?, ?, '')
|
|
||||||
ON DUPLICATE KEY UPDATE value = ?`
|
|
||||||
var valueBytes []byte
|
|
||||||
var err error
|
|
||||||
if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetGeneralSetting())
|
|
||||||
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_STORAGE {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetStorageSetting())
|
|
||||||
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_MEMO_RELATED {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetMemoRelatedSetting())
|
|
||||||
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_TELEGRAM_INTEGRATION {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetTelegramIntegrationSetting())
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("unsupported workspace setting key: %v", upsert.Key)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed to marshal workspace setting value")
|
|
||||||
}
|
|
||||||
valueString := string(valueBytes)
|
|
||||||
if _, err := d.db.ExecContext(ctx, stmt, upsert.Key.String(), valueString, valueString); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return upsert, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DB) ListWorkspaceSettingsV1(ctx context.Context, find *store.FindWorkspaceSettingV1) ([]*storepb.WorkspaceSetting, error) {
|
|
||||||
where, args := []string{"1 = 1"}, []any{}
|
|
||||||
if find.Key != storepb.WorkspaceSettingKey_WORKSPACE_SETTING_KEY_UNSPECIFIED {
|
|
||||||
where, args = append(where, "name = ?"), append(args, find.Key.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
query := `SELECT name, value FROM system_setting WHERE ` + strings.Join(where, " AND ")
|
|
||||||
|
|
||||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
list := []*storepb.WorkspaceSetting{}
|
|
||||||
for rows.Next() {
|
|
||||||
workspaceSetting := &storepb.WorkspaceSetting{}
|
|
||||||
var keyString, valueString string
|
|
||||||
if err := rows.Scan(
|
|
||||||
&keyString,
|
|
||||||
&valueString,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Key = storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[keyString])
|
|
||||||
if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL {
|
|
||||||
generalSetting := &storepb.WorkspaceGeneralSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), generalSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_GeneralSetting{GeneralSetting: generalSetting}
|
|
||||||
} else if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_STORAGE {
|
|
||||||
storageSetting := &storepb.WorkspaceStorageSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), storageSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_StorageSetting{StorageSetting: storageSetting}
|
|
||||||
} else if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_MEMO_RELATED {
|
|
||||||
memoRelatedSetting := &storepb.WorkspaceMemoRelatedSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), memoRelatedSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_MemoRelatedSetting{MemoRelatedSetting: memoRelatedSetting}
|
|
||||||
} else if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_TELEGRAM_INTEGRATION {
|
|
||||||
telegramIntegrationSetting := &storepb.WorkspaceTelegramIntegrationSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), telegramIntegrationSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_TelegramIntegrationSetting{TelegramIntegrationSetting: telegramIntegrationSetting}
|
|
||||||
} else {
|
|
||||||
// Skip unknown workspace setting key.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
list = append(list, workspaceSetting)
|
|
||||||
}
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return list, nil
|
|
||||||
}
|
|
||||||
|
@ -2,13 +2,8 @@ package postgres
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"google.golang.org/protobuf/encoding/protojson"
|
|
||||||
|
|
||||||
storepb "github.com/usememos/memos/proto/gen/store"
|
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -75,96 +70,3 @@ func (d *DB) DeleteWorkspaceSetting(ctx context.Context, delete *store.DeleteWor
|
|||||||
_, err := d.db.ExecContext(ctx, stmt, delete.Name)
|
_, err := d.db.ExecContext(ctx, stmt, delete.Name)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DB) UpsertWorkspaceSettingV1(ctx context.Context, upsert *storepb.WorkspaceSetting) (*storepb.WorkspaceSetting, error) {
|
|
||||||
stmt := `
|
|
||||||
INSERT INTO system_setting (
|
|
||||||
name, value, description
|
|
||||||
)
|
|
||||||
VALUES ($1, $2, '')
|
|
||||||
ON CONFLICT(name) DO UPDATE
|
|
||||||
SET value = EXCLUDED.value`
|
|
||||||
var valueBytes []byte
|
|
||||||
var err error
|
|
||||||
if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetGeneralSetting())
|
|
||||||
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_STORAGE {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetStorageSetting())
|
|
||||||
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_MEMO_RELATED {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetMemoRelatedSetting())
|
|
||||||
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_TELEGRAM_INTEGRATION {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetTelegramIntegrationSetting())
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("unsupported workspace setting key: %v", upsert.Key)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed to marshal workspace setting value")
|
|
||||||
}
|
|
||||||
valueString := string(valueBytes)
|
|
||||||
if _, err := d.db.ExecContext(ctx, stmt, upsert.Key.String(), valueString); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return upsert, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DB) ListWorkspaceSettingsV1(ctx context.Context, find *store.FindWorkspaceSettingV1) ([]*storepb.WorkspaceSetting, error) {
|
|
||||||
where, args := []string{"1 = 1"}, []any{}
|
|
||||||
if find.Key != storepb.WorkspaceSettingKey_WORKSPACE_SETTING_KEY_UNSPECIFIED {
|
|
||||||
where, args = append(where, "name = "+placeholder(len(args)+1)), append(args, find.Key.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
query := `SELECT name, value FROM system_setting WHERE ` + strings.Join(where, " AND ")
|
|
||||||
|
|
||||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
list := []*storepb.WorkspaceSetting{}
|
|
||||||
for rows.Next() {
|
|
||||||
workspaceSetting := &storepb.WorkspaceSetting{}
|
|
||||||
var keyString, valueString string
|
|
||||||
if err := rows.Scan(
|
|
||||||
&keyString,
|
|
||||||
&valueString,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Key = storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[keyString])
|
|
||||||
if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL {
|
|
||||||
generalSetting := &storepb.WorkspaceGeneralSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), generalSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_GeneralSetting{GeneralSetting: generalSetting}
|
|
||||||
} else if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_STORAGE {
|
|
||||||
storageSetting := &storepb.WorkspaceStorageSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), storageSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_StorageSetting{StorageSetting: storageSetting}
|
|
||||||
} else if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_MEMO_RELATED {
|
|
||||||
memoRelatedSetting := &storepb.WorkspaceMemoRelatedSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), memoRelatedSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_MemoRelatedSetting{MemoRelatedSetting: memoRelatedSetting}
|
|
||||||
} else if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_TELEGRAM_INTEGRATION {
|
|
||||||
telegramIntegrationSetting := &storepb.WorkspaceTelegramIntegrationSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), telegramIntegrationSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_TelegramIntegrationSetting{TelegramIntegrationSetting: telegramIntegrationSetting}
|
|
||||||
} else {
|
|
||||||
// Skip unknown workspace setting key.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
list = append(list, workspaceSetting)
|
|
||||||
}
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return list, nil
|
|
||||||
}
|
|
||||||
|
@ -2,13 +2,8 @@ package sqlite
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"google.golang.org/protobuf/encoding/protojson"
|
|
||||||
|
|
||||||
storepb "github.com/usememos/memos/proto/gen/store"
|
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -75,92 +70,3 @@ func (d *DB) DeleteWorkspaceSetting(ctx context.Context, delete *store.DeleteWor
|
|||||||
_, err := d.db.ExecContext(ctx, stmt, delete.Name)
|
_, err := d.db.ExecContext(ctx, stmt, delete.Name)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DB) UpsertWorkspaceSettingV1(ctx context.Context, upsert *storepb.WorkspaceSetting) (*storepb.WorkspaceSetting, error) {
|
|
||||||
stmt := `
|
|
||||||
INSERT INTO system_setting (
|
|
||||||
name, value
|
|
||||||
)
|
|
||||||
VALUES (?, ?)
|
|
||||||
ON CONFLICT(name) DO UPDATE
|
|
||||||
SET value = EXCLUDED.value`
|
|
||||||
var valueBytes []byte
|
|
||||||
var err error
|
|
||||||
if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetGeneralSetting())
|
|
||||||
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_STORAGE {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetStorageSetting())
|
|
||||||
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_MEMO_RELATED {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetMemoRelatedSetting())
|
|
||||||
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_TELEGRAM_INTEGRATION {
|
|
||||||
valueBytes, err = protojson.Marshal(upsert.GetTelegramIntegrationSetting())
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("unsupported workspace setting key: %v", upsert.Key)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed to marshal workspace setting value")
|
|
||||||
}
|
|
||||||
valueString := string(valueBytes)
|
|
||||||
if _, err := d.db.ExecContext(ctx, stmt, upsert.Key.String(), valueString); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return upsert, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DB) ListWorkspaceSettingsV1(ctx context.Context, find *store.FindWorkspaceSettingV1) ([]*storepb.WorkspaceSetting, error) {
|
|
||||||
where, args := []string{"1 = 1"}, []any{}
|
|
||||||
if find.Key != storepb.WorkspaceSettingKey_WORKSPACE_SETTING_KEY_UNSPECIFIED {
|
|
||||||
where, args = append(where, "name = ?"), append(args, find.Key.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
query := `SELECT name, value FROM system_setting WHERE ` + strings.Join(where, " AND ")
|
|
||||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
list := []*storepb.WorkspaceSetting{}
|
|
||||||
for rows.Next() {
|
|
||||||
workspaceSetting := &storepb.WorkspaceSetting{}
|
|
||||||
var keyString, valueString string
|
|
||||||
if err := rows.Scan(&keyString, &valueString); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Key = storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[keyString])
|
|
||||||
if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL {
|
|
||||||
generalSetting := &storepb.WorkspaceGeneralSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), generalSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_GeneralSetting{GeneralSetting: generalSetting}
|
|
||||||
} else if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_STORAGE {
|
|
||||||
storageSetting := &storepb.WorkspaceStorageSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), storageSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_StorageSetting{StorageSetting: storageSetting}
|
|
||||||
} else if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_MEMO_RELATED {
|
|
||||||
memoRelatedSetting := &storepb.WorkspaceMemoRelatedSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), memoRelatedSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_MemoRelatedSetting{MemoRelatedSetting: memoRelatedSetting}
|
|
||||||
} else if workspaceSetting.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_TELEGRAM_INTEGRATION {
|
|
||||||
telegramIntegrationSetting := &storepb.WorkspaceTelegramIntegrationSetting{}
|
|
||||||
if err := protojson.Unmarshal([]byte(valueString), telegramIntegrationSetting); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
workspaceSetting.Value = &storepb.WorkspaceSetting_TelegramIntegrationSetting{TelegramIntegrationSetting: telegramIntegrationSetting}
|
|
||||||
} else {
|
|
||||||
// Skip unknown workspace setting key.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
list = append(list, workspaceSetting)
|
|
||||||
}
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return list, nil
|
|
||||||
}
|
|
||||||
|
@ -53,8 +53,6 @@ type Driver interface {
|
|||||||
UpsertWorkspaceSetting(ctx context.Context, upsert *WorkspaceSetting) (*WorkspaceSetting, error)
|
UpsertWorkspaceSetting(ctx context.Context, upsert *WorkspaceSetting) (*WorkspaceSetting, error)
|
||||||
ListWorkspaceSettings(ctx context.Context, find *FindWorkspaceSetting) ([]*WorkspaceSetting, error)
|
ListWorkspaceSettings(ctx context.Context, find *FindWorkspaceSetting) ([]*WorkspaceSetting, error)
|
||||||
DeleteWorkspaceSetting(ctx context.Context, delete *DeleteWorkspaceSetting) error
|
DeleteWorkspaceSetting(ctx context.Context, delete *DeleteWorkspaceSetting) error
|
||||||
UpsertWorkspaceSettingV1(ctx context.Context, upsert *storepb.WorkspaceSetting) (*storepb.WorkspaceSetting, error)
|
|
||||||
ListWorkspaceSettingsV1(ctx context.Context, find *FindWorkspaceSettingV1) ([]*storepb.WorkspaceSetting, error)
|
|
||||||
|
|
||||||
// User model related methods.
|
// User model related methods.
|
||||||
CreateUser(ctx context.Context, create *User) (*User, error)
|
CreateUser(ctx context.Context, create *User) (*User, error)
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
|
|
||||||
storepb "github.com/usememos/memos/proto/gen/store"
|
storepb "github.com/usememos/memos/proto/gen/store"
|
||||||
)
|
)
|
||||||
@ -68,36 +69,61 @@ func (s *Store) DeleteWorkspaceSetting(ctx context.Context, delete *DeleteWorksp
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type FindWorkspaceSettingV1 struct {
|
|
||||||
Key storepb.WorkspaceSettingKey
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) UpsertWorkspaceSettingV1(ctx context.Context, upsert *storepb.WorkspaceSetting) (*storepb.WorkspaceSetting, error) {
|
func (s *Store) UpsertWorkspaceSettingV1(ctx context.Context, upsert *storepb.WorkspaceSetting) (*storepb.WorkspaceSetting, error) {
|
||||||
workspaceSetting, err := s.driver.UpsertWorkspaceSettingV1(ctx, upsert)
|
workspaceSettingRaw := &WorkspaceSetting{
|
||||||
|
Name: upsert.Key.String(),
|
||||||
|
}
|
||||||
|
var valueBytes []byte
|
||||||
|
var err error
|
||||||
|
if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL {
|
||||||
|
valueBytes, err = protojson.Marshal(upsert.GetGeneralSetting())
|
||||||
|
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_STORAGE {
|
||||||
|
valueBytes, err = protojson.Marshal(upsert.GetStorageSetting())
|
||||||
|
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_MEMO_RELATED {
|
||||||
|
valueBytes, err = protojson.Marshal(upsert.GetMemoRelatedSetting())
|
||||||
|
} else if upsert.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_TELEGRAM_INTEGRATION {
|
||||||
|
valueBytes, err = protojson.Marshal(upsert.GetTelegramIntegrationSetting())
|
||||||
|
} else {
|
||||||
|
return nil, errors.Errorf("unsupported workspace setting key: %v", upsert.Key)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to marshal workspace setting value")
|
||||||
|
}
|
||||||
|
valueString := string(valueBytes)
|
||||||
|
workspaceSettingRaw.Value = valueString
|
||||||
|
workspaceSettingRaw, err = s.driver.UpsertWorkspaceSetting(ctx, workspaceSettingRaw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Failed to upsert workspace setting")
|
return nil, errors.Wrap(err, "Failed to upsert workspace setting")
|
||||||
}
|
}
|
||||||
|
workspaceSetting, err := convertWorkspaceSettingFromRaw(workspaceSettingRaw)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "Failed to convert workspace setting")
|
||||||
|
}
|
||||||
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
||||||
return workspaceSetting, nil
|
return workspaceSetting, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) ListWorkspaceSettingsV1(ctx context.Context, find *FindWorkspaceSettingV1) ([]*storepb.WorkspaceSetting, error) {
|
func (s *Store) ListWorkspaceSettingsV1(ctx context.Context, find *FindWorkspaceSetting) ([]*storepb.WorkspaceSetting, error) {
|
||||||
list, err := s.driver.ListWorkspaceSettingsV1(ctx, find)
|
list, err := s.driver.ListWorkspaceSettings(ctx, find)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, workspaceSetting := range list {
|
workspaceSettings := []*storepb.WorkspaceSetting{}
|
||||||
|
for _, workspaceSettingRaw := range list {
|
||||||
|
workspaceSetting, err := convertWorkspaceSettingFromRaw(workspaceSettingRaw)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "Failed to convert workspace setting")
|
||||||
|
}
|
||||||
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
||||||
|
workspaceSettings = append(workspaceSettings, workspaceSetting)
|
||||||
}
|
}
|
||||||
return list, nil
|
return workspaceSettings, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) GetWorkspaceSettingV1(ctx context.Context, find *FindWorkspaceSettingV1) (*storepb.WorkspaceSetting, error) {
|
func (s *Store) GetWorkspaceSettingV1(ctx context.Context, find *FindWorkspaceSetting) (*storepb.WorkspaceSetting, error) {
|
||||||
if find.Key != storepb.WorkspaceSettingKey_WORKSPACE_SETTING_KEY_UNSPECIFIED {
|
if cache, ok := s.workspaceSettingV1Cache.Load(find.Name); ok {
|
||||||
if cache, ok := s.workspaceSettingV1Cache.Load(find.Key.String()); ok {
|
return cache.(*storepb.WorkspaceSetting), nil
|
||||||
return cache.(*storepb.WorkspaceSetting), nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list, err := s.ListWorkspaceSettingsV1(ctx, find)
|
list, err := s.ListWorkspaceSettingsV1(ctx, find)
|
||||||
@ -107,18 +133,18 @@ func (s *Store) GetWorkspaceSettingV1(ctx context.Context, find *FindWorkspaceSe
|
|||||||
if len(list) == 0 {
|
if len(list) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
if len(list) > 1 {
|
||||||
workspaceSetting := list[0]
|
return nil, errors.Errorf("Found multiple workspace settings with key %s", find.Name)
|
||||||
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
}
|
||||||
return workspaceSetting, nil
|
return list[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) GetWorkspaceGeneralSetting(ctx context.Context) (*storepb.WorkspaceGeneralSetting, error) {
|
func (s *Store) GetWorkspaceGeneralSetting(ctx context.Context) (*storepb.WorkspaceGeneralSetting, error) {
|
||||||
workspaceSetting, err := s.GetWorkspaceSettingV1(ctx, &FindWorkspaceSettingV1{
|
workspaceSetting, err := s.GetWorkspaceSettingV1(ctx, &FindWorkspaceSetting{
|
||||||
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL,
|
Name: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL.String(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to get workspace setting")
|
return nil, errors.Wrap(err, "failed to get workspace general setting")
|
||||||
}
|
}
|
||||||
|
|
||||||
workspaceGeneralSetting := &storepb.WorkspaceGeneralSetting{}
|
workspaceGeneralSetting := &storepb.WorkspaceGeneralSetting{}
|
||||||
@ -127,3 +153,36 @@ func (s *Store) GetWorkspaceGeneralSetting(ctx context.Context) (*storepb.Worksp
|
|||||||
}
|
}
|
||||||
return workspaceGeneralSetting, nil
|
return workspaceGeneralSetting, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func convertWorkspaceSettingFromRaw(workspaceSettingRaw *WorkspaceSetting) (*storepb.WorkspaceSetting, error) {
|
||||||
|
workspaceSetting := &storepb.WorkspaceSetting{
|
||||||
|
Key: storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[workspaceSettingRaw.Name]),
|
||||||
|
}
|
||||||
|
switch workspaceSettingRaw.Name {
|
||||||
|
case storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL.String():
|
||||||
|
generalSetting := &storepb.WorkspaceGeneralSetting{}
|
||||||
|
if err := protojson.Unmarshal([]byte(workspaceSettingRaw.Value), generalSetting); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
workspaceSetting.Value = &storepb.WorkspaceSetting_GeneralSetting{GeneralSetting: generalSetting}
|
||||||
|
case storepb.WorkspaceSettingKey_WORKSPACE_SETTING_STORAGE.String():
|
||||||
|
storageSetting := &storepb.WorkspaceStorageSetting{}
|
||||||
|
if err := protojson.Unmarshal([]byte(workspaceSettingRaw.Value), storageSetting); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
workspaceSetting.Value = &storepb.WorkspaceSetting_StorageSetting{StorageSetting: storageSetting}
|
||||||
|
case storepb.WorkspaceSettingKey_WORKSPACE_SETTING_MEMO_RELATED.String():
|
||||||
|
memoRelatedSetting := &storepb.WorkspaceMemoRelatedSetting{}
|
||||||
|
if err := protojson.Unmarshal([]byte(workspaceSettingRaw.Value), memoRelatedSetting); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
workspaceSetting.Value = &storepb.WorkspaceSetting_MemoRelatedSetting{MemoRelatedSetting: memoRelatedSetting}
|
||||||
|
case storepb.WorkspaceSettingKey_WORKSPACE_SETTING_TELEGRAM_INTEGRATION.String():
|
||||||
|
telegramIntegrationSetting := &storepb.WorkspaceTelegramIntegrationSetting{}
|
||||||
|
if err := protojson.Unmarshal([]byte(workspaceSettingRaw.Value), telegramIntegrationSetting); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
workspaceSetting.Value = &storepb.WorkspaceSetting_TelegramIntegrationSetting{TelegramIntegrationSetting: telegramIntegrationSetting}
|
||||||
|
}
|
||||||
|
return workspaceSetting, nil
|
||||||
|
}
|
||||||
|
@ -22,7 +22,7 @@ func TestWorkspaceSettingV1Store(t *testing.T) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
list, err := ts.ListWorkspaceSettingsV1(ctx, &store.FindWorkspaceSettingV1{})
|
list, err := ts.ListWorkspaceSettingsV1(ctx, &store.FindWorkspaceSetting{})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, 1, len(list))
|
require.Equal(t, 1, len(list))
|
||||||
require.Equal(t, workspaceSetting, list[0])
|
require.Equal(t, workspaceSetting, list[0])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user