Skip to content

Commit

Permalink
Merge pull request #138 from anyproto/go-1616-filter-not-actual-relat…
Browse files Browse the repository at this point in the history
…ions-in-the-graph-edges

filter-out some relations from the graph's edges
  • Loading branch information
requilence authored Jun 29, 2023
2 parents 3941a0d + a984852 commit 5e2e72a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
22 changes: 20 additions & 2 deletions core/block/object/objectgraph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/anyproto/any-sync/app"
"github.com/gogo/protobuf/types"
"github.com/opentracing/opentracing-go/log"
"github.com/samber/lo"

"github.com/anyproto/anytype-heart/core/relation"
"github.com/anyproto/anytype-heart/core/relation/relationutils"
Expand All @@ -18,6 +19,17 @@ import (
"github.com/anyproto/anytype-heart/util/pbtypes"
)

// relationsSkipList contains relations that SHOULD NOT be included in the graph. These relations of Object/File type that make no sense in the graph for user
var relationsSkipList = []bundle.RelationKey{
bundle.RelationKeyType,
bundle.RelationKeySetOf,
bundle.RelationKeyCreator,
bundle.RelationKeyLastModifiedBy,
bundle.RelationKeyWorkspaceId,
bundle.RelationKeyIconImage,
bundle.RelationKeyCoverId,
}

type Service interface {
ObjectGraph(req *pb.RpcObjectGraphRequest) ([]*types.Struct, []*pb.RpcObjectGraphEdge, error)
}
Expand Down Expand Up @@ -76,6 +88,10 @@ func (gr *Builder) ObjectGraph(req *pb.RpcObjectGraphRequest) ([]*types.Struct,
return nodes, edges, nil
}

func isRelationShouldBeIncludedAsEdge(rel *relationutils.Relation) bool {
return rel != nil && (rel.Format == model.RelationFormat_object || rel.Format == model.RelationFormat_file) && !lo.Contains(relationsSkipList, bundle.RelationKey(rel.Key))
}

func (gr *Builder) extractGraph(
records []database.Record,
nodes []*types.Struct,
Expand All @@ -92,9 +108,11 @@ func (gr *Builder) extractGraph(
outgoingRelationLink := make(map[string]struct{}, 10)
for k, v := range rec.Details.GetFields() {
rel := relations.GetByKey(k)
if rel != nil && (rel.Format == model.RelationFormat_object || rel.Format == model.RelationFormat_file) {
edges = appendRelations(v, existedNodes, rel, edges, id, outgoingRelationLink)
if !isRelationShouldBeIncludedAsEdge(rel) {
continue
}

edges = appendRelations(v, existedNodes, rel, edges, id, outgoingRelationLink)
}

edges = gr.appendLinks(rec, outgoingRelationLink, existedNodes, edges, id)
Expand Down
46 changes: 46 additions & 0 deletions core/block/object/objectgraph/graph_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package objectgraph

import (
"testing"

"github.com/anyproto/anytype-heart/core/relation/relationutils"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
)

func Test_isRelationShouldBeIncludedAsEdge(t *testing.T) {

tests := []struct {
name string
rel *relationutils.Relation
want bool
}{
{"creator",
&relationutils.Relation{bundle.MustGetRelation(bundle.RelationKeyCreator)},
false,
},
{"assignee",
&relationutils.Relation{bundle.MustGetRelation(bundle.RelationKeyAssignee)},
true,
},
{"cover",
&relationutils.Relation{bundle.MustGetRelation(bundle.RelationKeyCoverId)},
false,
},
{"file relation",
&relationutils.Relation{bundle.MustGetRelation(bundle.RelationKeyTrailer)},
true,
},
{"custom relation",
&relationutils.Relation{&model.Relation{Name: "custom", Format: model.RelationFormat_object}},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isRelationShouldBeIncludedAsEdge(tt.rel); got != tt.want {
t.Errorf("isRelationShouldBeIncludedAsEdge() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 5e2e72a

Please sign in to comment.