partial docs update.
This commit is contained in:
@@ -41,7 +41,7 @@ After a client is authenticated and opens a new subscription with `REQ` or sends
|
||||
|
||||
```go
|
||||
relay.RejectFilter = append(relay.RejectFilter, func(ctx context.Context, filter nostr.Filter) (bool, string) {
|
||||
authenticatedUser := khatru.GetAuthed(ctx)
|
||||
authenticatedUser, isAuthenticated := khatru.GetAuthed(ctx)
|
||||
})
|
||||
```
|
||||
|
||||
@@ -77,7 +77,7 @@ You can use that to emulate a listener for these events in case you want to keep
|
||||
case <-ctx.Done():
|
||||
fmt.Println("connection closed")
|
||||
case <-conn.Authed:
|
||||
fmt.Println("authenticated as", conn.AuthedPublicKey)
|
||||
fmt.Println("authenticated as", conn.AuthedPubKey)
|
||||
}
|
||||
}(ctx)
|
||||
},
|
||||
|
||||
@@ -24,7 +24,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"fiatjaf.com/nostr/eventstore/badger"
|
||||
"github.com/fiatjaf/khatru"
|
||||
"fiatjaf.com/nostr/khatru"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -35,11 +35,7 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
|
||||
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
|
||||
relay.CountEvents = append(relay.CountEvents, db.CountEvents)
|
||||
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
|
||||
relay.ReplaceEvent = append(relay.ReplaceEvent, db.ReplaceEvent)
|
||||
relay.UseEventstore(db)
|
||||
|
||||
fmt.Println("running on :3334")
|
||||
http.ListenAndServe(":3334", relay)
|
||||
@@ -58,7 +54,7 @@ If you want to use two different adapters at the same time that's easy. Just add
|
||||
|
||||
```go
|
||||
relay.StoreEvent = append(relay.StoreEvent, db1.SaveEvent, db2.SaveEvent)
|
||||
relay.QueryEvents = append(relay.QueryEvents, db1.QueryEvents, db2.SaveEvent)
|
||||
relay.QueryEvents = append(relay.QueryEvents, db1.QueryEvents, db2.QueryEvents)
|
||||
```
|
||||
|
||||
But that will duplicate events on both and then return duplicated events on each query.
|
||||
@@ -72,28 +68,29 @@ For example, maybe you want kind 1 events in `db1` and kind 30023 events in `db3
|
||||
```go
|
||||
relay.StoreEvent = append(relay.StoreEvent, func (ctx context.Context, evt *nostr.Event) error {
|
||||
switch evt.Kind {
|
||||
case 1:
|
||||
return db1.StoreEvent(ctx, evt)
|
||||
case 30023:
|
||||
return db30023.StoreEvent(ctx, evt)
|
||||
case nostr.Kind(1):
|
||||
return db1.SaveEvent(evt)
|
||||
case nostr.Kind(30023):
|
||||
return db30023.SaveEvent(evt)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
})
|
||||
relay.QueryEvents = append(relay.QueryEvents, func (ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) {
|
||||
relay.QueryEvents = append(relay.QueryEvents, func (ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] {
|
||||
for _, kind := range filter.Kinds {
|
||||
switch kind {
|
||||
case 1:
|
||||
switch nostr.Kind(kind) {
|
||||
case nostr.Kind(1):
|
||||
filter1 := filter
|
||||
filter1.Kinds = []int{1}
|
||||
return db1.QueryEvents(ctx, filter1)
|
||||
case 30023:
|
||||
filter1.Kinds = []nostr.Kind{1}
|
||||
return db1.QueryEvents(filter1)
|
||||
case nostr.Kind(30023):
|
||||
filter30023 := filter
|
||||
filter30023.Kinds = []int{30023}
|
||||
return db30023.QueryEvents(ctx, filter30023)
|
||||
filter30023.Kinds = []nostr.Kind{30023}
|
||||
return db30023.QueryEvents(filter30023)
|
||||
default:
|
||||
return nil, nil
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
@@ -6,15 +6,15 @@ outline: deep
|
||||
|
||||
[NIP-86](https://nips.nostr.com/86) specifies a set of RPC methods for managing the boring aspects of relays, such as whitelisting or banning users, banning individual events, banning IPs and so on.
|
||||
|
||||
All [`khatru.Relay`](https://pkg.go.dev/github.com/fiatjaf/khatru#Relay) instances expose a field `ManagementAPI` with a [`RelayManagementAPI`](https://pkg.go.dev/github.com/fiatjaf/khatru#RelayManagementAPI) instance inside, which can be used for creating handlers for each of the RPC methods.
|
||||
All [`khatru.Relay`](https://pkg.go.dev/fiatjaf.com/nostr/khatru#Relay) instances expose a field `ManagementAPI` with a [`RelayManagementAPI`](https://pkg.go.dev/fiatjaf.com/nostr/khatru#RelayManagementAPI) instance inside, which can be used for creating handlers for each of the RPC methods.
|
||||
|
||||
There is also a generic `RejectAPICall` which is a slice of functions that will be called before any RPC method, if they exist and, if any of them returns true, the request will be rejected.
|
||||
|
||||
The most basic implementation of a `RejectAPICall` handler would be one that checks the public key of the caller with a hardcoded public key of the relay owner:
|
||||
|
||||
```go
|
||||
var owner = "<my-own-pubkey>"
|
||||
var allowedPubkeys = make([]string, 0, 10)
|
||||
var owner = nostr.MustPubKeyFromHex("<my-own-pubkey>")
|
||||
var allowedPubkeys = make([]nostr.PubKey, 0, 10)
|
||||
|
||||
func main () {
|
||||
relay := khatru.NewRelay()
|
||||
@@ -29,16 +29,17 @@ func main () {
|
||||
}
|
||||
)
|
||||
|
||||
relay.ManagementAPI.AllowPubKey = func(ctx context.Context, pubkey string, reason string) error {
|
||||
relay.ManagementAPI.AllowPubKey = func(ctx context.Context, pubkey nostr.PubKey, reason string) error {
|
||||
allowedPubkeys = append(allowedPubkeys, pubkey)
|
||||
return nil
|
||||
}
|
||||
relay.ManagementAPI.BanPubKey = func(ctx context.Context, pubkey string, reason string) error {
|
||||
relay.ManagementAPI.BanPubKey = func(ctx context.Context, pubkey nostr.PubKey, reason string) error {
|
||||
idx := slices.Index(allowedPubkeys, pubkey)
|
||||
if idx == -1 {
|
||||
return fmt.Errorf("pubkey already not allowed")
|
||||
}
|
||||
allowedPubkeys = slices.Delete(allowedPubkeys, idx, idx+1)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -48,12 +49,12 @@ You can also not provide any `RejectAPICall` handler and do the approval specifi
|
||||
In the following example any current member can include any other pubkey, and anyone who was added before is able to remove any pubkey that was added afterwards (not a very good idea, but serves as an example).
|
||||
|
||||
```go
|
||||
var allowedPubkeys = []string{"<my-own-pubkey>"}
|
||||
var allowedPubkeys = []nostr.PubKey{nostr.MustPubKeyFromHex("<my-own-pubkey>")}
|
||||
|
||||
func main () {
|
||||
relay := khatru.NewRelay()
|
||||
|
||||
relay.ManagementAPI.AllowPubKey = func(ctx context.Context, pubkey string, reason string) error {
|
||||
relay.ManagementAPI.AllowPubKey = func(ctx context.Context, pubkey nostr.PubKey, reason string) error {
|
||||
caller := khatru.GetAuthed(ctx)
|
||||
|
||||
if slices.Contains(allowedPubkeys, caller) {
|
||||
@@ -63,7 +64,7 @@ func main () {
|
||||
|
||||
return fmt.Errorf("you're not authorized")
|
||||
}
|
||||
relay.ManagementAPI.BanPubKey = func(ctx context.Context, pubkey string, reason string) error {
|
||||
relay.ManagementAPI.BanPubKey = func(ctx context.Context, pubkey nostr.PubKey, reason string) error {
|
||||
caller := khatru.GetAuthed(ctx)
|
||||
|
||||
callerIdx := slices.Index(allowedPubkeys, caller)
|
||||
@@ -82,4 +83,3 @@ func main () {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user