sdk: track event relays on kvstore.

This commit is contained in:
fiatjaf 2025-01-15 00:30:24 -03:00
parent 795f9516ae
commit ba38c28b99
2 changed files with 31 additions and 0 deletions

View File

@ -112,6 +112,8 @@ func NewSystem(mods ...SystemModifier) *System {
sys.Pool = nostr.NewSimplePool(context.Background(),
nostr.WithAuthorKindQueryMiddleware(sys.TrackQueryAttempts),
nostr.WithEventMiddleware(sys.TrackEventHints),
nostr.WithEventMiddleware(sys.TrackEventRelays),
nostr.WithDuplicateMiddleware(sys.TrackEventRelaysD),
nostr.WithPenaltyBox(),
)

View File

@ -1,6 +1,7 @@
package sdk
import (
"encoding/hex"
"net/url"
"github.com/nbd-wtf/go-nostr"
@ -108,3 +109,31 @@ func (sys *System) TrackEventHints(ie nostr.RelayEvent) {
}
}
}
func (sys *System) TrackEventRelays(ie nostr.RelayEvent) {
// decode the event ID hex into bytes
idBytes, err := hex.DecodeString(ie.ID)
if err != nil || len(idBytes) < 8 {
return
}
// store only first 8 bytes of event ID as key
key := idBytes[:8]
// store the relay URL as value
sys.KVStore.Set(key, []byte(ie.Relay.URL))
}
func (sys *System) TrackEventRelaysD(relay, id string) {
// decode the event ID hex into bytes
idBytes, err := hex.DecodeString(id)
if err != nil || len(idBytes) < 8 {
return
}
// store only first 8 bytes of event ID as key
key := idBytes[:8]
// store the relay URL as value
sys.KVStore.Set(key, []byte(relay))
}