2024-04-28 00:44:29 +08:00
|
|
|
package v1
|
2024-02-20 23:02:01 +08:00
|
|
|
|
|
|
|
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"
|
2024-02-20 23:02:01 +08:00
|
|
|
storepb "github.com/usememos/memos/proto/gen/store"
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2024-05-12 13:32:26 +08:00
|
|
|
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 {
|
2024-05-12 13:32:26 +08:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid workspace setting name: %v", err)
|
2024-05-12 08:09:14 +08:00
|
|
|
}
|
|
|
|
|
2024-05-12 13:32:26 +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:
|
2024-05-12 13:32:26 +08:00
|
|
|
_, err = s.Store.GetWorkspaceBasicSetting(ctx)
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.WorkspaceSettingKey_GENERAL:
|
2024-05-12 13:32:26 +08:00
|
|
|
_, err = s.Store.GetWorkspaceGeneralSetting(ctx)
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.WorkspaceSettingKey_MEMO_RELATED:
|
2024-05-12 13:32:26 +08:00
|
|
|
_, err = s.Store.GetWorkspaceMemoRelatedSetting(ctx)
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.WorkspaceSettingKey_STORAGE:
|
2024-05-12 13:32:26 +08:00
|
|
|
_, 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{
|
2024-05-12 13:32:26 +08:00
|
|
|
Name: workspaceSettingKey.String(),
|
2024-02-20 23:02:01 +08:00
|
|
|
})
|
|
|
|
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-02-20 23:02:01 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func (s *APIV1Service) SetWorkspaceSetting(ctx context.Context, request *v1pb.SetWorkspaceSettingRequest) (*v1pb.WorkspaceSetting, error) {
|
2024-03-17 19:18:45 +08:00
|
|
|
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)
|
2024-02-20 23:02:01 +08:00
|
|
|
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 {
|
2024-02-20 23:02:01 +08:00
|
|
|
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-02-20 23:02:01 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func convertWorkspaceSettingFromStore(setting *storepb.WorkspaceSetting) *v1pb.WorkspaceSetting {
|
|
|
|
workspaceSetting := &v1pb.WorkspaceSetting{
|
2024-02-20 23:02:01 +08:00
|
|
|
Name: fmt.Sprintf("%s%s", WorkspaceSettingNamePrefix, setting.Key.String()),
|
2024-04-10 23:01:01 +08:00
|
|
|
}
|
|
|
|
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()),
|
2024-04-10 23:01:01 +08:00
|
|
|
}
|
|
|
|
case *storepb.WorkspaceSetting_StorageSetting:
|
2024-04-28 00:44:29 +08:00
|
|
|
workspaceSetting.Value = &v1pb.WorkspaceSetting_StorageSetting{
|
2024-04-10 23:01:01 +08:00
|
|
|
StorageSetting: convertWorkspaceStorageSettingFromStore(setting.GetStorageSetting()),
|
|
|
|
}
|
|
|
|
case *storepb.WorkspaceSetting_MemoRelatedSetting:
|
2024-04-28 00:44:29 +08:00
|
|
|
workspaceSetting.Value = &v1pb.WorkspaceSetting_MemoRelatedSetting{
|
2024-04-10 23:01:01 +08:00
|
|
|
MemoRelatedSetting: convertWorkspaceMemoRelatedSettingFromStore(setting.GetMemoRelatedSetting()),
|
|
|
|
}
|
2024-02-20 23:02:01 +08:00
|
|
|
}
|
2024-04-10 23:01:01 +08:00
|
|
|
return workspaceSetting
|
2024-02-20 23:02:01 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func convertWorkspaceSettingToStore(setting *v1pb.WorkspaceSetting) *storepb.WorkspaceSetting {
|
2024-02-20 23:02:01 +08:00
|
|
|
settingKeyString, _ := ExtractWorkspaceSettingKeyFromName(setting.Name)
|
2024-04-10 23:01:01 +08:00
|
|
|
workspaceSetting := &storepb.WorkspaceSetting{
|
2024-02-20 23:02:01 +08:00
|
|
|
Key: storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[settingKeyString]),
|
2024-04-10 20:05:17 +08:00
|
|
|
Value: &storepb.WorkspaceSetting_GeneralSetting{
|
|
|
|
GeneralSetting: convertWorkspaceGeneralSettingToStore(setting.GetGeneralSetting()),
|
2024-02-20 23:02:01 +08:00
|
|
|
},
|
|
|
|
}
|
2024-04-10 23:01:01 +08:00
|
|
|
switch workspaceSetting.Key {
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.WorkspaceSettingKey_GENERAL:
|
2024-04-10 23:01:01 +08:00
|
|
|
workspaceSetting.Value = &storepb.WorkspaceSetting_GeneralSetting{
|
|
|
|
GeneralSetting: convertWorkspaceGeneralSettingToStore(setting.GetGeneralSetting()),
|
|
|
|
}
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.WorkspaceSettingKey_STORAGE:
|
2024-04-10 23:01:01 +08:00
|
|
|
workspaceSetting.Value = &storepb.WorkspaceSetting_StorageSetting{
|
|
|
|
StorageSetting: convertWorkspaceStorageSettingToStore(setting.GetStorageSetting()),
|
|
|
|
}
|
2024-05-13 20:03:04 +08:00
|
|
|
case storepb.WorkspaceSettingKey_MEMO_RELATED:
|
2024-04-10 23:01:01 +08:00
|
|
|
workspaceSetting.Value = &storepb.WorkspaceSetting_MemoRelatedSetting{
|
|
|
|
MemoRelatedSetting: convertWorkspaceMemoRelatedSettingToStore(setting.GetMemoRelatedSetting()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return workspaceSetting
|
2024-02-20 23:02:01 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func convertWorkspaceGeneralSettingFromStore(setting *storepb.WorkspaceGeneralSetting) *v1pb.WorkspaceGeneralSetting {
|
2024-02-20 23:02:01 +08:00
|
|
|
if setting == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-28 00:44:29 +08:00
|
|
|
generalSetting := &v1pb.WorkspaceGeneralSetting{
|
2024-08-29 08:28:11 +08:00
|
|
|
DisallowUserRegistration: setting.DisallowUserRegistration,
|
|
|
|
DisallowPasswordAuth: setting.DisallowPasswordAuth,
|
|
|
|
AdditionalScript: setting.AdditionalScript,
|
|
|
|
AdditionalStyle: setting.AdditionalStyle,
|
|
|
|
WeekStartDayOffset: setting.WeekStartDayOffset,
|
2024-09-12 13:45:04 +08:00
|
|
|
DisallowChangeUsername: setting.DisallowChangeUsername,
|
|
|
|
DisallowChangeNickname: setting.DisallowChangeNickname,
|
2024-02-20 23:02:01 +08:00
|
|
|
}
|
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-02-20 23:02:01 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func convertWorkspaceGeneralSettingToStore(setting *v1pb.WorkspaceGeneralSetting) *storepb.WorkspaceGeneralSetting {
|
2024-02-20 23:02:01 +08:00
|
|
|
if setting == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-12 08:32:54 +08:00
|
|
|
generalSetting := &storepb.WorkspaceGeneralSetting{
|
2024-08-29 08:28:11 +08:00
|
|
|
DisallowUserRegistration: setting.DisallowUserRegistration,
|
|
|
|
DisallowPasswordAuth: setting.DisallowPasswordAuth,
|
|
|
|
AdditionalScript: setting.AdditionalScript,
|
|
|
|
AdditionalStyle: setting.AdditionalStyle,
|
|
|
|
WeekStartDayOffset: setting.WeekStartDayOffset,
|
2024-09-12 13:45:04 +08:00
|
|
|
DisallowChangeUsername: setting.DisallowChangeUsername,
|
|
|
|
DisallowChangeNickname: setting.DisallowChangeNickname,
|
2024-02-20 23:02:01 +08:00
|
|
|
}
|
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-02-20 23:02:01 +08:00
|
|
|
}
|
2024-04-10 23:01:01 +08:00
|
|
|
|
2024-04-28 21:36:22 +08:00
|
|
|
func convertWorkspaceStorageSettingFromStore(settingpb *storepb.WorkspaceStorageSetting) *v1pb.WorkspaceStorageSetting {
|
|
|
|
if settingpb == nil {
|
2024-04-10 23:01:01 +08:00
|
|
|
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-10 23:01:01 +08:00
|
|
|
}
|
2024-04-28 21:36:22 +08:00
|
|
|
return setting
|
2024-04-10 23:01:01 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func convertWorkspaceStorageSettingToStore(setting *v1pb.WorkspaceStorageSetting) *storepb.WorkspaceStorageSetting {
|
2024-04-10 23:01:01 +08:00
|
|
|
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-10 23:01:01 +08:00
|
|
|
}
|
2024-04-28 21:36:22 +08:00
|
|
|
return settingpb
|
2024-04-10 23:01:01 +08:00
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func convertWorkspaceMemoRelatedSettingFromStore(setting *storepb.WorkspaceMemoRelatedSetting) *v1pb.WorkspaceMemoRelatedSetting {
|
2024-04-10 23:01:01 +08:00
|
|
|
if setting == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-28 00:44:29 +08:00
|
|
|
return &v1pb.WorkspaceMemoRelatedSetting{
|
2024-07-14 20:23:19 +08:00
|
|
|
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-09-26 22:11:37 +08:00
|
|
|
EnableLocation: setting.EnableLocation,
|
2024-10-08 22:45:18 +08:00
|
|
|
DefaultVisibility: setting.DefaultVisibility,
|
2024-10-10 21:06:32 +08:00
|
|
|
Reactions: setting.Reactions,
|
2024-04-10 23:01:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func convertWorkspaceMemoRelatedSettingToStore(setting *v1pb.WorkspaceMemoRelatedSetting) *storepb.WorkspaceMemoRelatedSetting {
|
2024-04-10 23:01:01 +08:00
|
|
|
if setting == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &storepb.WorkspaceMemoRelatedSetting{
|
2024-07-14 20:23:19 +08:00
|
|
|
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-09-26 22:11:37 +08:00
|
|
|
EnableLocation: setting.EnableLocation,
|
2024-10-08 22:45:18 +08:00
|
|
|
DefaultVisibility: setting.DefaultVisibility,
|
2024-10-10 21:06:32 +08:00
|
|
|
Reactions: setting.Reactions,
|
2024-04-10 23:01:01 +08:00
|
|
|
}
|
|
|
|
}
|