-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmanagement.go
53 lines (42 loc) · 1.39 KB
/
management.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
package main
import (
"context"
"fmt"
"github.com/fiatjaf/khatru"
"github.com/nbd-wtf/go-nostr/nip86"
)
func allowPubKeyHandler(ctx context.Context, pubkey, reason string) error {
loggedUser := khatru.GetAuthed(ctx)
if loggedUser != s.RelayPubkey && hasInvitedAtLeast(loggedUser, s.MaxInvitesPerPerson) {
return fmt.Errorf("cannot invite more than %d", s.MaxInvitesPerPerson)
}
if err := addToWhitelist(pubkey, loggedUser); err != nil {
return fmt.Errorf("failed to add to whitelist: %w", err)
}
return nil
}
func banPubKeyHandler(ctx context.Context, pubkey, reason string) error {
loggedUser := khatru.GetAuthed(ctx)
// check if this user is a descendant of the user who issued the delete command
if !isAncestorOf(loggedUser, pubkey) {
return fmt.Errorf("insufficient permissions to delete this")
}
// if we got here that means we have permission to delete the target
delete(whitelist, pubkey)
// delete all people who were invited by the target
removeDescendantsFromWhitelist(pubkey)
return saveWhitelist()
}
func listAllowedPubKeysHandler(ctx context.Context) ([]nip86.PubKeyReason, error) {
list := make([]nip86.PubKeyReason, len(whitelist))
i := 0
for pubkey, inviter := range whitelist {
reason := fmt.Sprintf("invited by %s", inviter)
if inviter == "" {
reason = "root user"
}
list[i] = nip86.PubKeyReason{PubKey: pubkey, Reason: reason}
i++
}
return list, nil
}