From 6152aec1b339c633b3d2f608244c78726ac28c53 Mon Sep 17 00:00:00 2001 From: Silberengel Date: Tue, 8 Jul 2025 21:05:39 +0200 Subject: [PATCH] fix user badges --- src/lib/components/EventDetails.svelte | 20 +++++++++++++++++-- src/lib/components/PublicationHeader.svelte | 16 ++++++++++++++- src/lib/components/cards/ProfileHeader.svelte | 2 +- src/lib/utils/nostrUtils.ts | 4 ++-- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/lib/components/EventDetails.svelte b/src/lib/components/EventDetails.svelte index d054cfe..b2bcfad 100644 --- a/src/lib/components/EventDetails.svelte +++ b/src/lib/components/EventDetails.svelte @@ -8,6 +8,7 @@ import type { NDKEvent } from '$lib/utils/nostrUtils'; import { getMatchingTags } from '$lib/utils/nostrUtils'; import ProfileHeader from "$components/cards/ProfileHeader.svelte"; + import { getUserMetadata } from "$lib/utils/nostrUtils"; const { event, profile = null, searchValue = null } = $props<{ event: NDKEvent; @@ -27,6 +28,7 @@ let showFullContent = $state(false); let parsedContent = $state(''); let contentPreview = $state(''); + let authorDisplayName = $state(undefined); function getEventTitle(event: NDKEvent): string { return getMatchingTags(event, 'title')[0]?.[1] || 'Untitled'; @@ -65,6 +67,16 @@ } }); + $effect(() => { + if (event?.pubkey) { + getUserMetadata(toNpub(event.pubkey) as string).then(profile => { + authorDisplayName = profile.displayName || (profile as any).display_name || profile.name || event.pubkey; + }); + } else { + authorDisplayName = undefined; + } + }); + // --- Identifier helpers --- function getIdentifiers(event: NDKEvent, profile: any): { label: string, value: string, link?: string }[] { const ids: { label: string, value: string, link?: string }[] = []; @@ -109,9 +121,13 @@
{#if toNpub(event.pubkey)} - Author: {@render userBadge(toNpub(event.pubkey) as string, profile?.display_name || event.pubkey)} + + Author: {@render userBadge(toNpub(event.pubkey) as string, authorDisplayName)} + {:else} - Author: {profile?.display_name || event.pubkey} + + Author: {authorDisplayName} + {/if}
diff --git a/src/lib/components/PublicationHeader.svelte b/src/lib/components/PublicationHeader.svelte index d31eae3..7f1f5d4 100644 --- a/src/lib/components/PublicationHeader.svelte +++ b/src/lib/components/PublicationHeader.svelte @@ -6,6 +6,7 @@ import { Card, Img } from "flowbite-svelte"; import CardActions from "$components/util/CardActions.svelte"; import { userBadge } from "$lib/snippets/UserSnippets.svelte"; + import { getUserMetadata, toNpub } from '$lib/utils/nostrUtils'; const { event } = $props<{ event: NDKEvent }>(); @@ -29,6 +30,19 @@ let image: string = $derived(event.getMatchingTags('image')[0]?.[1] ?? null); let authorPubkey: string = $derived(event.getMatchingTags('p')[0]?.[1] ?? null); + // New: fetch profile display name for authorPubkey + let authorDisplayName = $state(undefined); + + $effect(() => { + if (authorPubkey) { + getUserMetadata(toNpub(authorPubkey) as string).then(profile => { + authorDisplayName = profile.displayName || (profile as any).display_name || author || authorPubkey; + }); + } else { + authorDisplayName = undefined; + } + }); + console.log("PublicationHeader event:", event); @@ -46,7 +60,7 @@

by {#if authorPubkey != null} - {@render userBadge(authorPubkey, author)} + {@render userBadge(authorPubkey, authorDisplayName)} {:else} {author} {/if} diff --git a/src/lib/components/cards/ProfileHeader.svelte b/src/lib/components/cards/ProfileHeader.svelte index 206f994..55df5f4 100644 --- a/src/lib/components/cards/ProfileHeader.svelte +++ b/src/lib/components/cards/ProfileHeader.svelte @@ -41,7 +41,7 @@ {#if profile.picture} Profile avatar { (e.target as HTMLImageElement).src = '/favicon.png'; }} /> {/if} - {@render userBadge(toNpub(event.pubkey) as string, profile.displayName || profile.name || event.pubkey)} + {@render userBadge(toNpub(event.pubkey) as string, profile.displayName || profile.display_name || profile.name || event.pubkey)}
diff --git a/src/lib/utils/nostrUtils.ts b/src/lib/utils/nostrUtils.ts index 9d80b1c..35fc43d 100644 --- a/src/lib/utils/nostrUtils.ts +++ b/src/lib/utils/nostrUtils.ts @@ -85,7 +85,7 @@ export async function getUserMetadata(identifier: string): Promise const metadata: NostrProfile = { name: profile?.name || fallback.name, - displayName: profile?.displayName, + displayName: profile?.displayName || profile?.display_name, nip05: profile?.nip05, picture: profile?.image, about: profile?.about, @@ -155,7 +155,7 @@ export async function createProfileLinkWithVerification(identifier: string, disp const defaultText = `${cleanId.slice(0, 8)}...${cleanId.slice(-4)}`; const escapedText = escapeHtml(displayText || defaultText); - const displayIdentifier = profile?.displayName ?? profile?.name ?? escapedText; + const displayIdentifier = profile?.displayName ?? profile?.display_name ?? profile?.name ?? escapedText; const isVerified = await user.validateNip05(nip05);