Browse Source

fix user badges

master
Silberengel 8 months ago
parent
commit
6152aec1b3
  1. 20
      src/lib/components/EventDetails.svelte
  2. 16
      src/lib/components/PublicationHeader.svelte
  3. 2
      src/lib/components/cards/ProfileHeader.svelte
  4. 4
      src/lib/utils/nostrUtils.ts

20
src/lib/components/EventDetails.svelte

@ -8,6 +8,7 @@
import type { NDKEvent } from '$lib/utils/nostrUtils'; import type { NDKEvent } from '$lib/utils/nostrUtils';
import { getMatchingTags } from '$lib/utils/nostrUtils'; import { getMatchingTags } from '$lib/utils/nostrUtils';
import ProfileHeader from "$components/cards/ProfileHeader.svelte"; import ProfileHeader from "$components/cards/ProfileHeader.svelte";
import { getUserMetadata } from "$lib/utils/nostrUtils";
const { event, profile = null, searchValue = null } = $props<{ const { event, profile = null, searchValue = null } = $props<{
event: NDKEvent; event: NDKEvent;
@ -27,6 +28,7 @@
let showFullContent = $state(false); let showFullContent = $state(false);
let parsedContent = $state(''); let parsedContent = $state('');
let contentPreview = $state(''); let contentPreview = $state('');
let authorDisplayName = $state<string | undefined>(undefined);
function getEventTitle(event: NDKEvent): string { function getEventTitle(event: NDKEvent): string {
return getMatchingTags(event, 'title')[0]?.[1] || 'Untitled'; 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 --- // --- Identifier helpers ---
function getIdentifiers(event: NDKEvent, profile: any): { label: string, value: string, link?: string }[] { function getIdentifiers(event: NDKEvent, profile: any): { label: string, value: string, link?: string }[] {
const ids: { label: string, value: string, link?: string }[] = []; const ids: { label: string, value: string, link?: string }[] = [];
@ -109,9 +121,13 @@
<div class="flex items-center space-x-2"> <div class="flex items-center space-x-2">
{#if toNpub(event.pubkey)} {#if toNpub(event.pubkey)}
<span class="text-gray-700 dark:text-gray-300">Author: {@render userBadge(toNpub(event.pubkey) as string, profile?.display_name || event.pubkey)}</span> <span class="text-gray-700 dark:text-gray-300">
Author: {@render userBadge(toNpub(event.pubkey) as string, authorDisplayName)}
</span>
{:else} {:else}
<span class="text-gray-700 dark:text-gray-300">Author: {profile?.display_name || event.pubkey}</span> <span class="text-gray-700 dark:text-gray-300">
Author: {authorDisplayName}
</span>
{/if} {/if}
</div> </div>

16
src/lib/components/PublicationHeader.svelte

@ -6,6 +6,7 @@
import { Card, Img } from "flowbite-svelte"; import { Card, Img } from "flowbite-svelte";
import CardActions from "$components/util/CardActions.svelte"; import CardActions from "$components/util/CardActions.svelte";
import { userBadge } from "$lib/snippets/UserSnippets.svelte"; import { userBadge } from "$lib/snippets/UserSnippets.svelte";
import { getUserMetadata, toNpub } from '$lib/utils/nostrUtils';
const { event } = $props<{ event: NDKEvent }>(); const { event } = $props<{ event: NDKEvent }>();
@ -29,6 +30,19 @@
let image: string = $derived(event.getMatchingTags('image')[0]?.[1] ?? null); let image: string = $derived(event.getMatchingTags('image')[0]?.[1] ?? null);
let authorPubkey: string = $derived(event.getMatchingTags('p')[0]?.[1] ?? null); let authorPubkey: string = $derived(event.getMatchingTags('p')[0]?.[1] ?? null);
// New: fetch profile display name for authorPubkey
let authorDisplayName = $state<string | undefined>(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); console.log("PublicationHeader event:", event);
</script> </script>
@ -46,7 +60,7 @@
<h3 class='text-base font-normal'> <h3 class='text-base font-normal'>
by by
{#if authorPubkey != null} {#if authorPubkey != null}
{@render userBadge(authorPubkey, author)} {@render userBadge(authorPubkey, authorDisplayName)}
{:else} {:else}
{author} {author}
{/if} {/if}

2
src/lib/components/cards/ProfileHeader.svelte

@ -41,7 +41,7 @@
{#if profile.picture} {#if profile.picture}
<img src={profile.picture} alt="Profile avatar" class="w-16 h-16 rounded-full border" onerror={(e) => { (e.target as HTMLImageElement).src = '/favicon.png'; }} /> <img src={profile.picture} alt="Profile avatar" class="w-16 h-16 rounded-full border" onerror={(e) => { (e.target as HTMLImageElement).src = '/favicon.png'; }} />
{/if} {/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)}
</div> </div>
<div> <div>
<div class="mt-2 flex flex-col gap-4"> <div class="mt-2 flex flex-col gap-4">

4
src/lib/utils/nostrUtils.ts

@ -85,7 +85,7 @@ export async function getUserMetadata(identifier: string): Promise<NostrProfile>
const metadata: NostrProfile = { const metadata: NostrProfile = {
name: profile?.name || fallback.name, name: profile?.name || fallback.name,
displayName: profile?.displayName, displayName: profile?.displayName || profile?.display_name,
nip05: profile?.nip05, nip05: profile?.nip05,
picture: profile?.image, picture: profile?.image,
about: profile?.about, about: profile?.about,
@ -155,7 +155,7 @@ export async function createProfileLinkWithVerification(identifier: string, disp
const defaultText = `${cleanId.slice(0, 8)}...${cleanId.slice(-4)}`; const defaultText = `${cleanId.slice(0, 8)}...${cleanId.slice(-4)}`;
const escapedText = escapeHtml(displayText || defaultText); 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); const isVerified = await user.validateNip05(nip05);

Loading…
Cancel
Save