mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-17 19:13:56 +02:00
sdk: fetch and cache profiles.
This commit is contained in:
@@ -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)
|
||||
|
Reference in New Issue
Block a user