sdk: fetch and cache profiles.

This commit is contained in:
fiatjaf
2023-10-31 11:00:46 -03:00
parent aaaf608c2b
commit 374dbbe1a0
5 changed files with 47 additions and 15 deletions

View File

@@ -28,10 +28,32 @@ func (p ProfileMetadata) Npub() string {
}
func (p ProfileMetadata) Nprofile(ctx context.Context, sys *System, nrelays int) string {
v, _ := nip19.EncodeProfile(p.pubkey, sys.FetchOutboxRelaysForPubkey(ctx, p.pubkey))
v, _ := nip19.EncodeProfile(p.pubkey, sys.FetchOutboxRelays(ctx, p.pubkey))
return v
}
func FetchProfileMetadata(ctx context.Context, pool *nostr.SimplePool, pubkey string, relays ...string) ProfileMetadata {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ch := pool.SubManyEose(ctx, relays, nostr.Filters{
{
Kinds: []int{nostr.KindProfileMetadata},
Authors: []string{pubkey},
Limit: 1,
},
})
for ie := range ch {
if m, err := ParseMetadata(ie.Event); err == nil {
m.pubkey = pubkey
return *m
}
}
return ProfileMetadata{pubkey: pubkey}
}
func ParseMetadata(event *nostr.Event) (*ProfileMetadata, error) {
if event.Kind != 0 {
return nil, fmt.Errorf("event %s is kind %d, not 0", event.ID, event.Kind)