sdk: automatically store replaceable events since we already check the local store for them.

This commit is contained in:
fiatjaf
2025-12-21 22:14:59 -03:00
parent dd6d3c68fb
commit 99197d8321
4 changed files with 14 additions and 1 deletions

View File

@@ -73,6 +73,7 @@ func fetchGenericList[V comparable, I TagItemWithValue[V]](
newV := tryFetchListFromNetwork(ctx, sys, pubkey, replaceableIndex, parseTag) newV := tryFetchListFromNetwork(ctx, sys, pubkey, replaceableIndex, parseTag)
if newV != nil && newV.Event.CreatedAt > v.Event.CreatedAt { if newV != nil && newV.Event.CreatedAt > v.Event.CreatedAt {
v = *newV v = *newV
sys.Store.ReplaceEvent(*v.Event)
} }
// register this even if we didn't find anything because we tried // register this even if we didn't find anything because we tried
@@ -93,6 +94,7 @@ func fetchGenericList[V comparable, I TagItemWithValue[V]](
// we'll only save this if we got something which means we found at least one event // we'll only save this if we got something which means we found at least one event
lastFetchKey := makeLastFetchKey(actualKind, pubkey) lastFetchKey := makeLastFetchKey(actualKind, pubkey)
sys.KVStore.Set(lastFetchKey, encodeTimestamp(nostr.Now())) sys.KVStore.Set(lastFetchKey, encodeTimestamp(nostr.Now()))
sys.Store.SaveEvent(*v.Event)
} }
// save cache even if we didn't get anything // save cache even if we didn't get anything

View File

@@ -124,6 +124,8 @@ func (sys *System) FetchProfileMetadata(ctx context.Context, pubkey nostr.PubKey
newM := sys.tryFetchMetadataFromNetwork(ctx, pubkey) newM := sys.tryFetchMetadataFromNetwork(ctx, pubkey)
if newM != nil && newM.Event.CreatedAt > pm.Event.CreatedAt { if newM != nil && newM.Event.CreatedAt > pm.Event.CreatedAt {
pm = *newM pm = *newM
sys.Store.ReplaceEvent(*pm.Event)
} }
// even if we didn't find anything register this because we tried // even if we didn't find anything register this because we tried
@@ -140,6 +142,8 @@ func (sys *System) FetchProfileMetadata(ctx context.Context, pubkey nostr.PubKey
if newM := sys.tryFetchMetadataFromNetwork(ctx, pubkey); newM != nil { if newM := sys.tryFetchMetadataFromNetwork(ctx, pubkey); newM != nil {
pm = *newM pm = *newM
sys.Store.SaveEvent(*pm.Event)
// we'll only save this if we got something which means we found at least one event // we'll only save this if we got something which means we found at least one event
lastFetchKey := makeLastFetchKey(0, pubkey) lastFetchKey := makeLastFetchKey(0, pubkey)
sys.KVStore.Set(lastFetchKey, encodeTimestamp(nostr.Now())) sys.KVStore.Set(lastFetchKey, encodeTimestamp(nostr.Now()))

View File

@@ -63,6 +63,9 @@ func fetchGenericSets[V comparable, I TagItemWithValue[V]](
// unlike for lists, when fetching sets we will blindly trust whatever we get from the network // unlike for lists, when fetching sets we will blindly trust whatever we get from the network
v = *newV v = *newV
for _, evt := range newV.Events {
sys.Store.ReplaceEvent(evt)
}
// even if we didn't find anything register this because we tried // even if we didn't find anything register this because we tried
// (and we still have the previous event in our local store) // (and we still have the previous event in our local store)
@@ -79,6 +82,10 @@ func fetchGenericSets[V comparable, I TagItemWithValue[V]](
if newV := tryFetchSetsFromNetwork(ctx, sys, pubkey, addressableIndex, parseTag); newV != nil { if newV := tryFetchSetsFromNetwork(ctx, sys, pubkey, addressableIndex, parseTag); newV != nil {
v = *newV v = *newV
for _, evt := range newV.Events {
sys.Store.ReplaceEvent(evt)
}
// we'll only save this if we got something which means we found at least one event // we'll only save this if we got something which means we found at least one event
lastFetchKey := makeLastFetchKey(actualKind, pubkey) lastFetchKey := makeLastFetchKey(actualKind, pubkey)
sys.KVStore.Set(lastFetchKey, encodeTimestamp(nostr.Now())) sys.KVStore.Set(lastFetchKey, encodeTimestamp(nostr.Now()))

View File

@@ -93,7 +93,7 @@ func (rs *RelayStream) Next() string {
func NewSystem() *System { func NewSystem() *System {
sys := &System{ sys := &System{
KVStore: kvstore_memory.NewStore(), KVStore: kvstore_memory.NewStore(),
RelayListRelays: NewRelayStream("wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"), RelayListRelays: NewRelayStream("wss://indexer.coracle.social", "wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"),
FollowListRelays: NewRelayStream("wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"), FollowListRelays: NewRelayStream("wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"),
MetadataRelays: NewRelayStream("wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"), MetadataRelays: NewRelayStream("wss://purplepag.es", "wss://user.kindpag.es", "wss://relay.nos.social"),
FallbackRelays: NewRelayStream( FallbackRelays: NewRelayStream(