it never ends.

This commit is contained in:
fiatjaf
2025-04-16 02:59:47 -03:00
parent cb0dd45a32
commit 5b8954461f
53 changed files with 396 additions and 673 deletions

View File

@@ -2,8 +2,6 @@ package sdk
import (
"context"
"encoding/hex"
"fmt"
"slices"
"sync"
"sync/atomic"
@@ -16,10 +14,10 @@ const (
pubkeyStreamOldestPrefix = byte('O')
)
func makePubkeyStreamKey(prefix byte, pubkey string) []byte {
func makePubkeyStreamKey(prefix byte, pubkey nostr.PubKey) []byte {
key := make([]byte, 1+8)
key[0] = prefix
hex.Decode(key[1:], []byte(pubkey[0:16]))
copy(key[1:], pubkey[0:8])
return key
}
@@ -30,9 +28,9 @@ func makePubkeyStreamKey(prefix byte, pubkey string) []byte {
func (sys *System) StreamLiveFeed(
ctx context.Context,
pubkeys []nostr.PubKey,
kinds []int,
) (<-chan *nostr.Event, error) {
events := make(chan *nostr.Event)
kinds []uint16,
) (<-chan nostr.Event, error) {
events := make(chan nostr.Event)
active := atomic.Int32{}
active.Add(int32(len(pubkeys)))
@@ -61,15 +59,17 @@ func (sys *System) StreamLiveFeed(
}
filter := nostr.Filter{
Authors: []string{pubkey},
Authors: []nostr.PubKey{pubkey},
Since: since,
Kinds: kinds,
}
go func() {
sub := sys.Pool.SubscribeMany(ctx, relays, filter, nostr.WithLabel("livefeed"))
sub := sys.Pool.SubscribeMany(ctx, relays, filter, nostr.SubscriptionOptions{
Label: "livefeed",
})
for evt := range sub {
sys.StoreRelay.Publish(ctx, *evt.Event)
sys.Publisher.Publish(ctx, evt.Event)
if latest < evt.CreatedAt {
latest = evt.CreatedAt
serial++
@@ -101,8 +101,8 @@ func (sys *System) StreamLiveFeed(
// for events or if we should just return what we have stored locally.
func (sys *System) FetchFeedPage(
ctx context.Context,
pubkeys []string,
kinds []int,
pubkeys []nostr.PubKey,
kinds []uint16,
until nostr.Timestamp,
totalLimit int,
) ([]*nostr.Event, error) {
@@ -123,21 +123,21 @@ func (sys *System) FetchFeedPage(
}
}
filter := nostr.Filter{Authors: []string{pubkey}, Kinds: kinds}
filter := nostr.Filter{Authors: []nostr.PubKey{pubkey}, Kinds: kinds}
if until > oldestTimestamp {
// we can use our local database
filter.Until = &until
res, err := sys.StoreRelay.QuerySync(ctx, filter)
if err != nil {
return nil, fmt.Errorf("query failure at '%s': %w", pubkey, err)
}
if len(res) >= limitPerKey {
// we got enough from the local store
events = append(events, res...)
wg.Done()
continue
count := 0
for evt := range sys.Store.QueryEvents(filter) {
events = append(events, evt)
count++
if count >= limitPerKey {
// we got enough from the local store
wg.Done()
continue
}
}
}

View File

@@ -10,10 +10,10 @@ import (
)
func runTestWith(t *testing.T, hdb hints.HintsDB) {
const key1 = "0000000000000000000000000000000000000000000000000000000000000001"
const key2 = "0000000000000000000000000000000000000000000000000000000000000002"
const key3 = "0000000000000000000000000000000000000000000000000000000000000003"
const key4 = "0000000000000000000000000000000000000000000000000000000000000004"
key1 := nostr.MustPubKeyFromHex("0000000000000000000000000000000000000000000000000000000000000001")
key2 := nostr.MustPubKeyFromHex("0000000000000000000000000000000000000000000000000000000000000002")
key3 := nostr.MustPubKeyFromHex("0000000000000000000000000000000000000000000000000000000000000003")
key4 := nostr.MustPubKeyFromHex("0000000000000000000000000000000000000000000000000000000000000004")
const relayA = "wss://aaa.com"
const relayB = "wss://bbb.net"
const relayC = "wss://ccc.org"

View File

@@ -14,7 +14,7 @@ import (
// ProfileMetadata represents user profile information from kind 0 events.
// It contains both the raw event and parsed metadata fields.
type ProfileMetadata struct {
PubKey string `json:"-"` // must always be set otherwise things will break
PubKey nostr.PubKey `json:"-"` // must always be set otherwise things will break
Event *nostr.Event `json:"-"` // may be empty if a profile metadata event wasn't found
// every one of these may be empty
@@ -33,8 +33,7 @@ type ProfileMetadata struct {
// Npub returns the NIP-19 npub encoding of the profile's public key.
func (p ProfileMetadata) Npub() string {
v, _ := nip19.EncodePublicKey(p.PubKey)
return v
return nip19.EncodeNpub(p.PubKey)
}
// NpubShort returns a shortened version of the NIP-19 npub encoding,
@@ -47,8 +46,7 @@ func (p ProfileMetadata) NpubShort() string {
// Nprofile returns the NIP-19 nprofile encoding of the profile,
// including relay hints from the user's outbox.
func (p ProfileMetadata) Nprofile(ctx context.Context, sys *System, nrelays int) string {
v, _ := nip19.EncodeProfile(p.PubKey, sys.FetchOutboxRelays(ctx, p.PubKey, 2))
return v
return nip19.EncodeNprofile(p.PubKey, sys.FetchOutboxRelays(ctx, p.PubKey, 2))
}
// ShortName returns the best available name for display purposes.
@@ -105,7 +103,7 @@ func (sys System) FetchProfileFromInput(ctx context.Context, nip19OrNip05Code st
// FetchProfileMetadata fetches metadata for a given user from the local cache, or from the local store,
// or, failing these, from the target user's defined outbox relays -- then caches the result.
// It always returns a ProfileMetadata, even if no metadata was found (in which case only the PubKey field is set).
func (sys *System) FetchProfileMetadata(ctx context.Context, pubkey string) (pm ProfileMetadata) {
func (sys *System) FetchProfileMetadata(ctx context.Context, pubkey nostr.PubKey) (pm ProfileMetadata) {
if v, ok := sys.MetadataCache.Get(pubkey); ok {
return v
}

View File

@@ -5,6 +5,7 @@ import (
"math/rand/v2"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/eventstore/wrappers"
"fiatjaf.com/nostr/sdk/cache"
cache_memory "fiatjaf.com/nostr/sdk/cache/memory"
"fiatjaf.com/nostr/sdk/dataloader"
@@ -51,7 +52,7 @@ type System struct {
NoteSearchRelays *RelayStream
Store eventstore.Store
StoreRelay nostr.RelayStore
Publisher wrappers.StorePublisher
replaceableLoaders []*dataloader.Loader[nostr.PubKey, *nostr.Event]
addressableLoaders []*dataloader.Loader[nostr.PubKey, []*nostr.Event]