From ff4626f012f6f05e145808fe11ec45d9d29d5e23 Mon Sep 17 00:00:00 2001 From: highperfocused Date: Sun, 6 Sep 2026 18:10:48 +0200 Subject: [PATCH] fix: rootReference() no longer treats a mention as the root Follow-up to the isReply() fix: rootReference()'s positional fallback still matched any e tag regardless of marker, so an event with only a mention-marked e tag would incorrectly return the mentioned id as the thread root. The fallback now only considers unmarked e tags, per the deprecated positional NIP-10 scheme. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01BYiUtZMQeA5RHggQw73wto --- src/lib/nostrUtils.test.ts | 12 ++++++++++++ src/lib/nostrUtils.ts | 9 ++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/lib/nostrUtils.test.ts b/src/lib/nostrUtils.test.ts index afe93c8..e90a105 100644 --- a/src/lib/nostrUtils.test.ts +++ b/src/lib/nostrUtils.test.ts @@ -63,4 +63,16 @@ describe('rootReference', () => { it('is undefined for a root note', () => { expect(rootReference(note([]))).toBeUndefined(); }); + + it('is undefined for a note that only mentions another event', () => { + expect(rootReference(note([['e', 'mentioned-id', '', 'mention']]))).toBeUndefined(); + }); + + it('ignores a mention when falling back to the positional scheme', () => { + const event = note([ + ['e', 'mentioned-id', '', 'mention'], + ['e', 'root-id'], + ]); + expect(rootReference(event)).toBe('root-id'); + }); }); diff --git a/src/lib/nostrUtils.ts b/src/lib/nostrUtils.ts index 7477925..76de0a9 100644 --- a/src/lib/nostrUtils.ts +++ b/src/lib/nostrUtils.ts @@ -93,13 +93,16 @@ export function tagValues(event: NostrEvent, name: string): string[] { /** * The event a reply points at, following NIP-10: prefer an explicit `root` - * marker, fall back to the first positional `e` tag (the deprecated scheme - * puts the root id first: `["e", ], ["e", ]`). + * marker, fall back to the first *unmarked* `e` tag (the deprecated scheme + * puts the root id first: `["e", ], ["e", ]`). A `mention` + * or `reply`-only marker is never treated as the root — a mention isn't + * part of the thread, and a lone `reply` marker without `root` is malformed + * per NIP-10 rather than an implicit root. */ export function rootReference(event: NostrEvent): string | undefined { const marked = event.tags.find(([name, , , marker]) => name === 'e' && marker === 'root'); if (marked) return marked[1]; - const positional = event.tags.filter(([name]) => name === 'e'); + const positional = event.tags.filter(([name, , , marker]) => name === 'e' && !marker); return positional[0]?.[1]; }