Skip to content

Commit 7dda769

Browse files
committed
give more access to groups db for normal queries.
1 parent a733d3e commit 7dda769

2 files changed

Lines changed: 46 additions & 28 deletions

File tree

core.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,17 @@ func virtualInviteValidationEvent(inviter nostr.PubKey) nostr.Event {
448448

449449
// splits the query between the main relay and the groups relay
450450
func queryStored(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] {
451-
if len(filter.Kinds) == 0 {
451+
if filter.IDs != nil {
452+
// query both normal and groups
453+
return eventstore.SortedMerge(
454+
queryNormal(ctx, filter),
455+
groups.State.Query(ctx, filter),
456+
)
457+
}
458+
459+
if filter.Kinds == nil {
452460
// only normal kinds or no kinds specified
453-
return queryMain(ctx, filter)
461+
return queryNormal(ctx, filter)
454462
}
455463

456464
if len(filter.Tags["h"]) > 0 {
@@ -459,27 +467,50 @@ func queryStored(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event]
459467

460468
groupsFilter := filter
461469
groupsFilter.Kinds = nil
462-
mainFilter := filter
463-
mainFilter.Kinds = nil
470+
normalFilter := filter
471+
normalFilter.Kinds = nil
464472
for _, kind := range filter.Kinds {
465473
if slices.Contains(nip29.MetadataEventKinds, kind) {
466474
groupsFilter.Kinds = append(groupsFilter.Kinds, kind)
467475
} else {
468-
mainFilter.Kinds = append(mainFilter.Kinds, kind)
476+
normalFilter.Kinds = append(normalFilter.Kinds, kind)
469477
}
470478
}
471479

472-
if len(groupsFilter.Kinds) > 0 && len(mainFilter.Kinds) > 0 {
480+
if groupsFilter.Kinds != nil && normalFilter.Kinds != nil {
473481
// mixed kinds - need to split the filter and query both
474482
return eventstore.SortedMerge(
475-
queryMain(ctx, mainFilter),
483+
queryNormal(ctx, normalFilter),
476484
groups.State.Query(ctx, groupsFilter),
477485
)
478-
} else if len(groupsFilter.Kinds) > 0 && len(mainFilter.Kinds) == 0 {
486+
} else if groupsFilter.Kinds != nil && normalFilter.Kinds == nil {
479487
// only groups kinds requested
480488
return groups.State.Query(ctx, filter)
481489
} else {
482490
// only normal kinds requested
483-
return queryMain(ctx, filter)
491+
return queryNormal(ctx, filter)
492+
}
493+
}
494+
495+
func queryNormal(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] {
496+
// if the query includes ids or common references we'll pass it to the groups db too
497+
checkGroupsDB := false
498+
if filter.IDs != nil {
499+
checkGroupsDB = true
500+
} else {
501+
for _, tagName := range []string{"e", "E", "a", "A"} {
502+
if _, ok := filter.Tags[tagName]; ok {
503+
checkGroupsDB = true
504+
}
505+
}
484506
}
507+
if checkGroupsDB {
508+
return eventstore.SortedMerge(
509+
groups.State.Query(ctx, filter),
510+
queryMain(ctx, filter),
511+
)
512+
}
513+
514+
// otherwise only query the main db
515+
return queryMain(ctx, filter)
485516
}

groups/queries.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
func (s *GroupsState) Query(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] {
1313
return func(yield func(nostr.Event) bool) {
1414
authed := khatru.GetAllAuthed(ctx)
15+
1516
groupIds, hasGroupIds := filter.Tags["d"]
1617
if !hasGroupIds {
1718
groupIds, hasGroupIds = filter.Tags["h"]
@@ -64,24 +65,10 @@ func (s *GroupsState) Query(ctx context.Context, filter nostr.Filter) iter.Seq[n
6465
return
6566
}
6667
default:
67-
// to return all events from all groups would be insanity
68-
// so we do a careful inspection of the filter here
69-
//
70-
// to begin with, we only accept queries that want one specific event, by either id or addr
71-
var results iter.Seq[nostr.Event]
72-
if refE, ok := filter.Tags["e"]; ok && len(refE) > 0 {
73-
results = s.DB.QueryEvents(filter, 50)
74-
} else if refA, ok := filter.Tags["a"]; ok && len(refA) > 0 {
75-
results = s.DB.QueryEvents(filter, 50)
76-
} else if len(filter.IDs) > 0 {
77-
results = s.DB.QueryEvents(filter, len(filter.IDs))
78-
} else {
79-
results = func(yield func(nostr.Event) bool) {} // nothing
80-
}
81-
82-
// now here in refE/refA/ids we have to check for each result if it is allowed
83-
for evt := range results {
84-
if group := s.GetGroupFromEvent(evt); !group.Hidden {
68+
// query few events here, as we expect to be dealing with
69+
// either id queries or ref queries ("#e", "#a" etc)
70+
for evt := range s.DB.QueryEvents(filter, 50) {
71+
if group := s.GetGroupFromEvent(evt); !group.Hidden && !group.Private {
8572
if !yield(evt) {
8673
return
8774
}
@@ -144,7 +131,7 @@ func (s *GroupsState) Query(ctx context.Context, filter nostr.Filter) iter.Seq[n
144131
// normal (non-metadata) events
145132
default:
146133
// if we are here that means that filter already includes at least an "h" tag
147-
// and access control is already validated
134+
// and access control is already validated by RequestAuthWhenNecessary()
148135
for evt := range s.DB.QueryEvents(filter, 1500) {
149136
if !yield(evt) {
150137
return

0 commit comments

Comments
 (0)