Add access store

This commit is contained in:
Jon Staab
2025-09-25 13:35:07 -07:00
parent 3b652bdae4
commit 9259b5b0a1
8 changed files with 379 additions and 62 deletions
+44 -11
View File
@@ -1,15 +1,24 @@
package zooid
import (
"fiatjaf.com/nostr"
"fmt"
"github.com/BurntSushi/toml"
"path/filepath"
"slices"
)
type Role struct {
Pubkeys []string `toml:"pubkeys"`
CanInvite bool `toml:"can_invite"`
CanManage bool `toml:"can_manage"`
}
type Config struct {
Self struct {
Name string `toml:"name"`
Icon string `toml:"icon"`
Schema string `toml:"schema"`
Secret string `toml:"secret"`
Pubkey string `toml:"pubkey"`
Description string `toml:"description"`
@@ -27,19 +36,10 @@ type Config struct {
} `toml:"management"`
Blossom struct {
Enabled bool `toml:"enabled"`
Directory string `toml:"directory"`
Enabled bool `toml:"enabled"`
} `toml:"blossom"`
Roles map[string]struct {
Pubkeys []string `toml:"pubkeys"`
CanInvite bool `toml:"can_invite"`
} `toml:"roles"`
Data struct {
Events string `toml:"events"`
Blossom string `toml:"blossom"`
} `toml:"data"`
Roles map[string]Role `toml:"roles"`
}
func LoadConfig(hostname string) (*Config, error) {
@@ -52,3 +52,36 @@ func LoadConfig(hostname string) (*Config, error) {
return &config, nil
}
func (config *Config) IsSelf(pubkey nostr.PubKey) bool {
return pubkey == nostr.MustSecretKeyFromHex(config.Self.Secret).Public()
}
func (config *Config) IsOwner(pubkey nostr.PubKey) bool {
return pubkey == nostr.MustPubKeyFromHex(config.Self.Pubkey)
}
func (config *Config) GetRolesForPubkey(pubkey nostr.PubKey) []Role {
roles := make([]Role, 0)
for name, role := range config.Roles {
if name == "member" {
roles = append(roles, role)
}
if slices.Contains(role.Pubkeys, pubkey.Hex()) {
roles = append(roles, role)
}
}
return roles
}
func (config *Config) CanManage(roles []Role) bool {
for _, role := range roles {
if role.CanManage {
return true
}
}
return false
}