-
Notifications
You must be signed in to change notification settings - Fork 105
/
interface.go
72 lines (63 loc) · 1.49 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package nostr
import (
"context"
"errors"
"slices"
)
type RelayStore interface {
Publish(context.Context, Event) error
QueryEvents(context.Context, Filter) (chan *Event, error)
QuerySync(context.Context, Filter) ([]*Event, error)
}
var (
_ RelayStore = (*Relay)(nil)
_ RelayStore = (*MultiStore)(nil)
)
type MultiStore []RelayStore
func (multi MultiStore) Publish(ctx context.Context, event Event) error {
errs := make([]error, len(multi))
for i, s := range multi {
errs[i] = s.Publish(ctx, event)
}
return errors.Join(errs...)
}
func (multi MultiStore) QueryEvents(ctx context.Context, filter Filter) (chan *Event, error) {
multich := make(chan *Event)
errs := make([]error, len(multi))
var good bool
for i, s := range multi {
ch, err := s.QueryEvents(ctx, filter)
errs[i] = err
if err == nil {
good = true
go func(ch chan *Event) {
for evt := range ch {
multich <- evt
}
}(ch)
}
}
if good {
return multich, nil
} else {
return nil, errors.Join(errs...)
}
}
func (multi MultiStore) QuerySync(ctx context.Context, filter Filter) ([]*Event, error) {
errs := make([]error, len(multi))
events := make([]*Event, 0, max(filter.Limit, 250))
for i, s := range multi {
res, err := s.QuerySync(ctx, filter)
errs[i] = err
events = append(events, res...)
}
slices.SortFunc(events, func(a, b *Event) int {
if b.CreatedAt > a.CreatedAt {
return 1
} else if b.CreatedAt < a.CreatedAt {
return -1
}
return 0
})
return events, errors.Join(errs...)
}