2024-04-28 00:44:29 +08:00
|
|
|
package v1
|
2023-08-05 21:30:23 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
v1pb "github.com/usememos/memos/proto/gen/api/v1"
|
2024-03-21 21:44:43 +08:00
|
|
|
"github.com/usememos/memos/store"
|
2023-08-05 21:30:23 +08:00
|
|
|
)
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func (s *APIV1Service) GetWorkspaceProfile(ctx context.Context, _ *v1pb.GetWorkspaceProfileRequest) (*v1pb.WorkspaceProfile, error) {
|
|
|
|
workspaceProfile := &v1pb.WorkspaceProfile{
|
2024-01-28 07:35:42 +08:00
|
|
|
Version: s.Profile.Version,
|
|
|
|
Mode: s.Profile.Mode,
|
2024-06-19 22:03:12 +08:00
|
|
|
Public: s.Profile.Public,
|
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
|
2024-07-24 23:34:19 +08:00
|
|
|
} else {
|
|
|
|
// If owner is not found, set public to true.
|
|
|
|
workspaceProfile.Public = true
|
2024-03-21 21:39:34 +08:00
|
|
|
}
|
2024-04-27 22:02:15 +08:00
|
|
|
return workspaceProfile, nil
|
2023-09-06 21:54:12 +08:00
|
|
|
}
|
2024-03-21 21:39:34 +08:00
|
|
|
|
2024-06-05 08:39:56 +08:00
|
|
|
var ownerCache *v1pb.User
|
|
|
|
|
2024-04-28 00:44:29 +08:00
|
|
|
func (s *APIV1Service) GetInstanceOwner(ctx context.Context) (*v1pb.User, error) {
|
2024-03-27 18:51:11 +08:00
|
|
|
if ownerCache != nil {
|
|
|
|
return ownerCache, nil
|
2024-03-21 21:39:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-03-27 18:51:11 +08:00
|
|
|
return nil, nil
|
2024-03-21 21:39:34 +08:00
|
|
|
}
|
|
|
|
|
2024-03-27 18:51:11 +08:00
|
|
|
ownerCache = convertUserFromStore(user)
|
|
|
|
return ownerCache, nil
|
2024-03-21 21:39:34 +08:00
|
|
|
}
|