Browse Source

feat: render naddr, nevent and note previews

when embeded in event content

towards:
nostr:note1rs5shxk8ems0d878tymvntej4zcv4rvg7ntcpac5539xrcvvntss2vqxds
master
DanConwayDev 2 years ago
parent
commit
7dba21cece
No known key found for this signature in database
GPG Key ID: 68E15486D73F75E1
  1. 6
      src/lib/components/events/content/ParsedContent.svelte
  2. 53
      src/lib/components/events/content/utils.ts
  3. 58
      src/lib/wrappers/EventPreview.svelte

6
src/lib/components/events/content/ParsedContent.svelte

@ -3,7 +3,10 @@
import { import {
isImage, isImage,
isParsedLink, isParsedLink,
isParsedNaddr,
isParsedNevent,
isParsedNewLine, isParsedNewLine,
isParsedNote,
isParsedNprofile, isParsedNprofile,
isParsedNpub, isParsedNpub,
isParsedText, isParsedText,
@ -11,6 +14,7 @@
type ParsedPart, type ParsedPart,
} from './utils' } from './utils'
import UserHeader from '$lib/components/users/UserHeader.svelte' import UserHeader from '$lib/components/users/UserHeader.svelte'
import EventPreview from '$lib/wrappers/EventPreview.svelte'
export let content: string = '' export let content: string = ''
export let tags: NDKTag[] = [] export let tags: NDKTag[] = []
@ -38,6 +42,8 @@
<div class="badge badge-neutral"> <div class="badge badge-neutral">
<UserHeader user={part.hex} inline={true} size="sm" /> <UserHeader user={part.hex} inline={true} size="sm" />
</div> </div>
{:else if isParsedNevent(part) || isParsedNote(part) || isParsedNaddr(part)}
<EventPreview parsed_nostr_ref={part} />
{:else if isParsedText(part)} {:else if isParsedText(part)}
{part.value} {part.value}
{/if} {/if}

53
src/lib/components/events/content/utils.ts

@ -6,9 +6,6 @@ export const TOPIC = 'topic'
export const LINKCOLLECTION = 'link[]' export const LINKCOLLECTION = 'link[]'
export const HTML = 'html' export const HTML = 'html'
export const INVOICE = 'invoice' export const INVOICE = 'invoice'
export const NOSTR_NOTE = 'nostr:note'
export const NOSTR_NEVENT = 'nostr:nevent'
export const NOSTR_NADDR = 'nostr:naddr'
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const first = (list: any) => (list ? list[0] : undefined) const first = (list: any) => (list ? list[0] : undefined)
@ -66,7 +63,35 @@ export type ParsedNprofile = {
relays: string[] relays: string[]
} }
export type ParsedNostrLink = ParsedNpub | ParsedNprofile export const NOSTR_NOTE = 'nostr:note'
type PartTypeNote = 'nostr:note'
export type ParsedNote = {
type: PartTypeNote
id: string
relays: string[]
}
export const NOSTR_NEVENT = 'nostr:nevent'
type PartTypeNevent = 'nostr:nevent'
export type ParsedNevent = {
type: PartTypeNevent
id: string
relays: string[]
}
export const NOSTR_NADDR = 'nostr:naddr'
type PartTypeNaddr = 'nostr:naddr'
export type ParsedNaddr = {
type: PartTypeNaddr
identifier: string
pubkey: string
kind: number
relays: string[]
}
export type ParsedNostrLink = ParsedNpub | ParsedNprofile | ParsedNevent | ParsedNote | ParsedNaddr
export const TEXT = 'text' export const TEXT = 'text'
type PartTypeText = 'text' type PartTypeText = 'text'
@ -88,7 +113,7 @@ export const isParsedLink = (part: ParsedPart): part is ParsedLink =>
part.type == LINK part.type == LINK
export const isParsedNostrLink = (part: ParsedPart): part is ParsedNostrLink => export const isParsedNostrLink = (part: ParsedPart): part is ParsedNostrLink =>
part.type == NOSTR_NPUB || part.type == NOSTR_NPROFILE part.type == NOSTR_NPUB || part.type == NOSTR_NPROFILE || part.type == NOSTR_NEVENT || part.type == NOSTR_NOTE || part.type == NOSTR_NADDR
export const isParsedNpub = (part: ParsedPart): part is ParsedNpub => export const isParsedNpub = (part: ParsedPart): part is ParsedNpub =>
part.type == NOSTR_NPUB part.type == NOSTR_NPUB
@ -96,6 +121,15 @@ export const isParsedNpub = (part: ParsedPart): part is ParsedNpub =>
export const isParsedNprofile = (part: ParsedPart): part is ParsedNprofile => export const isParsedNprofile = (part: ParsedPart): part is ParsedNprofile =>
part.type == NOSTR_NPROFILE part.type == NOSTR_NPROFILE
export const isParsedNevent = (part: ParsedPart): part is ParsedNevent =>
part.type == NOSTR_NEVENT
export const isParsedNote = (part: ParsedPart): part is ParsedNote =>
part.type == NOSTR_NOTE
export const isParsedNaddr = (part: ParsedPart): part is ParsedNaddr =>
part.type == NOSTR_NADDR
export const isParsedText = (part: ParsedPart): part is ParsedText => export const isParsedText = (part: ParsedPart): part is ParsedText =>
part.type == TEXT part.type == TEXT
@ -184,6 +218,15 @@ export const parseContent = (content: string, tags: NDKTag[]): ParsedPart[] => {
if (decoded.type === 'nprofile') { if (decoded.type === 'nprofile') {
return [bech32, { type: NOSTR_NPUB, hex: decoded.data.pubkey }] return [bech32, { type: NOSTR_NPUB, hex: decoded.data.pubkey }]
} }
if (decoded.type === 'note') {
return [bech32, { type: NOSTR_NOTE, id: decoded.data, relays: [] }]
}
if (decoded.type === 'nevent') {
return [bech32, { type: NOSTR_NEVENT, id: decoded.data.id, relays: decoded.data.relays || [] }]
}
if (decoded.type === 'naddr') {
return [bech32, { ...decoded.data, type: NOSTR_NADDR, relays: decoded.data.relays || [] }]
}
} catch {} } catch {}
} }
} }

58
src/lib/wrappers/EventPreview.svelte

@ -0,0 +1,58 @@
<script lang="ts">
import EventWrapper from '$lib/components/events/EventWrapper.svelte'
import EventWrapperLite from '$lib/components/events/EventWrapperLite.svelte'
import Status from '$lib/components/events/content/Status.svelte'
import Patch from '$lib/components/events/content/Patch.svelte'
import ParsedContent from '$lib/components/events/content/ParsedContent.svelte'
import { defaults as user_defaults } from '$lib/components/users/type'
import { patch_kind, proposal_status_kinds } from '$lib/kinds'
import { ensureUser } from '$lib/stores/users'
import { NDKRelaySet, type NDKEvent } from '@nostr-dev-kit/ndk'
import { onDestroy, onMount } from 'svelte'
import { get, writable, type Unsubscriber, type Writable } from 'svelte/store'
import {
extractPatchMessage,
isCoverLetter,
isParsedNaddr,
type ParsedNaddr,
type ParsedNevent,
type ParsedNote,
} from '$lib/components/events/content/utils'
import { base_relays, ndk } from '$lib/stores/ndk'
import EventCard from './EventCard.svelte'
export let parsed_nostr_ref: ParsedNaddr | ParsedNevent | ParsedNote
let cannot_find_event = false;
let event: Writable<undefined | NDKEvent> = writable(undefined)
onMount(() => {
let sub = ndk.subscribe(
isParsedNaddr(parsed_nostr_ref) ? {
'#a': [`${parsed_nostr_ref.kind}:${parsed_nostr_ref.pubkey}:${parsed_nostr_ref.identifier}`],
} :
{ ids: [parsed_nostr_ref.id] },
{closeOnEose: true},
NDKRelaySet.fromRelayUrls([ ...base_relays, ...parsed_nostr_ref.relays ], ndk)
)
sub.on('event', (e: NDKEvent) => {
event.set(e)
})
sub.on('eose', () => {
if (!get(event)) cannot_find_event = true
})
})
</script>
<div class="card shadow-xl border border-base-400 p-2 pt-0 my-3">
{#if $event && $event.pubkey}
<EventCard event={$event} />
{:else if cannot_find_event}
cannot find event
{:else}
loading...
{/if}
</div>
Loading…
Cancel
Save