From bce89a2e83cf846ee960332298df2b54c827642c Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Wed, 24 Sep 2025 11:46:46 -0700 Subject: [PATCH] Use event store in relay --- sqlite/count.go | 2 +- sqlite/delete.go | 2 +- sqlite/helpers.go | 2 +- zooid/blossom.go | 2 +- zooid/config.go | 6 +++--- zooid/groups.go | 3 --- zooid/instance.go | 49 ++++++++++++++++++++++++++++++++------------- zooid/management.go | 3 --- 8 files changed, 42 insertions(+), 27 deletions(-) diff --git a/sqlite/count.go b/sqlite/count.go index dae8ff2..18fa0e8 100644 --- a/sqlite/count.go +++ b/sqlite/count.go @@ -1,7 +1,7 @@ package sqlite import ( - "errors" + "errors" "fiatjaf.com/nostr" ) diff --git a/sqlite/delete.go b/sqlite/delete.go index 3612d3d..645d7a5 100644 --- a/sqlite/delete.go +++ b/sqlite/delete.go @@ -10,4 +10,4 @@ func (s *SqliteBackend) DeleteEvent(id nostr.ID) error { _, err := s.db.Exec("DELETE FROM events WHERE id = ?", id.Hex()) return err -} \ No newline at end of file +} diff --git a/sqlite/helpers.go b/sqlite/helpers.go index f85f9c3..913e5f1 100644 --- a/sqlite/helpers.go +++ b/sqlite/helpers.go @@ -5,4 +5,4 @@ package sqlite const ( // Database configuration defaultTimeout = 30 // seconds -) \ No newline at end of file +) diff --git a/zooid/blossom.go b/zooid/blossom.go index d1a2d0a..6362b79 100644 --- a/zooid/blossom.go +++ b/zooid/blossom.go @@ -21,7 +21,7 @@ func EnableBlossom(instance *Instance) { } store := &sqlite.SqliteBackend{ - Path: instance.Config.Data.Blossom, + Path: instance.Config.Data.Blossom, } backend := blossom.New(instance.Relay, "https://"+instance.Host) diff --git a/zooid/config.go b/zooid/config.go index 0677a2d..616d532 100644 --- a/zooid/config.go +++ b/zooid/config.go @@ -32,12 +32,12 @@ type Config struct { } `toml:"blossom"` Roles map[string]struct { - Pubkeys []string `toml:"pubkeys"` - CanInvite bool `toml:"can_invite"` + Pubkeys []string `toml:"pubkeys"` + CanInvite bool `toml:"can_invite"` } `toml:"roles"` Data struct { - Events string `toml:"events"` + Events string `toml:"events"` Blossom string `toml:"blossom"` } `toml:"data"` } diff --git a/zooid/groups.go b/zooid/groups.go index e54e80d..2603bc4 100644 --- a/zooid/groups.go +++ b/zooid/groups.go @@ -1,8 +1,5 @@ package zooid -import ( -) - func EnableGroups(instance *Instance) { instance.Relay.Info.SupportedNIPs = append(instance.Relay.Info.SupportedNIPs, 29) } diff --git a/zooid/instance.go b/zooid/instance.go index c6cf889..d3949ff 100644 --- a/zooid/instance.go +++ b/zooid/instance.go @@ -1,18 +1,23 @@ package zooid import ( - "sync" - "iter" - "net/http" "context" + "iter" + "log" + "net/http" + "os" + "sync" - "fiatjaf.com/nostr/khatru" "fiatjaf.com/nostr" + "fiatjaf.com/nostr/eventstore" + "fiatjaf.com/nostr/khatru" + "zooid/sqlite" ) type Instance struct { Host string Config *Config + Events eventstore.Store Relay *khatru.Relay } @@ -35,6 +40,7 @@ func MakeInstance(hostname string) (*Instance, error) { instance := &Instance{ Host: hostname, Config: config, + Events: &sqlite.SqliteBackend{Path: config.Data.Events}, Relay: khatru.NewRelay(), } @@ -46,6 +52,8 @@ func MakeInstance(hostname string) (*Instance, error) { instance.Relay.Info.Software = "https://github.com/coracle-social/zooid" instance.Relay.Info.Version = "v0.1.0" + instance.Relay.UseEventstore(instance.Events, 400) + instance.Relay.OnConnect = instance.OnConnect instance.Relay.OnEvent = instance.OnEvent instance.Relay.StoreEvent = instance.StoreEvent @@ -70,6 +78,16 @@ func MakeInstance(hostname string) (*Instance, error) { EnableManagement(instance) } + // Initialize stuff + + if err := os.MkdirAll(instance.Config.Data.Events, 0755); err != nil { + log.Fatal("Failed to create event store path:", err) + } + + if err := instance.Events.Init(); err != nil { + log.Fatal("Failed to initialize event store:", err) + } + return instance, nil } @@ -114,23 +132,23 @@ func (instance *Instance) IsMember(pubkey nostr.PubKey) bool { // Handlers func (instance *Instance) OnConnect(ctx context.Context) { - khatru.RequestAuth(ctx) + khatru.RequestAuth(ctx) } func (instance *Instance) OnEvent(ctx context.Context, event nostr.Event) (reject bool, msg string) { - return false, "" + return false, "" } func (instance *Instance) StoreEvent(ctx context.Context, event nostr.Event) error { - return nil + return instance.Events.SaveEvent(event) } func (instance *Instance) ReplaceEvent(ctx context.Context, event nostr.Event) error { - return nil + return instance.Events.ReplaceEvent(event) } func (instance *Instance) DeleteEvent(ctx context.Context, id nostr.ID) error { - return nil + return instance.Events.DeleteEvent(id) } func (instance *Instance) OnEventSaved(ctx context.Context, event nostr.Event) { @@ -140,20 +158,23 @@ func (instance *Instance) OnEphemeralEvent(ctx context.Context, event nostr.Even } func (instance *Instance) OnRequest(ctx context.Context, filter nostr.Filter) (reject bool, msg string) { - return false, "" + return false, "" } func (instance *Instance) QueryStored(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] { return func(yield func(nostr.Event) bool) { - // TODO: Implement actual event querying logic - // For now, return empty sequence + for evt := range instance.Events.QueryEvents(filter, 400) { + if !yield(evt) { + return + } + } } } func (instance *Instance) RejectConnection(r *http.Request) bool { - return false + return false } func (instance *Instance) PreventBroadcast(ws *khatru.WebSocket, event nostr.Event) bool { - return event.Kind == 28934 + return event.Kind == 28934 } diff --git a/zooid/management.go b/zooid/management.go index 50929c0..5afa365 100644 --- a/zooid/management.go +++ b/zooid/management.go @@ -1,7 +1,4 @@ package zooid -import ( -) - func EnableManagement(instance *Instance) { }