forked from fiatjaf/pyramid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement.go
More file actions
210 lines (183 loc) · 6.16 KB
/
management.go
File metadata and controls
210 lines (183 loc) · 6.16 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
package main
import (
"context"
"fmt"
"slices"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/khatru"
"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 += inv.Hex()
}
}
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")
}
return deleteFromMain(id)
}
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 listAllowedKindsHandler(ctx context.Context) ([]nostr.Kind, error) {
return global.GetAllowedKinds(), nil
}
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 len(global.Settings.AllowedKinds) == 0 {
global.Settings.AllowedKinds = make([]nostr.Kind, len(global.SupportedKindsDefault))
copy(global.Settings.AllowedKinds, global.SupportedKindsDefault)
}
// check if kind is already in the list, otherwise add it in the correct position
if idx, has := slices.BinarySearch(global.Settings.AllowedKinds, kind); !has {
next := make([]nostr.Kind, len(global.Settings.AllowedKinds)+1)
copy(next[0:idx], global.Settings.AllowedKinds[0:idx])
next[idx] = kind
copy(next[idx+1:], global.Settings.AllowedKinds[idx:])
global.Settings.AllowedKinds = next
return global.SaveUserSettings()
}
return nil
}
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 len(global.Settings.AllowedKinds) == 0 {
global.Settings.AllowedKinds = make([]nostr.Kind, len(global.SupportedKindsDefault))
copy(global.Settings.AllowedKinds, global.SupportedKindsDefault)
}
// find and remove the kind from the list
idx := slices.Index(global.Settings.AllowedKinds, kind)
if idx != -1 {
global.Settings.AllowedKinds = append(
global.Settings.AllowedKinds[:idx],
global.Settings.AllowedKinds[idx+1:]...,
)
}
// if the list is now empty, remove it from settings
if len(global.Settings.AllowedKinds) == 0 {
global.Settings.AllowedKinds = nil
}
return global.SaveUserSettings()
}