-
Notifications
You must be signed in to change notification settings - Fork 84
/
add-event.go
59 lines (48 loc) · 1.2 KB
/
add-event.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
package relayer
import (
"context"
"fmt"
"regexp"
"github.com/fiatjaf/eventstore"
"github.com/nbd-wtf/go-nostr"
)
var nip20prefixmatcher = regexp.MustCompile(`^\w+: `)
// AddEvent has a business rule to add an event to the relayer
func AddEvent(ctx context.Context, relay Relay, evt *nostr.Event) (accepted bool, message string) {
if evt == nil {
return false, ""
}
store := relay.Storage(ctx)
wrapper := &eventstore.RelayWrapper{
Store: store,
}
advancedSaver, _ := store.(AdvancedSaver)
if !relay.AcceptEvent(ctx, evt) {
return false, "blocked: event blocked by relay"
}
if 20000 <= evt.Kind && evt.Kind < 30000 {
// do not store ephemeral events
} else {
if advancedSaver != nil {
advancedSaver.BeforeSave(ctx, evt)
}
if saveErr := wrapper.Publish(ctx, *evt); saveErr != nil {
switch saveErr {
case eventstore.ErrDupEvent:
return true, saveErr.Error()
default:
errmsg := saveErr.Error()
if nip20prefixmatcher.MatchString(errmsg) {
return false, errmsg
} else {
return false, fmt.Sprintf("error: failed to save (%s)", errmsg)
}
}
}
if advancedSaver != nil {
advancedSaver.AfterSave(evt)
}
}
notifyListeners(evt)
return true, ""
}