mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-10 18:10:49 +09:00
GO-3645: add link block to BlockListConvertToObject request
Signed-off-by: AnastasiaShemyakinskaya <shem98a@mail.ru>
This commit is contained in:
parent
c60fe78403
commit
466469d20d
5 changed files with 1227 additions and 1120 deletions
|
@ -2,6 +2,7 @@ package basic
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/globalsign/mgo/bson"
|
||||
|
@ -62,7 +63,7 @@ func (bs *basic) ExtractBlocksToObjects(
|
|||
return nil, fmt.Errorf("create child object: %w", err)
|
||||
}
|
||||
|
||||
linkID, err := bs.changeToBlockWithLink(newState, rootBlock, objectID)
|
||||
linkID, err := bs.changeToBlockWithLink(newState, rootBlock, objectID, req.Block)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create link to object %s: %w", objectID, err)
|
||||
}
|
||||
|
@ -117,17 +118,27 @@ func insertBlocksToState(
|
|||
objState.Set(simple.New(rootB))
|
||||
}
|
||||
|
||||
func (bs *basic) changeToBlockWithLink(newState *state.State, blockToChange simple.Block, objectID string) (string, error) {
|
||||
return bs.CreateBlock(newState, pb.RpcBlockCreateRequest{
|
||||
TargetId: blockToChange.Model().Id,
|
||||
Block: &model.Block{
|
||||
func (bs *basic) changeToBlockWithLink(newState *state.State, blockToChange simple.Block, objectID string, block *model.Block) (string, error) {
|
||||
if block == nil {
|
||||
block = &model.Block{
|
||||
Content: &model.BlockContentOfLink{
|
||||
Link: &model.BlockContentLink{
|
||||
TargetBlockId: objectID,
|
||||
Style: model.BlockContentLink_Page,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
} else {
|
||||
link := block.GetLink()
|
||||
if link == nil {
|
||||
return "", errors.New("block content is not a link")
|
||||
} else {
|
||||
link.TargetBlockId = objectID
|
||||
}
|
||||
}
|
||||
return bs.CreateBlock(newState, pb.RpcBlockCreateRequest{
|
||||
TargetId: blockToChange.Model().Id,
|
||||
Block: block,
|
||||
Position: model.Block_Replace,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -323,6 +323,39 @@ func TestExtractObjects(t *testing.T) {
|
|||
|
||||
assert.Contains(t, fields, bundle.RelationKeyName.String())
|
||||
})
|
||||
t.Run("add custom link block", func(t *testing.T) {
|
||||
fixture := newFixture(t)
|
||||
defer fixture.cleanUp()
|
||||
creator := testCreator{objects: map[string]*smarttest.SmartTest{}}
|
||||
sb := makeTestObject()
|
||||
creator.Add(sb)
|
||||
|
||||
ts := testTemplateService{templates: map[string]*state.State{}}
|
||||
tmpl := makeTemplateState()
|
||||
ts.AddTemplate("template", tmpl)
|
||||
|
||||
req := pb.RpcBlockListConvertToObjectsRequest{
|
||||
ContextId: "test",
|
||||
BlockIds: []string{"1"},
|
||||
ObjectTypeUniqueKey: domain.MustUniqueKey(coresb.SmartBlockTypeObjectType, bundle.TypeKeyNote.String()).Marshal(),
|
||||
Block: &model.Block{Id: "newId", Content: &model.BlockContentOfLink{
|
||||
Link: &model.BlockContentLink{
|
||||
CardStyle: model.BlockContentLink_Card,
|
||||
},
|
||||
}},
|
||||
}
|
||||
ctx := session.NewContext()
|
||||
_, err := NewBasic(sb, fixture.store, converter.NewLayoutConverter()).ExtractBlocksToObjects(ctx, creator, ts, req)
|
||||
assert.NoError(t, err)
|
||||
var block *model.Block
|
||||
for _, block = range sb.Blocks() {
|
||||
if block.GetLink() != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.NotNil(t, block)
|
||||
assert.Equal(t, block.GetLink().GetCardStyle(), model.BlockContentLink_Card)
|
||||
})
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
|
|
|
@ -3939,6 +3939,7 @@ common simple block command
|
|||
| blockIds | [string](#string) | repeated | |
|
||||
| objectTypeUniqueKey | [string](#string) | | |
|
||||
| templateId | [string](#string) | | |
|
||||
| block | [model.Block](#anytype-model-Block) | | |
|
||||
|
||||
|
||||
|
||||
|
|
2289
pb/commands.pb.go
2289
pb/commands.pb.go
File diff suppressed because it is too large
Load diff
|
@ -4118,6 +4118,7 @@ message Rpc {
|
|||
repeated string blockIds = 2;
|
||||
string objectTypeUniqueKey = 3;
|
||||
string templateId = 4;
|
||||
anytype.model.Block block = 5;
|
||||
}
|
||||
|
||||
message Response {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue