2024-09-17 01:01:42 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RelayWrapper struct {
|
|
|
|
Store
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ nostr.RelayStore = (*RelayWrapper)(nil)
|
|
|
|
|
|
|
|
func (w RelayWrapper) Publish(ctx context.Context, evt nostr.Event) error {
|
2024-11-11 19:56:01 +00:00
|
|
|
if nostr.IsEphemeralKind(evt.Kind) {
|
|
|
|
// do not store ephemeral events
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-17 01:01:42 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-11-11 19:56:01 +00:00
|
|
|
if nostr.IsRegularKind(evt.Kind) {
|
|
|
|
// regular events are just saved directly
|
|
|
|
if err := w.SaveEvent(ctx, &evt); err != nil && err != ErrDupEvent {
|
|
|
|
return fmt.Errorf("failed to save: %w", err)
|
2024-09-17 01:01:42 +00:00
|
|
|
}
|
2024-11-11 19:56:01 +00:00
|
|
|
}
|
2024-09-17 01:01:42 +00:00
|
|
|
|
2024-11-11 19:56:01 +00:00
|
|
|
// from now on we know they are replaceable or addressable
|
|
|
|
filter := nostr.Filter{Limit: 1, Kinds: []int{evt.Kind}, Authors: []string{evt.PubKey}}
|
|
|
|
if nostr.IsAddressableKind(evt.Kind) {
|
|
|
|
// when addressable, add the "d" tag to the filter
|
|
|
|
filter.Tags = nostr.TagMap{"d": []string{evt.Tags.GetD()}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we fetch the event, whatever it is, delete it and then save the new
|
|
|
|
ch, err := w.Store.QueryEvents(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to query before replacing: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldStore := true
|
|
|
|
for previous := range ch {
|
|
|
|
if previous == nil {
|
|
|
|
continue
|
2024-09-17 01:01:42 +00:00
|
|
|
}
|
2024-11-11 19:56:01 +00:00
|
|
|
if isOlder(previous, &evt) {
|
|
|
|
if err := w.Store.DeleteEvent(ctx, previous); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete event for replacing: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// already, newer event is stored.
|
|
|
|
shouldStore = false
|
2024-09-17 01:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-11 19:56:01 +00:00
|
|
|
if shouldStore {
|
|
|
|
if err := w.SaveEvent(ctx, &evt); err != nil && err != ErrDupEvent {
|
|
|
|
return fmt.Errorf("failed to save: %w", err)
|
|
|
|
}
|
2024-09-17 01:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w RelayWrapper) QuerySync(ctx context.Context, filter nostr.Filter) ([]*nostr.Event, error) {
|
|
|
|
ch, err := w.Store.QueryEvents(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to query: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n := filter.Limit
|
|
|
|
if n == 0 {
|
|
|
|
n = 500
|
|
|
|
}
|
|
|
|
|
|
|
|
results := make([]*nostr.Event, 0, n)
|
|
|
|
for evt := range ch {
|
|
|
|
results = append(results, evt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|