mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-08-08 20:21:43 +02:00
fetchspecificevent also accepts a hex id and saves hints.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
"github.com/nbd-wtf/go-nostr/nip19"
|
"github.com/nbd-wtf/go-nostr/nip19"
|
||||||
|
"github.com/nbd-wtf/go-nostr/sdk/hints"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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.
|
||||||
@@ -20,11 +21,6 @@ func (sys *System) FetchSpecificEvent(
|
|||||||
// 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)
|
||||||
|
|
||||||
prefix, data, err := nip19.Decode(code)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, fmt.Errorf("failed to decode %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
author := ""
|
author := ""
|
||||||
|
|
||||||
var filter nostr.Filter
|
var filter nostr.Filter
|
||||||
@@ -32,6 +28,16 @@ func (sys *System) FetchSpecificEvent(
|
|||||||
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)
|
||||||
|
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) {
|
switch v := data.(type) {
|
||||||
case nostr.EventPointer:
|
case nostr.EventPointer:
|
||||||
author = v.Author
|
author = v.Author
|
||||||
@@ -40,9 +46,7 @@ func (sys *System) FetchSpecificEvent(
|
|||||||
relays = appendUnique(relays, sys.FallbackRelays.Next())
|
relays = appendUnique(relays, sys.FallbackRelays.Next())
|
||||||
fallback = append(fallback, sys.JustIDRelays.URLs...)
|
fallback = append(fallback, sys.JustIDRelays.URLs...)
|
||||||
fallback = appendUnique(fallback, sys.FallbackRelays.Next())
|
fallback = appendUnique(fallback, sys.FallbackRelays.Next())
|
||||||
for _, r := range v.Relays {
|
priorityRelays = append(priorityRelays, v.Relays...)
|
||||||
priorityRelays = append(priorityRelays, r)
|
|
||||||
}
|
|
||||||
case nostr.EntityPointer:
|
case nostr.EntityPointer:
|
||||||
author = v.PublicKey
|
author = v.PublicKey
|
||||||
filter.Authors = []string{v.PublicKey}
|
filter.Authors = []string{v.PublicKey}
|
||||||
@@ -51,6 +55,7 @@ func (sys *System) FetchSpecificEvent(
|
|||||||
relays = append(relays, v.Relays...)
|
relays = append(relays, v.Relays...)
|
||||||
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...)
|
||||||
case string:
|
case string:
|
||||||
if prefix == "note" {
|
if prefix == "note" {
|
||||||
filter.IDs = []string{v}
|
filter.IDs = []string{v}
|
||||||
@@ -69,6 +74,15 @@ func (sys *System) FetchSpecificEvent(
|
|||||||
if author != "" {
|
if author != "" {
|
||||||
// fetch relays for author
|
// fetch relays for author
|
||||||
authorRelays := sys.FetchOutboxRelays(ctx, author, 3)
|
authorRelays := sys.FetchOutboxRelays(ctx, author, 3)
|
||||||
|
|
||||||
|
// after that we register these hints as associated with author
|
||||||
|
// (we do this after fetching author outbox relays because we are already going to prioritize these hints)
|
||||||
|
now := nostr.Now()
|
||||||
|
for _, relay := range priorityRelays {
|
||||||
|
sys.Hints.Save(author, relay, hints.LastInNevent, now)
|
||||||
|
}
|
||||||
|
|
||||||
|
// arrange these
|
||||||
relays = appendUnique(relays, authorRelays...)
|
relays = appendUnique(relays, authorRelays...)
|
||||||
priorityRelays = appendUnique(priorityRelays, authorRelays...)
|
priorityRelays = appendUnique(priorityRelays, authorRelays...)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user