sdk: fix default Publisher to work with any store.

This commit is contained in:
fiatjaf
2026-02-22 18:02:22 -03:00
parent 7aa127a8c3
commit 32dd39da81
2 changed files with 42 additions and 2 deletions
+40
View File
@@ -0,0 +1,40 @@
package wrappers
import (
"context"
"fmt"
"iter"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/eventstore"
)
var _ nostr.Publisher = DynamicPublisher{}
type DynamicPublisher struct {
GetStore func() eventstore.Store
MaxLimit int
}
func (w DynamicPublisher) QueryEvents(filter nostr.Filter) iter.Seq[nostr.Event] {
return w.GetStore().QueryEvents(filter, w.MaxLimit)
}
func (w DynamicPublisher) Publish(ctx context.Context, evt nostr.Event) error {
if evt.Kind.IsEphemeral() {
return nil
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if evt.Kind.IsRegular() {
if err := w.GetStore().SaveEvent(evt); err != nil && err != eventstore.ErrDupEvent {
return fmt.Errorf("failed to save: %w", err)
} else {
return err
}
}
return w.GetStore().ReplaceEvent(evt)
}
+2 -2
View File
@@ -72,7 +72,7 @@ type System struct {
NoteSearchRelays *RelayStream
Store eventstore.Store
Publisher wrappers.StorePublisher
Publisher nostr.Publisher
replaceableLoaders []*dataloader.Loader[nostr.PubKey, nostr.Event]
addressableLoaders []*dataloader.Loader[nostr.PubKey, []nostr.Event]
@@ -178,7 +178,7 @@ func NewSystem() *System {
sys.Store = &nullstore.NullStore{}
sys.Store.Init()
}
sys.Publisher = wrappers.StorePublisher{Store: sys.Store, MaxLimit: 1000}
sys.Publisher = wrappers.DynamicPublisher{GetStore: func() eventstore.Store { return sys.Store }, MaxLimit: 1000}
sys.initializeReplaceableDataloaders()
sys.initializeAddressableDataloaders()