Skip to content

Commit

Permalink
make filters pointers again
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Jan 3, 2024
1 parent 9cb7d82 commit 614607c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions cmd/replicatrd/replicatr/adding.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
if evt.Kind == 0 || evt.Kind == 3 || (10000 <= evt.Kind && evt.Kind < 20000) {
// replaceable event, delete before storing
for _, query := range rl.QueryEvents {
ch, err := query(ctx, nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}})
ch, err := query(ctx, &nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}})
if err != nil {
continue
}
Expand All @@ -46,7 +46,7 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
d := evt.Tags.GetFirst([]string{"d", ""})
if d != nil {
for _, query := range rl.QueryEvents {
ch, err := query(ctx, nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}, Tags: nostr.TagMap{"d": []string{d.Value()}}})
ch, err := query(ctx, &nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}, Tags: nostr.TagMap{"d": []string{d.Value()}}})
if err != nil {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/replicatrd/replicatr/deleting.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (rl *Relay) handleDeleteRequest(ctx context.Context, evt *nostr.Event) erro
if len(tag) >= 2 && tag[0] == "e" {
// first we fetch the event
for _, query := range rl.QueryEvents {
ch, err := query(ctx, nostr.Filter{IDs: []string{tag[1]}})
ch, err := query(ctx, &nostr.Filter{IDs: []string{tag[1]}})
if err != nil {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/replicatrd/replicatr/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
}
var total int64
for _, filter := range env.Filters {
total += rl.handleCountRequest(ctx, ws, filter)
total += rl.handleCountRequest(ctx, ws, &filter)
}
ws.WriteJSON(nostr.CountEnvelope{SubscriptionID: env.SubscriptionID, Count: &total})
case *nostr.ReqEnvelope:
Expand All @@ -175,7 +175,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {

// handle each filter separately -- dispatching events as they're loaded from databases
for _, filter := range env.Filters {
err := rl.handleRequest(reqCtx, env.SubscriptionID, &eose, ws, filter)
err := rl.handleRequest(reqCtx, env.SubscriptionID, &eose, ws, &filter)
if err != nil {
// fail everything if any filter is rejected
reason := err.Error()
Expand Down
8 changes: 4 additions & 4 deletions cmd/replicatrd/replicatr/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ func NewRelay(appName string) *Relay {
type Relay struct {
ServiceURL string
RejectEvent []func(ctx context.Context, event *nostr.Event) (reject bool, msg string)
RejectFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
RejectCountFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
RejectFilter []func(ctx context.Context, filter *nostr.Filter) (reject bool, msg string)
RejectCountFilter []func(ctx context.Context, filter *nostr.Filter) (reject bool, msg string)
OverwriteDeletionOutcome []func(ctx context.Context, target *nostr.Event, deletion *nostr.Event) (acceptDeletion bool, msg string)
OverwriteResponseEvent []func(ctx context.Context, event *nostr.Event)
OverwriteFilter []func(ctx context.Context, filter *nostr.Filter)
OverwriteCountFilter []func(ctx context.Context, filter *nostr.Filter)
OverwriteRelayInformation []func(ctx context.Context, r *http.Request, info nip11.RelayInformationDocument) nip11.RelayInformationDocument
StoreEvent []func(ctx context.Context, event *nostr.Event) error
DeleteEvent []func(ctx context.Context, event *nostr.Event) error
QueryEvents []func(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error)
CountEvents []func(ctx context.Context, filter nostr.Filter) (int64, error)
QueryEvents []func(ctx context.Context, filter *nostr.Filter) (chan *nostr.Event, error)
CountEvents []func(ctx context.Context, filter *nostr.Filter) (int64, error)
OnConnect []func(ctx context.Context)
OnDisconnect []func(ctx context.Context)
OnEventSaved []func(ctx context.Context, event *nostr.Event)
Expand Down
8 changes: 4 additions & 4 deletions cmd/replicatrd/replicatr/responding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"github.com/nbd-wtf/go-nostr"
)

func (rl *Relay) handleRequest(ctx context.Context, id string, eose *sync.WaitGroup, ws *WebSocket, filter nostr.Filter) error {
func (rl *Relay) handleRequest(ctx context.Context, id string, eose *sync.WaitGroup, ws *WebSocket, filter *nostr.Filter) error {
defer eose.Done()

// overwrite the filter (for example, to eliminate some kinds or
// that we know we don't support)
for _, ovw := range rl.OverwriteFilter {
ovw(ctx, &filter)
ovw(ctx, filter)
}

if filter.Limit < 0 {
Expand Down Expand Up @@ -57,10 +57,10 @@ func (rl *Relay) handleRequest(ctx context.Context, id string, eose *sync.WaitGr
return nil
}

func (rl *Relay) handleCountRequest(ctx context.Context, ws *WebSocket, filter nostr.Filter) int64 {
func (rl *Relay) handleCountRequest(ctx context.Context, ws *WebSocket, filter *nostr.Filter) int64 {
// overwrite the filter (for example, to eliminate some kinds or tags that we know we don't support)
for _, ovw := range rl.OverwriteCountFilter {
ovw(ctx, &filter)
ovw(ctx, filter)
}

// then check if we'll reject this filter
Expand Down
2 changes: 1 addition & 1 deletion pkg/eventstore/badger/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
nostr_binary "github.com/nbd-wtf/go-nostr/binary"
)

func (b BadgerBackend) CountEvents(ctx context.Context, filter nostr.Filter) (int64, error) {
func (b BadgerBackend) CountEvents(ctx context.Context, filter *nostr.Filter) (int64, error) {
var count int64 = 0

queries, extraFilter, since, err := prepareQueries(filter)
Expand Down
4 changes: 2 additions & 2 deletions pkg/eventstore/badger/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type queryEvent struct {
query int
}

func (b BadgerBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) {
func (b BadgerBackend) QueryEvents(ctx context.Context, filter *nostr.Filter) (chan *nostr.Event, error) {
ch := make(chan *nostr.Event)

queries, extraFilter, since, err := prepareQueries(filter)
Expand Down Expand Up @@ -192,7 +192,7 @@ func (pq *priorityQueue) Pop() any {
return item
}

func prepareQueries(filter nostr.Filter) (
func prepareQueries(filter *nostr.Filter) (
queries []query,
extraFilter *nostr.Filter,
since uint32,
Expand Down
18 changes: 9 additions & 9 deletions pkg/eventstore/relay_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

// RelayInterface is a wrapper thing that unifies Store and nostr.Relay under a common API.
type RelayInterface interface {
Publish(ctx context.Context, event nostr.Event) error
QuerySync(ctx context.Context, filter nostr.Filter, opts ...nostr.SubscriptionOption) ([]*nostr.Event, error)
Publish(ctx context.Context, event *nostr.Event) error
QuerySync(ctx context.Context, filter *nostr.Filter, opts ...nostr.SubscriptionOption) ([]*nostr.Event, error)
}

type RelayWrapper struct {
Expand All @@ -19,17 +19,17 @@ type RelayWrapper struct {

var _ RelayInterface = (*RelayWrapper)(nil)

func (w RelayWrapper) Publish(ctx context.Context, evt nostr.Event) error {
func (w RelayWrapper) Publish(ctx context.Context, evt *nostr.Event) error {
if 20000 <= evt.Kind && evt.Kind < 30000 {
// do not store ephemeral events
return nil
} else if evt.Kind == 0 || evt.Kind == 3 || (10000 <= evt.Kind && evt.Kind < 20000) {
// replaceable event, delete before storing
ch, err := w.Store.QueryEvents(ctx, nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}})
ch, err := w.Store.QueryEvents(ctx, &nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}})
if err != nil {
return fmt.Errorf("failed to query before replacing: %w", err)
}
if previous := <-ch; previous != nil && isOlder(previous, &evt) {
if previous := <-ch; previous != nil && isOlder(previous, evt) {
if err := w.Store.DeleteEvent(ctx, previous); err != nil {
return fmt.Errorf("failed to delete event for replacing: %w", err)
}
Expand All @@ -38,26 +38,26 @@ func (w RelayWrapper) Publish(ctx context.Context, evt nostr.Event) error {
// parameterized replaceable event, delete before storing
d := evt.Tags.GetFirst([]string{"d", ""})
if d != nil {
ch, err := w.Store.QueryEvents(ctx, nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}, Tags: nostr.TagMap{"d": []string{d.Value()}}})
ch, err := w.Store.QueryEvents(ctx, &nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}, Tags: nostr.TagMap{"d": []string{d.Value()}}})
if err != nil {
return fmt.Errorf("failed to query before parameterized replacing: %w", err)
}
if previous := <-ch; previous != nil && isOlder(previous, &evt) {
if previous := <-ch; previous != nil && isOlder(previous, evt) {
if err := w.Store.DeleteEvent(ctx, previous); err != nil {
return fmt.Errorf("failed to delete event for parameterized replacing: %w", err)
}
}
}
}

if err := w.SaveEvent(ctx, &evt); err != nil && err != ErrDupEvent {
if err := w.SaveEvent(ctx, evt); err != nil && err != ErrDupEvent {
return fmt.Errorf("failed to save: %w", err)
}

return nil
}

func (w RelayWrapper) QuerySync(ctx context.Context, filter nostr.Filter, opts ...nostr.SubscriptionOption) ([]*nostr.Event, error) {
func (w RelayWrapper) QuerySync(ctx context.Context, filter *nostr.Filter, opts ...nostr.SubscriptionOption) ([]*nostr.Event, error) {
ch, err := w.Store.QueryEvents(ctx, filter)
if err != nil {
return nil, fmt.Errorf("failed to query: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/eventstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Store interface {
// QueryEvents is invoked upon a client's REQ as described in NIP-01.
// it should return a channel with the events as they're recovered from a database.
// the channel should be closed after the events are all delivered.
QueryEvents(context.Context, nostr.Filter) (chan *nostr.Event, error)
QueryEvents(context.Context, *nostr.Filter) (chan *nostr.Event, error)
// DeleteEvent is used to handle deletion events, as per NIP-09.
DeleteEvent(context.Context, *nostr.Event) error
// SaveEvent is called once Relay.AcceptEvent reports true.
Expand Down

0 comments on commit 614607c

Please sign in to comment.