memos/server/router/api/v1/workspace_setting_service.go

247 lines
9.3 KiB
Go
Raw Normal View History

2024-04-28 00:44:29 +08:00
package v1
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2024-04-28 00:44:29 +08:00
v1pb "github.com/usememos/memos/proto/gen/api/v1"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
func (s *APIV1Service) GetWorkspaceSetting(ctx context.Context, request *v1pb.GetWorkspaceSettingRequest) (*v1pb.WorkspaceSetting, error) {
workspaceSettingKeyString, err := ExtractWorkspaceSettingKeyFromName(request.Name)
2024-05-12 08:09:14 +08:00
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid workspace setting name: %v", err)
2024-05-12 08:09:14 +08:00
}
workspaceSettingKey := storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[workspaceSettingKeyString])
// Get workspace setting from store with default value.
switch workspaceSettingKey {
2024-05-13 20:03:04 +08:00
case storepb.WorkspaceSettingKey_BASIC:
_, err = s.Store.GetWorkspaceBasicSetting(ctx)
2024-05-13 20:03:04 +08:00
case storepb.WorkspaceSettingKey_GENERAL:
_, err = s.Store.GetWorkspaceGeneralSetting(ctx)
2024-05-13 20:03:04 +08:00
case storepb.WorkspaceSettingKey_MEMO_RELATED:
_, err = s.Store.GetWorkspaceMemoRelatedSetting(ctx)
2024-05-13 20:03:04 +08:00
case storepb.WorkspaceSettingKey_STORAGE:
_, err = s.Store.GetWorkspaceStorageSetting(ctx)
default:
return nil, status.Errorf(codes.InvalidArgument, "unsupported workspace setting key: %v", workspaceSettingKey)
2024-05-12 08:09:14 +08:00
}
2024-04-12 08:32:54 +08:00
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
}
2024-04-17 08:56:52 +08:00
workspaceSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Name: workspaceSettingKey.String(),
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
}
if workspaceSetting == nil {
return nil, status.Errorf(codes.NotFound, "workspace setting not found")
}
2024-06-01 09:05:09 +08:00
// For storage setting, only host can get it.
if workspaceSetting.Key == storepb.WorkspaceSettingKey_STORAGE {
user, err := s.GetCurrentUser(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
}
if user == nil || user.Role != store.RoleHost {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
}
2024-04-27 23:14:58 +08:00
return convertWorkspaceSettingFromStore(workspaceSetting), nil
}
2024-04-28 00:44:29 +08:00
func (s *APIV1Service) SetWorkspaceSetting(ctx context.Context, request *v1pb.SetWorkspaceSettingRequest) (*v1pb.WorkspaceSetting, error) {
if s.Profile.Mode == "demo" {
return nil, status.Errorf(codes.InvalidArgument, "setting workspace setting is not allowed in demo mode")
}
2024-05-26 11:02:23 +08:00
user, err := s.GetCurrentUser(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
}
if user.Role != store.RoleHost {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
2024-04-27 23:14:58 +08:00
workspaceSetting, err := s.Store.UpsertWorkspaceSetting(ctx, convertWorkspaceSettingToStore(request.Setting))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to upsert workspace setting: %v", err)
}
2024-04-27 23:14:58 +08:00
return convertWorkspaceSettingFromStore(workspaceSetting), nil
}
2024-04-28 00:44:29 +08:00
func convertWorkspaceSettingFromStore(setting *storepb.WorkspaceSetting) *v1pb.WorkspaceSetting {
workspaceSetting := &v1pb.WorkspaceSetting{
Name: fmt.Sprintf("%s%s", WorkspaceSettingNamePrefix, setting.Key.String()),
}
switch setting.Value.(type) {
case *storepb.WorkspaceSetting_GeneralSetting:
2024-04-28 00:44:29 +08:00
workspaceSetting.Value = &v1pb.WorkspaceSetting_GeneralSetting{
2024-04-10 20:05:17 +08:00
GeneralSetting: convertWorkspaceGeneralSettingFromStore(setting.GetGeneralSetting()),
}
case *storepb.WorkspaceSetting_StorageSetting:
2024-04-28 00:44:29 +08:00
workspaceSetting.Value = &v1pb.WorkspaceSetting_StorageSetting{
StorageSetting: convertWorkspaceStorageSettingFromStore(setting.GetStorageSetting()),
}
case *storepb.WorkspaceSetting_MemoRelatedSetting:
2024-04-28 00:44:29 +08:00
workspaceSetting.Value = &v1pb.WorkspaceSetting_MemoRelatedSetting{
MemoRelatedSetting: convertWorkspaceMemoRelatedSettingFromStore(setting.GetMemoRelatedSetting()),
}
}
return workspaceSetting
}
2024-04-28 00:44:29 +08:00
func convertWorkspaceSettingToStore(setting *v1pb.WorkspaceSetting) *storepb.WorkspaceSetting {
settingKeyString, _ := ExtractWorkspaceSettingKeyFromName(setting.Name)
workspaceSetting := &storepb.WorkspaceSetting{
Key: storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[settingKeyString]),
2024-04-10 20:05:17 +08:00
Value: &storepb.WorkspaceSetting_GeneralSetting{
GeneralSetting: convertWorkspaceGeneralSettingToStore(setting.GetGeneralSetting()),
},
}
switch workspaceSetting.Key {
2024-05-13 20:03:04 +08:00
case storepb.WorkspaceSettingKey_GENERAL:
workspaceSetting.Value = &storepb.WorkspaceSetting_GeneralSetting{
GeneralSetting: convertWorkspaceGeneralSettingToStore(setting.GetGeneralSetting()),
}
2024-05-13 20:03:04 +08:00
case storepb.WorkspaceSettingKey_STORAGE:
workspaceSetting.Value = &storepb.WorkspaceSetting_StorageSetting{
StorageSetting: convertWorkspaceStorageSettingToStore(setting.GetStorageSetting()),
}
2024-05-13 20:03:04 +08:00
case storepb.WorkspaceSettingKey_MEMO_RELATED:
workspaceSetting.Value = &storepb.WorkspaceSetting_MemoRelatedSetting{
MemoRelatedSetting: convertWorkspaceMemoRelatedSettingToStore(setting.GetMemoRelatedSetting()),
}
}
return workspaceSetting
}
2024-04-28 00:44:29 +08:00
func convertWorkspaceGeneralSettingFromStore(setting *storepb.WorkspaceGeneralSetting) *v1pb.WorkspaceGeneralSetting {
if setting == nil {
return nil
}
2024-04-28 00:44:29 +08:00
generalSetting := &v1pb.WorkspaceGeneralSetting{
2024-08-28 23:46:06 +08:00
DisallowSignup: setting.DisallowSignup,
DisallowPasswordSignin: setting.DisallowPasswordSignin,
AdditionalScript: setting.AdditionalScript,
AdditionalStyle: setting.AdditionalStyle,
WeekStartDayOffset: setting.WeekStartDayOffset,
}
2024-04-12 08:32:54 +08:00
if setting.CustomProfile != nil {
2024-04-28 00:44:29 +08:00
generalSetting.CustomProfile = &v1pb.WorkspaceCustomProfile{
2024-04-12 08:32:54 +08:00
Title: setting.CustomProfile.Title,
Description: setting.CustomProfile.Description,
LogoUrl: setting.CustomProfile.LogoUrl,
Locale: setting.CustomProfile.Locale,
Appearance: setting.CustomProfile.Appearance,
}
}
return generalSetting
}
2024-04-28 00:44:29 +08:00
func convertWorkspaceGeneralSettingToStore(setting *v1pb.WorkspaceGeneralSetting) *storepb.WorkspaceGeneralSetting {
if setting == nil {
return nil
}
2024-04-12 08:32:54 +08:00
generalSetting := &storepb.WorkspaceGeneralSetting{
2024-08-28 23:46:06 +08:00
DisallowSignup: setting.DisallowSignup,
DisallowPasswordSignin: setting.DisallowPasswordSignin,
AdditionalScript: setting.AdditionalScript,
AdditionalStyle: setting.AdditionalStyle,
WeekStartDayOffset: setting.WeekStartDayOffset,
}
2024-04-12 08:32:54 +08:00
if setting.CustomProfile != nil {
generalSetting.CustomProfile = &storepb.WorkspaceCustomProfile{
Title: setting.CustomProfile.Title,
Description: setting.CustomProfile.Description,
LogoUrl: setting.CustomProfile.LogoUrl,
Locale: setting.CustomProfile.Locale,
Appearance: setting.CustomProfile.Appearance,
}
}
return generalSetting
}
2024-04-28 21:36:22 +08:00
func convertWorkspaceStorageSettingFromStore(settingpb *storepb.WorkspaceStorageSetting) *v1pb.WorkspaceStorageSetting {
if settingpb == nil {
return nil
}
2024-04-28 21:36:22 +08:00
setting := &v1pb.WorkspaceStorageSetting{
StorageType: v1pb.WorkspaceStorageSetting_StorageType(settingpb.StorageType),
FilepathTemplate: settingpb.FilepathTemplate,
UploadSizeLimitMb: settingpb.UploadSizeLimitMb,
}
if settingpb.S3Config != nil {
setting.S3Config = &v1pb.WorkspaceStorageSetting_S3Config{
AccessKeyId: settingpb.S3Config.AccessKeyId,
AccessKeySecret: settingpb.S3Config.AccessKeySecret,
Endpoint: settingpb.S3Config.Endpoint,
Region: settingpb.S3Config.Region,
Bucket: settingpb.S3Config.Bucket,
}
}
2024-04-28 21:36:22 +08:00
return setting
}
2024-04-28 00:44:29 +08:00
func convertWorkspaceStorageSettingToStore(setting *v1pb.WorkspaceStorageSetting) *storepb.WorkspaceStorageSetting {
if setting == nil {
return nil
}
2024-04-28 21:36:22 +08:00
settingpb := &storepb.WorkspaceStorageSetting{
StorageType: storepb.WorkspaceStorageSetting_StorageType(setting.StorageType),
FilepathTemplate: setting.FilepathTemplate,
UploadSizeLimitMb: setting.UploadSizeLimitMb,
}
if setting.S3Config != nil {
2024-05-17 08:50:02 +08:00
settingpb.S3Config = &storepb.StorageS3Config{
2024-04-28 21:36:22 +08:00
AccessKeyId: setting.S3Config.AccessKeyId,
AccessKeySecret: setting.S3Config.AccessKeySecret,
Endpoint: setting.S3Config.Endpoint,
Region: setting.S3Config.Region,
Bucket: setting.S3Config.Bucket,
}
}
2024-04-28 21:36:22 +08:00
return settingpb
}
2024-04-28 00:44:29 +08:00
func convertWorkspaceMemoRelatedSettingFromStore(setting *storepb.WorkspaceMemoRelatedSetting) *v1pb.WorkspaceMemoRelatedSetting {
if setting == nil {
return nil
}
2024-04-28 00:44:29 +08:00
return &v1pb.WorkspaceMemoRelatedSetting{
DisallowPublicVisibility: setting.DisallowPublicVisibility,
DisplayWithUpdateTime: setting.DisplayWithUpdateTime,
ContentLengthLimit: setting.ContentLengthLimit,
EnableAutoCompact: setting.EnableAutoCompact,
EnableDoubleClickEdit: setting.EnableDoubleClickEdit,
EnableLinkPreview: setting.EnableLinkPreview,
2024-08-19 09:13:44 +08:00
EnableComment: setting.EnableComment,
}
}
2024-04-28 00:44:29 +08:00
func convertWorkspaceMemoRelatedSettingToStore(setting *v1pb.WorkspaceMemoRelatedSetting) *storepb.WorkspaceMemoRelatedSetting {
if setting == nil {
return nil
}
return &storepb.WorkspaceMemoRelatedSetting{
DisallowPublicVisibility: setting.DisallowPublicVisibility,
DisplayWithUpdateTime: setting.DisplayWithUpdateTime,
ContentLengthLimit: setting.ContentLengthLimit,
EnableAutoCompact: setting.EnableAutoCompact,
EnableDoubleClickEdit: setting.EnableDoubleClickEdit,
EnableLinkPreview: setting.EnableLinkPreview,
2024-08-19 09:13:44 +08:00
EnableComment: setting.EnableComment,
}
}