2023-09-06 20:49:18 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-10-28 21:40:54 -03:00
|
|
|
"fmt"
|
2023-09-06 20:49:18 +02:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
)
|
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
const WHITELIST_FILE = "users.json"
|
2023-09-06 20:49:18 +02:00
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
type Whitelist map[string]string // { [user_pubkey]: [invited_by] }
|
2023-09-06 20:49:18 +02:00
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
func addToWhitelist(pubkey string, inviter string) error {
|
|
|
|
if nostr.IsValidPublicKeyHex(pubkey) && isPublicKeyInWhitelist(inviter) && !isPublicKeyInWhitelist(pubkey) {
|
|
|
|
whitelist[pubkey] = inviter
|
2023-09-06 20:49:18 +02:00
|
|
|
}
|
2023-10-29 13:45:46 -03:00
|
|
|
return saveWhitelist()
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
func isPublicKeyInWhitelist(pubkey string) bool {
|
|
|
|
_, ok := whitelist[pubkey]
|
|
|
|
return ok
|
|
|
|
}
|
2023-09-06 20:49:18 +02:00
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
func isAncestorOf(pubkey string, target string) bool {
|
|
|
|
ancestor := target // we must find out if we are an ancestor of the target, but we can delete ourselves
|
|
|
|
for {
|
|
|
|
if ancestor == pubkey {
|
|
|
|
break
|
2023-09-06 20:49:18 +02:00
|
|
|
}
|
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
parent, ok := whitelist[ancestor]
|
|
|
|
if !ok {
|
|
|
|
// parent is not in whitelist, this means this is a top-level user and can
|
|
|
|
// only be deleted by manually editing the users.json file
|
|
|
|
return false
|
|
|
|
}
|
2023-09-06 20:49:18 +02:00
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
ancestor = parent
|
2023-10-28 20:21:15 -03:00
|
|
|
}
|
2023-10-29 13:45:46 -03:00
|
|
|
return true
|
2023-10-28 20:21:15 -03:00
|
|
|
}
|
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
func removeFromWhitelist(target string, deleter string) error {
|
|
|
|
// check if this user is a descendant of the user who issued the delete command
|
|
|
|
if !isAncestorOf(deleter, target) {
|
|
|
|
return fmt.Errorf("insufficient permissions to delete this")
|
2023-10-28 21:40:54 -03:00
|
|
|
}
|
2023-10-28 20:21:15 -03:00
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
// if we got here that means we have permission to delete the target
|
|
|
|
delete(whitelist, target)
|
|
|
|
|
|
|
|
// delete all people who were invited by the target
|
|
|
|
removeDescendantsFromWhitelist(target)
|
|
|
|
|
2023-10-28 20:21:15 -03:00
|
|
|
return saveWhitelist()
|
|
|
|
}
|
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
func removeDescendantsFromWhitelist(ancestor string) {
|
|
|
|
for pubkey, inviter := range whitelist {
|
|
|
|
if inviter == ancestor {
|
|
|
|
delete(whitelist, pubkey)
|
|
|
|
removeDescendantsFromWhitelist(pubkey)
|
|
|
|
}
|
2023-09-06 20:49:18 +02:00
|
|
|
}
|
2023-10-29 13:45:46 -03:00
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
func loadWhitelist() error {
|
|
|
|
b, err := os.ReadFile(WHITELIST_FILE)
|
2023-09-06 20:49:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
if err := json.Unmarshal(b, &whitelist); err != nil {
|
2023-09-06 20:49:18 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-09-06 20:49:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-11 00:19:33 +02:00
|
|
|
func saveWhitelist() error {
|
2023-09-06 20:49:18 +02:00
|
|
|
jsonBytes, err := json.Marshal(whitelist)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-10-29 13:45:46 -03:00
|
|
|
if err := os.WriteFile(WHITELIST_FILE, jsonBytes, 0644); err != nil {
|
2023-09-06 20:49:18 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-10-11 00:19:33 +02:00
|
|
|
|
2023-09-06 20:49:18 +02:00
|
|
|
return nil
|
|
|
|
}
|