Small fixes and refactoring.

This commit is contained in:
Jay
2026-03-03 17:30:13 -05:00
parent e43ed4af55
commit f638c8b72d
4 changed files with 25 additions and 25 deletions

View File

@@ -31,7 +31,7 @@ type Distance struct {
} }
func MarshalJSON(f HeartwoodFilter) ([]byte, error) { func MarshalJSON(f HeartwoodFilter) ([]byte, error) {
legacyFilter := f.Root rootFilter := f.Root
if f.Graph != nil { if f.Graph != nil {
graphArray, err := marshalGraphArray(f.Graph) graphArray, err := marshalGraphArray(f.Graph)
@@ -44,14 +44,14 @@ func MarshalJSON(f HeartwoodFilter) ([]byte, error) {
return nil, fmt.Errorf("error marshalling graph field: %w", err) return nil, fmt.Errorf("error marshalling graph field: %w", err)
} }
if legacyFilter.Extensions == nil { if rootFilter.Extensions == nil {
legacyFilter.Extensions = make(roots.FilterExtensions) rootFilter.Extensions = make(roots.FilterExtensions)
} }
legacyFilter.Extensions["graph"] = graphField rootFilter.Extensions["graph"] = graphField
} }
return roots.MarshalJSON(legacyFilter) return roots.MarshalJSON(rootFilter)
} }
func UnmarshalJSON(data []byte, f *HeartwoodFilter) error { func UnmarshalJSON(data []byte, f *HeartwoodFilter) error {

View File

@@ -59,7 +59,7 @@ func TestMarshalJSON(t *testing.T) {
expected: `{}`, expected: `{}`,
}, },
{ {
name: "legacy fields only", name: "root fields only",
filter: HeartwoodFilter{ filter: HeartwoodFilter{
Root: roots.Filter{ Root: roots.Filter{
IDs: []string{"abc"}, IDs: []string{"abc"},
@@ -86,7 +86,7 @@ func TestMarshalJSON(t *testing.T) {
expected: `{"graph":[{"kinds":[1]}]}`, expected: `{"graph":[{"kinds":[1]}]}`,
}, },
{ {
name: "legacy and graph present", name: "root and graph present",
filter: HeartwoodFilter{ filter: HeartwoodFilter{
Root: roots.Filter{ Root: roots.Filter{
IDs: []string{"abc"}, IDs: []string{"abc"},
@@ -125,7 +125,7 @@ func TestUnmarshalJSON(t *testing.T) {
expected: HeartwoodFilter{}, expected: HeartwoodFilter{},
}, },
{ {
name: "legacy fields only", name: "root fields only",
input: `{"ids":["abc"],"kinds":[1],"since":1000}`, input: `{"ids":["abc"],"kinds":[1],"since":1000}`,
expected: HeartwoodFilter{ expected: HeartwoodFilter{
Root: roots.Filter{ Root: roots.Filter{
@@ -152,7 +152,7 @@ func TestUnmarshalJSON(t *testing.T) {
}, },
}, },
{ {
name: "legacy and graph present", name: "root and graph present",
input: `{"ids":["abc"],"graph":[{"kinds":[1]}]}`, input: `{"ids":["abc"],"graph":[{"kinds":[1]}]}`,
expected: HeartwoodFilter{ expected: HeartwoodFilter{
Root: roots.Filter{ Root: roots.Filter{
@@ -164,7 +164,7 @@ func TestUnmarshalJSON(t *testing.T) {
}, },
}, },
{ {
name: "graph is removed from legacy extensions", name: "graph is removed from root extensions",
input: `{"ids":["abc"],"graph":[{"kinds":[1]}],"search":"bitcoin"}`, input: `{"ids":["abc"],"graph":[{"kinds":[1]}],"search":"bitcoin"}`,
expected: HeartwoodFilter{ expected: HeartwoodFilter{
Root: roots.Filter{ Root: roots.Filter{
@@ -187,7 +187,7 @@ func TestUnmarshalJSON(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
expectEqualHeartwoodFilters(t, result, tc.expected) expectEqualHeartwoodFilters(t, result, tc.expected)
// Ensure graph extension was popped from legacy filter // Ensure graph extension was popped from root filter
assert.Nil(t, result.Root.Extensions["graph"]) assert.Nil(t, result.Root.Extensions["graph"])
}) })
} }

View File

@@ -5,7 +5,7 @@ import (
"testing" "testing"
) )
func TestMatchKeys(t *testing.T) { func TestSimpleMatchKeys(t *testing.T) {
matchKeys := &SimpleMatchKeys{ matchKeys := &SimpleMatchKeys{
Keys: map[string][]string{ Keys: map[string][]string{
"User": {"pubkey"}, "User": {"pubkey"},
@@ -34,7 +34,7 @@ func TestMatchKeys(t *testing.T) {
}) })
} }
func TestMatchProps(t *testing.T) { func TestSimpleMatchProps(t *testing.T) {
matchKeys := &SimpleMatchKeys{ matchKeys := &SimpleMatchKeys{
Keys: map[string][]string{ Keys: map[string][]string{
"User": {"pubkey"}, "User": {"pubkey"},

View File

@@ -65,10 +65,10 @@ func EventToSubgraph(e roots.Event, p ExpanderPipeline) *EventSubgraph {
s := NewEventSubgraph() s := NewEventSubgraph()
// Create core entities // Create core entities
eventNode := newEventNode(e) eventNode := newEventNode(e.ID, e.CreatedAt, e.Kind, e.Content)
userNode := newUserNode(e) userNode := newUserNode(e.PubKey)
signedRel := newSignedRel(userNode, eventNode) signedRel := newSignedRel(userNode, eventNode)
tagNodes := newTagNodes(e) tagNodes := newTagNodes(e.Tags)
tagRels := newTagRels(eventNode, tagNodes) tagRels := newTagRels(eventNode, tagNodes)
// Populate subgraph // Populate subgraph
@@ -90,25 +90,25 @@ func EventToSubgraph(e roots.Event, p ExpanderPipeline) *EventSubgraph {
return s return s
} }
func newEventNode(e roots.Event) *graph.Node { func newEventNode(eventID string, createdAt int, kind int, content string) *graph.Node {
eventNode := graph.NewEventNode(e.ID) eventNode := graph.NewEventNode(eventID)
eventNode.Props["created_at"] = e.CreatedAt eventNode.Props["created_at"] = createdAt
eventNode.Props["kind"] = e.Kind eventNode.Props["kind"] = kind
eventNode.Props["content"] = e.Content eventNode.Props["content"] = content
return eventNode return eventNode
} }
func newUserNode(e roots.Event) *graph.Node { func newUserNode(pubkey string) *graph.Node {
return graph.NewUserNode(e.PubKey) return graph.NewUserNode(pubkey)
} }
func newSignedRel(user, event *graph.Node) *graph.Relationship { func newSignedRel(user, event *graph.Node) *graph.Relationship {
return graph.NewSignedRel(user, event, nil) return graph.NewSignedRel(user, event, nil)
} }
func newTagNodes(e roots.Event) []*graph.Node { func newTagNodes(tags []roots.Tag) []*graph.Node {
nodes := []*graph.Node{} nodes := []*graph.Node{}
for _, tag := range e.Tags { for _, tag := range tags {
if !isValidTag(tag) { if !isValidTag(tag) {
continue continue
} }