2023-08-05 21:30:23 +08:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-06 21:54:12 +08:00
|
|
|
"strconv"
|
2023-08-05 21:30:23 +08:00
|
|
|
|
2023-09-17 22:55:13 +08:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2023-08-05 21:30:23 +08:00
|
|
|
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
|
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2023-10-27 09:07:35 +08:00
|
|
|
func (s *APIV2Service) GetSystemInfo(ctx context.Context, _ *apiv2pb.GetSystemInfoRequest) (*apiv2pb.GetSystemInfoResponse, error) {
|
2023-08-05 21:30:23 +08:00
|
|
|
defaultSystemInfo := &apiv2pb.SystemInfo{}
|
|
|
|
|
|
|
|
// Get the database size if the user is a host.
|
2023-09-14 20:16:17 +08:00
|
|
|
currentUser, err := getCurrentUser(ctx, s.Store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
|
|
|
|
}
|
|
|
|
if currentUser != nil && currentUser.Role == store.RoleHost {
|
2023-10-16 21:07:21 +08:00
|
|
|
size, err := s.Store.GetCurrentDBSize(ctx)
|
2023-08-05 21:30:23 +08:00
|
|
|
if err != nil {
|
2023-10-16 21:07:21 +08:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get db size: %v", err)
|
2023-08-05 21:30:23 +08:00
|
|
|
}
|
2023-10-16 21:07:21 +08:00
|
|
|
defaultSystemInfo.DbSize = size
|
2023-08-05 21:30:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
response := &apiv2pb.GetSystemInfoResponse{
|
|
|
|
SystemInfo: defaultSystemInfo,
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
2023-09-06 21:54:12 +08:00
|
|
|
|
2023-10-27 09:07:35 +08:00
|
|
|
func (s *APIV2Service) UpdateSystemInfo(ctx context.Context, request *apiv2pb.UpdateSystemInfoRequest) (*apiv2pb.UpdateSystemInfoResponse, error) {
|
2023-09-14 20:16:17 +08:00
|
|
|
user, err := getCurrentUser(ctx, s.Store)
|
2023-09-06 21:54:12 +08:00
|
|
|
if err != nil {
|
2023-09-14 20:16:17 +08:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
|
2023-09-06 21:54:12 +08:00
|
|
|
}
|
|
|
|
if user.Role != store.RoleHost {
|
|
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
|
|
}
|
2023-10-21 12:41:55 +08:00
|
|
|
if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
|
2023-09-06 21:54:12 +08:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update system settings.
|
2023-11-30 23:08:54 +08:00
|
|
|
for _, field := range request.UpdateMask.Paths {
|
|
|
|
if field == "allow_registration" {
|
2023-09-06 21:54:12 +08:00
|
|
|
_, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
|
|
|
|
Name: "allow-signup",
|
|
|
|
Value: strconv.FormatBool(request.SystemInfo.AllowRegistration),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to update allow_registration system setting: %v", err)
|
|
|
|
}
|
2023-11-30 23:08:54 +08:00
|
|
|
} else if field == "disable_password_login" {
|
2023-09-06 21:54:12 +08:00
|
|
|
_, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
|
|
|
|
Name: "disable-password-login",
|
|
|
|
Value: strconv.FormatBool(request.SystemInfo.DisablePasswordLogin),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to update disable_password_login system setting: %v", err)
|
|
|
|
}
|
2023-11-30 23:08:54 +08:00
|
|
|
} else if field == "additional_script" {
|
2023-09-06 21:54:12 +08:00
|
|
|
_, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
|
|
|
|
Name: "additional-script",
|
|
|
|
Value: request.SystemInfo.AdditionalScript,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to update additional_script system setting: %v", err)
|
|
|
|
}
|
2023-11-30 23:08:54 +08:00
|
|
|
} else if field == "additional_style" {
|
2023-09-06 21:54:12 +08:00
|
|
|
_, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
|
|
|
|
Name: "additional-style",
|
|
|
|
Value: request.SystemInfo.AdditionalStyle,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to update additional_style system setting: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
systemInfo, err := s.GetSystemInfo(ctx, &apiv2pb.GetSystemInfoRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get system info: %v", err)
|
|
|
|
}
|
|
|
|
return &apiv2pb.UpdateSystemInfoResponse{
|
|
|
|
SystemInfo: systemInfo.SystemInfo,
|
|
|
|
}, nil
|
|
|
|
}
|