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

fix local details&relations injetion

introduce state.SetDetailAndBundledRelation
This commit is contained in:
requilence 2021-03-16 21:36:05 +04:00
parent 3492200b8e
commit e452e568e8
No known key found for this signature in database
GPG key ID: F07A7D55A2684852
9 changed files with 51 additions and 38 deletions

View file

@ -174,6 +174,9 @@ func (sb *smartBlock) Init(s source.Source, allowEmpty bool, objectType []string
func (sb *smartBlock) NormalizeRelations() error {
st := sb.NewState()
if sb.Type() == pb.SmartBlockType_Archive || sb.source.Virtual() {
return nil
}
relations := sb.Relations()
details := sb.Details()
@ -720,7 +723,7 @@ func (sb *smartBlock) SetObjectTypes(ctx *state.Context, objectTypes []string) (
ot := otypes[len(otypes)-1]
s := sb.NewState().SetObjectTypes(objectTypes)
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Float64(float64(ot.Layout)))
s.SetDetailAndBundledRelation(bundle.RelationKeyLayout, pbtypes.Float64(float64(ot.Layout)))
// send event here to send updated details to client
if err = sb.Apply(s); err != nil {
@ -1119,7 +1122,7 @@ func mergeAndSortRelations(objTypeRelations []*pbrelation.Relation, extraRelatio
}
func (sb *smartBlock) baseRelations() []*pbrelation.Relation {
rels := []*pbrelation.Relation{bundle.MustGetRelation(bundle.RelationKeyId), bundle.MustGetRelation(bundle.RelationKeyLayout), bundle.MustGetRelation(bundle.RelationKeyIconEmoji)}
rels := []*pbrelation.Relation{bundle.MustGetRelation(bundle.RelationKeyId), bundle.MustGetRelation(bundle.RelationKeyLayout), bundle.MustGetRelation(bundle.RelationKeyIconEmoji), bundle.MustGetRelation(bundle.RelationKeyName)}
for _, rel := range rels {
rel.Scope = pbrelation.Relation_object
}

View file

@ -624,6 +624,15 @@ func (s *State) SetDetails(d *types.Struct) *State {
return s
}
// SetDetailAndBundledRelation sets the detail value and bundled relation in case it is missing
func (s *State) SetDetailAndBundledRelation(key bundle.RelationKey, value *types.Value) {
s.SetDetail(key.String(), value)
// AddRelation adds only in case of missing relation
s.AddRelation(bundle.MustGetRelation(key))
return
}
func (s *State) SetDetail(key string, value *types.Value) {
if s.details == nil && s.parent != nil {
s.details = pbtypes.CopyStruct(s.parent.Details())
@ -653,6 +662,8 @@ func (s *State) SetExtraRelation(rel *pbrelation.Relation) {
}
}
// AddRelation adds new extraRelation to the state.
// In case the one is already exists with the same key it does nothing
func (s *State) AddRelation(relation *pbrelation.Relation) *State {
for _, rel := range s.ExtraRelations() {
if rel.Key == relation.Key {
@ -722,23 +733,14 @@ func (s *State) SetObjectType(objectType string) *State {
func (s *State) SetObjectTypes(objectTypes []string) *State {
s.objectTypes = objectTypes
s.SetDetail(bundle.RelationKeyType.String(), pbtypes.String(s.ObjectType()))
if pbtypes.GetRelation(s.ExtraRelations(), bundle.RelationKeyType.String()) == nil {
s.SetExtraRelation(bundle.MustGetRelation(bundle.RelationKeyType))
}
s.SetDetailAndBundledRelation(bundle.RelationKeyType, pbtypes.String(s.ObjectType()))
return s
}
func (s *State) InjectDerivedDetails() {
s.SetDetail(string(bundle.RelationKeyId), pbtypes.String(s.rootId))
s.SetDetail(string(bundle.RelationKeyType), pbtypes.String(s.ObjectType()))
if !pbtypes.HasRelation(s.ExtraRelations(), bundle.RelationKeyId.String()) {
s.SetExtraRelation(bundle.MustGetRelation(bundle.RelationKeyId))
}
if !pbtypes.HasRelation(s.ExtraRelations(), bundle.RelationKeyType.String()) {
s.SetExtraRelation(bundle.MustGetRelation(bundle.RelationKeyType))
}
s.SetDetailAndBundledRelation(bundle.RelationKeyId, pbtypes.String(s.rootId))
s.SetDetailAndBundledRelation(bundle.RelationKeyType, pbtypes.String(s.ObjectType()))
}
// ObjectScopedDetails contains only persistent details that are going to be saved in changes/snapshots

View file

@ -56,8 +56,8 @@ var WithObjectTypeLayoutMigration = func() StateTransformer {
return
}
s.SetDetail(bundle.RelationKeyRecommendedLayout.String(), pbtypes.Float64(layout))
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Float64(float64(relation.ObjectType_objectType)))
s.SetDetailAndBundledRelation(bundle.RelationKeyRecommendedLayout, pbtypes.Float64(layout))
s.SetDetailAndBundledRelation(bundle.RelationKeyLayout, pbtypes.Float64(float64(relation.ObjectType_objectType)))
}
}
@ -82,7 +82,7 @@ var WithObjectTypeRecommendedRelationsMigration = func(relations []*relation.Rel
}
}
s.SetDetail(bundle.RelationKeyRecommendedRelations.String(), pbtypes.StringList(keys))
s.SetDetailAndBundledRelation(bundle.RelationKeyRecommendedRelations, pbtypes.StringList(keys))
}
}
@ -99,7 +99,7 @@ var WithObjectTypesAndLayout = func(otypes []string) StateTransformer {
if err != nil {
continue
}
s.SetDetail(bundle.RelationKeyLayout.String(), pbtypes.Float64(float64(t.Layout)))
s.SetDetailAndBundledRelation(bundle.RelationKeyLayout, pbtypes.Float64(float64(t.Layout)))
s.SetExtraRelation(bundle.MustGetRelation(bundle.RelationKeyLayout))
}
}

View file

@ -97,7 +97,7 @@ func (v *files) ReadMeta(_ ChangeReceiver) (doc state.Doc, err error) {
}
s.SetDetails(d)
s.SetDetail("id", pbtypes.String(v.id))
s.SetDetail(bundle.RelationKeyId.String(), pbtypes.String(v.id))
s.SetObjectTypes(pbtypes.GetStringList(d, bundle.RelationKeyType.String()))
return s, nil
}

View file

@ -203,15 +203,8 @@ func (s *source) buildState() (doc state.Doc, err error) {
}
// set local-only details
if details, err := s.a.ObjectStore().GetDetails(s.id); err == nil {
if details != nil && details.Details != nil {
for key, v := range details.Details.Fields {
if slice.FindPos(bundle.LocalOnlyRelationsKeys, key) != -1 {
st.SetDetail(key, v)
}
}
}
}
InjectLocalDetails(s, st)
if s.Type() != pb.SmartBlockType_Archive && !s.Virtual() {
// we do not need details for archive or breadcrumbs
st.InjectDerivedDetails()
@ -264,13 +257,27 @@ func InjectCreationInfo(s Source, st *state.State) (err error) {
createdBy = fc.Account
}
st.SetDetail(bundle.RelationKeyCreatedDate.String(), pbtypes.Float64(float64(createdDate)))
st.SetDetailAndBundledRelation(bundle.RelationKeyCreatedDate, pbtypes.Float64(float64(createdDate)))
if profileId, e := threads.ProfileThreadIDFromAccountAddress(createdBy); e == nil {
st.SetDetail(bundle.RelationKeyCreator.String(), pbtypes.String(profileId.String()))
st.SetDetailAndBundledRelation(bundle.RelationKeyCreator, pbtypes.String(profileId.String()))
}
return
}
func InjectLocalDetails(s Source, st *state.State) {
if details, err := s.Anytype().ObjectStore().GetDetails(s.Id()); err == nil {
if details != nil && details.Details != nil {
for key, v := range details.Details.Fields {
if slice.FindPos(bundle.LocalOnlyRelationsKeys, key) != -1 {
st.SetDetail(key, v)
if !pbtypes.HasRelation(st.ExtraRelations(), key) {
st.SetExtraRelation(bundle.MustGetRelation(bundle.RelationKey(key)))
}
}
}
}
}
}
type PushChangeParams struct {
State *state.State
Changes []*pb.ChangeContent

View file

@ -141,7 +141,8 @@ func (d *doc) injectLocalRelations(st *state.State) {
if details != nil && details.Details != nil {
for key, v := range details.Details.Fields {
if slice.FindPos(bundle.LocalOnlyRelationsKeys, key) != -1 {
st.SetDetail(key, v)
// safe to call SetDetailAndBundledRelation as bundle.LocalOnlyRelationsKeys contains only bundled relations
st.SetDetailAndBundledRelation(bundle.RelationKey(key), v)
}
}
}
@ -209,9 +210,9 @@ func (d *doc) injectCreationInfo(st *state.State) (err error) {
return fmt.Errorf("failed to find first change to derive creation info")
}
st.SetDetail(bundle.RelationKeyCreatedDate.String(), pbtypes.Float64(float64(fc.Timestamp)))
st.SetDetailAndBundledRelation(bundle.RelationKeyCreatedDate, pbtypes.Float64(float64(fc.Timestamp)))
if profileId, e := threads.ProfileThreadIDFromAccountAddress(fc.Account); e == nil {
st.SetDetail(bundle.RelationKeyCreator.String(), pbtypes.String(profileId.String()))
st.SetDetailAndBundledRelation(bundle.RelationKeyCreator, pbtypes.String(profileId.String()))
}
return
}

2
go.mod
View file

@ -73,7 +73,7 @@ require (
replace github.com/JohannesKaufmann/html-to-markdown => github.com/anytypeio/html-to-markdown v0.0.0-20200617145221-2afd2a14bae1
replace github.com/textileio/go-threads => github.com/anytypeio/go-threads v1.0.2-0.20210310115034-a6122e977ff0
replace github.com/textileio/go-threads => github.com/anytypeio/go-threads v1.0.2-0.20210316173055-61330c7f1d6f
replace github.com/ipfs/go-log/v2 => github.com/anytypeio/go-log/v2 v2.1.2-0.20200810212702-264b187bb04f

4
go.sum
View file

@ -50,8 +50,8 @@ github.com/anytypeio/go-slip10 v0.0.0-20200330112030-a352ca8495e4 h1:jB5Ke7NVoW5
github.com/anytypeio/go-slip10 v0.0.0-20200330112030-a352ca8495e4/go.mod h1:/8GIEJBE5wmdgcE49JPdupnHNUf7bEn6C+aArfWqvw8=
github.com/anytypeio/go-slip21 v0.0.0-20200218204727-e2e51e20ab51 h1:3Y+18zBC8LZgcL3l2dgoTEIzIUzCZa/kN0UV3ZWpbuA=
github.com/anytypeio/go-slip21 v0.0.0-20200218204727-e2e51e20ab51/go.mod h1:SoKy+W8Mf6v7XBV30xFWkIFMs7UnXwsNGrGV12yVkEs=
github.com/anytypeio/go-threads v1.0.2-0.20210310115034-a6122e977ff0 h1:QsAN+OyWvEAtrqJPktqvvOfnA74EhnGRAG51UHRxUu4=
github.com/anytypeio/go-threads v1.0.2-0.20210310115034-a6122e977ff0/go.mod h1:WwGXFHSbOZHO/b49PSe2xUk/KKAs1W9M9pn7hKK2VQY=
github.com/anytypeio/go-threads v1.0.2-0.20210316173055-61330c7f1d6f h1:5XgbTu5I8r0f1xuRzfx3RU/pG9NB5bbpdXGdjjaU9+c=
github.com/anytypeio/go-threads v1.0.2-0.20210316173055-61330c7f1d6f/go.mod h1:WwGXFHSbOZHO/b49PSe2xUk/KKAs1W9M9pn7hKK2VQY=
github.com/anytypeio/html-to-markdown v0.0.0-20200617145221-2afd2a14bae1 h1:g/LEIEQ0ACBOKX9MhORhlmluUKvuxvrIDbGMI0cqF5A=
github.com/anytypeio/html-to-markdown v0.0.0-20200617145221-2afd2a14bae1/go.mod h1:Qnhxlb4mi8T2624UtHX8EgDyYZXWbjQfLGuciFHZ+Go=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=

View file

@ -35,7 +35,7 @@ var ErrNotFound = fmt.Errorf("not found")
func init() {
for _, r := range relations {
if r.DataSource != relation.Relation_details {
if r.DataSource == relation.Relation_account {
LocalOnlyRelationsKeys = append(LocalOnlyRelationsKeys, r.Key)
}
}