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]; }