update RelayInterface() to match new go-nostr API.

This commit is contained in:
fiatjaf
2023-12-09 14:42:35 -03:00
parent 37e16afaba
commit 59ac03f08d
3 changed files with 12 additions and 13 deletions

2
go.mod
View File

@@ -12,7 +12,7 @@ require (
github.com/lib/pq v1.10.9
github.com/mailru/easyjson v0.7.7
github.com/mattn/go-sqlite3 v1.14.18
github.com/nbd-wtf/go-nostr v0.25.3
github.com/nbd-wtf/go-nostr v0.27.0
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.7
golang.org/x/exp v0.0.0-20231006140011-7918f672742d

4
go.sum
View File

@@ -116,8 +116,8 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI=
github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/nbd-wtf/go-nostr v0.25.3 h1:RPPh4cOosw0OZi5KG627pZ3GlKxiKsjARluzen/mB9g=
github.com/nbd-wtf/go-nostr v0.25.3/go.mod h1:bkffJI+x914sPQWum9ZRUn66D7NpDnAoWo1yICvj3/0=
github.com/nbd-wtf/go-nostr v0.27.0 h1:h6JmMMmfNcAORTL2kk/K3+U6Mju6rk/IjcHA/PMeOc8=
github.com/nbd-wtf/go-nostr v0.27.0/go.mod h1:bkffJI+x914sPQWum9ZRUn66D7NpDnAoWo1yICvj3/0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View File

@@ -9,7 +9,7 @@ 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) (nostr.Status, error)
Publish(ctx context.Context, event nostr.Event) error
QuerySync(ctx context.Context, filter nostr.Filter, opts ...nostr.SubscriptionOption) ([]*nostr.Event, error)
}
@@ -19,19 +19,19 @@ type RelayWrapper struct {
var _ RelayInterface = (*RelayWrapper)(nil)
func (w RelayWrapper) Publish(ctx context.Context, evt nostr.Event) (nostr.Status, 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 nostr.PublishStatusSucceeded, nil
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}})
if err != nil {
return nostr.PublishStatusFailed, fmt.Errorf("failed to query before replacing: %w", err)
return fmt.Errorf("failed to query before replacing: %w", err)
}
if previous := <-ch; previous != nil && isOlder(previous, &evt) {
if err := w.Store.DeleteEvent(ctx, previous); err != nil {
return nostr.PublishStatusFailed, fmt.Errorf("failed to delete event for replacing: %w", err)
return fmt.Errorf("failed to delete event for replacing: %w", err)
}
}
} else if 30000 <= evt.Kind && evt.Kind < 40000 {
@@ -40,22 +40,21 @@ func (w RelayWrapper) Publish(ctx context.Context, evt nostr.Event) (nostr.Statu
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()}}})
if err != nil {
return nostr.PublishStatusFailed, fmt.Errorf("failed to query before parameterized replacing: %w", err)
return fmt.Errorf("failed to query before parameterized replacing: %w", err)
}
if previous := <-ch; previous != nil && isOlder(previous, &evt) {
if err := w.Store.DeleteEvent(ctx, previous); err != nil {
return nostr.PublishStatusFailed,
fmt.Errorf("failed to delete event for parameterized replacing: %w", err)
return fmt.Errorf("failed to delete event for parameterized replacing: %w", err)
}
}
}
}
if err := w.SaveEvent(ctx, &evt); err != nil && err != ErrDupEvent {
return nostr.PublishStatusFailed, fmt.Errorf("failed to save: %w", err)
return fmt.Errorf("failed to save: %w", err)
}
return nostr.PublishStatusSucceeded, nil
return nil
}
func (w RelayWrapper) QuerySync(ctx context.Context, filter nostr.Filter, opts ...nostr.SubscriptionOption) ([]*nostr.Event, error) {