From 88cbd7c4d97ae7c395a7d96334fd620d31fe133f Mon Sep 17 00:00:00 2001 From: silberengel Date: Mon, 4 Aug 2025 01:13:48 +0200 Subject: [PATCH] fixed publication link on redirect --- .../util/ViewPublicationLink.svelte | 69 ++++++++++++------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/src/lib/components/util/ViewPublicationLink.svelte b/src/lib/components/util/ViewPublicationLink.svelte index d29cb7b..6217bce 100644 --- a/src/lib/components/util/ViewPublicationLink.svelte +++ b/src/lib/components/util/ViewPublicationLink.svelte @@ -21,52 +21,69 @@ return getEventType(event.kind || 0) === "addressable"; } - function getNaddrAddress(event: NDKEvent): string | null { - if (!isAddressableEvent(event)) { - return null; - } - try { - return naddrEncode(event, $activeInboxRelays); - } catch { - return null; - } - } - + // AI-NOTE: Always ensure the returned address is a valid naddr1... string. + // If the tag value is a raw coordinate (kind:pubkey:d-tag), encode it. + // If it's already naddr1..., use as-is. Otherwise, fallback to event's own naddr. function getViewPublicationNaddr(event: NDKEvent): string | null { // First, check for a-tags with 'defer' - these indicate the event is deferring to someone else's version const aTags = getMatchingTags(event, "a"); for (const tag of aTags) { if (tag.length >= 2 && tag.includes("defer")) { - // This is a deferral to someone else's addressable event - return tag[1]; // Return the addressable event address + const value = tag[1]; + if (value.startsWith("naddr1")) { + return value; + } + // Check for coordinate format: kind:pubkey:d-tag + const coordMatch = value.match(/^(\d+):([0-9a-fA-F]{64}):(.+)$/); + if (coordMatch) { + const [_, kind, pubkey, dTag] = coordMatch; + try { + return naddrEncode({ kind: Number(kind), pubkey, tags: [["d", dTag]] } as NDKEvent, $activeInboxRelays); + } catch { + return null; + } + } + // Fallback: if not naddr1 or coordinate, ignore } } // For deferred events with deferral tag, use the deferral naddr instead of the event's own naddr const deferralNaddr = getDeferralNaddr(event); if (deferralNaddr) { - return deferralNaddr; + if (deferralNaddr.startsWith("naddr1")) { + return deferralNaddr; + } + const coordMatch = deferralNaddr.match(/^(\d+):([0-9a-fA-F]{64}):(.+)$/); + if (coordMatch) { + const [_, kind, pubkey, dTag] = coordMatch; + try { + return naddrEncode({ kind: Number(kind), pubkey, tags: [["d", dTag]] } as NDKEvent, $activeInboxRelays); + } catch { + return null; + } + } } // Otherwise, use the event's own naddr if it's addressable return getNaddrAddress(event); } + function getNaddrAddress(event: NDKEvent): string | null { + if (!isAddressableEvent(event)) { + return null; + } + try { + return naddrEncode(event, $activeInboxRelays); + } catch { + return null; + } + } + function navigateToPublication() { const naddrAddress = getViewPublicationNaddr(event); - console.log("ViewPublicationLink: navigateToPublication called", { - eventKind: event.kind, - naddrAddress, - isAddressable: isAddressableEvent(event), - }); if (naddrAddress) { - console.log( - "ViewPublicationLink: Navigating to publication:", - naddrAddress, - ); - goto(`/publication/naddr/${naddrAddress}`); - } else { - console.log("ViewPublicationLink: No naddr address found for event"); + const url = `/publication/naddr/${naddrAddress}`; + goto(url); } }