@estiva-app/conversation 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/LICENSE +21 -0
  3. package/README.md +42 -0
  4. package/dist/builders.d.ts +73 -0
  5. package/dist/builders.d.ts.map +1 -0
  6. package/dist/builders.js +110 -0
  7. package/dist/builders.js.map +1 -0
  8. package/dist/decorations.d.ts +88 -0
  9. package/dist/decorations.d.ts.map +1 -0
  10. package/dist/decorations.js +133 -0
  11. package/dist/decorations.js.map +1 -0
  12. package/dist/drafts.d.ts +74 -0
  13. package/dist/drafts.d.ts.map +1 -0
  14. package/dist/drafts.js +111 -0
  15. package/dist/drafts.js.map +1 -0
  16. package/dist/edits.d.ts +76 -0
  17. package/dist/edits.d.ts.map +1 -0
  18. package/dist/edits.js +123 -0
  19. package/dist/edits.js.map +1 -0
  20. package/dist/index.d.ts +25 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +25 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/kinds.d.ts +17 -0
  25. package/dist/kinds.d.ts.map +1 -0
  26. package/dist/kinds.js +22 -0
  27. package/dist/kinds.js.map +1 -0
  28. package/dist/order.d.ts +24 -0
  29. package/dist/order.d.ts.map +1 -0
  30. package/dist/order.js +21 -0
  31. package/dist/order.js.map +1 -0
  32. package/dist/reactions.d.ts +93 -0
  33. package/dist/reactions.d.ts.map +1 -0
  34. package/dist/reactions.js +99 -0
  35. package/dist/reactions.js.map +1 -0
  36. package/dist/strength.d.ts +104 -0
  37. package/dist/strength.d.ts.map +1 -0
  38. package/dist/strength.js +166 -0
  39. package/dist/strength.js.map +1 -0
  40. package/dist/threads.d.ts +54 -0
  41. package/dist/threads.d.ts.map +1 -0
  42. package/dist/threads.js +70 -0
  43. package/dist/threads.js.map +1 -0
  44. package/package.json +56 -0
  45. package/src/builders.ts +133 -0
  46. package/src/decorations.ts +187 -0
  47. package/src/drafts.ts +135 -0
  48. package/src/edits.ts +139 -0
  49. package/src/index.ts +74 -0
  50. package/src/kinds.ts +24 -0
  51. package/src/order.ts +38 -0
  52. package/src/reactions.ts +151 -0
  53. package/src/strength.ts +170 -0
  54. package/src/threads.ts +104 -0
@@ -0,0 +1,99 @@
1
+ import { isEventId, KIND_REACTION } from './kinds.js';
2
+ import { byOrder } from './order.js';
3
+ /**
4
+ * How many targets a reaction read asks about โ€” SPEC ยง6.6, decided by CON-1.
5
+ * The value is the specification's; two apps with different N disagree about
6
+ * a count, legitimately and unfixably.
7
+ */
8
+ export const REACTION_HORIZON = 100;
9
+ /**
10
+ * The emoji a person can pick, and what each one means โ€” the same five in
11
+ * both apps, in the same order, because a reaction you can read is one you
12
+ * should be able to answer. The name is the tooltip and the accessible name.
13
+ */
14
+ export const REACTION_EMOJI = [
15
+ { emoji: '๐Ÿ‘', name: 'Makes sense' },
16
+ { emoji: '๐Ÿ’ฏ', name: 'Agree' },
17
+ { emoji: '๐Ÿ™', name: 'Thank you' },
18
+ { emoji: '๐Ÿš€', name: "Let's go!" },
19
+ { emoji: '๐ŸŽ‰', name: 'Congrats' },
20
+ ];
21
+ /** The event a `kind:7` reacts to: the **last** 64-hex `e`, lowercased. */
22
+ export function reactionTargetOf(event) {
23
+ for (let i = event.tags.length - 1; i >= 0; i--) {
24
+ const t = event.tags[i];
25
+ if (t[0] === 'e' && isEventId(t[1]))
26
+ return t[1].toLowerCase();
27
+ }
28
+ return undefined;
29
+ }
30
+ /** The emoji a `kind:7` carries: its content untrimmed, `+` when empty. */
31
+ export function reactionEmojiOf(event) {
32
+ return event.content === '' ? '+' : event.content;
33
+ }
34
+ /**
35
+ * The horizon applied: **the newest {@link REACTION_HORIZON} targets by
36
+ * `created_at`, ties broken on the higher id**, in one budget across every id
37
+ * space the caller mixes (roots and replies alike). `at` is the target's
38
+ * `created_at` in seconds; a repeated id counts once.
39
+ */
40
+ export function reactionHorizon(targets, limit = REACTION_HORIZON) {
41
+ const unique = new Map();
42
+ for (const t of targets)
43
+ if (!unique.has(t.id))
44
+ unique.set(t.id, t.at);
45
+ const newestFirst = [...unique].sort((a, b) => b[1] - a[1] || (a[0] > b[0] ? -1 : a[0] < b[0] ? 1 : 0));
46
+ return { asked: newestFirst.slice(0, limit).map(([id]) => id), omitted: Math.max(0, newestFirst.length - limit) };
47
+ }
48
+ /**
49
+ * Every `kind:7` in `events`, read by ยง6.6 and deduplicated per
50
+ * `(target, pubkey, emoji)` โ€” the earliest wins, so a retraction names it.
51
+ * Oldest first by ยง6.3. A reaction with no 64-hex `e` is nobody's.
52
+ */
53
+ export function reactionEventsOf(events) {
54
+ const out = [];
55
+ const counted = new Set();
56
+ const seen = new Set();
57
+ for (const event of [...events].sort(byOrder)) {
58
+ if (event.kind !== KIND_REACTION || seen.has(event.id))
59
+ continue;
60
+ seen.add(event.id);
61
+ const target = reactionTargetOf(event);
62
+ if (!target)
63
+ continue;
64
+ const emoji = reactionEmojiOf(event);
65
+ const key = JSON.stringify([target, event.pubkey, emoji]);
66
+ if (counted.has(key))
67
+ continue;
68
+ counted.add(key);
69
+ out.push({ id: event.id, target, emoji, by: event.pubkey, at: event.created_at });
70
+ }
71
+ return out;
72
+ }
73
+ /**
74
+ * Fold `kind:7` events into per-target counts (C13): target id โ†’ one entry per
75
+ * emoji, in first-reaction order. `viewer` fills `mine`.
76
+ */
77
+ export function foldReactions(events, viewer) {
78
+ return groupReactions(reactionEventsOf(events), viewer);
79
+ }
80
+ /** {@link foldReactions} over reactions already read by {@link reactionEventsOf}. */
81
+ export function groupReactions(reactions, viewer) {
82
+ const out = {};
83
+ for (const r of reactions) {
84
+ const list = (out[r.target] ??= []);
85
+ let entry = list.find((e) => e.emoji === r.emoji);
86
+ if (!entry) {
87
+ entry = { emoji: r.emoji, count: 0, by: [] };
88
+ list.push(entry);
89
+ }
90
+ if (entry.by.includes(r.by))
91
+ continue;
92
+ entry.by.push(r.by);
93
+ entry.count += 1;
94
+ if (viewer !== undefined && r.by === viewer)
95
+ entry.mine = r.id;
96
+ }
97
+ return out;
98
+ }
99
+ //# sourceMappingURL=reactions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reactions.js","sourceRoot":"","sources":["../src/reactions.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEpC;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAA;AAEnC;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAA+C;IACxE,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,aAAa,EAAE;IACpC,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE;IAC9B,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE;IAClC,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE;IAClC,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE;CAClC,CAAA;AAED,2EAA2E;AAC3E,MAAM,UAAU,gBAAgB,CAAC,KAAgC;IAC/D,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAChD,MAAM,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACvB,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAA;IAChE,CAAC;IACD,OAAO,SAAS,CAAA;AAClB,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,eAAe,CAAC,KAAmC;IACjE,OAAO,KAAK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,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAA;QACnC,IAAI,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,KAAK,CAAC,CAAA;QACjD,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,KAAK,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAA;YAC5C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAClB,CAAC;QACD,IAAI,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;YAAE,SAAQ;QACrC,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;QACnB,KAAK,CAAC,KAAK,IAAI,CAAC,CAAA;QAChB,IAAI,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,EAAE,KAAK,MAAM;YAAE,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAA;IAChE,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Which object a conversation is about, and how strongly โ€” SPEC ยง6.4.
3
+ *
4
+ * A thread attaches to every object anyone in it referenced, and that is only
5
+ * defensible because attachment has **two strengths** an app MUST present
6
+ * apart:
7
+ *
8
+ * | on the thread's root | strength |
9
+ * | --- | --- |
10
+ * | a `kind:1111`'s `A` | **comment**: NIP-22's root object |
11
+ * | a `kind:1111`'s `a` that is not its `A` | **mention**: the index of an address the body names |
12
+ * | a `kind:9`'s `a`, or a `nostr:naddrโ€ฆ` in any body | **mention** |
13
+ * | any tag on a reply | nothing: only the root decides |
14
+ *
15
+ * A `kind:1111` with no `A` is malformed; a reader takes **every** `a` it
16
+ * carries as an `A` rather than dropping the thread (clarified 2026-09-29).
17
+ *
18
+ * **A `kind:9` is not a comment** (SPEC ยง6.4, corrected 2026-09-30, CON-20):
19
+ * every comment-shaped `kind:9` on production was republished as a
20
+ * `kind:1111`, so a channel's chat lists under no object whatever `a` it
21
+ * carries (C10). Ship's `anchorIndex` and interop's `isCommentOn` read a
22
+ * `kind:9` whose `a` the body does not name as a pre-REW-10 comment; that row
23
+ * is gone, except for an app whose manifest still declares such a history
24
+ * (`emits.alsoRead`, ยง7.3) โ€” see {@link isCommentOn}'s `declaredKinds`.
25
+ *
26
+ * **A reply is never a root** (added 2026-09-29, CON-5). An event carrying a
27
+ * reply `e` โ€” a `kind:9` with any `e`, a `kind:1111` with a lowercase `e` that
28
+ * differs from its `E` โ€” is not a comment of its own, whatever `a` it carries.
29
+ */
30
+ import { type NostrTag, type SignedEvent } from '@estiva-app/protocol';
31
+ type Tagged = Pick<SignedEvent, 'kind' | 'tags'>;
32
+ type Readable = Pick<SignedEvent, 'kind' | 'tags' | 'content'>;
33
+ /** Presented as the object's own discussion, or as *Mentioned in*. */
34
+ export type Strength = 'comment' | 'mention';
35
+ /** The addresses a body names by `nostr:naddrโ€ฆ`. A pointer that will not decode is prose. */
36
+ export declare function namedInBody(body: string): Set<string>;
37
+ /**
38
+ * Whether the event answers another: a `kind:9` with any `e`, or a
39
+ * `kind:1111` with a lowercase `e` that differs from its `E`.
40
+ *
41
+ * A top-level comment on an *event* root carries `E` and `e` naming the same
42
+ * event and is not a reply. On an address root it carries no `e` at all.
43
+ */
44
+ export declare function isReply(event: Pick<SignedEvent, 'kind' | 'tags'>): boolean;
45
+ /**
46
+ * The addresses the event's thread is **about** โ€” its `A` tags, or every `a`
47
+ * when it carries no `A`. Only a `kind:1111` has any.
48
+ *
49
+ * On a reply this is the thread's object, not the reply's own strength: a
50
+ * reply copies the comment's `A` (ยง6.4 *Replies*). {@link isCommentOn} is the
51
+ * test for whether the event itself lists as a comment.
52
+ */
53
+ export declare function anchorsOf(event: Tagged): string[];
54
+ /**
55
+ * Is the event a **comment** on the address: a root whose thread is about it
56
+ * (C10, C11).
57
+ *
58
+ * `declaredKinds` is the object's owner's comment kinds, as its manifest
59
+ * declares them (ยง7.3: `emits.kind` and `emits.alsoRead`); the default is
60
+ * NIP-22's alone. A kind other than `1111` is a comment only when its owner
61
+ * declares it โ€” the history ยง7.3 says an app declares when it has *not*
62
+ * migrated it โ€” and is then read the way such a comment was written: by an
63
+ * `a` its body does not name. Undeclared, a `kind:9` is chat and lists under
64
+ * no object whatever `a` it carries (ยง6.4, CON-20).
65
+ */
66
+ export declare function isCommentOn(event: Readable, address: string, declaredKinds?: readonly number[]): boolean;
67
+ /** Does the event name the address at all โ€” any `a` or `A`, or a `nostr:naddrโ€ฆ` in its body. */
68
+ export declare function referencesAddress(event: Readable, address: string): boolean;
69
+ /**
70
+ * How one event attaches to the address on its own: a comment, a mention, or
71
+ * nothing. A reply is always nothing (C11): its tags name its parent or repeat
72
+ * the root's, and only the root decides.
73
+ *
74
+ * For a whole thread โ€” which attaches as a mention when any message in it
75
+ * names the address โ€” see {@link threadStrength}.
76
+ */
77
+ export declare function strengthOn(event: Readable, address: string): Strength | null;
78
+ /**
79
+ * How a thread attaches to the address: a **comment** when its root is one,
80
+ * else a **mention** when any message in it โ€” the root or a reply โ€” names the
81
+ * address, else nothing.
82
+ *
83
+ * Thread-wide on purpose (ยง6.4): a discussion that names an object halfway
84
+ * through is from then on about that object, and the earlier messages are the
85
+ * context somebody needs. Pass `root` undefined when the root is outside the
86
+ * read; the thread can then only be a mention, since only the root decides
87
+ * the comment.
88
+ */
89
+ export declare function threadStrength(root: Readable | undefined, replies: readonly Readable[], address: string): Strength | null;
90
+ /**
91
+ * The `a` tags a body earns: every address it names by `nostr:naddrโ€ฆ`, except
92
+ * `own` (the object the event is already a comment on, which is its `A`).
93
+ *
94
+ * ยง6.4: a writer MUST NOT put an address in a `kind:1111`'s `a` that the body
95
+ * does not name, unless it is the `A` โ€” which is what keeps the mention row
96
+ * readable without decoding the body.
97
+ */
98
+ export declare function referenceTagsFor(body: string, own?: string): NostrTag[];
99
+ /** The most `p` tags a message carries โ€” Buzz's cap, and `buildMessage`'s. */
100
+ export declare const MAX_MENTIONED_PEOPLE = 50;
101
+ /** The people a body mentions by `nostr:npubโ€ฆ`, as `p` tags, capped at {@link MAX_MENTIONED_PEOPLE}. */
102
+ export declare function mentionTagsFor(body: string): NostrTag[];
103
+ export {};
104
+ //# sourceMappingURL=strength.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strength.d.ts","sourceRoot":"","sources":["../src/strength.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,EAAwE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAG5I,KAAK,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAAC,CAAA;AAChD,KAAK,QAAQ,GAAG,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC,CAAA;AAE9D,sEAAsE;AACtE,MAAM,MAAM,QAAQ,GAAG,SAAS,GAAG,SAAS,CAAA;AAK5C,6FAA6F;AAC7F,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAUrD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG,OAAO,CAO1E;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAIjD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,GAAE,SAAS,MAAM,EAAmB,GAAG,OAAO,CAKxH;AAED,gGAAgG;AAChG,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAG3E;AAED;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI,CAI5E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,QAAQ,GAAG,SAAS,EAAE,OAAO,EAAE,SAAS,QAAQ,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI,CAIzH;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,SAAK,GAAG,QAAQ,EAAE,CAInE;AAED,8EAA8E;AAC9E,eAAO,MAAM,oBAAoB,KAAK,CAAA;AAEtC,wGAAwG;AACxG,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,EAAE,CAWvD"}
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Which object a conversation is about, and how strongly โ€” SPEC ยง6.4.
3
+ *
4
+ * A thread attaches to every object anyone in it referenced, and that is only
5
+ * defensible because attachment has **two strengths** an app MUST present
6
+ * apart:
7
+ *
8
+ * | on the thread's root | strength |
9
+ * | --- | --- |
10
+ * | a `kind:1111`'s `A` | **comment**: NIP-22's root object |
11
+ * | a `kind:1111`'s `a` that is not its `A` | **mention**: the index of an address the body names |
12
+ * | a `kind:9`'s `a`, or a `nostr:naddrโ€ฆ` in any body | **mention** |
13
+ * | any tag on a reply | nothing: only the root decides |
14
+ *
15
+ * A `kind:1111` with no `A` is malformed; a reader takes **every** `a` it
16
+ * carries as an `A` rather than dropping the thread (clarified 2026-09-29).
17
+ *
18
+ * **A `kind:9` is not a comment** (SPEC ยง6.4, corrected 2026-09-30, CON-20):
19
+ * every comment-shaped `kind:9` on production was republished as a
20
+ * `kind:1111`, so a channel's chat lists under no object whatever `a` it
21
+ * carries (C10). Ship's `anchorIndex` and interop's `isCommentOn` read a
22
+ * `kind:9` whose `a` the body does not name as a pre-REW-10 comment; that row
23
+ * is gone, except for an app whose manifest still declares such a history
24
+ * (`emits.alsoRead`, ยง7.3) โ€” see {@link isCommentOn}'s `declaredKinds`.
25
+ *
26
+ * **A reply is never a root** (added 2026-09-29, CON-5). An event carrying a
27
+ * reply `e` โ€” a `kind:9` with any `e`, a `kind:1111` with a lowercase `e` that
28
+ * differs from its `E` โ€” is not a comment of its own, whatever `a` it carries.
29
+ */
30
+ import { decodeNaddr, decodeNpub, findNaddrs, findNostrUris, pointerToAddress } from '@estiva-app/protocol';
31
+ import { KIND_COMMENT } from './kinds.js';
32
+ const values = (event, name) => event.tags.filter((t) => t[0] === name && typeof t[1] === 'string' && t[1] !== '').map((t) => t[1]);
33
+ /** The addresses a body names by `nostr:naddrโ€ฆ`. A pointer that will not decode is prose. */
34
+ export function namedInBody(body) {
35
+ const addresses = new Set();
36
+ for (const naddr of findNaddrs(body)) {
37
+ try {
38
+ addresses.add(pointerToAddress(decodeNaddr(naddr)));
39
+ }
40
+ catch {
41
+ // A malformed pointer is prose.
42
+ }
43
+ }
44
+ return addresses;
45
+ }
46
+ /**
47
+ * Whether the event answers another: a `kind:9` with any `e`, or a
48
+ * `kind:1111` with a lowercase `e` that differs from its `E`.
49
+ *
50
+ * A top-level comment on an *event* root carries `E` and `e` naming the same
51
+ * event and is not a reply. On an address root it carries no `e` at all.
52
+ */
53
+ export function isReply(event) {
54
+ // A NIP-10 `mention`-marked `e` quotes an event; it answers nothing.
55
+ const es = event.tags.filter((t) => t[0] === 'e' && typeof t[1] === 'string' && t[1] !== '' && t[3] !== 'mention').map((t) => t[1]);
56
+ if (es.length === 0)
57
+ return false;
58
+ if (event.kind !== KIND_COMMENT)
59
+ return true;
60
+ const root = values(event, 'E')[0];
61
+ return es.some((e) => e !== root);
62
+ }
63
+ /**
64
+ * The addresses the event's thread is **about** โ€” its `A` tags, or every `a`
65
+ * when it carries no `A`. Only a `kind:1111` has any.
66
+ *
67
+ * On a reply this is the thread's object, not the reply's own strength: a
68
+ * reply copies the comment's `A` (ยง6.4 *Replies*). {@link isCommentOn} is the
69
+ * test for whether the event itself lists as a comment.
70
+ */
71
+ export function anchorsOf(event) {
72
+ if (event.kind !== KIND_COMMENT)
73
+ return [];
74
+ const roots = values(event, 'A');
75
+ return [...new Set(roots.length > 0 ? roots : values(event, 'a'))];
76
+ }
77
+ /**
78
+ * Is the event a **comment** on the address: a root whose thread is about it
79
+ * (C10, C11).
80
+ *
81
+ * `declaredKinds` is the object's owner's comment kinds, as its manifest
82
+ * declares them (ยง7.3: `emits.kind` and `emits.alsoRead`); the default is
83
+ * NIP-22's alone. A kind other than `1111` is a comment only when its owner
84
+ * declares it โ€” the history ยง7.3 says an app declares when it has *not*
85
+ * migrated it โ€” and is then read the way such a comment was written: by an
86
+ * `a` its body does not name. Undeclared, a `kind:9` is chat and lists under
87
+ * no object whatever `a` it carries (ยง6.4, CON-20).
88
+ */
89
+ export function isCommentOn(event, address, declaredKinds = [KIND_COMMENT]) {
90
+ if (isReply(event))
91
+ return false;
92
+ if (event.kind === KIND_COMMENT)
93
+ return anchorsOf(event).includes(address);
94
+ if (!declaredKinds.includes(event.kind))
95
+ return false;
96
+ return event.tags.some((t) => t[0] === 'a' && t[1] === address) && !namedInBody(event.content).has(address);
97
+ }
98
+ /** Does the event name the address at all โ€” any `a` or `A`, or a `nostr:naddrโ€ฆ` in its body. */
99
+ export function referencesAddress(event, address) {
100
+ if (event.tags.some((t) => (t[0] === 'a' || t[0] === 'A') && t[1] === address))
101
+ return true;
102
+ return namedInBody(event.content).has(address);
103
+ }
104
+ /**
105
+ * How one event attaches to the address on its own: a comment, a mention, or
106
+ * nothing. A reply is always nothing (C11): its tags name its parent or repeat
107
+ * the root's, and only the root decides.
108
+ *
109
+ * For a whole thread โ€” which attaches as a mention when any message in it
110
+ * names the address โ€” see {@link threadStrength}.
111
+ */
112
+ export function strengthOn(event, address) {
113
+ if (isReply(event))
114
+ return null;
115
+ if (isCommentOn(event, address))
116
+ return 'comment';
117
+ return referencesAddress(event, address) ? 'mention' : null;
118
+ }
119
+ /**
120
+ * How a thread attaches to the address: a **comment** when its root is one,
121
+ * else a **mention** when any message in it โ€” the root or a reply โ€” names the
122
+ * address, else nothing.
123
+ *
124
+ * Thread-wide on purpose (ยง6.4): a discussion that names an object halfway
125
+ * through is from then on about that object, and the earlier messages are the
126
+ * context somebody needs. Pass `root` undefined when the root is outside the
127
+ * read; the thread can then only be a mention, since only the root decides
128
+ * the comment.
129
+ */
130
+ export function threadStrength(root, replies, address) {
131
+ if (root && isCommentOn(root, address))
132
+ return 'comment';
133
+ const all = root ? [root, ...replies] : replies;
134
+ return all.some((event) => referencesAddress(event, address)) ? 'mention' : null;
135
+ }
136
+ /**
137
+ * The `a` tags a body earns: every address it names by `nostr:naddrโ€ฆ`, except
138
+ * `own` (the object the event is already a comment on, which is its `A`).
139
+ *
140
+ * ยง6.4: a writer MUST NOT put an address in a `kind:1111`'s `a` that the body
141
+ * does not name, unless it is the `A` โ€” which is what keeps the mention row
142
+ * readable without decoding the body.
143
+ */
144
+ export function referenceTagsFor(body, own = '') {
145
+ const addresses = namedInBody(body);
146
+ addresses.delete(own);
147
+ return [...addresses].map((a) => ['a', a]);
148
+ }
149
+ /** The most `p` tags a message carries โ€” Buzz's cap, and `buildMessage`'s. */
150
+ export const MAX_MENTIONED_PEOPLE = 50;
151
+ /** The people a body mentions by `nostr:npubโ€ฆ`, as `p` tags, capped at {@link MAX_MENTIONED_PEOPLE}. */
152
+ export function mentionTagsFor(body) {
153
+ const pubkeys = new Set();
154
+ for (const uri of findNostrUris(body)) {
155
+ if (!/^nostr:npub1/i.test(uri))
156
+ continue;
157
+ try {
158
+ pubkeys.add(decodeNpub(uri));
159
+ }
160
+ catch {
161
+ // A malformed key is prose.
162
+ }
163
+ }
164
+ return [...pubkeys].slice(0, MAX_MENTIONED_PEOPLE).map((p) => ['p', p]);
165
+ }
166
+ //# s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* Threading โ€” SPEC ยง6.4 *Replies*.
3
+ *
4
+ * Two reply shapes, one per kind, and they do not mix:
5
+ *
6
+ * - **A reply to a comment is a flat `kind:1111`.** Uppercase `A`/`K`/`P` are
7
+ * the object; lowercase `e`/`k`/`p` name the thread's top-level comment,
8
+ * never another reply. A reader that meets a reply to a reply anyway SHOULD
9
+ * walk its parents to the top-level comment rather than drop it.
10
+ * - **A reply in a channel stays a `kind:9`**, threaded by Buzz's
11
+ * `thread_tags`: `['e', <root>, '', 'reply']` for a direct reply, and a
12
+ * `root`/`reply` pair for a nested one. A reader files a nested one under
13
+ * its `root`-marked `e`, which names the thread, rather than dropping it
14
+ * because its parent is not a root.
15
+ */
16
+ import type { SignedEvent } from '@estiva-app/protocol';
17
+ type Tagged = Pick<SignedEvent, 'kind' | 'tags'>;
18
+ /**
19
+ * The event this one answers, or null when it is a root.
20
+ *
21
+ * - `kind:1111`: the lowercase `e` that is not its `E` โ€” the parent comment.
22
+ * A top-level comment on an event root carries `E` and `e` naming the same
23
+ * event, and is a root.
24
+ * - `kind:9` (and any other kind): the thread โ€” the `root`-marked `e`, else
25
+ * the `reply`-marked one, else the first unmarked `e` when there is only one,
26
+ * else the first of several (NIP-10's positional form).
27
+ */
28
+ export declare function parentOf(event: Tagged): string | null;
29
+ /** A conversation's events, threaded. */
30
+ export interface Threaded<E extends Tagged & Pick<SignedEvent, 'id' | 'created_at'>> {
31
+ /** The roots present in the read, oldest first. */
32
+ roots: E[];
33
+ /**
34
+ * Every reply per thread, keyed by the thread's root id, oldest first. A
35
+ * reply to a reply is filed under the thread's root, not its parent. A root
36
+ * with no replies is absent, not `[]`.
37
+ */
38
+ replies: Record<string, E[]>;
39
+ /**
40
+ * Thread roots that replies point at but the read does not hold โ€” deleted,
41
+ * or outside the read. Their replies are in `replies` under that id; what to
42
+ * draw in the root's place is the app's (Peek draws "This comment was
43
+ * deleted", PER-24).
44
+ */
45
+ missingRoots: string[];
46
+ }
47
+ /**
48
+ * Thread a conversation: every root, and every reply filed under its thread's
49
+ * root by walking parents (ยง6.4). Duplicate ids are read once; order is
50
+ * ยง6.3's.
51
+ */
52
+ export declare function groupThreads<E extends Tagged & Pick<SignedEvent, 'id' | 'created_at'>>(events: readonly E[]): Threaded<E>;
53
+ export {};
54
+ //# sourceMappingURL=threads.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"threads.d.ts","sourceRoot":"","sources":["../src/threads.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAIvD,KAAK,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAAC,CAAA;AAMhD;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CASrD;AAED,yCAAyC;AACzC,MAAM,WAAW,QAAQ,CAAC,CAAC,SAAS,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,IAAI,GAAG,YAAY,CAAC;IACjF,mDAAmD;IACnD,KAAK,EAAE,CAAC,EAAE,CAAA;IACV;;;;OAIG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAA;IAC5B;;;;;OAKG;IACH,YAAY,EAAE,MAAM,EAAE,CAAA;CACvB;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,IAAI,GAAG,YAAY,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,CAiCzH"}
@@ -0,0 +1,70 @@
1
+ import { KIND_COMMENT } from './kinds.js';
2
+ import { byOrder } from './order.js';
3
+ /** The `e` tags that can thread: a NIP-10 `mention`-marked one quotes an event and answers nothing. */
4
+ const es = (event) => event.tags.filter((t) => t[0] === 'e' && typeof t[1] === 'string' && t[1] !== '' && t[3] !== 'mention');
5
+ /**
6
+ * The event this one answers, or null when it is a root.
7
+ *
8
+ * - `kind:1111`: the lowercase `e` that is not its `E` โ€” the parent comment.
9
+ * A top-level comment on an event root carries `E` and `e` naming the same
10
+ * event, and is a root.
11
+ * - `kind:9` (and any other kind): the thread โ€” the `root`-marked `e`, else
12
+ * the `reply`-marked one, else the first unmarked `e` when there is only one,
13
+ * else the first of several (NIP-10's positional form).
14
+ */
15
+ export function parentOf(event) {
16
+ const tags = es(event);
17
+ if (tags.length === 0)
18
+ return null;
19
+ if (event.kind === KIND_COMMENT) {
20
+ const root = event.tags.find((t) => t[0] === 'E')?.[1];
21
+ return tags.find((t) => t[1] !== root)?.[1] ?? null;
22
+ }
23
+ const marked = (marker) => tags.find((t) => t[3] === marker)?.[1];
24
+ return marked('root') ?? marked('reply') ?? tags[0][1];
25
+ }
26
+ /**
27
+ * Thread a conversation: every root, and every reply filed under its thread's
28
+ * root by walking parents (ยง6.4). Duplicate ids are read once; order is
29
+ * ยง6.3's.
30
+ */
31
+ export function groupThreads(events) {
32
+ const byId = new Map();
33
+ for (const event of events)
34
+ if (!byId.has(event.id))
35
+ byId.set(event.id, event);
36
+ const rootOf = (event) => {
37
+ const visited = new Set([event.id]);
38
+ let parent = parentOf(event);
39
+ while (parent !== null) {
40
+ const next = byId.get(parent);
41
+ if (!next || visited.has(parent))
42
+ return parent;
43
+ visited.add(parent);
44
+ const up = parentOf(next);
45
+ if (up === null)
46
+ return parent;
47
+ parent = up;
48
+ }
49
+ return event.id;
50
+ };
51
+ const roots = [];
52
+ const replies = {};
53
+ const missing = new Set();
54
+ for (const event of byId.values()) {
55
+ const root = rootOf(event);
56
+ if (root === event.id) {
57
+ roots.push(event);
58
+ continue;
59
+ }
60
+ ;
61
+ (replies[root] ??= []).push(event);
62
+ if (!byId.has(root))
63
+ missing.add(root);
64
+ }
65
+ roots.sort(byOrder);
66
+ for (const list of Object.values(replies))
67
+ list.sort(byOrder);
68
+ return { roots, replies, missingRoots: [...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K,MAAM,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC;QAClC,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAA;QAC1B,IAAI,IAAI,KAAK,KAAK,CAAC,EAAE,EAAE,CAAC;YACtB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YACjB,SAAQ;QACV,CAAC;QACD,CAAC;QAAA,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QACnC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACxC,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACnB,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC;QAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAC7D,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC,GAAG,OAAO,CAAC,EAAE,CAAA;AACvD,CAAC"}
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@estiva-app/conversation",
3
+ "version": "0.1.0",
4
+ "description": "The conversation rules of the Estiva SPEC (ยง6.2โ€“ยง6.8): comment and mention strengths, threading, the edit, reaction and attachment folds, NIP-22 comment and reply builders, and drafts. A convenience an app adopts, never a requirement.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "files": [
17
+ "dist",
18
+ "src",
19
+ "README.md",
20
+ "CHANGELOG.md",
21
+ "LICENSE"
22
+ ],
23
+ "sideEffects": false,
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/estiva-app/estiva-foundation.git",
33
+ "directory": "packages/conversation"
34
+ },
35
+ "keywords": [
36
+ "nostr",
37
+ "nip-22",
38
+ "nip-25",
39
+ "conversation",
40
+ "estiva"
41
+ ],
42
+ "scripts": {
43
+ "prebuild": "npm run build --workspace @estiva-app/protocol",
44
+ "build": "tsc -p tsconfig.json",
45
+ "test": "npm run build && node --test test/*.test.ts test/*.test.mjs",
46
+ "typecheck": "npm run build && tsc -p tsconfig.test.json",
47
+ "prepublishOnly": "npm run build"
48
+ },
49
+ "peerDependencies": {
50
+ "@estiva-app/protocol": ">=0.24.0"
51
+ },
52
+ "devDependencies": {
53
+ "@estiva-app/protocol": ">=0.24.0",
54
+ "typescript": "~5.9.3"
55
+ }
56
+ }