sdk: fetch and cache profiles.
This commit is contained in:
31
sdk/init.go
31
sdk/init.go
@@ -9,15 +9,16 @@ import (
|
||||
)
|
||||
|
||||
type System struct {
|
||||
relaysCache cache.Cache32[[]Relay]
|
||||
followsCache cache.Cache32[[]Follow]
|
||||
metadataCache cache.Cache32[*ProfileMetadata]
|
||||
pool *nostr.SimplePool
|
||||
metadataRelays []string
|
||||
relayListRelays []string
|
||||
relaysCache cache.Cache32[[]Relay]
|
||||
followsCache cache.Cache32[[]Follow]
|
||||
metadataCache cache.Cache32[ProfileMetadata]
|
||||
pool *nostr.SimplePool
|
||||
relayListRelays []string
|
||||
followListRelays []string
|
||||
metadataRelays []string
|
||||
}
|
||||
|
||||
func (sys System) FetchRelaysForPubkey(ctx context.Context, pubkey string) []Relay {
|
||||
func (sys System) FetchRelays(ctx context.Context, pubkey string) []Relay {
|
||||
if v, ok := sys.relaysCache.Get(pubkey); ok {
|
||||
return v
|
||||
}
|
||||
@@ -29,8 +30,8 @@ func (sys System) FetchRelaysForPubkey(ctx context.Context, pubkey string) []Rel
|
||||
return res
|
||||
}
|
||||
|
||||
func (sys System) FetchOutboxRelaysForPubkey(ctx context.Context, pubkey string) []string {
|
||||
relays := sys.FetchRelaysForPubkey(ctx, pubkey)
|
||||
func (sys System) FetchOutboxRelays(ctx context.Context, pubkey string) []string {
|
||||
relays := sys.FetchRelays(ctx, pubkey)
|
||||
result := make([]string, 0, len(relays))
|
||||
for _, relay := range relays {
|
||||
if relay.Outbox {
|
||||
@@ -39,3 +40,15 @@ func (sys System) FetchOutboxRelaysForPubkey(ctx context.Context, pubkey string)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (sys System) FetchProfileMetadata(ctx context.Context, pubkey string) ProfileMetadata {
|
||||
if v, ok := sys.metadataCache.Get(pubkey); ok {
|
||||
return v
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, time.Second*5)
|
||||
defer cancel()
|
||||
res := FetchProfileMetadata(ctx, sys.pool, pubkey, sys.metadataRelays...)
|
||||
sys.metadataCache.SetWithTTL(pubkey, res, time.Hour*6)
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user