Browse Source

fixed publication link on redirect

master
silberengel 7 months ago
parent
commit
88cbd7c4d9
  1. 69
      src/lib/components/util/ViewPublicationLink.svelte

69
src/lib/components/util/ViewPublicationLink.svelte

@ -21,52 +21,69 @@
return getEventType(event.kind || 0) === "addressable"; return getEventType(event.kind || 0) === "addressable";
} }
function getNaddrAddress(event: NDKEvent): string | null { // AI-NOTE: Always ensure the returned address is a valid naddr1... string.
if (!isAddressableEvent(event)) { // If the tag value is a raw coordinate (kind:pubkey:d-tag), encode it.
return null; // If it's already naddr1..., use as-is. Otherwise, fallback to event's own naddr.
}
try {
return naddrEncode(event, $activeInboxRelays);
} catch {
return null;
}
}
function getViewPublicationNaddr(event: NDKEvent): string | null { function getViewPublicationNaddr(event: NDKEvent): string | null {
// First, check for a-tags with 'defer' - these indicate the event is deferring to someone else's version // First, check for a-tags with 'defer' - these indicate the event is deferring to someone else's version
const aTags = getMatchingTags(event, "a"); const aTags = getMatchingTags(event, "a");
for (const tag of aTags) { for (const tag of aTags) {
if (tag.length >= 2 && tag.includes("defer")) { if (tag.length >= 2 && tag.includes("defer")) {
// This is a deferral to someone else's addressable event const value = tag[1];
return tag[1]; // Return the addressable event address 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 // For deferred events with deferral tag, use the deferral naddr instead of the event's own naddr
const deferralNaddr = getDeferralNaddr(event); const deferralNaddr = getDeferralNaddr(event);
if (deferralNaddr) { 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 // Otherwise, use the event's own naddr if it's addressable
return getNaddrAddress(event); return getNaddrAddress(event);
} }
function getNaddrAddress(event: NDKEvent): string | null {
if (!isAddressableEvent(event)) {
return null;
}
try {
return naddrEncode(event, $activeInboxRelays);
} catch {
return null;
}
}
function navigateToPublication() { function navigateToPublication() {
const naddrAddress = getViewPublicationNaddr(event); const naddrAddress = getViewPublicationNaddr(event);
console.log("ViewPublicationLink: navigateToPublication called", {
eventKind: event.kind,
naddrAddress,
isAddressable: isAddressableEvent(event),
});
if (naddrAddress) { if (naddrAddress) {
console.log( const url = `/publication/naddr/${naddrAddress}`;
"ViewPublicationLink: Navigating to publication:", goto(url);
naddrAddress,
);
goto(`/publication/naddr/${naddrAddress}`);
} else {
console.log("ViewPublicationLink: No naddr address found for event");
} }
} }

Loading…
Cancel
Save