forked from coracle/zooid
Switch to instance, stub out relay methods
This commit is contained in:
@@ -3,10 +3,14 @@
|
||||
|
||||
## Codebase Overview
|
||||
|
||||
- **zooid/config.go**: Defines `Config` struct with TOML tags for relay configuration (self info, groups, roles, data paths). Contains `loadConfig()` function that parses hostname-based config files from `configs/` directory.
|
||||
- **zooid/config.go**: Defines `Config` struct with TOML tags for relay configuration (self, groups, management, blossom, roles, data). Contains `LoadConfig()` function and `IsMember()` method.
|
||||
|
||||
- **zooid/http.go**: Core HTTP handling with dynamic instance creation. `getInstance()` function loads config and creates khatru relay instances on-demand. `ServeHTTP()` function routes requests to appropriate relay instances based on hostname.
|
||||
- **zooid/http.go**: Simple HTTP handler that calls `GetInstance()` and delegates to khatru relay.
|
||||
|
||||
- **zooid/util.go**: Environment variable utilities. `Env()` function with fallback support for configuration.
|
||||
- **zooid/instance.go**: Core instance management. `Instance` struct holds config and khatru relay. `MakeInstance()` creates configured relay instances with handlers. `GetInstance()` provides singleton access with lazy loading.
|
||||
|
||||
- **cmd/relay/main.go**: Main entry point that starts HTTP server with graceful shutdown handling. Uses `zooid.ServeHTTP` as the handler wrapped in `http.HandlerFunc`.
|
||||
- **zooid/blossom.go**: Blossom file storage integration with member-only access controls.
|
||||
|
||||
- **zooid/util.go**: Environment variable utilities with `Env()` function.
|
||||
|
||||
- **cmd/relay/main.go**: HTTP server entry point with graceful shutdown.
|
||||
|
||||
@@ -61,6 +61,7 @@ A special `[roles.member]` heading may be used to configure policies for all rel
|
||||
Contains information related to data persistence.
|
||||
|
||||
- `events` - the location of the sqlite database file used to store events. Defaults to `./data/{my-relay}/events`.
|
||||
- `media` - the location of the sqlite database file used to store file metadata. Defaults to `./data/{my-relay}/media`.
|
||||
|
||||
### Example
|
||||
|
||||
|
||||
@@ -3,12 +3,14 @@ module zooid
|
||||
go 1.24.1
|
||||
|
||||
require (
|
||||
fiatjaf.com/nostr v0.0.0-20250923223459-3c540e726e17
|
||||
fiatjaf.com/nostr v0.0.0-20250924142401-59bd3c29fffd
|
||||
github.com/BurntSushi/toml v1.5.0
|
||||
github.com/spf13/afero v1.15.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/ImVexed/fasturl v0.0.0-20230304231329-4e41488060f3 // indirect
|
||||
github.com/PowerDNS/lmdb-go v1.9.3 // indirect
|
||||
github.com/andybalholm/brotli v1.1.1 // indirect
|
||||
github.com/bep/debounce v1.2.1 // indirect
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
|
||||
@@ -20,6 +22,7 @@ require (
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
github.com/liamg/magic v0.0.1 // indirect
|
||||
github.com/mailru/easyjson v0.9.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
@@ -33,4 +36,5 @@ require (
|
||||
github.com/valyala/fasthttp v1.59.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
|
||||
golang.org/x/net v0.41.0 // indirect
|
||||
golang.org/x/text v0.28.0 // indirect
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
fiatjaf.com/nostr v0.0.0-20250923223459-3c540e726e17 h1:bCx2ExbAMz3TXOiPn5TS2HWK/90KOq3GOTlHFSguobs=
|
||||
fiatjaf.com/nostr v0.0.0-20250923223459-3c540e726e17/go.mod h1:Nq86Jjsd0OmsOEImUg0iCcLuqM5B67Nj2eu/2dP74Ss=
|
||||
fiatjaf.com/nostr v0.0.0-20250924142401-59bd3c29fffd h1:LnbRz+TxZAROXglKFT+Lqsdqe5Pu8PG0rSpmXGnES90=
|
||||
fiatjaf.com/nostr v0.0.0-20250924142401-59bd3c29fffd/go.mod h1:Nq86Jjsd0OmsOEImUg0iCcLuqM5B67Nj2eu/2dP74Ss=
|
||||
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/ImVexed/fasturl v0.0.0-20230304231329-4e41488060f3 h1:ClzzXMDDuUbWfNNZqGeYq4PnYOlwlOVIvSyNaIy0ykg=
|
||||
@@ -26,6 +26,8 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0/go.mod h1:ZXNYxsqcloTdSy/rNShjY
|
||||
github.com/dvyukov/go-fuzz v0.0.0-20200318091601-be3528f3a813/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
|
||||
github.com/fasthttp/websocket v1.5.12 h1:e4RGPpWW2HTbL3zV0Y/t7g0ub294LkiuXXUuTOUInlE=
|
||||
github.com/fasthttp/websocket v1.5.12/go.mod h1:I+liyL7/4moHojiOgUOIKEWm9EIxHqxZChS+aMFltyg=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||
@@ -33,6 +35,12 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/liamg/magic v0.0.1 h1:Ru22ElY+sCh6RvRTWjQzKKCxsEco8hE0co8n1qe7TBM=
|
||||
github.com/liamg/magic v0.0.1/go.mod h1:yQkOmZZI52EA+SQ2xyHpVw8fNvTBruF873Y+Vt6S+fk=
|
||||
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
|
||||
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
@@ -40,14 +48,20 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/puzpuzpuz/xsync/v3 v3.5.1 h1:GJYJZwO6IdxN/IKbneznS6yPkVC+c3zyY/j19c++5Fg=
|
||||
github.com/puzpuzpuz/xsync/v3 v3.5.1/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
|
||||
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
||||
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 h1:D0vL7YNisV2yqE55+q0lFuGse6U8lxlg7fYTctlT5Gc=
|
||||
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38/go.mod h1:sM7Mt7uEoCeFSCBM+qBrqvEo+/9vdmj19wzp3yzUhmg=
|
||||
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
|
||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
@@ -70,7 +84,11 @@ golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE
|
||||
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM=
|
||||
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
|
||||
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
|
||||
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
|
||||
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
||||
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
||||
|
||||
+86
-2
@@ -1,8 +1,92 @@
|
||||
package zooid
|
||||
|
||||
import (
|
||||
"fiatjaf.com/nostr/khatru"
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net/url"
|
||||
|
||||
"fiatjaf.com/nostr"
|
||||
"fiatjaf.com/nostr/eventstore/lmdb"
|
||||
"fiatjaf.com/nostr/khatru/blossom"
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
func EnableBlossom(config *Config, relay *khatru.Relay) {
|
||||
func EnableBlossom(instance *Instance) {
|
||||
fs := afero.NewOsFs()
|
||||
|
||||
if err := fs.MkdirAll(instance.Config.Blossom.Directory, 0755); err != nil {
|
||||
log.Fatal("🚫 error creating blossom path:", err)
|
||||
}
|
||||
|
||||
backend := &lmdb.LMDBBackend{Path: instance.Config.Data.Blossom}
|
||||
if err := backend.Init(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
blossom := blossom.New(instance.Relay, "https://"+instance.Host)
|
||||
|
||||
blossom.Store = backend
|
||||
|
||||
blossom.StoreBlob = func(ctx context.Context, sha256 string, ext string, body []byte) error {
|
||||
file, err := fs.Create(instance.Config.Blossom.Directory + "/" + sha256)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := io.Copy(file, bytes.NewReader(body)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
blossom.LoadBlob = func(ctx context.Context, sha256 string, ext string) (io.ReadSeeker, *url.URL, error) {
|
||||
file, err := fs.Open(instance.Config.Blossom.Directory + "/" + sha256)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return file, nil, nil
|
||||
}
|
||||
|
||||
blossom.DeleteBlob = func(ctx context.Context, sha256 string, ext string) error {
|
||||
return fs.Remove(instance.Config.Blossom.Directory + "/" + sha256)
|
||||
}
|
||||
|
||||
blossom.RejectUpload = func(ctx context.Context, auth *nostr.Event, size int, ext string) (bool, string, int) {
|
||||
if size > 10*1024*1024 {
|
||||
return true, "file too large", 413
|
||||
}
|
||||
|
||||
if auth == nil || !instance.IsMember(auth.PubKey) {
|
||||
return true, "unauthorized", 403
|
||||
}
|
||||
|
||||
return false, ext, size
|
||||
}
|
||||
|
||||
blossom.RejectGet = func(ctx context.Context, auth *nostr.Event, sha256 string, ext string) (bool, string, int) {
|
||||
if auth == nil || !instance.IsMember(auth.PubKey) {
|
||||
return true, "unauthorized", 403
|
||||
}
|
||||
|
||||
return false, "", 200
|
||||
}
|
||||
|
||||
blossom.RejectList = func(ctx context.Context, auth *nostr.Event, pubkey nostr.PubKey) (bool, string, int) {
|
||||
if auth == nil || !instance.IsMember(auth.PubKey) {
|
||||
return true, "unauthorized", 403
|
||||
}
|
||||
|
||||
return false, "", 200
|
||||
}
|
||||
|
||||
blossom.RejectDelete = func(ctx context.Context, auth *nostr.Event, sha256 string, ext string) (bool, string, int) {
|
||||
if auth == nil || !instance.IsMember(auth.PubKey) {
|
||||
return true, "unauthorized", 403
|
||||
}
|
||||
|
||||
return false, "", 200
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ type Config struct {
|
||||
|
||||
Data struct {
|
||||
Events string `toml:"events"`
|
||||
Blossom string `toml:"blossom"`
|
||||
} `toml:"data"`
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -1,9 +1,8 @@
|
||||
package zooid
|
||||
|
||||
import (
|
||||
"fiatjaf.com/nostr/khatru"
|
||||
)
|
||||
|
||||
func EnableGroups(config *Config, relay *khatru.Relay) {
|
||||
relay.Info.SupportedNIPs = append(relay.Info.SupportedNIPs, 29)
|
||||
func EnableGroups(instance *Instance) {
|
||||
instance.Relay.Info.SupportedNIPs = append(instance.Relay.Info.SupportedNIPs, 29)
|
||||
}
|
||||
|
||||
+3
-3
@@ -6,12 +6,12 @@ import (
|
||||
)
|
||||
|
||||
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
relay, err := GetRelay(r.Host)
|
||||
instance, err := GetInstance(r.Host)
|
||||
if err != nil {
|
||||
log.Printf("Failed to load relay config for hostname %s: %v", r.Host, err)
|
||||
log.Printf("Failed to load config for hostname %s: %v", r.Host, err)
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
relay.ServeHTTP(w, r)
|
||||
instance.Relay.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
package zooid
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"iter"
|
||||
"net/http"
|
||||
"context"
|
||||
|
||||
"fiatjaf.com/nostr/khatru"
|
||||
"fiatjaf.com/nostr"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
Host string
|
||||
Config *Config
|
||||
Relay *khatru.Relay
|
||||
}
|
||||
|
||||
func MakeInstance(hostname string) (*Instance, error) {
|
||||
config, err := LoadConfig(hostname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pubkey, err := nostr.PubKeyFromHex(config.Self.Pubkey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// secret, err := nostr.SecretKeyFromHex(config.Self.Secret)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
instance := &Instance{
|
||||
Host: hostname,
|
||||
Config: config,
|
||||
Relay: khatru.NewRelay(),
|
||||
}
|
||||
|
||||
instance.Relay.Info.Name = config.Self.Name
|
||||
instance.Relay.Info.Icon = config.Self.Icon
|
||||
instance.Relay.Info.PubKey = &pubkey
|
||||
instance.Relay.Info.Description = config.Self.Description
|
||||
// instance.Relay.Info.Self = nostr.GetPublicKey(secret)
|
||||
instance.Relay.Info.Software = "https://github.com/coracle-social/zooid"
|
||||
instance.Relay.Info.Version = "v0.1.0"
|
||||
|
||||
instance.Relay.OnConnect = instance.OnConnect
|
||||
instance.Relay.OnEvent = instance.OnEvent
|
||||
instance.Relay.StoreEvent = instance.StoreEvent
|
||||
instance.Relay.ReplaceEvent = instance.ReplaceEvent
|
||||
instance.Relay.DeleteEvent = instance.DeleteEvent
|
||||
instance.Relay.OnEventSaved = instance.OnEventSaved
|
||||
instance.Relay.OnEphemeralEvent = instance.OnEphemeralEvent
|
||||
instance.Relay.OnRequest = instance.OnRequest
|
||||
instance.Relay.QueryStored = instance.QueryStored
|
||||
instance.Relay.RejectConnection = instance.RejectConnection
|
||||
instance.Relay.PreventBroadcast = instance.PreventBroadcast
|
||||
|
||||
if config.Groups.Enabled {
|
||||
EnableGroups(instance)
|
||||
}
|
||||
|
||||
if config.Blossom.Enabled {
|
||||
EnableBlossom(instance)
|
||||
}
|
||||
|
||||
if config.Management.Enabled {
|
||||
EnableManagement(instance)
|
||||
}
|
||||
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
var (
|
||||
instances map[string]*Instance
|
||||
instanceOnce sync.Once
|
||||
)
|
||||
|
||||
func GetInstance(hostname string) (*Instance, error) {
|
||||
instanceOnce.Do(func() {
|
||||
instances = make(map[string]*Instance)
|
||||
})
|
||||
|
||||
instance, exists := instances[hostname]
|
||||
if !exists {
|
||||
newInstance, err := MakeInstance(hostname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
instances[hostname] = newInstance
|
||||
instance = newInstance
|
||||
}
|
||||
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
// Utility methods
|
||||
|
||||
func (instance *Instance) IsMember(pubkey nostr.PubKey) bool {
|
||||
pubkeyStr := pubkey.String()
|
||||
for _, role := range instance.Config.Roles {
|
||||
for _, pk := range role.Pubkeys {
|
||||
if pk == pubkeyStr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Handlers
|
||||
|
||||
func (instance *Instance) OnConnect(ctx context.Context) {
|
||||
khatru.RequestAuth(ctx)
|
||||
}
|
||||
|
||||
func (instance *Instance) OnEvent(ctx context.Context, event nostr.Event) (reject bool, msg string) {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
func (instance *Instance) StoreEvent(ctx context.Context, event nostr.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (instance *Instance) ReplaceEvent(ctx context.Context, event nostr.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (instance *Instance) DeleteEvent(ctx context.Context, id nostr.ID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (instance *Instance) OnEventSaved(ctx context.Context, event nostr.Event) {
|
||||
}
|
||||
|
||||
func (instance *Instance) OnEphemeralEvent(ctx context.Context, event nostr.Event) {
|
||||
}
|
||||
|
||||
func (instance *Instance) OnRequest(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
func (instance *Instance) QueryStored(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] {
|
||||
return func(yield func(nostr.Event) bool) {
|
||||
// TODO: Implement actual event querying logic
|
||||
// For now, return empty sequence
|
||||
}
|
||||
}
|
||||
|
||||
func (instance *Instance) RejectConnection(r *http.Request) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (instance *Instance) PreventBroadcast(ws *khatru.WebSocket, event nostr.Event) bool {
|
||||
return event.Kind == 28934
|
||||
}
|
||||
+1
-2
@@ -1,8 +1,7 @@
|
||||
package zooid
|
||||
|
||||
import (
|
||||
"fiatjaf.com/nostr/khatru"
|
||||
)
|
||||
|
||||
func EnableManagement(config *Config, relay *khatru.Relay) {
|
||||
func EnableManagement(instance *Instance) {
|
||||
}
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
package zooid
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"fiatjaf.com/nostr/khatru"
|
||||
)
|
||||
|
||||
func MakeRelay(hostname string, config *Config) *khatru.Relay {
|
||||
relay := khatru.NewRelay()
|
||||
relay.Info.Name = config.Name
|
||||
relay.Info.Icon = config.Icon
|
||||
relay.Info.PubKey = config.Pubkey
|
||||
relay.Info.Description = config.Description
|
||||
relay.Info.Self = nostr.GetPublicKey(config.Secret)
|
||||
relay.Info.Software = "https://github.com/coracle-social/zooid"
|
||||
relay.Info.Version = "v0.1.0"
|
||||
|
||||
relay.OnConnect = append(relay.OnConnect, khatru.RequestAuth)
|
||||
relay.RejectFilter = append(relay.RejectFilter, RejectFilter)
|
||||
relay.QueryEvents = append(relay.QueryEvents, QueryEvents)
|
||||
relay.DeleteEvent = append(relay.DeleteEvent, DeleteEvent)
|
||||
relay.RejectEvent = append(relay.RejectEvent, RejectEvent)
|
||||
relay.StoreEvent = append(relay.StoreEvent, SaveEvent)
|
||||
relay.OnEventSaved = append(relay.OnEventSaved, OnEventSaved)
|
||||
|
||||
if config.Groups.Enabled {
|
||||
EnableGroups(config, relay)
|
||||
}
|
||||
|
||||
if config.Blossom.Enabled {
|
||||
EnableBlossom(config, relay)
|
||||
}
|
||||
|
||||
if config.Management.Enabled {
|
||||
EnableManagement(config, relay)
|
||||
}
|
||||
|
||||
return relay
|
||||
}
|
||||
|
||||
var (
|
||||
relays map[string]*khatru.Relay
|
||||
relayOnce sync.Once
|
||||
)
|
||||
|
||||
func GetRelay(hostname string) (*khatru.Relay, error) {
|
||||
relayOnce.Do(func() {
|
||||
relays = make(map[string]*khatru.Relay)
|
||||
})
|
||||
|
||||
relay, exists := relays[hostname]
|
||||
if !exists {
|
||||
config, err := LoadConfig(hostname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newRelay := MakeRelay(hostname, config)
|
||||
|
||||
relays[hostname] = newRelay
|
||||
relay = newRelay
|
||||
}
|
||||
|
||||
return relay, nil
|
||||
}
|
||||
Reference in New Issue
Block a user