2023-08-05 21:30:23 +08:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-05 23:32:01 +08:00
|
|
|
|
2024-03-21 21:39:34 +08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2024-03-21 21:44:43 +08:00
|
|
|
|
|
|
|
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
|
|
|
|
"github.com/usememos/memos/store"
|
2023-08-05 21:30:23 +08:00
|
|
|
)
|
|
|
|
|
2024-03-21 21:42:44 +08:00
|
|
|
var owner *apiv2pb.User
|
2024-03-21 21:39:34 +08:00
|
|
|
|
|
|
|
func (s *APIV2Service) GetWorkspaceProfile(ctx context.Context, _ *apiv2pb.GetWorkspaceProfileRequest) (*apiv2pb.GetWorkspaceProfileResponse, error) {
|
2024-01-28 07:35:42 +08:00
|
|
|
workspaceProfile := &apiv2pb.WorkspaceProfile{
|
|
|
|
Version: s.Profile.Version,
|
|
|
|
Mode: s.Profile.Mode,
|
2023-09-14 20:16:17 +08:00
|
|
|
}
|
2024-03-21 21:39:34 +08:00
|
|
|
owner, err := s.GetInstanceOwner(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get instance owner: %v", err)
|
|
|
|
}
|
|
|
|
if owner != nil {
|
|
|
|
workspaceProfile.Owner = owner.Name
|
|
|
|
}
|
|
|
|
generalSetting, err := s.Store.GetWorkspaceGeneralSetting(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get workspace general setting: %v", err)
|
|
|
|
}
|
|
|
|
workspaceProfile.DisallowSignup = generalSetting.DisallowSignup
|
|
|
|
workspaceProfile.DisablePasswordLogin = generalSetting.DisallowPasswordLogin
|
|
|
|
workspaceProfile.AdditionalStyle = generalSetting.AdditionalStyle
|
|
|
|
workspaceProfile.AdditionalScript = generalSetting.AdditionalScript
|
2024-02-20 23:02:01 +08:00
|
|
|
return &apiv2pb.GetWorkspaceProfileResponse{
|
2024-01-28 07:35:42 +08:00
|
|
|
WorkspaceProfile: workspaceProfile,
|
2023-09-06 21:54:12 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2024-03-21 21:39:34 +08:00
|
|
|
|
|
|
|
func (s *APIV2Service) GetInstanceOwner(ctx context.Context) (*apiv2pb.User, error) {
|
|
|
|
if owner != nil {
|
|
|
|
return owner, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hostUserType := store.RoleHost
|
|
|
|
user, err := s.Store.GetUser(ctx, &store.FindUser{
|
|
|
|
Role: &hostUserType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to find owner")
|
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return nil, errors.New("owner not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
owner = convertUserFromStore(user)
|
|
|
|
return owner, nil
|
|
|
|
}
|