forked from fiatjaf/njump
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement.go
More file actions
94 lines (84 loc) · 2.52 KB
/
Copy pathmanagement.go
File metadata and controls
94 lines (84 loc) · 2.52 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"context"
"slices"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/khatru"
"fiatjaf.com/nostr/nip86"
)
func deleteEvent(id nostr.ID) {
sys.Store.DeleteEvent(id)
sys.EraseAccessTime(id)
sys.EraseEventRelays(id)
}
func deleteAllEventsFromPubKey(pk nostr.PubKey) {
for evt := range sys.Store.QueryEvents(nostr.Filter{Authors: []nostr.PubKey{pk}}, DB_MAX_LIMIT) {
deleteEvent(evt.ID)
}
}
func setupRelayManagement(relay *khatru.Relay) {
relay.ManagementAPI.OnAPICall = func(ctx context.Context, mp nip86.MethodParams) (reject bool, msg string) {
for _, authed := range khatru.GetConnection(ctx).AuthedPublicKeys {
if slices.Contains(s.trustedPubKeys, authed) {
return false, ""
}
}
return true, "you are not a trusted pubkey"
}
relay.ManagementAPI.BanEvent = func(ctx context.Context, id nostr.ID, reason string) error {
log.Info().Str("id", id.Hex()).Str("reason", reason).Msg("banning event")
deleteEvent(id)
authed, ok := khatru.GetAuthed(ctx)
if !ok {
panic("not authed")
}
evt := nostr.Event{
Kind: 5,
Tags: nostr.Tags{{"e", id.Hex()}},
Content: reason,
PubKey: authed,
CreatedAt: nostr.Now(),
}
evt.ID = evt.GetID()
return sys.Store.SaveEvent(evt)
}
relay.ManagementAPI.AllowEvent = func(ctx context.Context, id nostr.ID, reason string) error {
log.Info().Str("id", id.Hex()).Str("reason", reason).Msg("unbanning event")
for evt := range sys.Store.QueryEvents(nostr.Filter{
Kinds: []nostr.Kind{5},
Tags: nostr.TagMap{"e": []string{id.Hex()}},
Authors: s.trustedPubKeys,
}, DB_MAX_LIMIT) {
deleteEvent(evt.ID)
}
return nil
}
relay.ManagementAPI.BanPubKey = func(ctx context.Context, pk nostr.PubKey, reason string) error {
log.Info().Str("pubkey", pk.Hex()).Str("reason", reason).Msg("banning pubkey")
deleteAllEventsFromPubKey(pk)
authed, ok := khatru.GetAuthed(ctx)
if !ok {
panic("not authed")
}
evt := nostr.Event{
Kind: 5,
Tags: nostr.Tags{{"p", pk.Hex()}},
Content: reason,
PubKey: authed,
CreatedAt: nostr.Now(),
}
evt.ID = evt.GetID()
return sys.Store.SaveEvent(evt)
}
relay.ManagementAPI.AllowPubKey = func(ctx context.Context, pk nostr.PubKey, reason string) error {
log.Info().Str("pk", pk.Hex()).Str("reason", reason).Msg("unbanning pubkey")
for evt := range sys.Store.QueryEvents(nostr.Filter{
Kinds: []nostr.Kind{5},
Tags: nostr.TagMap{"p": []string{pk.Hex()}},
Authors: s.trustedPubKeys,
}, DB_MAX_LIMIT) {
deleteEvent(evt.ID)
}
return nil
}
}