mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 09:35:00 +09:00
Merge pull request #2264 from anyproto/go-5365-rename-joinspace-param-to-includespaceinfo-in
GO-5365: refactor publish
This commit is contained in:
commit
8b080ef2c8
2 changed files with 82 additions and 63 deletions
|
@ -124,7 +124,7 @@ func uniqName() string {
|
|||
return time.Now().Format("Anytype.WebPublish.20060102.150405.99")
|
||||
}
|
||||
|
||||
func (s *service) exportToDir(ctx context.Context, spaceId, pageId string) (dirEntries []fs.DirEntry, exportPath string, err error) {
|
||||
func (s *service) exportToDir(ctx context.Context, spaceId, pageId string, includeSpaceInfo bool) (dirEntries []fs.DirEntry, exportPath string, err error) {
|
||||
tempDir := os.TempDir()
|
||||
exportPath, _, err = s.exportService.Export(ctx, pb.RpcObjectListExportRequest{
|
||||
SpaceId: spaceId,
|
||||
|
@ -137,7 +137,7 @@ func (s *service) exportToDir(ctx context.Context, spaceId, pageId string) (dirE
|
|||
NoProgress: true,
|
||||
IncludeNested: true,
|
||||
IncludeBacklinks: true,
|
||||
IncludeSpace: true,
|
||||
IncludeSpace: includeSpaceInfo,
|
||||
LinksStateFilters: &pb.RpcObjectListExportStateFilters{
|
||||
RelationsWhiteList: relationsWhiteListToPbModel(),
|
||||
RemoveBlocks: true,
|
||||
|
@ -155,7 +155,12 @@ func (s *service) exportToDir(ctx context.Context, spaceId, pageId string) (dirE
|
|||
}
|
||||
|
||||
func (s *service) publishToPublishServer(ctx context.Context, spaceId, pageId, uri, globalName string, joinSpace bool) (err error) {
|
||||
dirEntries, exportPath, err := s.exportToDir(ctx, spaceId, pageId)
|
||||
spc, err := s.spaceService.Get(ctx, spaceId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
includeInviteLinkAndSpaceInfo := joinSpace && !spc.IsPersonal()
|
||||
dirEntries, exportPath, err := s.exportToDir(ctx, spaceId, pageId, includeInviteLinkAndSpaceInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -177,12 +182,7 @@ func (s *service) publishToPublishServer(ctx context.Context, spaceId, pageId, u
|
|||
return err
|
||||
}
|
||||
|
||||
spc, err := s.spaceService.Get(ctx, spaceId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.applyInviteLink(ctx, spc, &uberSnapshot, joinSpace)
|
||||
err = s.applyInviteLink(ctx, spaceId, &uberSnapshot, includeInviteLinkAndSpaceInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -210,12 +210,18 @@ func (s *service) publishToPublishServer(ctx context.Context, spaceId, pageId, u
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *service) applyInviteLink(ctx context.Context, spc clientspace.Space, snapshot *PublishingUberSnapshot, joinSpace bool) error {
|
||||
inviteLink, err := s.extractInviteLink(ctx, spc.Id(), joinSpace, spc.IsPersonal())
|
||||
func (s *service) applyInviteLink(ctx context.Context, spaceId string, snapshot *PublishingUberSnapshot, includeInviteLink bool) error {
|
||||
if !includeInviteLink {
|
||||
return nil
|
||||
}
|
||||
inviteInfo, err := s.inviteService.GetCurrent(ctx, spaceId)
|
||||
if err != nil && errors.Is(err, inviteservice.ErrInviteNotExists) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
snapshot.Meta.InviteLink = inviteLink
|
||||
snapshot.Meta.InviteLink = fmt.Sprintf(inviteLinkUrlTemplate, inviteInfo.InviteFileCid, inviteInfo.InviteFileKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -366,21 +372,6 @@ func (s *service) publishToServer(ctx context.Context, spaceId, pageId, uri, ver
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *service) extractInviteLink(ctx context.Context, spaceId string, joinSpace, isPersonal bool) (string, error) {
|
||||
var inviteLink string
|
||||
if joinSpace && !isPersonal {
|
||||
inviteInfo, err := s.inviteService.GetCurrent(ctx, spaceId)
|
||||
if err != nil && errors.Is(err, inviteservice.ErrInviteNotExists) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
inviteLink = fmt.Sprintf(inviteLinkUrlTemplate, inviteInfo.InviteFileCid, inviteInfo.InviteFileKey)
|
||||
}
|
||||
return inviteLink, nil
|
||||
}
|
||||
|
||||
func (s *service) evaluateDocumentVersion(ctx context.Context, spc clientspace.Space, pageId string, joinSpace bool) (string, error) {
|
||||
treeStorage, err := spc.Storage().TreeStorage(ctx, pageId)
|
||||
if err != nil {
|
||||
|
|
|
@ -139,7 +139,9 @@ func TestPublish(t *testing.T) {
|
|||
t.Run("success", func(t *testing.T) {
|
||||
// given
|
||||
isPersonal := true
|
||||
spaceService, err := prepareSpaceService(t, isPersonal)
|
||||
includeSpaceInfo := false
|
||||
|
||||
spaceService, err := prepareSpaceService(t, isPersonal, includeSpaceInfo)
|
||||
|
||||
objectTypeId := "customObjectType"
|
||||
expectedUri := "test"
|
||||
|
@ -159,7 +161,7 @@ func TestPublish(t *testing.T) {
|
|||
identityService := mock_identity.NewMockService(t)
|
||||
identityService.EXPECT().GetMyProfileDetails(context.Background()).Return("identity", nil, domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{}))
|
||||
|
||||
exp := prepareExporter(t, objectTypeId, spaceService)
|
||||
exp := prepareExporter(t, objectTypeId, spaceService, false)
|
||||
|
||||
svc := &service{
|
||||
spaceService: spaceService,
|
||||
|
@ -169,7 +171,7 @@ func TestPublish(t *testing.T) {
|
|||
}
|
||||
|
||||
// when
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, false)
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, includeSpaceInfo)
|
||||
|
||||
// then
|
||||
assert.NoError(t, err)
|
||||
|
@ -182,7 +184,9 @@ func TestPublish(t *testing.T) {
|
|||
t.Run("success with space sharing", func(t *testing.T) {
|
||||
// given
|
||||
isPersonal := false
|
||||
spaceService, err := prepareSpaceService(t, isPersonal)
|
||||
includeSpaceInfo := true
|
||||
|
||||
spaceService, err := prepareSpaceService(t, isPersonal, includeSpaceInfo)
|
||||
|
||||
objectTypeId := "customObjectType"
|
||||
expectedUri := "test"
|
||||
|
@ -206,7 +210,7 @@ func TestPublish(t *testing.T) {
|
|||
identityService := mock_identity.NewMockService(t)
|
||||
identityService.EXPECT().GetMyProfileDetails(context.Background()).Return("identity", nil, domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{}))
|
||||
|
||||
exp := prepareExporter(t, objectTypeId, spaceService)
|
||||
exp := prepareExporter(t, objectTypeId, spaceService, includeSpaceInfo)
|
||||
|
||||
inviteService := mock_inviteservice.NewMockInviteService(t)
|
||||
inviteService.EXPECT().GetCurrent(context.Background(), "spaceId").Return(domain.InviteInfo{
|
||||
|
@ -223,7 +227,7 @@ func TestPublish(t *testing.T) {
|
|||
}
|
||||
|
||||
// when
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, true)
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, includeSpaceInfo)
|
||||
|
||||
// then
|
||||
assert.NoError(t, err)
|
||||
|
@ -235,7 +239,9 @@ func TestPublish(t *testing.T) {
|
|||
})
|
||||
t.Run("success with space sharing - invite not exists", func(t *testing.T) {
|
||||
isPersonal := false
|
||||
spaceService, err := prepareSpaceService(t, isPersonal)
|
||||
includeSpaceInfo := true
|
||||
|
||||
spaceService, err := prepareSpaceService(t, isPersonal, includeSpaceInfo)
|
||||
|
||||
objectTypeId := "customObjectType"
|
||||
expectedUri := "test"
|
||||
|
@ -256,7 +262,7 @@ func TestPublish(t *testing.T) {
|
|||
identityService := mock_identity.NewMockService(t)
|
||||
identityService.EXPECT().GetMyProfileDetails(context.Background()).Return("identity", nil, domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{}))
|
||||
|
||||
exp := prepareExporter(t, objectTypeId, spaceService)
|
||||
exp := prepareExporter(t, objectTypeId, spaceService, includeSpaceInfo)
|
||||
|
||||
inviteService := mock_inviteservice.NewMockInviteService(t)
|
||||
inviteService.EXPECT().GetCurrent(context.Background(), "spaceId").Return(domain.InviteInfo{}, inviteservice.ErrInviteNotExists)
|
||||
|
@ -270,7 +276,7 @@ func TestPublish(t *testing.T) {
|
|||
}
|
||||
|
||||
// when
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, true)
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, includeSpaceInfo)
|
||||
|
||||
// then
|
||||
assert.NoError(t, err)
|
||||
|
@ -283,7 +289,8 @@ func TestPublish(t *testing.T) {
|
|||
t.Run("success for member", func(t *testing.T) {
|
||||
// given
|
||||
isPersonal := false
|
||||
spaceService, err := prepareSpaceService(t, isPersonal)
|
||||
includeSpaceInfo := true
|
||||
spaceService, err := prepareSpaceService(t, isPersonal, includeSpaceInfo)
|
||||
|
||||
objectTypeId := "customObjectType"
|
||||
expectedUri := "test"
|
||||
|
@ -311,7 +318,7 @@ func TestPublish(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
exp := prepareExporter(t, objectTypeId, spaceService)
|
||||
exp := prepareExporter(t, objectTypeId, spaceService, includeSpaceInfo)
|
||||
|
||||
inviteService := mock_inviteservice.NewMockInviteService(t)
|
||||
inviteService.EXPECT().GetCurrent(context.Background(), "spaceId").Return(domain.InviteInfo{
|
||||
|
@ -328,7 +335,7 @@ func TestPublish(t *testing.T) {
|
|||
}
|
||||
|
||||
// when
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, true)
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, includeSpaceInfo)
|
||||
|
||||
// then
|
||||
assert.NoError(t, err)
|
||||
|
@ -341,7 +348,9 @@ func TestPublish(t *testing.T) {
|
|||
t.Run("internal error", func(t *testing.T) {
|
||||
// given
|
||||
isPersonal := true
|
||||
spaceService, err := prepareSpaceService(t, isPersonal)
|
||||
includeSpaceInfo := true
|
||||
|
||||
spaceService, err := prepareSpaceService(t, isPersonal, includeSpaceInfo)
|
||||
|
||||
objectTypeId := "customObjectType"
|
||||
expectedUri := "test"
|
||||
|
@ -363,7 +372,7 @@ func TestPublish(t *testing.T) {
|
|||
expectedErr: fmt.Errorf("internal error"),
|
||||
}
|
||||
|
||||
exp := prepareExporter(t, objectTypeId, spaceService)
|
||||
exp := prepareExporter(t, objectTypeId, spaceService, false)
|
||||
|
||||
svc := &service{
|
||||
spaceService: spaceService,
|
||||
|
@ -373,7 +382,7 @@ func TestPublish(t *testing.T) {
|
|||
}
|
||||
|
||||
// when
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, true)
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, includeSpaceInfo)
|
||||
|
||||
// then
|
||||
assert.Error(t, err)
|
||||
|
@ -385,7 +394,14 @@ func TestPublish(t *testing.T) {
|
|||
})
|
||||
t.Run("limit error for members", func(t *testing.T) {
|
||||
// given
|
||||
isPersonal := false
|
||||
includeSpaceInfo := true
|
||||
|
||||
spaceService := mock_space.NewMockService(t)
|
||||
space := mock_clientspace.NewMockSpace(t)
|
||||
space.EXPECT().DerivedIDs().Return(threads.DerivedSmartblockIds{Workspace: workspaceId})
|
||||
space.EXPECT().IsPersonal().Return(isPersonal)
|
||||
spaceService.EXPECT().Get(context.Background(), spaceId).Return(space, nil)
|
||||
|
||||
expectedUri := "test"
|
||||
testFile := "test"
|
||||
|
@ -415,7 +431,7 @@ func TestPublish(t *testing.T) {
|
|||
}
|
||||
|
||||
// when
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, true)
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, includeSpaceInfo)
|
||||
|
||||
// then
|
||||
assert.Error(t, err)
|
||||
|
@ -424,7 +440,14 @@ func TestPublish(t *testing.T) {
|
|||
})
|
||||
t.Run("default limit error", func(t *testing.T) {
|
||||
// given
|
||||
isPersonal := false
|
||||
includeSpaceInfo := true
|
||||
|
||||
spaceService := mock_space.NewMockService(t)
|
||||
space := mock_clientspace.NewMockSpace(t)
|
||||
space.EXPECT().DerivedIDs().Return(threads.DerivedSmartblockIds{Workspace: workspaceId})
|
||||
space.EXPECT().IsPersonal().Return(isPersonal)
|
||||
spaceService.EXPECT().Get(context.Background(), spaceId).Return(space, nil)
|
||||
|
||||
expectedUri := "test"
|
||||
testFile := "test"
|
||||
|
@ -451,7 +474,7 @@ func TestPublish(t *testing.T) {
|
|||
}
|
||||
|
||||
// when
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, true)
|
||||
publish, err := svc.Publish(context.Background(), spaceId, objectId, expectedUri, includeSpaceInfo)
|
||||
|
||||
// then
|
||||
assert.Error(t, err)
|
||||
|
@ -666,24 +689,27 @@ func TestService_PublishingList(t *testing.T) {
|
|||
|
||||
var ctx = context.Background()
|
||||
|
||||
func prepareSpaceService(t *testing.T, isPersonal bool) (*mock_space.MockService, error) {
|
||||
func prepareSpaceService(t *testing.T, isPersonal bool, includeSpaceInfo bool) (*mock_space.MockService, error) {
|
||||
spaceService := mock_space.NewMockService(t)
|
||||
space := mock_clientspace.NewMockSpace(t)
|
||||
ctrl := gomock.NewController(t)
|
||||
space.EXPECT().IsPersonal().Return(isPersonal)
|
||||
space.EXPECT().Id().Return(spaceId)
|
||||
|
||||
st := mock_anystorage.NewMockClientSpaceStorage(t)
|
||||
mockSt := mock_objecttree.NewMockStorage(ctrl)
|
||||
st.EXPECT().TreeStorage(mock.Anything, mock.Anything).Return(mockSt, nil)
|
||||
mockSt.EXPECT().Heads(gomock.Any()).Return([]string{"heads"}, nil)
|
||||
space.EXPECT().Storage().Return(st)
|
||||
space.EXPECT().DerivedIDs().Return(threads.DerivedSmartblockIds{Workspace: workspaceId})
|
||||
if includeSpaceInfo && !isPersonal {
|
||||
space.EXPECT().DerivedIDs().Return(threads.DerivedSmartblockIds{Workspace: workspaceId})
|
||||
}
|
||||
if includeSpaceInfo {
|
||||
space.EXPECT().IsPersonal().Return(isPersonal)
|
||||
}
|
||||
spaceService.EXPECT().Get(context.Background(), spaceId).Return(space, nil)
|
||||
return spaceService, nil
|
||||
}
|
||||
|
||||
func prepareExporter(t *testing.T, objectTypeId string, spaceService *mock_space.MockService) export.Export {
|
||||
func prepareExporter(t *testing.T, objectTypeId string, spaceService *mock_space.MockService, includeSpaceInfo bool) export.Export {
|
||||
storeFixture := objectstore.NewStoreFixture(t)
|
||||
objectTypeUniqueKey, err := domain.NewUniqueKey(smartblock.SmartBlockTypeObjectType, objectTypeId)
|
||||
assert.Nil(t, err)
|
||||
|
@ -740,23 +766,25 @@ func prepareExporter(t *testing.T, objectTypeId string, spaceService *mock_space
|
|||
objectType.Doc = objectTypeDoc
|
||||
objectType.SetType(smartblock.SmartBlockTypeObjectType)
|
||||
|
||||
workspaceTest := smarttest.New(workspaceId)
|
||||
workspaceDoc := workspaceTest.NewState().SetDetails(domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{
|
||||
bundle.RelationKeyId: domain.String(workspaceId),
|
||||
bundle.RelationKeyType: domain.String(objectTypeId),
|
||||
}))
|
||||
workspaceDoc.AddRelationLinks(&model.RelationLink{
|
||||
Key: bundle.RelationKeyId.String(),
|
||||
Format: model.RelationFormat_longtext,
|
||||
}, &model.RelationLink{
|
||||
Key: bundle.RelationKeyType.String(),
|
||||
Format: model.RelationFormat_longtext,
|
||||
})
|
||||
workspaceTest.Doc = workspaceDoc
|
||||
if includeSpaceInfo {
|
||||
workspaceTest := smarttest.New(workspaceId)
|
||||
workspaceDoc := workspaceTest.NewState().SetDetails(domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{
|
||||
bundle.RelationKeyId: domain.String(workspaceId),
|
||||
bundle.RelationKeyType: domain.String(objectTypeId),
|
||||
}))
|
||||
workspaceDoc.AddRelationLinks(&model.RelationLink{
|
||||
Key: bundle.RelationKeyId.String(),
|
||||
Format: model.RelationFormat_longtext,
|
||||
}, &model.RelationLink{
|
||||
Key: bundle.RelationKeyType.String(),
|
||||
Format: model.RelationFormat_longtext,
|
||||
})
|
||||
workspaceTest.Doc = workspaceDoc
|
||||
objectGetter.EXPECT().GetObject(context.Background(), workspaceId).Return(workspaceTest, nil)
|
||||
}
|
||||
|
||||
objectGetter.EXPECT().GetObject(context.Background(), objectId).Return(smartBlockTest, nil)
|
||||
objectGetter.EXPECT().GetObject(context.Background(), objectTypeId).Return(objectType, nil)
|
||||
objectGetter.EXPECT().GetObject(context.Background(), workspaceId).Return(workspaceTest, nil)
|
||||
|
||||
a := &app.App{}
|
||||
mockSender := mock_event.NewMockSender(t)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue