forked from coracle/zooid
Add more nip 86 methods
This commit is contained in:
+44
-1
@@ -4,6 +4,7 @@ import (
|
|||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
)
|
)
|
||||||
@@ -21,7 +22,6 @@ type Config struct {
|
|||||||
Info struct {
|
Info struct {
|
||||||
Name string `toml:"name"`
|
Name string `toml:"name"`
|
||||||
Icon string `toml:"icon"`
|
Icon string `toml:"icon"`
|
||||||
Secret string `toml:"secret"`
|
|
||||||
Pubkey string `toml:"pubkey"`
|
Pubkey string `toml:"pubkey"`
|
||||||
Description string `toml:"description"`
|
Description string `toml:"description"`
|
||||||
} `toml:"info"`
|
} `toml:"info"`
|
||||||
@@ -47,6 +47,7 @@ type Config struct {
|
|||||||
Roles map[string]Role `toml:"roles"`
|
Roles map[string]Role `toml:"roles"`
|
||||||
|
|
||||||
// Private/parsed values
|
// Private/parsed values
|
||||||
|
path string
|
||||||
secret nostr.SecretKey
|
secret nostr.SecretKey
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +72,9 @@ func LoadConfig(filename string) (*Config, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the path for later
|
||||||
|
config.path = path
|
||||||
|
|
||||||
// Make the secret... secret
|
// Make the secret... secret
|
||||||
config.Secret = ""
|
config.Secret = ""
|
||||||
config.secret = secret
|
config.secret = secret
|
||||||
@@ -78,6 +82,45 @@ func LoadConfig(filename string) (*Config, error) {
|
|||||||
return &config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config *Config) Save() error {
|
||||||
|
// Restore the secret key to the public field for saving
|
||||||
|
config.Secret = config.secret.Hex()
|
||||||
|
|
||||||
|
file, err := os.Create(config.path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to open config file %s: %w", config.path, err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
encoder := toml.NewEncoder(file)
|
||||||
|
if err := encoder.Encode(config); err != nil {
|
||||||
|
return fmt.Errorf("Failed to encode config file %s: %w", config.path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the secret again
|
||||||
|
config.Secret = ""
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *Config) SetName(name string) error {
|
||||||
|
config.Info.Name = name
|
||||||
|
|
||||||
|
return config.Save()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *Config) SetDescription(description string) error {
|
||||||
|
config.Info.Description = description
|
||||||
|
|
||||||
|
return config.Save()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *Config) SetIcon(icon string) error {
|
||||||
|
config.Info.Icon = icon
|
||||||
|
|
||||||
|
return config.Save()
|
||||||
|
}
|
||||||
|
|
||||||
func (config *Config) Sign(event *nostr.Event) error {
|
func (config *Config) Sign(event *nostr.Event) error {
|
||||||
return event.Sign(config.secret)
|
return event.Sign(config.secret)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -319,6 +319,20 @@ func (m *ManagementStore) Enable(instance *Instance) {
|
|||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
instance.Relay.ManagementAPI.ChangeRelayName = func(ctx context.Context, name string) error {
|
||||||
|
return m.Config.SetName(name)
|
||||||
|
}
|
||||||
|
instance.Relay.ManagementAPI.ChangeRelayDescription = func(ctx context.Context, desc string) error {
|
||||||
|
return m.Config.SetDescription(desc)
|
||||||
|
}
|
||||||
|
instance.Relay.ManagementAPI.ChangeRelayIcon = func(ctx context.Context, icon string) error {
|
||||||
|
return m.Config.SetIcon(icon)
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.Relay.ManagementAPI.BanPubKey = func(ctx context.Context, pubkey nostr.PubKey, reason string) error {
|
||||||
|
return m.BanPubkey(pubkey, reason)
|
||||||
|
}
|
||||||
|
|
||||||
instance.Relay.ManagementAPI.BanPubKey = func(ctx context.Context, pubkey nostr.PubKey, reason string) error {
|
instance.Relay.ManagementAPI.BanPubKey = func(ctx context.Context, pubkey nostr.PubKey, reason string) error {
|
||||||
return m.BanPubkey(pubkey, reason)
|
return m.BanPubkey(pubkey, reason)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user