Refactor api
This commit is contained in:
+224
-369
@@ -1,7 +1,6 @@
|
|||||||
package zooid
|
package zooid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -21,38 +20,6 @@ type APIHandler struct {
|
|||||||
configDir string
|
configDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
// RelayConfigJSON is the JSON schema for relay configuration input
|
|
||||||
type RelayConfigJSON struct {
|
|
||||||
Host string `json:"host"`
|
|
||||||
Schema string `json:"schema"`
|
|
||||||
Secret string `json:"secret"`
|
|
||||||
Info struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Icon string `json:"icon"`
|
|
||||||
Pubkey string `json:"pubkey"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
} `json:"info"`
|
|
||||||
Policy struct {
|
|
||||||
PublicJoin bool `json:"public_join"`
|
|
||||||
StripSignatures bool `json:"strip_signatures"`
|
|
||||||
} `json:"policy"`
|
|
||||||
Groups struct {
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
AutoJoin bool `json:"auto_join"`
|
|
||||||
} `json:"groups"`
|
|
||||||
Push struct {
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
} `json:"push"`
|
|
||||||
Management struct {
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
Methods []string `json:"methods"`
|
|
||||||
} `json:"management"`
|
|
||||||
Blossom struct {
|
|
||||||
Enabled bool `json:"enabled"`
|
|
||||||
} `json:"blossom"`
|
|
||||||
Roles map[string]Role `json:"roles"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewAPIHandler creates a new API handler with the given whitelist
|
// NewAPIHandler creates a new API handler with the given whitelist
|
||||||
func NewAPIHandler(whitelist string, configDir string) *APIHandler {
|
func NewAPIHandler(whitelist string, configDir string) *APIHandler {
|
||||||
w := make(map[string]bool)
|
w := make(map[string]bool)
|
||||||
@@ -70,21 +37,18 @@ func NewAPIHandler(whitelist string, configDir string) *APIHandler {
|
|||||||
|
|
||||||
// ServeHTTP implements the http.Handler interface
|
// ServeHTTP implements the http.Handler interface
|
||||||
func (api *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (api *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// Set JSON content type
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
// Authenticate the request using NIP-98
|
// Authenticate the request using NIP-98
|
||||||
pubkey, err := api.authenticateNIP98(r)
|
pubkey, err := api.authenticateNIP98(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
writeError(w, http.StatusUnauthorized, err.Error())
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if pubkey is in whitelist
|
// Check if pubkey is in whitelist
|
||||||
if !api.whitelist[pubkey.Hex()] {
|
if !api.whitelist[pubkey.Hex()] {
|
||||||
w.WriteHeader(http.StatusForbidden)
|
writeError(w, http.StatusForbidden, "pubkey not in whitelist")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "pubkey not in whitelist"})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,15 +57,13 @@ func (api *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
parts := strings.Split(path, "/")
|
parts := strings.Split(path, "/")
|
||||||
|
|
||||||
if len(parts) < 2 || parts[0] != "relay" {
|
if len(parts) < 2 || parts[0] != "relay" {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
writeError(w, http.StatusNotFound, "not found")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "not found"})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id := parts[1]
|
id := parts[1]
|
||||||
if id == "" {
|
if id == "" {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
writeError(w, http.StatusBadRequest, "relay id is required")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "relay id is required"})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,11 +77,22 @@ func (api *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
api.deleteRelay(w, r, id)
|
api.deleteRelay(w, r, id)
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "method not allowed"})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// writeError writes a JSON error response
|
||||||
|
func writeError(w http.ResponseWriter, status int, message string) {
|
||||||
|
w.WriteHeader(status)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"error": message})
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeJSON writes a JSON success response
|
||||||
|
func writeJSON(w http.ResponseWriter, status int, data map[string]string) {
|
||||||
|
w.WriteHeader(status)
|
||||||
|
json.NewEncoder(w).Encode(data)
|
||||||
|
}
|
||||||
|
|
||||||
// authenticateNIP98 validates NIP-98 HTTP AUTH
|
// authenticateNIP98 validates NIP-98 HTTP AUTH
|
||||||
func (api *APIHandler) authenticateNIP98(r *http.Request) (nostr.PubKey, error) {
|
func (api *APIHandler) authenticateNIP98(r *http.Request) (nostr.PubKey, error) {
|
||||||
authHeader := r.Header.Get("Authorization")
|
authHeader := r.Header.Get("Authorization")
|
||||||
@@ -127,37 +100,31 @@ func (api *APIHandler) authenticateNIP98(r *http.Request) (nostr.PubKey, error)
|
|||||||
return nostr.PubKey{}, fmt.Errorf("missing authorization header")
|
return nostr.PubKey{}, fmt.Errorf("missing authorization header")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the Authorization header
|
|
||||||
parts := strings.SplitN(authHeader, " ", 2)
|
parts := strings.SplitN(authHeader, " ", 2)
|
||||||
if len(parts) != 2 || strings.ToLower(parts[0]) != "nostr" {
|
if len(parts) != 2 || strings.ToLower(parts[0]) != "nostr" {
|
||||||
return nostr.PubKey{}, fmt.Errorf("invalid authorization header format")
|
return nostr.PubKey{}, fmt.Errorf("invalid authorization header format")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the base64 event
|
|
||||||
eventJSON, err := base64.StdEncoding.DecodeString(parts[1])
|
eventJSON, err := base64.StdEncoding.DecodeString(parts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nostr.PubKey{}, fmt.Errorf("invalid base64 encoding: %w", err)
|
return nostr.PubKey{}, fmt.Errorf("invalid base64 encoding: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the event
|
|
||||||
var event nostr.Event
|
var event nostr.Event
|
||||||
if err := json.Unmarshal(eventJSON, &event); err != nil {
|
if err := json.Unmarshal(eventJSON, &event); err != nil {
|
||||||
return nostr.PubKey{}, fmt.Errorf("invalid event json: %w", err)
|
return nostr.PubKey{}, fmt.Errorf("invalid event json: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify the event kind is HTTP Auth (27235)
|
|
||||||
if event.Kind != nostr.KindHTTPAuth {
|
if event.Kind != nostr.KindHTTPAuth {
|
||||||
return nostr.PubKey{}, fmt.Errorf("invalid event kind: expected %d, got %d", nostr.KindHTTPAuth, event.Kind)
|
return nostr.PubKey{}, fmt.Errorf("invalid event kind: expected %d, got %d", nostr.KindHTTPAuth, event.Kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify the event signature
|
|
||||||
if !event.VerifySignature() {
|
if !event.VerifySignature() {
|
||||||
return nostr.PubKey{}, fmt.Errorf("invalid event signature")
|
return nostr.PubKey{}, fmt.Errorf("invalid event signature")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify the event tags contain the correct URL and method
|
|
||||||
var hasURL, hasMethod bool
|
|
||||||
expectedURL := fmt.Sprintf("%s://%s%s", scheme(r), r.Host, r.URL.Path)
|
expectedURL := fmt.Sprintf("%s://%s%s", scheme(r), r.Host, r.URL.Path)
|
||||||
|
var hasURL, hasMethod bool
|
||||||
|
|
||||||
for _, tag := range event.Tags {
|
for _, tag := range event.Tags {
|
||||||
if len(tag) < 2 {
|
if len(tag) < 2 {
|
||||||
@@ -195,254 +162,119 @@ func scheme(r *http.Request) string {
|
|||||||
|
|
||||||
// createRelay creates a new relay config file
|
// createRelay creates a new relay config file
|
||||||
func (api *APIHandler) createRelay(w http.ResponseWriter, r *http.Request, id string) {
|
func (api *APIHandler) createRelay(w http.ResponseWriter, r *http.Request, id string) {
|
||||||
configPath := filepath.Join(api.configDir, id+".toml")
|
configPath := api.configPath(id)
|
||||||
|
|
||||||
// Check if file already exists
|
|
||||||
if _, err := os.Stat(configPath); err == nil {
|
if _, err := os.Stat(configPath); err == nil {
|
||||||
w.WriteHeader(http.StatusConflict)
|
writeError(w, http.StatusConflict, "relay with this id already exists")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "relay with this id already exists"})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse and validate the JSON config
|
|
||||||
config, err := api.parseAndValidateConfig(r)
|
config, err := api.parseAndValidateConfig(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
writeError(w, http.StatusBadRequest, err.Error())
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for duplicate schema or host
|
|
||||||
if err := api.checkDuplicateSchemaOrHost(config, ""); err != nil {
|
if err := api.checkDuplicateSchemaOrHost(config, ""); err != nil {
|
||||||
w.WriteHeader(http.StatusConflict)
|
writeError(w, http.StatusConflict, err.Error())
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to TOML and write
|
if err := api.saveConfig(configPath, config); err != nil {
|
||||||
if err := api.writeConfigAsTOML(configPath, config); err != nil {
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to write config: %v", err))
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to write config: %v", err)})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
writeJSON(w, http.StatusCreated, map[string]string{"message": "relay created successfully"})
|
||||||
json.NewEncoder(w).Encode(map[string]string{"message": "relay created successfully"})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateRelay updates an existing relay config file
|
// updateRelay updates an existing relay config file
|
||||||
func (api *APIHandler) updateRelay(w http.ResponseWriter, r *http.Request, id string) {
|
func (api *APIHandler) updateRelay(w http.ResponseWriter, r *http.Request, id string) {
|
||||||
configPath := filepath.Join(api.configDir, id+".toml")
|
configPath := api.configPath(id)
|
||||||
|
|
||||||
// Check if file exists
|
if err := api.checkConfigExists(configPath); err != nil {
|
||||||
if _, err := os.Stat(configPath); err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
writeError(w, http.StatusNotFound, "relay not found")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "relay not found"})
|
} else {
|
||||||
return
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to check config: %v", err))
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to check config: %v", err)})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse and validate the JSON config
|
|
||||||
config, err := api.parseAndValidateConfig(r)
|
config, err := api.parseAndValidateConfig(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
writeError(w, http.StatusBadRequest, err.Error())
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for duplicate schema or host (excluding this config file)
|
|
||||||
if err := api.checkDuplicateSchemaOrHost(config, id+".toml"); err != nil {
|
if err := api.checkDuplicateSchemaOrHost(config, id+".toml"); err != nil {
|
||||||
w.WriteHeader(http.StatusConflict)
|
writeError(w, http.StatusConflict, err.Error())
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to TOML and write
|
if err := api.saveConfig(configPath, config); err != nil {
|
||||||
if err := api.writeConfigAsTOML(configPath, config); err != nil {
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to write config: %v", err))
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to write config: %v", err)})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
writeJSON(w, http.StatusOK, map[string]string{"message": "relay updated successfully"})
|
||||||
json.NewEncoder(w).Encode(map[string]string{"message": "relay updated successfully"})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// patchRelay partially updates an existing relay config by recursively merging changes
|
// patchRelay partially updates an existing relay config
|
||||||
func (api *APIHandler) patchRelay(w http.ResponseWriter, r *http.Request, id string) {
|
func (api *APIHandler) patchRelay(w http.ResponseWriter, r *http.Request, id string) {
|
||||||
configPath := filepath.Join(api.configDir, id+".toml")
|
configPath := api.configPath(id)
|
||||||
|
|
||||||
// Check if file exists
|
if err := api.checkConfigExists(configPath); err != nil {
|
||||||
if _, err := os.Stat(configPath); err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
writeError(w, http.StatusNotFound, "relay not found")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "relay not found"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to check config: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read existing config
|
|
||||||
var existingConfig RelayConfigJSON
|
|
||||||
if _, err := toml.DecodeFile(configPath, &existingConfig); err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to read existing config: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read and parse the patch
|
|
||||||
r.Body = http.MaxBytesReader(nil, r.Body, 1024*1024) // 1MB limit
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to read body: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var patch map[string]interface{}
|
|
||||||
if err := json.Unmarshal(body, &patch); err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("invalid json: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert existing config to map for merging
|
|
||||||
existingJSON, _ := json.Marshal(existingConfig)
|
|
||||||
var existingMap map[string]interface{}
|
|
||||||
json.Unmarshal(existingJSON, &existingMap)
|
|
||||||
|
|
||||||
// Recursively merge patch into existing
|
|
||||||
mergedMap := deepMerge(existingMap, patch)
|
|
||||||
|
|
||||||
// Convert back to RelayConfigJSON
|
|
||||||
mergedJSON, _ := json.Marshal(mergedMap)
|
|
||||||
var mergedConfig RelayConfigJSON
|
|
||||||
if err := json.Unmarshal(mergedJSON, &mergedConfig); err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to process merged config: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate required fields are still present
|
|
||||||
if mergedConfig.Host == "" {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "host is required"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if mergedConfig.Schema == "" {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "schema is required"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if mergedConfig.Secret == "" {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "secret is required"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate the secret key
|
|
||||||
if _, err := nostr.SecretKeyFromHex(mergedConfig.Secret); err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("invalid secret key: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate info.pubkey if provided
|
|
||||||
if mergedConfig.Info.Pubkey != "" {
|
|
||||||
if _, err := nostr.PubKeyFromHex(mergedConfig.Info.Pubkey); err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("invalid info.pubkey: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for duplicate schema or host (excluding this config file)
|
|
||||||
if err := api.checkDuplicateSchemaOrHost(&mergedConfig, id+".toml"); err != nil {
|
|
||||||
w.WriteHeader(http.StatusConflict)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to TOML and write
|
|
||||||
if err := api.writeConfigAsTOML(configPath, &mergedConfig); err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to write config: %v", err)})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"message": "relay patched successfully"})
|
|
||||||
}
|
|
||||||
|
|
||||||
// deepMerge recursively merges patch into base
|
|
||||||
func deepMerge(base, patch map[string]interface{}) map[string]interface{} {
|
|
||||||
result := make(map[string]interface{})
|
|
||||||
|
|
||||||
// Copy all values from base
|
|
||||||
for k, v := range base {
|
|
||||||
result[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply patches
|
|
||||||
for k, v := range patch {
|
|
||||||
if v == nil {
|
|
||||||
// Remove key if explicitly set to null
|
|
||||||
delete(result, k)
|
|
||||||
} else if patchMap, ok := v.(map[string]interface{}); ok {
|
|
||||||
// Recursively merge nested maps
|
|
||||||
if baseMap, ok := base[k].(map[string]interface{}); ok {
|
|
||||||
result[k] = deepMerge(baseMap, patchMap)
|
|
||||||
} else {
|
|
||||||
result[k] = v
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Replace value
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to check config: %v", err))
|
||||||
result[k] = v
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// deleteRelay deletes a relay config file
|
|
||||||
func (api *APIHandler) deleteRelay(w http.ResponseWriter, r *http.Request, id string) {
|
|
||||||
configPath := filepath.Join(api.configDir, id+".toml")
|
|
||||||
|
|
||||||
// Check if file exists
|
|
||||||
if _, err := os.Stat(configPath); err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": "relay not found"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to check config: %v", err)})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the config file
|
// Load existing config
|
||||||
if err := os.Remove(configPath); err != nil {
|
existing, err := api.loadConfigFromPath(configPath)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
if err != nil {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("failed to delete config: %v", err)})
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to read existing config: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
// Parse patch
|
||||||
json.NewEncoder(w).Encode(map[string]string{"message": "relay deleted successfully"})
|
patch, err := api.readPatch(r)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply patch to existing config
|
||||||
|
if err := api.applyPatch(existing, patch); err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate the patched config
|
||||||
|
if err := api.validatePatchedConfig(existing); err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := api.checkDuplicateSchemaOrHost(existing, id+".toml"); err != nil {
|
||||||
|
writeError(w, http.StatusConflict, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := api.saveConfig(configPath, existing); err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to write config: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, map[string]string{"message": "relay patched successfully"})
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseAndValidateConfig parses and validates the JSON config from the request body
|
// readPatch reads and parses the patch JSON from the request
|
||||||
func (api *APIHandler) parseAndValidateConfig(r *http.Request) (*RelayConfigJSON, error) {
|
func (api *APIHandler) readPatch(r *http.Request) (map[string]interface{}, error) {
|
||||||
// Limit body size to prevent abuse
|
r.Body = http.MaxBytesReader(nil, r.Body, 1024*1024)
|
||||||
r.Body = http.MaxBytesReader(nil, r.Body, 1024*1024) // 1MB limit
|
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
body, err := io.ReadAll(r.Body)
|
||||||
@@ -450,162 +282,185 @@ func (api *APIHandler) parseAndValidateConfig(r *http.Request) (*RelayConfigJSON
|
|||||||
return nil, fmt.Errorf("failed to read body: %w", err)
|
return nil, fmt.Errorf("failed to read body: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the JSON config
|
var patch map[string]interface{}
|
||||||
var config RelayConfigJSON
|
if err := json.Unmarshal(body, &patch); err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid json: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return patch, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// applyPatch applies a JSON patch to a config using reflection via JSON marshaling
|
||||||
|
func (api *APIHandler) applyPatch(config *Config, patch map[string]interface{}) error {
|
||||||
|
// Convert config to map for merging
|
||||||
|
configJSON, _ := json.Marshal(config)
|
||||||
|
var configMap map[string]interface{}
|
||||||
|
json.Unmarshal(configJSON, &configMap)
|
||||||
|
|
||||||
|
// Merge patch
|
||||||
|
merged := deepMerge(configMap, patch)
|
||||||
|
|
||||||
|
// Convert back to a new config (don't modify original until validation passes)
|
||||||
|
mergedJSON, _ := json.Marshal(merged)
|
||||||
|
var patched Config
|
||||||
|
if err := json.Unmarshal(mergedJSON, &patched); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy patched values to original config
|
||||||
|
*config = patched
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// deepMerge recursively merges patch into base
|
||||||
|
func deepMerge(base, patch map[string]interface{}) map[string]interface{} {
|
||||||
|
result := make(map[string]interface{})
|
||||||
|
|
||||||
|
for k, v := range base {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range patch {
|
||||||
|
if v == nil {
|
||||||
|
delete(result, k)
|
||||||
|
} else if patchMap, ok := v.(map[string]interface{}); ok {
|
||||||
|
if baseMap, ok := base[k].(map[string]interface{}); ok {
|
||||||
|
result[k] = deepMerge(baseMap, patchMap)
|
||||||
|
} else {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// validatePatchedConfig validates a config after patching
|
||||||
|
func (api *APIHandler) validatePatchedConfig(config *Config) error {
|
||||||
|
if config.Host == "" {
|
||||||
|
return fmt.Errorf("host is required")
|
||||||
|
}
|
||||||
|
if config.Schema == "" {
|
||||||
|
return fmt.Errorf("schema is required")
|
||||||
|
}
|
||||||
|
if config.Secret == "" {
|
||||||
|
return fmt.Errorf("secret is required")
|
||||||
|
}
|
||||||
|
if _, err := nostr.SecretKeyFromHex(config.Secret); err != nil {
|
||||||
|
return fmt.Errorf("invalid secret key: %w", err)
|
||||||
|
}
|
||||||
|
if config.Info.Pubkey != "" {
|
||||||
|
if _, err := nostr.PubKeyFromHex(config.Info.Pubkey); err != nil {
|
||||||
|
return fmt.Errorf("invalid info.pubkey: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteRelay deletes a relay config file
|
||||||
|
func (api *APIHandler) deleteRelay(w http.ResponseWriter, r *http.Request, id string) {
|
||||||
|
configPath := api.configPath(id)
|
||||||
|
|
||||||
|
if err := api.checkConfigExists(configPath); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
writeError(w, http.StatusNotFound, "relay not found")
|
||||||
|
} else {
|
||||||
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to check config: %v", err))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Remove(configPath); err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, fmt.Sprintf("failed to delete config: %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, map[string]string{"message": "relay deleted successfully"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// configPath returns the full path for a config file
|
||||||
|
func (api *APIHandler) configPath(id string) string {
|
||||||
|
return filepath.Join(api.configDir, id+".toml")
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkConfigExists checks if a config file exists
|
||||||
|
func (api *APIHandler) checkConfigExists(path string) error {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadConfigFromPath loads a config from a file path
|
||||||
|
func (api *APIHandler) loadConfigFromPath(path string) (*Config, error) {
|
||||||
|
var config Config
|
||||||
|
_, err := toml.DecodeFile(path, &config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseAndValidateConfig parses and validates the JSON config from the request body
|
||||||
|
func (api *APIHandler) parseAndValidateConfig(r *http.Request) (*Config, error) {
|
||||||
|
r.Body = http.MaxBytesReader(nil, r.Body, 1024*1024)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var config Config
|
||||||
if err := json.Unmarshal(body, &config); err != nil {
|
if err := json.Unmarshal(body, &config); err != nil {
|
||||||
return nil, fmt.Errorf("invalid json config: %w", err)
|
return nil, fmt.Errorf("invalid json config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate required fields
|
if err := api.validatePatchedConfig(&config); err != nil {
|
||||||
if config.Host == "" {
|
return nil, err
|
||||||
return nil, fmt.Errorf("host is required")
|
|
||||||
}
|
|
||||||
if config.Schema == "" {
|
|
||||||
return nil, fmt.Errorf("schema is required")
|
|
||||||
}
|
|
||||||
if config.Secret == "" {
|
|
||||||
return nil, fmt.Errorf("secret is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate the secret key
|
|
||||||
if _, err := nostr.SecretKeyFromHex(config.Secret); err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid secret key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate info.pubkey if provided
|
|
||||||
if config.Info.Pubkey != "" {
|
|
||||||
if _, err := nostr.PubKeyFromHex(config.Info.Pubkey); err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid info.pubkey: %w", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeConfigAsTOML writes the config as TOML to the given path
|
// saveConfig saves a config to a file as TOML
|
||||||
func (api *APIHandler) writeConfigAsTOML(path string, config *RelayConfigJSON) error {
|
func (api *APIHandler) saveConfig(path string, config *Config) error {
|
||||||
// Convert JSON config to TOML-compatible struct
|
file, err := os.Create(path)
|
||||||
tomlConfig := struct {
|
if err != nil {
|
||||||
Host string `toml:"host"`
|
return fmt.Errorf("failed to create file: %w", err)
|
||||||
Schema string `toml:"schema"`
|
|
||||||
Secret string `toml:"secret"`
|
|
||||||
Info struct {
|
|
||||||
Name string `toml:"name"`
|
|
||||||
Icon string `toml:"icon"`
|
|
||||||
Pubkey string `toml:"pubkey"`
|
|
||||||
Description string `toml:"description"`
|
|
||||||
} `toml:"info"`
|
|
||||||
Policy struct {
|
|
||||||
PublicJoin bool `toml:"public_join"`
|
|
||||||
StripSignatures bool `toml:"strip_signatures"`
|
|
||||||
} `toml:"policy"`
|
|
||||||
Groups struct {
|
|
||||||
Enabled bool `toml:"enabled"`
|
|
||||||
AutoJoin bool `toml:"auto_join"`
|
|
||||||
} `toml:"groups"`
|
|
||||||
Push struct {
|
|
||||||
Enabled bool `toml:"enabled"`
|
|
||||||
} `toml:"push"`
|
|
||||||
Management struct {
|
|
||||||
Enabled bool `toml:"enabled"`
|
|
||||||
Methods []string `toml:"methods"`
|
|
||||||
} `toml:"management"`
|
|
||||||
Blossom struct {
|
|
||||||
Enabled bool `toml:"enabled"`
|
|
||||||
} `toml:"blossom"`
|
|
||||||
Roles map[string]Role `toml:"roles"`
|
|
||||||
}{
|
|
||||||
Host: config.Host,
|
|
||||||
Schema: config.Schema,
|
|
||||||
Secret: config.Secret,
|
|
||||||
Info: struct {
|
|
||||||
Name string `toml:"name"`
|
|
||||||
Icon string `toml:"icon"`
|
|
||||||
Pubkey string `toml:"pubkey"`
|
|
||||||
Description string `toml:"description"`
|
|
||||||
}{
|
|
||||||
Name: config.Info.Name,
|
|
||||||
Icon: config.Info.Icon,
|
|
||||||
Pubkey: config.Info.Pubkey,
|
|
||||||
Description: config.Info.Description,
|
|
||||||
},
|
|
||||||
Policy: struct {
|
|
||||||
PublicJoin bool `toml:"public_join"`
|
|
||||||
StripSignatures bool `toml:"strip_signatures"`
|
|
||||||
}{
|
|
||||||
PublicJoin: config.Policy.PublicJoin,
|
|
||||||
StripSignatures: config.Policy.StripSignatures,
|
|
||||||
},
|
|
||||||
Groups: struct {
|
|
||||||
Enabled bool `toml:"enabled"`
|
|
||||||
AutoJoin bool `toml:"auto_join"`
|
|
||||||
}{
|
|
||||||
Enabled: config.Groups.Enabled,
|
|
||||||
AutoJoin: config.Groups.AutoJoin,
|
|
||||||
},
|
|
||||||
Push: struct {
|
|
||||||
Enabled bool `toml:"enabled"`
|
|
||||||
}{
|
|
||||||
Enabled: config.Push.Enabled,
|
|
||||||
},
|
|
||||||
Management: struct {
|
|
||||||
Enabled bool `toml:"enabled"`
|
|
||||||
Methods []string `toml:"methods"`
|
|
||||||
}{
|
|
||||||
Enabled: config.Management.Enabled,
|
|
||||||
Methods: config.Management.Methods,
|
|
||||||
},
|
|
||||||
Blossom: struct {
|
|
||||||
Enabled bool `toml:"enabled"`
|
|
||||||
}{
|
|
||||||
Enabled: config.Blossom.Enabled,
|
|
||||||
},
|
|
||||||
Roles: config.Roles,
|
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
// Encode to TOML
|
encoder := toml.NewEncoder(file)
|
||||||
var buf bytes.Buffer
|
if err := encoder.Encode(config); err != nil {
|
||||||
encoder := toml.NewEncoder(&buf)
|
|
||||||
if err := encoder.Encode(tomlConfig); err != nil {
|
|
||||||
return fmt.Errorf("failed to encode toml: %w", err)
|
return fmt.Errorf("failed to encode toml: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to file
|
|
||||||
if err := os.WriteFile(path, buf.Bytes(), 0644); err != nil {
|
|
||||||
return fmt.Errorf("failed to write file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkDuplicateSchemaOrHost checks if the schema or host is already in use by another config
|
// checkDuplicateSchemaOrHost checks if the schema or host is already in use by another config
|
||||||
func (api *APIHandler) checkDuplicateSchemaOrHost(config *RelayConfigJSON, excludeFilename string) error {
|
func (api *APIHandler) checkDuplicateSchemaOrHost(config *Config, excludeFilename string) error {
|
||||||
entries, err := os.ReadDir(api.configDir)
|
entries, err := os.ReadDir(api.configDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to read config directory: %w", err)
|
return fmt.Errorf("failed to read config directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if entry.IsDir() {
|
if entry.IsDir() || entry.Name() == excludeFilename || !strings.HasSuffix(entry.Name(), ".toml") {
|
||||||
continue
|
|
||||||
}
|
|
||||||
if entry.Name() == excludeFilename {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !strings.HasSuffix(entry.Name(), ".toml") {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
path := filepath.Join(api.configDir, entry.Name())
|
path := filepath.Join(api.configDir, entry.Name())
|
||||||
var existingConfig Config
|
var existing Config
|
||||||
if _, err := toml.DecodeFile(path, &existingConfig); err != nil {
|
if _, err := toml.DecodeFile(path, &existing); err != nil {
|
||||||
continue // Skip invalid configs
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if existingConfig.Schema == config.Schema {
|
if existing.Schema == config.Schema {
|
||||||
return fmt.Errorf("schema %q is already in use", config.Schema)
|
return fmt.Errorf("schema %q is already in use", config.Schema)
|
||||||
}
|
}
|
||||||
if existingConfig.Host == config.Host {
|
if existing.Host == config.Host {
|
||||||
return fmt.Errorf("host %q is already in use", config.Host)
|
return fmt.Errorf("host %q is already in use", config.Host)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-25
@@ -10,46 +10,46 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Role struct {
|
type Role struct {
|
||||||
Pubkeys []string `toml:"pubkeys"`
|
Pubkeys []string `toml:"pubkeys" json:"pubkeys"`
|
||||||
CanInvite bool `toml:"can_invite"`
|
CanInvite bool `toml:"can_invite" json:"can_invite"`
|
||||||
CanManage bool `toml:"can_manage"`
|
CanManage bool `toml:"can_manage" json:"can_manage"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string `toml:"host"`
|
Host string `toml:"host" json:"host"`
|
||||||
Schema string `toml:"schema"`
|
Schema string `toml:"schema" json:"schema"`
|
||||||
Secret string `toml:"secret"`
|
Secret string `toml:"secret" json:"secret"`
|
||||||
Info struct {
|
Info struct {
|
||||||
Name string `toml:"name"`
|
Name string `toml:"name" json:"name"`
|
||||||
Icon string `toml:"icon"`
|
Icon string `toml:"icon" json:"icon"`
|
||||||
Pubkey string `toml:"pubkey"`
|
Pubkey string `toml:"pubkey" json:"pubkey"`
|
||||||
Description string `toml:"description"`
|
Description string `toml:"description" json:"description"`
|
||||||
} `toml:"info"`
|
} `toml:"info" json:"info"`
|
||||||
|
|
||||||
Policy struct {
|
Policy struct {
|
||||||
PublicJoin bool `toml:"public_join"`
|
PublicJoin bool `toml:"public_join" json:"public_join"`
|
||||||
StripSignatures bool `toml:"strip_signatures"`
|
StripSignatures bool `toml:"strip_signatures" json:"strip_signatures"`
|
||||||
} `toml:"policy"`
|
} `toml:"policy" json:"policy"`
|
||||||
|
|
||||||
Groups struct {
|
Groups struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" json:"enabled"`
|
||||||
AutoJoin bool `toml:"auto_join"`
|
AutoJoin bool `toml:"auto_join" json:"auto_join"`
|
||||||
} `toml:"groups"`
|
} `toml:"groups" json:"groups"`
|
||||||
|
|
||||||
Push struct {
|
Push struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" json:"enabled"`
|
||||||
} `toml:"push"`
|
} `toml:"push" json:"push"`
|
||||||
|
|
||||||
Management struct {
|
Management struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" json:"enabled"`
|
||||||
Methods []string `toml:"methods"`
|
Methods []string `toml:"methods" json:"methods"`
|
||||||
} `toml:"management"`
|
} `toml:"management" json:"management"`
|
||||||
|
|
||||||
Blossom struct {
|
Blossom struct {
|
||||||
Enabled bool `toml:"enabled"`
|
Enabled bool `toml:"enabled" json:"enabled"`
|
||||||
} `toml:"blossom"`
|
} `toml:"blossom" json:"blossom"`
|
||||||
|
|
||||||
Roles map[string]Role `toml:"roles"`
|
Roles map[string]Role `toml:"roles" json:"roles"`
|
||||||
|
|
||||||
// Private/parsed values
|
// Private/parsed values
|
||||||
path string
|
path string
|
||||||
|
|||||||
+5
-34
@@ -10,16 +10,8 @@ func TestConfig_IsOwner(t *testing.T) {
|
|||||||
ownerPubkey := nostr.MustPubKeyFromHex("1234567890123456789012345678901234567890123456789012345678901234")
|
ownerPubkey := nostr.MustPubKeyFromHex("1234567890123456789012345678901234567890123456789012345678901234")
|
||||||
otherPubkey := nostr.MustPubKeyFromHex("abcdef1234567890123456789012345678901234567890123456789012345678")
|
otherPubkey := nostr.MustPubKeyFromHex("abcdef1234567890123456789012345678901234567890123456789012345678")
|
||||||
|
|
||||||
config := &Config{
|
config := &Config{}
|
||||||
Info: struct {
|
config.Info.Pubkey = ownerPubkey.Hex()
|
||||||
Name string `toml:"name"`
|
|
||||||
Icon string `toml:"icon"`
|
|
||||||
Pubkey string `toml:"pubkey"`
|
|
||||||
Description string `toml:"description"`
|
|
||||||
}{
|
|
||||||
Pubkey: ownerPubkey.Hex(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if !config.IsOwner(ownerPubkey) {
|
if !config.IsOwner(ownerPubkey) {
|
||||||
t.Error("IsOwner() should return true for owner pubkey")
|
t.Error("IsOwner() should return true for owner pubkey")
|
||||||
@@ -87,14 +79,6 @@ func TestConfig_CanManage(t *testing.T) {
|
|||||||
|
|
||||||
config := &Config{
|
config := &Config{
|
||||||
secret: nostr.Generate(),
|
secret: nostr.Generate(),
|
||||||
Info: struct {
|
|
||||||
Name string `toml:"name"`
|
|
||||||
Icon string `toml:"icon"`
|
|
||||||
Pubkey string `toml:"pubkey"`
|
|
||||||
Description string `toml:"description"`
|
|
||||||
}{
|
|
||||||
Pubkey: ownerPubkey.Hex(),
|
|
||||||
},
|
|
||||||
Roles: map[string]Role{
|
Roles: map[string]Role{
|
||||||
"admin": {
|
"admin": {
|
||||||
Pubkeys: []string{adminPubkey.Hex()},
|
Pubkeys: []string{adminPubkey.Hex()},
|
||||||
@@ -106,6 +90,7 @@ func TestConfig_CanManage(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
config.Info.Pubkey = ownerPubkey.Hex()
|
||||||
|
|
||||||
if !config.CanManage(adminPubkey) {
|
if !config.CanManage(adminPubkey) {
|
||||||
t.Error("CanManage() should return true for admin")
|
t.Error("CanManage() should return true for admin")
|
||||||
@@ -123,14 +108,6 @@ func TestConfig_CanInvite(t *testing.T) {
|
|||||||
|
|
||||||
config := &Config{
|
config := &Config{
|
||||||
secret: nostr.Generate(),
|
secret: nostr.Generate(),
|
||||||
Info: struct {
|
|
||||||
Name string `toml:"name"`
|
|
||||||
Icon string `toml:"icon"`
|
|
||||||
Pubkey string `toml:"pubkey"`
|
|
||||||
Description string `toml:"description"`
|
|
||||||
}{
|
|
||||||
Pubkey: ownerPubkey.Hex(),
|
|
||||||
},
|
|
||||||
Roles: map[string]Role{
|
Roles: map[string]Role{
|
||||||
"inviter": {
|
"inviter": {
|
||||||
Pubkeys: []string{inviterPubkey.Hex()},
|
Pubkeys: []string{inviterPubkey.Hex()},
|
||||||
@@ -142,6 +119,7 @@ func TestConfig_CanInvite(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
config.Info.Pubkey = ownerPubkey.Hex()
|
||||||
|
|
||||||
if !config.CanInvite(inviterPubkey) {
|
if !config.CanInvite(inviterPubkey) {
|
||||||
t.Error("CanInvite() should return true for inviter")
|
t.Error("CanInvite() should return true for inviter")
|
||||||
@@ -158,14 +136,6 @@ func TestConfig_MemberRole(t *testing.T) {
|
|||||||
|
|
||||||
config := &Config{
|
config := &Config{
|
||||||
secret: nostr.Generate(),
|
secret: nostr.Generate(),
|
||||||
Info: struct {
|
|
||||||
Name string `toml:"name"`
|
|
||||||
Icon string `toml:"icon"`
|
|
||||||
Pubkey string `toml:"pubkey"`
|
|
||||||
Description string `toml:"description"`
|
|
||||||
}{
|
|
||||||
Pubkey: ownerPubkey.Hex(),
|
|
||||||
},
|
|
||||||
Roles: map[string]Role{
|
Roles: map[string]Role{
|
||||||
"member": {
|
"member": {
|
||||||
Pubkeys: []string{},
|
Pubkeys: []string{},
|
||||||
@@ -173,6 +143,7 @@ func TestConfig_MemberRole(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
config.Info.Pubkey = ownerPubkey.Hex()
|
||||||
|
|
||||||
roles := config.GetAllRoles(anyPubkey)
|
roles := config.GetAllRoles(anyPubkey)
|
||||||
if len(roles) != 1 {
|
if len(roles) != 1 {
|
||||||
|
|||||||
@@ -14,15 +14,6 @@ func createTestInstance() *Instance {
|
|||||||
config := &Config{
|
config := &Config{
|
||||||
Host: "test.com",
|
Host: "test.com",
|
||||||
secret: ownerSecret,
|
secret: ownerSecret,
|
||||||
Info: struct {
|
|
||||||
Name string `toml:"name"`
|
|
||||||
Icon string `toml:"icon"`
|
|
||||||
Pubkey string `toml:"pubkey"`
|
|
||||||
Description string `toml:"description"`
|
|
||||||
}{
|
|
||||||
Name: "Test Relay",
|
|
||||||
Pubkey: ownerPubkey.Hex(),
|
|
||||||
},
|
|
||||||
Roles: map[string]Role{
|
Roles: map[string]Role{
|
||||||
"admin": {
|
"admin": {
|
||||||
Pubkeys: []string{ownerPubkey.Hex()},
|
Pubkeys: []string{ownerPubkey.Hex()},
|
||||||
@@ -31,6 +22,8 @@ func createTestInstance() *Instance {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
config.Info.Name = "Test Relay"
|
||||||
|
config.Info.Pubkey = ownerPubkey.Hex()
|
||||||
|
|
||||||
schema := &Schema{Name: "test_" + RandomString(8)}
|
schema := &Schema{Name: "test_" + RandomString(8)}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user