workspace_service.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package v1
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "google.golang.org/grpc/codes"
  6. "google.golang.org/grpc/status"
  7. v1pb "github.com/usememos/memos/proto/gen/api/v1"
  8. "github.com/usememos/memos/store"
  9. )
  10. func (s *APIV1Service) GetWorkspaceProfile(ctx context.Context, _ *v1pb.GetWorkspaceProfileRequest) (*v1pb.WorkspaceProfile, error) {
  11. workspaceProfile := &v1pb.WorkspaceProfile{
  12. Version: s.Profile.Version,
  13. Mode: s.Profile.Mode,
  14. Public: s.Profile.Public,
  15. }
  16. owner, err := s.GetInstanceOwner(ctx)
  17. if err != nil {
  18. return nil, status.Errorf(codes.Internal, "failed to get instance owner: %v", err)
  19. }
  20. if owner != nil {
  21. workspaceProfile.Owner = owner.Name
  22. }
  23. return workspaceProfile, nil
  24. }
  25. var ownerCache *v1pb.User
  26. func (s *APIV1Service) GetInstanceOwner(ctx context.Context) (*v1pb.User, error) {
  27. if ownerCache != nil {
  28. return ownerCache, nil
  29. }
  30. hostUserType := store.RoleHost
  31. user, err := s.Store.GetUser(ctx, &store.FindUser{
  32. Role: &hostUserType,
  33. })
  34. if err != nil {
  35. return nil, errors.Wrapf(err, "failed to find owner")
  36. }
  37. if user == nil {
  38. return nil, nil
  39. }
  40. ownerCache = convertUserFromStore(user)
  41. return ownerCache, nil
  42. }