mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 09:35:00 +09:00
fix bundled templates in the bundled types
This commit is contained in:
parent
b30b32a1a1
commit
3185102549
4 changed files with 37 additions and 12 deletions
|
@ -6,10 +6,12 @@ import (
|
|||
"github.com/anytypeio/go-anytype-middleware/core/block/editor/template"
|
||||
"github.com/anytypeio/go-anytype-middleware/core/relation/relationutils"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/bundle"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/addr"
|
||||
"github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/pbtypes"
|
||||
"github.com/anytypeio/go-anytype-middleware/util/slice"
|
||||
"github.com/gogo/protobuf/types"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ObjectType struct {
|
||||
|
@ -47,10 +49,16 @@ func (p *ObjectType) Init(ctx *smartblock.InitContext) (err error) {
|
|||
},
|
||||
},
|
||||
}
|
||||
var templatesSource string
|
||||
if strings.HasPrefix(p.Id(), addr.BundledObjectTypeURLPrefix) {
|
||||
templatesSource = bundle.TypeKeyTemplate.BundledURL()
|
||||
} else {
|
||||
templatesSource = bundle.TypeKeyTemplate.URL()
|
||||
}
|
||||
|
||||
templatesDataview := model.BlockContentOfDataview{
|
||||
Dataview: &model.BlockContentDataview{
|
||||
Source: []string{bundle.TypeKeyTemplate.URL()},
|
||||
Source: []string{templatesSource},
|
||||
Views: []*model.BlockContentDataviewView{
|
||||
{
|
||||
Id: "_view2_1",
|
||||
|
|
|
@ -21,8 +21,22 @@ import (
|
|||
"github.com/mb0/diff"
|
||||
)
|
||||
|
||||
func NewDocFromSnapshot(rootId string, snapshot *pb.ChangeSnapshot) Doc {
|
||||
type snapshotOptions struct {
|
||||
doNotMigrateTypes bool
|
||||
}
|
||||
|
||||
type SnapshotOption func(*snapshotOptions)
|
||||
|
||||
func DoNotMigrateTypes(o *snapshotOptions) {
|
||||
o.doNotMigrateTypes = true
|
||||
}
|
||||
|
||||
func NewDocFromSnapshot(rootId string, snapshot *pb.ChangeSnapshot, opts ...SnapshotOption) Doc {
|
||||
var typesToMigrate []string
|
||||
sOpts := snapshotOptions{}
|
||||
for _, opt := range opts {
|
||||
opt(&sOpts)
|
||||
}
|
||||
blocks := make(map[string]simple.Block)
|
||||
for _, b := range snapshot.Data.Blocks {
|
||||
// migrate old dataview blocks with relations
|
||||
|
@ -30,8 +44,9 @@ func NewDocFromSnapshot(rootId string, snapshot *pb.ChangeSnapshot) Doc {
|
|||
if len(dvBlock.RelationLinks) == 0 {
|
||||
dvBlock.RelationLinks = relationutils.MigrateRelationsModels(dvBlock.Relations)
|
||||
}
|
||||
|
||||
dvBlock.Source, typesToMigrate = relationutils.MigrateObjectTypeIds(dvBlock.Source)
|
||||
if !sOpts.doNotMigrateTypes {
|
||||
dvBlock.Source, typesToMigrate = relationutils.MigrateObjectTypeIds(dvBlock.Source)
|
||||
}
|
||||
}
|
||||
blocks[b.Id] = simple.New(b)
|
||||
}
|
||||
|
@ -57,9 +72,10 @@ func NewDocFromSnapshot(rootId string, snapshot *pb.ChangeSnapshot) Doc {
|
|||
store: snapshot.Data.Collections,
|
||||
}
|
||||
|
||||
s.objectTypes, s.objectTypesToMigrate = relationutils.MigrateObjectTypeIds(s.objectTypes)
|
||||
|
||||
s.objectTypesToMigrate = append(s.objectTypesToMigrate, typesToMigrate...)
|
||||
if !sOpts.doNotMigrateTypes {
|
||||
s.objectTypes, s.objectTypesToMigrate = relationutils.MigrateObjectTypeIds(s.objectTypes)
|
||||
s.objectTypesToMigrate = append(s.objectTypesToMigrate, typesToMigrate...)
|
||||
}
|
||||
s.InjectDerivedDetails()
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -854,6 +854,7 @@ func (s *State) SetObjectType(objectType string) *State {
|
|||
|
||||
func (s *State) SetObjectTypes(objectTypes []string) *State {
|
||||
s.objectTypes = objectTypes
|
||||
// todo: we lost the second type here, so it becomes inconsistent with the objectTypes in the state
|
||||
s.SetDetailAndBundledRelation(bundle.RelationKeyType, pbtypes.String(s.ObjectType()))
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -93,7 +93,7 @@ func (b *builtinTemplate) registerBuiltin(rd io.ReadCloser) (err error) {
|
|||
if err = snapshot.Unmarshal(data); err != nil {
|
||||
return
|
||||
}
|
||||
st := state.NewDocFromSnapshot("", snapshot).(*state.State)
|
||||
st := state.NewDocFromSnapshot("", snapshot, state.DoNotMigrateTypes).(*state.State)
|
||||
id, err := threads.PatchSmartBlockType(st.RootId(), smartblock.SmartBlockTypeBundledTemplate)
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -104,9 +104,9 @@ func (b *builtinTemplate) registerBuiltin(rd io.ReadCloser) (err error) {
|
|||
st.RemoveDetail(bundle.RelationKeyCreator.String(), bundle.RelationKeyLastModifiedBy.String())
|
||||
st.SetLocalDetail(bundle.RelationKeyCreator.String(), pbtypes.String(addr.AnytypeProfileId))
|
||||
st.SetLocalDetail(bundle.RelationKeyLastModifiedBy.String(), pbtypes.String(addr.AnytypeProfileId))
|
||||
if ots := st.ObjectTypes(); len(ots) != 2 {
|
||||
st.SetObjectTypes([]string{bundle.TypeKeyTemplate.URL(), pbtypes.Get(st.Details(), bundle.RelationKeyTargetObjectType.String()).GetStringValue()})
|
||||
}
|
||||
st.SetLocalDetail(bundle.RelationKeyWorkspaceId.String(), pbtypes.String(addr.AnytypeMarketplaceWorkspace))
|
||||
st.SetObjectTypes([]string{bundle.TypeKeyTemplate.BundledURL(), pbtypes.Get(st.Details(), bundle.RelationKeyTargetObjectType.String()).GetStringValue()})
|
||||
|
||||
st.InjectDerivedDetails()
|
||||
|
||||
// fix divergence between extra relations and simple block relations
|
||||
|
@ -131,7 +131,7 @@ func (b *builtinTemplate) registerBuiltin(rd io.ReadCloser) (err error) {
|
|||
|
||||
func (b *builtinTemplate) validate(st *state.State) (err error) {
|
||||
cd := st.CombinedDetails()
|
||||
if st.ObjectType() != addr.ObjectTypeKeyToIdPrefix+bundle.TypeKeyTemplate.String() {
|
||||
if st.ObjectType() != bundle.TypeKeyTemplate.BundledURL() {
|
||||
return fmt.Errorf("bundled template validation: %s unexpected object type: %v", st.RootId(), st.ObjectType())
|
||||
}
|
||||
if !pbtypes.GetBool(cd, bundle.RelationKeyTemplateIsBundled.String()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue