From f2e3cc08b23da687ef173aedbb5c1413bccdc37a Mon Sep 17 00:00:00 2001 From: silberengel Date: Sun, 10 Aug 2025 22:33:06 +0200 Subject: [PATCH] render embedded reposts --- src/lib/components/Notifications.svelte | 33 ++++++++++--------- src/lib/utils/notification_utils.ts | 42 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/src/lib/components/Notifications.svelte b/src/lib/components/Notifications.svelte index 8677a64..a38da37 100644 --- a/src/lib/components/Notifications.svelte +++ b/src/lib/components/Notifications.svelte @@ -22,6 +22,7 @@ truncateContent, truncateRenderedContent, parseContent, + parseRepostContent, renderQuotedContent, getNotificationType, fetchAuthorProfiles @@ -692,7 +693,7 @@ {#if isOwnProfile && $userStore.signedIn} -
+
Notifications @@ -740,7 +741,7 @@

No public messages found.

{:else} -
+
{#if filteredByUser}
@@ -818,7 +819,7 @@
-
+
{isFromUser ? 'Your Message' : 'Public Message'} @@ -848,11 +849,13 @@ {/if} {#if message.content}
- {#await parseContent(message.content) then parsedContent} - {@html parsedContent} - {:catch} - {@html message.content} - {/await} +
+ {#await ((message.kind === 6 || message.kind === 16) ? parseRepostContent(message.content) : parseContent(message.content)) then parsedContent} + {@html parsedContent} + {:catch} + {@html message.content} + {/await} +
{/if} @@ -877,7 +880,7 @@

No notifications {notificationMode === "to-me" ? "received" : "sent"} found.

{:else} -
+
{#each notifications.slice(0, 100) as notification} {@const authorProfile = authorProfiles.get(notification.pubkey)}
@@ -929,11 +932,13 @@ {#if notification.content}
- {#await parseContent(notification.content) then parsedContent} - {@html parsedContent} - {:catch} - {@html truncateContent(notification.content)} - {/await} +
+ {#await ((notification.kind === 6 || notification.kind === 16) ? parseRepostContent(notification.content) : parseContent(notification.content)) then parsedContent} + {@html parsedContent} + {:catch} + {@html truncateContent(notification.content)} + {/await} +
{/if} diff --git a/src/lib/utils/notification_utils.ts b/src/lib/utils/notification_utils.ts index b0f15c9..c4e01c2 100644 --- a/src/lib/utils/notification_utils.ts +++ b/src/lib/utils/notification_utils.ts @@ -73,6 +73,48 @@ export async function parseContent(content: string): Promise { return await parseBasicmarkup(content); } +/** + * Parses repost content and renders it as an embedded event + */ +export async function parseRepostContent(content: string): Promise { + if (!content) return ""; + + try { + // Try to parse the content as JSON (repost events contain the original event as JSON) + const originalEvent = JSON.parse(content); + + // Extract the original event's content + const originalContent = originalEvent.content || ""; + const originalAuthor = originalEvent.pubkey || ""; + const originalCreatedAt = originalEvent.created_at || 0; + + // Parse the original content with basic markup + const parsedOriginalContent = await parseBasicmarkup(originalContent); + + // Create an embedded event display + const formattedDate = originalCreatedAt ? new Date(originalCreatedAt * 1000).toLocaleDateString() : "Unknown date"; + const shortAuthor = originalAuthor ? `${originalAuthor.slice(0, 8)}...${originalAuthor.slice(-4)}` : "Unknown"; + + return ` +
+
+ Reposted by: + ${shortAuthor} + + ${formattedDate} +
+
+ ${parsedOriginalContent} +
+
+ `; + } catch (error) { + // If JSON parsing fails, fall back to basic markup + console.warn("Failed to parse repost content as JSON, falling back to basic markup:", error); + return await parseBasicmarkup(content); + } +} + /** * Renders quoted content for a message */