-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrelay_interface.go
56 lines (43 loc) · 1.04 KB
/
relay_interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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 {
if nostr.IsEphemeralKind(evt.Kind) {
// do not store ephemeral events
return nil
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
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)
}
return nil
}
// others are replaced
w.Store.ReplaceEvent(ctx, &evt)
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
}