-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmanagement.go
More file actions
250 lines (215 loc) · 7.15 KB
/
management.go
File metadata and controls
250 lines (215 loc) · 7.15 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"context"
"fmt"
"slices"
"strconv"
"strings"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/khatru"
"fiatjaf.com/nostr/nip19"
"fiatjaf.com/nostr/nip86"
"github.com/fiatjaf/pyramid/global"
"github.com/fiatjaf/pyramid/pyramid"
)
func allowPubKeyHandler(ctx context.Context, pubkey nostr.PubKey, reason string) error {
caller, ok := khatru.GetAuthed(ctx)
if !ok {
return fmt.Errorf("not authenticated")
}
log.Info().Str("caller", caller.Hex()).Str("pubkey", pubkey.Hex()).Str("reason", reason).Msg("management allowpubkey called")
err := pyramid.AddAction("invite", caller, pubkey)
if err == nil {
publishMembershipChange(pubkey, true)
}
return err
}
func banPubKeyHandler(ctx context.Context, pubkey nostr.PubKey, reason string) error {
caller, ok := khatru.GetAuthed(ctx)
if !ok {
return fmt.Errorf("not authenticated")
}
log.Info().Str("caller", caller.Hex()).Str("pubkey", pubkey.Hex()).Str("reason", reason).Msg("management banpubkey called")
err := pyramid.AddAction("drop", caller, pubkey)
if err == nil {
publishMembershipChange(pubkey, false)
}
return err
}
func listAllowedPubKeysHandler(ctx context.Context) ([]nip86.PubKeyReason, error) {
log.Info().Msg("management listallowedpubkeys called")
list := make([]nip86.PubKeyReason, 0, pyramid.Members.Size())
for pubkey, member := range pyramid.Members.Range {
if len(member.Parents) == 0 || member.Removed {
continue
}
reason := "invited by "
for j, inv := range member.Parents {
if j > 0 {
reason += ", "
}
if inv == pyramid.AbsoluteKey {
reason += "root"
} else {
reason += "nostr:" + nip19.EncodeNpub(inv)
}
}
list = append(list, nip86.PubKeyReason{PubKey: pubkey, Reason: reason})
}
return list, nil
}
func listBannedPubKeysHandler(ctx context.Context) ([]nip86.PubKeyReason, error) {
log.Info().Msg("management listbannedpubkeys called")
list := make([]nip86.PubKeyReason, 0, pyramid.Members.Size())
for pubkey, member := range pyramid.Members.Range {
if !member.Removed {
continue
}
reason := "removed member"
list = append(list, nip86.PubKeyReason{PubKey: pubkey, Reason: reason})
}
return list, nil
}
func banEventHandler(ctx context.Context, id nostr.ID, reason string) error {
caller, ok := khatru.GetAuthed(ctx)
if !ok {
return fmt.Errorf("not authenticated")
}
// allow if caller is a root user
if pyramid.IsRoot(caller) {
log.Info().Str("caller", caller.Hex()).Str("id", id.Hex()).Str("reason", reason).Msg("management banevent called by root")
} else {
// check if the caller is the author of the event being banned
var isAuthor bool
for evt := range global.IL.Main.QueryEvents(nostr.Filter{IDs: []nostr.ID{id}}, 1) {
if evt.PubKey == caller {
isAuthor = true
break
}
}
if !isAuthor {
return fmt.Errorf("must be a root user or the event author to ban an event")
}
log.Info().Str("caller", caller.Hex()).Str("id", id.Hex()).Str("reason", reason).Msg("management banevent called by author")
}
var deleted nostr.Event
for evt := range global.IL.Main.QueryEvents(nostr.Filter{IDs: []nostr.ID{id}}, 1) {
deleted = evt
}
if deleted.PubKey != nostr.ZeroPK {
if err := deleteFromMain(id); err != nil {
return err
}
handleDeleted(ctx, deleted)
}
return nil
}
func changeRelayNameHandler(ctx context.Context, name string) error {
caller, ok := khatru.GetAuthed(ctx)
if !ok {
return fmt.Errorf("not authenticated")
}
if !pyramid.IsRoot(caller) {
return fmt.Errorf("unauthorized")
}
log.Info().Str("caller", caller.Hex()).Str("name", name).Msg("management changerelayname called")
global.Settings.RelayName = name
return global.SaveUserSettings()
}
func changeRelayDescriptionHandler(ctx context.Context, description string) error {
caller, ok := khatru.GetAuthed(ctx)
if !ok {
return fmt.Errorf("not authenticated")
}
if !pyramid.IsRoot(caller) {
return fmt.Errorf("unauthorized")
}
log.Info().Str("caller", caller.Hex()).Str("description", description).Msg("management changerelaydescription called")
global.Settings.RelayDescription = description
return global.SaveUserSettings()
}
func changeRelayIconHandler(ctx context.Context, icon string) error {
caller, ok := khatru.GetAuthed(ctx)
if !ok {
return fmt.Errorf("not authenticated")
}
if !pyramid.IsRoot(caller) {
return fmt.Errorf("unauthorized")
}
log.Info().Str("caller", caller.Hex()).Str("icon", icon).Msg("management changerelayicon called")
global.Settings.RelayIcon = icon
return global.SaveUserSettings()
}
func allowKindHandler(ctx context.Context, kind nostr.Kind) error {
caller, ok := khatru.GetAuthed(ctx)
if !ok {
return fmt.Errorf("not authenticated")
}
if !pyramid.IsRoot(caller) {
return fmt.Errorf("unauthorized")
}
log.Info().Str("caller", caller.Hex()).Uint16("kind", uint16(kind)).Msg("management allowkind called")
if global.Settings.AllowedKindsSpec == "all" {
return fmt.Errorf("all kinds are supported already")
}
list, err := global.ParseKinds(global.Settings.AllowedKindsSpec, global.SupportedKindsDefault)
if err != nil {
return err
}
if slices.Contains(list, kind) {
return nil
}
if strings.Contains(global.Settings.AllowedKindsSpec, "+") || strings.Contains(global.Settings.AllowedKindsSpec, "-") || strings.TrimSpace(global.Settings.AllowedKindsSpec) == "" {
// is delta
global.Settings.AllowedKindsSpec += ",+" + strconv.Itoa(int(kind))
} else {
// is specific
global.Settings.AllowedKindsSpec += "," + strconv.Itoa(int(kind))
}
// rebuild
global.KindIsAllowed, _ = global.BuildKindIsAllowedFunction(global.Settings.AllowedKindsSpec, global.SupportedKindsDefault)
return global.SaveUserSettings()
}
func listAllowedKindsHandler(ctx context.Context) ([]nostr.Kind, error) {
if global.Settings.AllowedKindsSpec == "all" {
return []nostr.Kind{}, nil
} else {
return global.ParseKinds(global.Settings.AllowedKindsSpec, global.SupportedKindsDefault)
}
}
func disallowKindHandler(ctx context.Context, kind nostr.Kind) error {
caller, ok := khatru.GetAuthed(ctx)
if !ok {
return fmt.Errorf("not authenticated")
}
if !pyramid.IsRoot(caller) {
return fmt.Errorf("unauthorized")
}
log.Info().Str("caller", caller.Hex()).Uint16("kind", uint16(kind)).Msg("management disallowkind called")
if global.Settings.AllowedKindsSpec == "all" {
return fmt.Errorf("all kinds are supported, must change that in the settings")
}
list, err := global.ParseKinds(global.Settings.AllowedKindsSpec, global.SupportedKindsDefault)
if err != nil {
return err
}
if !slices.Contains(list, kind) {
return nil
}
if strings.Contains(global.Settings.AllowedKindsSpec, "+") || strings.Contains(global.Settings.AllowedKindsSpec, "-") || strings.TrimSpace(global.Settings.AllowedKindsSpec) == "" {
// is delta
global.Settings.AllowedKindsSpec += ",-" + strconv.Itoa(int(kind))
} else {
// is specific
listStr := make([]string, 0, len(list))
for _, ek := range list {
if ek != kind {
listStr = append(listStr, strconv.Itoa(int(ek)))
}
}
global.Settings.AllowedKindsSpec = strings.Join(listStr, ",")
}
// rebuild this
global.KindIsAllowed, _ = global.BuildKindIsAllowedFunction(global.Settings.AllowedKindsSpec, global.SupportedKindsDefault)
return global.SaveUserSettings()
}