forked from fiatjaf/relayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-event.go
48 lines (38 loc) · 995 Bytes
/
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
package relayer
import (
"context"
"fmt"
"github.com/fiatjaf/relayer/v2/storage"
"github.com/nbd-wtf/go-nostr"
)
// 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)
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 := store.SaveEvent(ctx, evt); saveErr != nil {
switch saveErr {
case storage.ErrDupEvent:
return true, saveErr.Error()
default:
return false, fmt.Sprintf("error: failed to save: %s", saveErr.Error())
}
}
if advancedSaver != nil {
advancedSaver.AfterSave(evt)
}
}
notifyListeners(evt)
return true, ""
}