From 1653a74da20e342866433e7ce5e1582d3b4f93bd Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 18 Aug 2024 21:39:20 +0800 Subject: [PATCH] problem: no help thread resolve https://github.com/nostrocket/hypergolic/issues/90 --- src/components/HelpThreadNoteTree.svelte | 37 +++++++++ src/lib/event_helpers/help_thread.ts | 69 +++++++++++++++++ src/routes/help/+page.svelte | 96 +++++++++++++++++++++++- 3 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 src/components/HelpThreadNoteTree.svelte create mode 100644 src/lib/event_helpers/help_thread.ts diff --git a/src/components/HelpThreadNoteTree.svelte b/src/components/HelpThreadNoteTree.svelte new file mode 100644 index 0000000..f864774 --- /dev/null +++ b/src/components/HelpThreadNoteTree.svelte @@ -0,0 +1,37 @@ + + +
    + {#each notes as note, index (note.id)} + {#if isRoot && index > 0} + + {/if} +
  1. +
    + + +
    +
    {note.content}
    +
    + {note.created_at ? unixToRelativeTime(note.created_at * 1000) : 'Loading'} +
    + {#if note.children.length > 0} +
    + +
    + {/if} +
  2. + {/each} +
diff --git a/src/lib/event_helpers/help_thread.ts b/src/lib/event_helpers/help_thread.ts new file mode 100644 index 0000000..b578584 --- /dev/null +++ b/src/lib/event_helpers/help_thread.ts @@ -0,0 +1,69 @@ +import { prepareNostrEvent } from '@/helpers'; +import { NDKKind, type NDKEvent } from '@nostr-dev-kit/ndk'; +import type NDKSvelte from '@nostr-dev-kit/ndk-svelte'; + +export const HELP_THREAD_ROOT_EVENT_ID = + 'f05059e5d33716c38a10b392538a592de91014b6e9610c91e5f50543f2fdb4fd'; +const HELP_THREAD_ROOT_AUTHOR_PUBKEY = + '887f827161338ef4d3e83482498664ad7454caf9bda7d080c3b32821f1394708'; + +export interface TreeNote { + id: string; + pubkey: string; + content: string; + created_at: number; + reply?: string; + root: string; + children: TreeNote[]; +} + +export function buildNoteTree(notes: NDKEvent[]): TreeNote[] { + const noteMap = new Map(); + + notes.forEach((note) => { + const rootTag = note.getMatchingTags('e', 'root')[0]?.[1]; + const replyTag = note.getMatchingTags('e', 'reply')[0]?.[1]; + + noteMap.set(note.id, { + id: note.id, + pubkey: note.author.pubkey, + content: note.content, + created_at: note.created_at!, + root: rootTag, + reply: replyTag, + children: [] + }); + }); + + notes.forEach((note) => { + const replyTag = note.getMatchingTags('e', 'reply')[0]?.[1]; + if (replyTag) { + const parent = noteMap.get(replyTag); + const self = noteMap.get(note.id); + if (parent && self) { + parent.children.push(self); + } + } + }); + + const result = Array.from(noteMap.values()).filter( + (note) => + !note.reply || + (note.reply === HELP_THREAD_ROOT_EVENT_ID && note.root === HELP_THREAD_ROOT_EVENT_ID) + ); + + return result; +} + +export function prepareQuestionNoteEvent(args: { ndk: NDKSvelte; content: string }) { + const tags = [ + ['p', HELP_THREAD_ROOT_AUTHOR_PUBKEY], + ['e', HELP_THREAD_ROOT_EVENT_ID, 'wss://relay.nostrocket.org', 'reply'], + ['e', HELP_THREAD_ROOT_EVENT_ID, 'wss://relay.nostrocket.org', 'root'] + ]; + return prepareNostrEvent({ + ...args, + kind: NDKKind.Text, + tags + }); +} diff --git a/src/routes/help/+page.svelte b/src/routes/help/+page.svelte index 23def85..0d10cde 100644 --- a/src/routes/help/+page.svelte +++ b/src/routes/help/+page.svelte @@ -1 +1,95 @@ -Wouldn't it be cool to have a nostr help thread here? + + +
+ {#if $devmode} +
+ +
+ {/if} + +
+ If the answers to the questions below do not address your issue, you can send your question here +
+
+ +