1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-10 18:10:49 +09:00

GO-4588 Take layout from local details

This commit is contained in:
kirillston 2025-01-07 17:18:52 +03:00
parent a39b82d0e8
commit 5eb4f3e34a
No known key found for this signature in database
GPG key ID: BE4BF014F0ECDFE8
15 changed files with 54 additions and 38 deletions

View file

@ -253,5 +253,5 @@ func copySubtreeOfBlocks(s *state.State, oldRootId string, oldBlocks []simple.Bl
func hasNoteLayout(s *state.State) bool {
//nolint:gosec
return model.ObjectTypeLayout(s.Details().GetInt64(bundle.RelationKeyResolvedLayout)) == model.ObjectType_note
return model.ObjectTypeLayout(s.LocalDetails().GetInt64(bundle.RelationKeyResolvedLayout)) == model.ObjectType_note
}

View file

@ -127,7 +127,7 @@ func (sf *sfile) SetFileTargetObjectId(ctx session.Context, blockId, targetObjec
}
var blockContentFileType model.BlockContentFileType
//nolint:gosec
switch model.ObjectTypeLayout(sb.Details().GetInt64(bundle.RelationKeyResolvedLayout)) {
switch model.ObjectTypeLayout(sb.LocalDetails().GetInt64(bundle.RelationKeyResolvedLayout)) {
case model.ObjectType_image:
blockContentFileType = model.BlockContentFile_Image
case model.ObjectType_audio:

View file

@ -17,6 +17,7 @@ import (
_ "github.com/anyproto/anytype-heart/core/block/simple/link"
_ "github.com/anyproto/anytype-heart/core/block/simple/text"
"github.com/anyproto/anytype-heart/core/block/source"
"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/core/event/mock_event"
"github.com/anyproto/anytype-heart/core/session"
"github.com/anyproto/anytype-heart/pb"

View file

@ -1684,7 +1684,7 @@ func (s *State) GetChangedStoreKeys(prefixPath ...string) (paths [][]string) {
}
func (s *State) Layout() (model.ObjectTypeLayout, bool) {
if det := s.Details(); det != nil {
if det := s.LocalDetails(); det != nil {
if det.Has(bundle.RelationKeyResolvedLayout) {
//nolint:gosec
return model.ObjectTypeLayout(det.GetInt64(bundle.RelationKeyResolvedLayout)), true

View file

@ -98,7 +98,7 @@ var WithObjectTypesAndLayout = func(otypes []domain.TypeKey, layout model.Object
otypes = s.ObjectTypeKeys()
}
if !s.Details().Has(bundle.RelationKeyResolvedLayout) {
if !s.LocalDetails().Has(bundle.RelationKeyResolvedLayout) {
s.SetDetailAndBundledRelation(bundle.RelationKeyResolvedLayout, domain.Int64(layout))
}
}

View file

@ -171,27 +171,40 @@ func (s *service) createObjectFromTemplate(
func (s *service) injectResolvedLayout(
ctx context.Context, space clientspace.Space, typeKey string, details *domain.Details,
) error {
) (err error) {
var layout int64
if bundle.IsInternalType(domain.TypeKey(typeKey)) {
ot := bundle.MustGetType(domain.TypeKey(typeKey))
layout = int64(ot.Layout)
} else {
layout, err = s.getLayoutFromType(ctx, space, typeKey)
if err != nil {
return err
}
}
details.Set(bundle.RelationKeyResolvedLayout, domain.Int64(layout))
// we should remove layout relation from details, if client accidentally add it in request
details.Delete(bundle.RelationKeyLayout)
return nil
}
func (s *service) getLayoutFromType(ctx context.Context, space clientspace.Space, typeKey string) (int64, error) {
typeObjectId, err := space.DeriveObjectID(ctx, domain.MustUniqueKey(smartblock.SmartBlockTypeObjectType, typeKey))
if err != nil {
return fmt.Errorf("failed to derive object type id: %w", err)
return 0, fmt.Errorf("failed to derive object type id: %w", err)
}
records, err := s.objectStore.SpaceIndex(space.Id()).QueryByIds([]string{typeObjectId})
if err != nil {
return fmt.Errorf("failed to query details of object type: %w", err)
return 0, fmt.Errorf("failed to query details of object type: %w", err)
}
if len(records) != 1 {
return fmt.Errorf("expected to get 1 record on querying object type details, got %d", len(records))
return 0, fmt.Errorf("expected to get 1 record on querying object type details, got %d", len(records))
}
layout := records[0].Details.GetInt64(bundle.RelationKeyRecommendedLayout)
details.Set(bundle.RelationKeyResolvedLayout, domain.Int64(layout))
// we should remove layout relation from details, if client accidentally add it in request
details.Delete(bundle.RelationKeyLayout)
return nil
return records[0].Details.GetInt64(bundle.RelationKeyRecommendedLayout), nil
}
// buildDateObject does not create real date object. It just builds date object details

View file

@ -131,7 +131,7 @@ func extractTargetDetails(originDetails *domain.Details, templateDetails *domain
templateVal := templateDetails.Get(key)
if templateVal.Ok() {
inTemplateEmpty := templateVal.IsEmpty()
if key == bundle.RelationKeyLayout {
if key == bundle.RelationKeyResolvedLayout {
// layout = 0 is actually basic layout, so it counts
inTemplateEmpty = false
}

View file

@ -217,7 +217,7 @@ func TestService_CreateTemplateStateWithDetails(t *testing.T) {
// then
assert.NoError(t, err)
assert.Equal(t, layout, model.ObjectTypeLayout(st.Details().GetInt64(bundle.RelationKeyResolvedLayout)))
assert.Equal(t, layout, model.ObjectTypeLayout(st.LocalDetails().GetInt64(bundle.RelationKeyResolvedLayout)))
assertLayoutBlocks(t, st, layout)
})
}

View file

@ -118,7 +118,7 @@ func (d *dot) Add(space smartblock.Space, st *state.State) error {
}
n.Set("type", string(st.ObjectTypeKey()))
layout := st.Details().GetInt64(bundle.RelationKeyResolvedLayout)
layout := st.LocalDetails().GetInt64(bundle.RelationKeyResolvedLayout)
n.Set("layout", fmt.Sprintf("%d", layout))
// TODO: add relations

View file

@ -84,7 +84,7 @@ func (g *graphjson) Add(space smartblock.Space, st *state.State) error {
IconEmoji: st.Details().GetString(bundle.RelationKeyIconEmoji),
Description: st.Details().GetString(bundle.RelationKeyDescription),
Type: st.ObjectTypeKey(),
Layout: int(st.Details().GetInt64(bundle.RelationKeyResolvedLayout)),
Layout: int(st.LocalDetails().GetInt64(bundle.RelationKeyResolvedLayout)),
}
g.nodes[st.RootId()] = &n

View file

@ -121,6 +121,7 @@ func (i *indexer) Close(ctx context.Context) (err error) {
}
func (i *indexer) RemoveAclIndexes(spaceId string) (err error) {
// TODO: It seems we should also filter objects by Layout, because participants should be re-indexed to receive resolvedLayout
ids, _, err := i.store.SpaceIndex(spaceId).QueryObjectIds(database.Query{
Filters: []database.FilterRequest{
{

View file

@ -256,6 +256,7 @@ func (i *indexer) removeOldFiles(spaceId string, flags reindexFlags) error {
return nil
}
store := i.store.SpaceIndex(spaceId)
// TODO: It seems we should also filter objects by Layout, because file objects should be re-indexed to receive resolvedLayout
ids, _, err := store.QueryObjectIds(database.Query{
Filters: []database.FilterRequest{
{

View file

@ -49,11 +49,11 @@ func Test_GrouperTags(t *testing.T) {
store := objectStore.SpaceIndex(spaceId)
require.NoError(t, store.UpdateObjectDetails(context.Background(), "rel-tag", domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{
"id": domain.String("rel-tag"),
"relationKey": domain.String("tag"),
"relationFormat": domain.Int64(int64(model.RelationFormat_tag)),
"type": domain.String(bundle.TypeKeyRelation.URL()),
"layout": domain.Int64(int64(model.ObjectType_relation)),
bundle.RelationKeyId: domain.String("rel-tag"),
bundle.RelationKeyRelationKey: domain.String("tag"),
bundle.RelationKeyRelationFormat: domain.Int64(int64(model.RelationFormat_tag)),
bundle.RelationKeyType: domain.String(bundle.TypeKeyRelation.URL()),
bundle.RelationKeyResolvedLayout: domain.Int64(int64(model.ObjectType_relation)),
})))
idTag1 := bson.NewObjectId().Hex()
@ -61,23 +61,23 @@ func Test_GrouperTags(t *testing.T) {
idTag3 := bson.NewObjectId().Hex()
require.NoError(t, store.UpdateObjectDetails(context.Background(), idTag1, domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{
"id": domain.String(idTag1),
"relationKey": domain.String("tag"),
"type": domain.String(bundle.TypeKeyRelationOption.URL()),
"layout": domain.Int64(int64(model.ObjectType_relationOption)),
bundle.RelationKeyId: domain.String(idTag1),
bundle.RelationKeyRelationKey: domain.String("tag"),
bundle.RelationKeyType: domain.String(bundle.TypeKeyRelationOption.URL()),
bundle.RelationKeyResolvedLayout: domain.Int64(int64(model.ObjectType_relationOption)),
})))
require.NoError(t, store.UpdateObjectDetails(context.Background(), idTag2, domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{
"id": domain.String(idTag2),
"relationKey": domain.String("tag"),
"type": domain.String(bundle.TypeKeyRelationOption.URL()),
"layout": domain.Int64(int64(model.ObjectType_relationOption)),
bundle.RelationKeyId: domain.String(idTag2),
bundle.RelationKeyRelationKey: domain.String("tag"),
bundle.RelationKeyType: domain.String(bundle.TypeKeyRelationOption.URL()),
bundle.RelationKeyResolvedLayout: domain.Int64(int64(model.ObjectType_relationOption)),
})))
require.NoError(t, store.UpdateObjectDetails(context.Background(), idTag3, domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{
"id": domain.String(idTag3),
"relationKey": domain.String("tag"),
"type": domain.String(bundle.TypeKeyRelationOption.URL()),
"layout": domain.Int64(int64(model.ObjectType_relationOption)),
bundle.RelationKeyId: domain.String(idTag3),
bundle.RelationKeyRelationKey: domain.String("tag"),
bundle.RelationKeyType: domain.String(bundle.TypeKeyRelationOption.URL()),
bundle.RelationKeyResolvedLayout: domain.Int64(int64(model.ObjectType_relationOption)),
})))
id1 := bson.NewObjectId().Hex()

View file

@ -226,7 +226,7 @@ func (u *syncStatusUpdater) setSyncDetails(sb smartblock.SmartBlock, status doma
return nil
}
st := sb.NewState()
if !u.isLayoutSuitableForSyncRelations(sb.Details()) {
if !u.isLayoutSuitableForSyncRelations(sb.LocalDetails()) {
return nil
}
if fileStatus, ok := st.Details().TryFloat64(bundle.RelationKeyFileBackupStatus); ok {

View file

@ -535,7 +535,7 @@ func TestQuery(t *testing.T) {
Filters: []database.FilterRequest{
{
Operator: 0,
RelationKey: "layout",
RelationKey: bundle.RelationKeyResolvedLayout,
Condition: model.BlockContentDataviewFilter_NotIn,
Value: domain.Int64List([]int64{int64(model.ObjectType_relationOption)}),
},
@ -593,7 +593,7 @@ func TestQuery(t *testing.T) {
Filters: []database.FilterRequest{
{
Operator: 0,
RelationKey: "layout",
RelationKey: bundle.RelationKeyResolvedLayout,
Condition: model.BlockContentDataviewFilter_NotIn,
Value: domain.Int64List([]int64{int64(model.ObjectType_objectType)}),
},