sdk: FetchSpecific from input string or from a pointer.

This commit is contained in:
fiatjaf
2025-01-05 14:18:53 -03:00
parent 967a08fb65
commit 36ddf1eb36

View File

@@ -12,33 +12,53 @@ import (
"github.com/nbd-wtf/go-nostr/sdk/hints" "github.com/nbd-wtf/go-nostr/sdk/hints"
) )
// FetchSpecificEventFromInput tries to get a specific event from a NIP-19 code using whatever means necessary.
func (sys *System) FetchSpecificEventFromInput(
ctx context.Context,
input string,
withRelays bool,
) (event *nostr.Event, successRelays []string, err error) {
var pointer nostr.Pointer
_, data, err := nip19.Decode(input)
if err == nil {
switch p := data.(type) {
case nostr.EventPointer:
pointer = p
case nostr.EntityPointer:
pointer = p
case string:
pointer = nostr.EventPointer{ID: input}
default:
return nil, nil, fmt.Errorf("invalid code '%s'", input)
}
} else {
if nostr.IsValid32ByteHex(input) {
pointer = nostr.EventPointer{ID: input}
} else {
return nil, nil, fmt.Errorf("failed to decode '%s': %w", input, err)
}
}
return sys.FetchSpecificEvent(ctx, pointer, withRelays)
}
// FetchSpecificEvent tries to get a specific event from a NIP-19 code using whatever means necessary. // FetchSpecificEvent tries to get a specific event from a NIP-19 code using whatever means necessary.
func (sys *System) FetchSpecificEvent( func (sys *System) FetchSpecificEvent(
ctx context.Context, ctx context.Context,
code string, pointer nostr.Pointer,
withRelays bool, withRelays bool,
) (event *nostr.Event, successRelays []string, err error) { ) (event *nostr.Event, successRelays []string, err error) {
// this is for deciding what relays will go on nevent and nprofile later // this is for deciding what relays will go on nevent and nprofile later
priorityRelays := make([]string, 0, 8) priorityRelays := make([]string, 0, 8)
author := ""
var filter nostr.Filter var filter nostr.Filter
author := ""
relays := make([]string, 0, 10) relays := make([]string, 0, 10)
fallback := make([]string, 0, 10) fallback := make([]string, 0, 10)
successRelays = make([]string, 0, 10) successRelays = make([]string, 0, 10)
prefix, data, err := nip19.Decode(code) switch v := pointer.(type) {
if err != nil {
if nostr.IsValid32ByteHex(code) {
data = code
prefix = "note"
} else {
return nil, nil, fmt.Errorf("failed to decode %w", err)
}
}
switch v := data.(type) {
case nostr.EventPointer: case nostr.EventPointer:
author = v.Author author = v.Author
filter.IDs = []string{v.ID} filter.IDs = []string{v.ID}
@@ -56,13 +76,6 @@ func (sys *System) FetchSpecificEvent(
relays = appendUnique(relays, sys.FallbackRelays.Next()) relays = appendUnique(relays, sys.FallbackRelays.Next())
fallback = append(fallback, sys.FallbackRelays.Next(), sys.FallbackRelays.Next()) fallback = append(fallback, sys.FallbackRelays.Next(), sys.FallbackRelays.Next())
priorityRelays = append(priorityRelays, v.Relays...) priorityRelays = append(priorityRelays, v.Relays...)
case string:
if prefix == "note" {
filter.IDs = []string{v}
relays = append(relays, sys.JustIDRelays.Next(), sys.JustIDRelays.Next())
fallback = appendUnique(fallback,
sys.FallbackRelays.Next(), sys.JustIDRelays.Next(), sys.FallbackRelays.Next())
}
} }
// try to fetch in our internal eventstore first // try to fetch in our internal eventstore first
@@ -97,14 +110,14 @@ attempts:
slowWithRelays bool slowWithRelays bool
}{ }{
{ {
label: "fetch-" + prefix, label: "fetchspecific",
relays: relays, relays: relays,
// set this to true if the caller wants relays, so we won't return immediately // set this to true if the caller wants relays, so we won't return immediately
// but will instead wait a little while to see if more relays respond // but will instead wait a little while to see if more relays respond
slowWithRelays: withRelays, slowWithRelays: withRelays,
}, },
{ {
label: "fetchf-" + prefix, label: "fetchspecific",
relays: fallback, relays: fallback,
slowWithRelays: false, slowWithRelays: false,
}, },
@@ -159,7 +172,7 @@ attempts:
} }
if result == nil { if result == nil {
return nil, nil, fmt.Errorf("couldn't find this %s", prefix) return nil, nil, fmt.Errorf("couldn't find this %v", pointer)
} }
// save stuff in cache and in internal store // save stuff in cache and in internal store