@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
package/CHANGELOG.md ADDED
@@ -0,0 +1,37 @@
1
+ # @estiva-app/conversation
2
+
3
+ Every entry answers the **SPEC** question explicitly: which rule of SPEC §6 or
4
+ §9 changed how this package reads or writes, including when the answer is
5
+ none. A change to how an event is read is a MINOR in `0.x` even when no
6
+ signature moved, because two apps on different versions would then disagree
7
+ about a conversation.
8
+
9
+ ## 0.1.0 — 2026-09-30
10
+
11
+ **First release** (CON-5). Extracted from Peek (`src/nostr/fileConversation.ts`,
12
+ `src/lib/drafts.ts`), Ship (`src/nostr/edits.ts`, `reactions.ts`,
13
+ `references.ts`, `attachments.ts`, the comment builders in `events.ts`,
14
+ `src/lib/drafts.ts`) and `@estiva-app/interop` 0.39 (`commentDecorationsOf`,
15
+ `editTargetOf`, `REACTION_HORIZON`, `isCommentOn`), lined up to the SPEC as
16
+ decided in estiva-docs#178. Needs protocol 0.24.0.
17
+
18
+ Where it reads differently from one of the copies it replaces — each row is
19
+ SPEC §6.9's table, and none changes anything that exists on production:
20
+
21
+ - **`ts` is trusted only when `floor(ts / 1000) == created_at`** (§6.2).
22
+ Peek's edit fold and interop accepted ±1 s.
23
+ - **A `kind:9` is never a comment** (§6.4, CON-20), and **a reply is never a
24
+ root**. Ship's `anchorIndex` and interop's `isCommentOn` read a `kind:9`'s
25
+ unnamed `a` as a comment; interop's applied per event, so a reply carrying
26
+ `a` would have listed twice.
27
+ - **A nested chat reply files under its `root`-marked `e`** (§6.4). Peek's
28
+ channel read dropped it.
29
+ - **Reactions** (§6.6): target is the last 64-hex `e` (Ship and interop took
30
+ the first), the emoji is untrimmed and empty is `+` (Ship trimmed and
31
+ skipped empty), one per `(target, pubkey, emoji)` (Peek and interop counted
32
+ every event), and a read at the relay's page ceiling is reported
33
+ (`reactionEventsCut`).
34
+ - **The horizon** is the newest 100 by `created_at`, ties on the higher id —
35
+ interop's rule; Ship broke ties by thread order.
36
+ - **Edit and Delete** are offered on the viewer's own message and on a
37
+ `bot: true` author's (§6.5, `offersEditAndDelete`).
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Estiva
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # @estiva-app/conversation
2
+
3
+ The conversation rules of the [Estiva SPEC](https://github.com/estiva-app/estiva-docs/blob/main/protocol/SPEC.md)
4
+ §6.2–§6.8, for an app that would rather not implement them itself. Peek, Ship
5
+ and estiva-agent read conversations through it.
6
+
7
+ **A convenience, never a requirement.** SPEC §9's conversation checks
8
+ (C10–C16) are written so an app can build conversations from the specification
9
+ alone, and this package's tests are those checks
10
+ (`test/conformance.test.ts`). If you find this package disagreeing with the
11
+ SPEC, the package is wrong.
12
+
13
+ ```bash
14
+ npm install @estiva-app/conversation @estiva-app/protocol
15
+ ```
16
+
17
+ `@estiva-app/protocol` is a peer. Nothing here opens a socket, holds a
18
+ credential or touches a browser global: the relay read is a `QueryFn` you
19
+ pass, the clock is a `createdAtMs` you pass, and drafts take their storage.
20
+
21
+ ## What is in it
22
+
23
+ | | SPEC | exports |
24
+ | --- | --- | --- |
25
+ | comment or mention | §6.4 | `isCommentOn`, `strengthOn`, `threadStrength`, `anchorsOf`, `isReply`, `namedInBody` |
26
+ | threading | §6.4 *Replies* | `parentOf`, `groupThreads` |
27
+ | writing | §6.4 | `buildComment`, `buildReply`, `referenceTagsFor`, `mentionTagsFor` |
28
+ | ordering | §6.2, §6.3 | `trustedTs`, `orderingMs`, `byOrder` |
29
+ | edits and attachments | §6.8 | `editTargetOf`, `foldEdits`, `foldAttachments` |
30
+ | reactions | §6.6 | `REACTION_HORIZON`, `reactionHorizon`, `reactionTargetOf`, `reactionEmojiOf`, `foldReactions` |
31
+ | one read for all of the above | §6.6, §6.8 | `decorationsOf` |
32
+ | which controls to offer | §6.5 | `offersEditAndDelete` |
33
+ | drafts | — | `createDraftStore`, `draftKeys` |
34
+
35
+ Chat, an edit, a reaction and a deletion are built with
36
+ `@estiva-app/protocol`'s `buildMessage`, `buildEdit`, `buildReaction` and
37
+ `buildDeletion`.
38
+
39
+ ## What is not in it
40
+
41
+ The views (CON-18), unread (CON-19), the composer's editor, and anything only
42
+ one app needs.
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Writing a comment and a reply — SPEC §6.4 — and which controls to offer
3
+ * on a message, §6.5.
4
+ *
5
+ * Chat, an edit, a reaction and a deletion are `@estiva-app/protocol`'s
6
+ * `buildMessage`, `buildEdit`, `buildReaction` and `buildDeletion`, which are
7
+ * already Buzz-shaped and byte-pinned. What is conversation-specific is the
8
+ * NIP-22 pair below, whose shape the SPEC decided rather than Buzz.
9
+ */
10
+ import { type Imeta, type NostrTag, type UnsignedEvent } from '@estiva-app/protocol';
11
+ /**
12
+ * A top-level comment on an object. Tag order: `A`, `K`, `P`, `a`, `k`, `p`,
13
+ * `[block]`, `h`, `ts`, `[imeta…]`, then `tags`.
14
+ *
15
+ * Uppercase and lowercase name the same object, as NIP-22 says for a
16
+ * top-level comment. `K` and `P` come from the address, so a caller cannot
17
+ * state a kind that contradicts its own `A`. `block` is SPEC §13.6: which part
18
+ * of the object, when the author was looking at one. `tags` is for what the
19
+ * body earns — {@link referenceTagsFor} and {@link mentionTagsFor} — and
20
+ * nothing else: an `a` there that the body does not name breaks §6.4.
21
+ */
22
+ export declare function buildComment(pubkey: string, createdAtMs: number, args: {
23
+ about: string;
24
+ folder: string;
25
+ body: string;
26
+ blockId?: string;
27
+ files?: readonly Imeta[];
28
+ tags?: readonly NostrTag[];
29
+ }): UnsignedEvent;
30
+ /** The top-level comment a reply answers. */
31
+ export interface CommentRef {
32
+ /** Its event id. */
33
+ id: string;
34
+ /** The object its thread is about: its `A`. */
35
+ anchor: string;
36
+ /**
37
+ * Its author. Undefined when the comment is outside the read; the reply
38
+ * then carries no `p`, because a wrong one notifies the wrong person.
39
+ */
40
+ author?: string;
41
+ }
42
+ /**
43
+ * A reply to a comment — §6.4 *Replies*, C16. Tag order: `A`, `K`, `P`, `e`,
44
+ * `k`, `[p]`, `h`, `ts`, `[imeta…]`, then `tags`.
45
+ *
46
+ * **Flat, one level deep**: `e` names the thread's top-level comment, never
47
+ * another reply. **No lowercase `a`** for the object: NIP-22 reads it as the
48
+ * parent, so it would claim the reply is itself a top-level comment. The reply
49
+ * is found by `#e` on the comment.
50
+ */
51
+ export declare function buildReply(pubkey: string, createdAtMs: number, args: {
52
+ comment: CommentRef;
53
+ folder: string;
54
+ body: string;
55
+ files?: readonly Imeta[];
56
+ tags?: readonly NostrTag[];
57
+ }): UnsignedEvent;
58
+ /**
59
+ * Whether to offer Edit and Delete on a message — §6.5, decided 2026-09-29
60
+ * (C14). Offered on the viewer's own message, and on one whose author's
61
+ * profile declares `bot: true` (the viewer may be its NIP-OA owner, whom the
62
+ * relay accepts). Not on another human's: the relay refuses that every time.
63
+ *
64
+ * `authorIsBot` is the author's `kind:0` `bot` field: `undefined` when the
65
+ * profile could not be read, which offers the control — never wrong, since
66
+ * the relay adjudicates and the app surfaces a refusal in its words.
67
+ */
68
+ export declare function offersEditAndDelete(args: {
69
+ viewer: string | null | undefined;
70
+ author: string;
71
+ authorIsBot: boolean | undefined;
72
+ }): boolean;
73
+ //# sourceMappingURL=builders.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builders.d.ts","sourceRoot":"","sources":["../src/builders.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAA4B,KAAK,KAAK,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAqB9G;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,IAAI,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,SAAS,KAAK,EAAE,CAAC;IAAC,IAAI,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAA;CAAE,GAC5H,aAAa,CAsBf;AAED,6CAA6C;AAC7C,MAAM,WAAW,UAAU;IACzB,oBAAoB;IACpB,EAAE,EAAE,MAAM,CAAA;IACV,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,IAAI,EAAE;IAAE,OAAO,EAAE,UAAU,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,SAAS,KAAK,EAAE,CAAC;IAAC,IAAI,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAA;CAAE,GAChH,aAAa,CAsBf;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE;IAAE,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,GAAG,OAAO,CAG1I"}
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Writing a comment and a reply — SPEC §6.4 — and which controls to offer
3
+ * on a message, §6.5.
4
+ *
5
+ * Chat, an edit, a reaction and a deletion are `@estiva-app/protocol`'s
6
+ * `buildMessage`, `buildEdit`, `buildReaction` and `buildDeletion`, which are
7
+ * already Buzz-shaped and byte-pinned. What is conversation-specific is the
8
+ * NIP-22 pair below, whose shape the SPEC decided rather than Buzz.
9
+ */
10
+ import { imetaTag, toNostrSeconds } from '@estiva-app/protocol';
11
+ import { KIND_COMMENT } from './kinds.js';
12
+ const FOLDER_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
13
+ /**
14
+ * A `kind:1111` is channel-scoped when it carries an `h` and **global when it
15
+ * does not**, and the relay accepts both — so a comment without one is stored
16
+ * world-readable however private its object. Refused here, loudly.
17
+ */
18
+ function assertFolder(folder) {
19
+ if (!FOLDER_ID.test(folder))
20
+ throw new Error(`not a Folder id (lowercase UUID v4): ${JSON.stringify(folder)}`);
21
+ }
22
+ /** `<kind>:<pubkey>:<d>` → the `K` and `P` it already carries. */
23
+ function kindAndAuthor(address) {
24
+ const [kind, author] = address.split(':');
25
+ if (!kind || !author)
26
+ throw new Error(`not an address: ${JSON.stringify(address)}`);
27
+ return [kind, author];
28
+ }
29
+ /**
30
+ * A top-level comment on an object. Tag order: `A`, `K`, `P`, `a`, `k`, `p`,
31
+ * `[block]`, `h`, `ts`, `[imeta…]`, then `tags`.
32
+ *
33
+ * Uppercase and lowercase name the same object, as NIP-22 says for a
34
+ * top-level comment. `K` and `P` come from the address, so a caller cannot
35
+ * state a kind that contradicts its own `A`. `block` is SPEC §13.6: which part
36
+ * of the object, when the author was looking at one. `tags` is for what the
37
+ * body earns — {@link referenceTagsFor} and {@link mentionTagsFor} — and
38
+ * nothing else: an `a` there that the body does not name breaks §6.4.
39
+ */
40
+ export function buildComment(pubkey, createdAtMs, args) {
41
+ assertFolder(args.folder);
42
+ const [kind, author] = kindAndAuthor(args.about);
43
+ return {
44
+ pubkey,
45
+ created_at: toNostrSeconds(createdAtMs),
46
+ kind: KIND_COMMENT,
47
+ tags: [
48
+ ['A', args.about],
49
+ ['K', kind],
50
+ ['P', author],
51
+ ['a', args.about],
52
+ ['k', kind],
53
+ ['p', author],
54
+ ...(args.blockId ? [['block', args.blockId]] : []),
55
+ ['h', args.folder],
56
+ ['ts', String(Math.floor(createdAtMs))],
57
+ ...(args.files ?? []).map(imetaTag),
58
+ ...(args.tags ?? []),
59
+ ],
60
+ content: args.body,
61
+ };
62
+ }
63
+ /**
64
+ * A reply to a comment — §6.4 *Replies*, C16. Tag order: `A`, `K`, `P`, `e`,
65
+ * `k`, `[p]`, `h`, `ts`, `[imeta…]`, then `tags`.
66
+ *
67
+ * **Flat, one level deep**: `e` names the thread's top-level comment, never
68
+ * another reply. **No lowercase `a`** for the object: NIP-22 reads it as the
69
+ * parent, so it would claim the reply is itself a top-level comment. The reply
70
+ * is found by `#e` on the comment.
71
+ */
72
+ export function buildReply(pubkey, createdAtMs, args) {
73
+ assertFolder(args.folder);
74
+ const [kind, author] = kindAndAuthor(args.comment.anchor);
75
+ const earned = (args.tags ?? []).filter((t) => !(t[0] === 'a' && t[1] === args.comment.anchor));
76
+ return {
77
+ pubkey,
78
+ created_at: toNostrSeconds(createdAtMs),
79
+ kind: KIND_COMMENT,
80
+ tags: [
81
+ ['A', args.comment.anchor],
82
+ ['K', kind],
83
+ ['P', author],
84
+ ['e', args.comment.id],
85
+ ['k', String(KIND_COMMENT)],
86
+ ...(args.comment.author ? [['p', args.comment.author]] : []),
87
+ ['h', args.folder],
88
+ ['ts', String(Math.floor(createdAtMs))],
89
+ ...(args.files ?? []).map(imetaTag),
90
+ ...earned,
91
+ ],
92
+ content: args.body,
93
+ };
94
+ }
95
+ /**
96
+ * Whether to offer Edit and Delete on a message — §6.5, decided 2026-09-29
97
+ * (C14). Offered on the viewer's own message, and on one whose author's
98
+ * profile declares `bot: true` (the viewer may be its NIP-OA owner, whom the
99
+ * relay accepts). Not on another human's: the relay refuses that every time.
100
+ *
101
+ * `authorIsBot` is the author's `kind:0` `bot` field: `undefined` when the
102
+ * profile could not be read, which offers the control — never wrong, since
103
+ * the relay adjudicates and the app surfaces a refusal in its words.
104
+ */
105
+ export function offersEditAndDelete(args) {
106
+ if (args.viewer && args.viewer === args.author)
107
+ return true;
108
+ return args.authorIsBot !== false;
109
+ }
110
+ //# sourceMappingURL=builders.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builders.js","sourceRoot":"","sources":["../src/builders.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAiD,MAAM,sBAAsB,CAAA;AAC9G,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAEzC,MAAM,SAAS,GAAG,uEAAuE,CAAA;AAEzF;;;;GAIG;AACH,SAAS,YAAY,CAAC,MAAc;IAClC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,wCAAwC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;AAChH,CAAC;AAED,kEAAkE;AAClE,SAAS,aAAa,CAAC,OAAe;IACpC,MAAM,CAAC,IAAI,EAAE,MAAM,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CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC;YACnC,GAAG,MAAM;SACV;QACD,OAAO,EAAE,IAAI,CAAC,IAAI;KACnB,CAAA;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,CAAC,IAA6F;IAC/H,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IAC3D,OAAO,IAAI,CAAC,WAAW,KAAK,KAAK,CAAA;AACnC,CAAC"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * What has been done to a set of messages since they were written — edits,
3
+ * reactions, resolutions — in one request, read by `#e`.
4
+ *
5
+ * Moved here from `@estiva-app/interop` (CON-5), where it was
6
+ * `commentDecorationsOf` since CON-8. It is a second round trip by nature:
7
+ * an edit, a reaction and a resolution name the *message* by `e`, never the
8
+ * file or the channel, so they cannot be asked for until the message ids are
9
+ * known.
10
+ */
11
+ import { type SignedEvent } from '@estiva-app/protocol';
12
+ import { type EditFold } from './edits.js';
13
+ import { type ReactionEvent } from './reactions.js';
14
+ /**
15
+ * How a caller reads the relay: filters in, events out. The environment's
16
+ * one touch, injected — this package opens no socket and holds no credential.
17
+ * Structurally the same as `@estiva-app/interop`'s `QueryFn`.
18
+ */
19
+ export type QueryFn = (filters: Record<string, unknown>[]) => Promise<SignedEvent[]>;
20
+ /** A `kind:9101` resolution assertion on a message (PEEK-128). */
21
+ export interface Resolution {
22
+ id: string;
23
+ action: 'resolved' | 'reopened';
24
+ by: string;
25
+ at: number;
26
+ /** The assertion's free text, when it carried one. */
27
+ message?: string;
28
+ /** The reply that carried the resolution, when the writer named one. */
29
+ supportingEventId?: string;
30
+ }
31
+ /** What has been done to one message since it was written. */
32
+ export interface Decoration {
33
+ /** Its edits, folded (§6.8). Absent when nobody edited it. */
34
+ edit?: EditFold;
35
+ /** Its reactions, one per `(pubkey, emoji)`, oldest first. Empty inside the horizon means none. */
36
+ reactions: ReactionEvent[];
37
+ /** Oldest first; the last one is the current state. Empty means never resolved. */
38
+ resolutions: Resolution[];
39
+ }
40
+ /** The answer of {@link decorationsOf}. */
41
+ export interface Decorations {
42
+ /** Keyed by message id. A target nothing happened to is present, with nothing in it. */
43
+ byId: Record<string, Decoration>;
44
+ /**
45
+ * How many targets were **not** asked about reactions, because they fell
46
+ * outside the horizon. §6.6 makes reporting this a MUST: a cap nobody can
47
+ * see is indistinguishable from "nobody reacted".
48
+ */
49
+ reactionTargetsOmitted: number;
50
+ /**
51
+ * Whether a reaction read came back at the relay's page ceiling, so some
52
+ * reaction *events* may be missing. §6.6: a reader that caps events MUST
53
+ * report the cut as it reports the target cut.
54
+ */
55
+ reactionEventsCut: boolean;
56
+ /**
57
+ * Whether an edit and resolution read came back at the page ceiling, so a
58
+ * message may show an older body or state than the relay holds.
59
+ */
60
+ editEventsCut: boolean;
61
+ }
62
+ /**
63
+ * Edits, reactions and resolutions for `targets`, in as few requests as the
64
+ * relay allows.
65
+ *
66
+ * - **Edits** fold by §6.8 ({@link foldEdits}): the target is the first 64-hex
67
+ * `e` and no other. Pass each target's `body` so "edited" means the body
68
+ * changed; without it the first edit counts as a change.
69
+ * - **Reactions** have a horizon ({@link reactionHorizon}): the newest 100
70
+ * targets by `at`, one budget across whatever id spaces the caller mixes,
71
+ * the number left out reported. Target is the last 64-hex `e`, empty is `+`,
72
+ * one per `(target, pubkey, emoji)`.
73
+ * - **Resolutions** are `kind:9101` carrying `t=resolution`; the `action` tag
74
+ * is the state, the content the rationale, and an `e` marked `support` names
75
+ * the reply that carried it. The target is the first unmarked `e` naming a
76
+ * target.
77
+ *
78
+ * Edits and resolutions have no horizon: they are one event per change, not
79
+ * one per reader. A refused read is whatever `query` does with one.
80
+ */
81
+ export declare function decorationsOf(
82
+ /** `at` is the target's `created_at`, seconds. */
83
+ targets: readonly {
84
+ id: string;
85
+ at: number;
86
+ body?: string;
87
+ }[], query: QueryFn): Promise<Decorations>;
88
+ //# sourceMappingUR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a;AACjC,kDAAkD;AAClD,OAAO,EAAE,SAAS;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,EAAE,EAC7D,KAAK,EAAE,OAAO,GACb,OAAO,CAAC,WAAW,CAAC,CAqFtB"}
@@ -0,0 +1,133 @@
1
+ /**
2
+ * What has been done to a set of messages since they were written — edits,
3
+ * reactions, resolutions — in one request, read by `#e`.
4
+ *
5
+ * Moved here from `@estiva-app/interop` (CON-5), where it was
6
+ * `commentDecorationsOf` since CON-8. It is a second round trip by nature:
7
+ * an edit, a reaction and a resolution name the *message* by `e`, never the
8
+ * file or the channel, so they cannot be asked for until the message ids are
9
+ * known.
10
+ */
11
+ import { MAX_FILTERS_PER_QUERY, RELAY_PAGE_CEILING } from '@estiva-app/protocol';
12
+ import { editTargetOf, foldEdits } from './edits.js';
13
+ import { KIND_ASSERTION, KIND_EDIT, KIND_REACTION } from './kinds.js';
14
+ import { reactionEventsOf, reactionHorizon, reactionTargetOf } from './reactions.js';
15
+ /** Ids per `#e` filter. A comfortable fraction of the relay's page, so one filter's answer is rarely cut. */
16
+ const TARGETS_PER_FILTER = 100;
17
+ const chunked = (list) => {
18
+ const out = [];
19
+ for (let i = 0; i < list.length; i += TARGETS_PER_FILTER)
20
+ out.push(list.slice(i, i + TARGETS_PER_FILTER));
21
+ return out;
22
+ };
23
+ const tagValue = (event, name) => event.tags.find((t) => t[0] === name)?.[1];
24
+ /**
25
+ * Edits, reactions and resolutions for `targets`, in as few requests as the
26
+ * relay allows.
27
+ *
28
+ * - **Edits** fold by §6.8 ({@link foldEdits}): the target is the first 64-hex
29
+ * `e` and no other. Pass each target's `body` so "edited" means the body
30
+ * changed; without it the first edit counts as a change.
31
+ * - **Reactions** have a horizon ({@link reactionHorizon}): the newest 100
32
+ * targets by `at`, one budget across whatever id spaces the caller mixes,
33
+ * the number left out reported. Target is the last 64-hex `e`, empty is `+`,
34
+ * one per `(target, pubkey, emoji)`.
35
+ * - **Resolutions** are `kind:9101` carrying `t=resolution`; the `action` tag
36
+ * is the state, the content the rationale, and an `e` marked `support` names
37
+ * the reply that carried it. The target is the first unmarked `e` naming a
38
+ * target.
39
+ *
40
+ * Edits and resolutions have no horizon: they are one event per change, not
41
+ * one per reader. A refused read is whatever `query` does with one.
42
+ */
43
+ export async function decorationsOf(
44
+ /** `at` is the target's `created_at`, seconds. */
45
+ targets, query) {
46
+ // Keyed lowercase: every target this reads off a tag is lowercased (`editTargetOf`, `reactionTargetOf`).
47
+ const byId = {};
48
+ const unique = [];
49
+ for (const t of targets) {
50
+ const id = t.id.toLowerCase();
51
+ if (Object.hasOwn(byId, id))
52
+ continue;
53
+ byId[id] = { reactions: [], resolutions: [] };
54
+ unique.push({ id, at: t.at });
55
+ }
56
+ if (unique.length === 0)
57
+ return { byId, reactionTargetsOmitted: 0, reactionEventsCut: false, editEventsCut: false };
58
+ const { asked, omitted } = reactionHorizon(unique);
59
+ const reactionChunks = chunked(asked);
60
+ const editChunks = chunked(unique.map((t) => t.id));
61
+ const filters = [];
62
+ for (const chunk of editChunks) {
63
+ filters.push({ kinds: [KIND_EDIT, KIND_ASSERTION], '#e': chunk, limit: RELAY_PAGE_CEILING });
64
+ }
65
+ for (const chunk of reactionChunks)
66
+ filters.push({ kinds: [KIND_REACTION], '#e': chunk, limit: RELAY_PAGE_CEILING });
67
+ const seen = new Set();
68
+ const events = [];
69
+ for (let start = 0; start < filters.length; start += MAX_FILTERS_PER_QUERY) {
70
+ for (const event of await query(filters.slice(start, start + MAX_FILTERS_PER_QUERY))) {
71
+ if (seen.has(event.id))
72
+ continue;
73
+ seen.add(event.id);
74
+ events.push(event);
75
+ }
76
+ }
77
+ // `hasOwn`, not `in`: an `e` of `constructor` is anybody's to write.
78
+ const ours = (id) => id !== undefined && Object.hasOwn(byId, id);
79
+ const bodies = targets.flatMap((t) => (t.body === undefined ? [] : [{ id: t.id, body: t.body }]));
80
+ for (const [target, fold] of Object.entries(foldEdits(events, bodies))) {
81
+ if (ours(target))
82
+ byId[target].edit = fold;
83
+ }
84
+ /*
85
+ A filter answered at its limit may have been cut. Counted per filter by
86
+ which chunk each event's target sits in: one pass, each target read once.
87
+ */
88
+ const cut = (chunks, kinds, targetOf) => {
89
+ const chunkOf = new Map();
90
+ chunks.forEach((chunk, i) => chunk.forEach((id) => chunkOf.set(id, i)));
91
+ const answered = new Array(chunks.length).fill(0);
92
+ for (const event of events) {
93
+ if (!kinds.includes(event.kind))
94
+ continue;
95
+ const i = chunkOf.get(targetOf(event) ?? '');
96
+ if (i !== undefined)
97
+ answered[i] += 1;
98
+ }
99
+ return answered.some((n) => n >= RELAY_PAGE_CEILING);
100
+ };
101
+ const reactionEventsCut = cut(reactionChunks, [KIND_REACTION], reactionTargetOf);
102
+ const editEventsCut = cut(editChunks, [KIND_EDIT, KIND_ASSERTION], (e) => e.kind === KIND_EDIT ? editTargetOf(e) : e.tags.find((t) => t[0] === 'e' && ours(t[1]) && !t[3])?.[1]);
103
+ const inHorizon = new Set(asked);
104
+ const reactions = events.filter((e) => e.kind === KIND_REACTION);
105
+ for (const reaction of reactionEventsOf(reactions)) {
106
+ if (ours(reaction.target) && inHorizon.has(reaction.target))
107
+ byId[reaction.target].reactions.push(reaction);
108
+ }
109
+ for (const event of events) {
110
+ if (event.kind !== KIND_ASSERTION || tagValue(event, 't') !== 'resolution')
111
+ continue;
112
+ const action = tagValue(event, 'action');
113
+ if (action !== 'resolved' && action !== 'reopened')
114
+ continue;
115
+ const target = event.tags.find((t) => t[0] === 'e' && ours(t[1]) && !t[3])?.[1];
116
+ if (!ours(target))
117
+ continue;
118
+ const support = event.tags.find((t) => t[0] === 'e' && t[3] === 'support')?.[1];
119
+ byId[target].resolutions.push({
120
+ id: event.id,
121
+ action,
122
+ by: event.pubkey,
123
+ at: event.created_at,
124
+ ...(event.content ? { message: event.content } : {}),
125
+ ...(support ? { supportingEventId: support } : {}),
126
+ });
127
+ }
128
+ const oldestFirst = (a, b) => a.at - b.at || (a.id < b.id ? -1 : 1);
129
+ for (const decoration of Object.values(byId))
130
+ decoration.resolutions.sort(oldestFirst);
131
+ return { byId, reactionTargetsOmitted: omitted, reactionEventsCut, editEventsCut };
132
+ }
133
+ //# sourceMappingURL=decorations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorations.js","sourceRoot":"","sources":["../src/decorations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAoB,MAAM,sBAAsB,CAAA;AAClG,OAAO,EAAE,YAAY,EAAE,SAAS,EAAiB,MAAM,YAAY,CAAA;AACnE,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AACrE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,EAAsB,MAAM,gBAAgB,CAAA;AAsDxG,6GAA6G;AAC7G,MAAM,kBAAkB,GAAG,GAAG,CAAA;AAE9B,MAAM,OAAO,GAAG,CAAC,IAAuB,EAAE,EAAE;IAC1C,MAAM,GAAG,GAAe,EAAE,CAAA;IAC1B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,kBAAkB;QAAE,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAA;IACzG,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAED,MAAM,QAAQ,GAAG,CAAC,KAAkB,EAAE,IAAY,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;AAEjG;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,KAAK,UAA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Drafts — CON-10.
3
+ *
4
+ * A message you started and did not send is still there when you come back, in
5
+ * the composer you were typing in and in no other. Moved here from Peek's and
6
+ * Ship's `src/lib/drafts.ts`, which were one file copied (CON-5); Peek's
7
+ * `container` key comes with it.
8
+ *
9
+ * ## Local, and never on the relay
10
+ *
11
+ * A draft is **unsent**. Publishing one — even to SPEC §12's app-private
12
+ * layer — turns something a person chose not to say into something the
13
+ * workspace holds, and a `kind:5` is a request rather than a reversal. The
14
+ * cost is the right way round: drafts do not follow you to another device.
15
+ *
16
+ * ## The key is the whole feature
17
+ *
18
+ * PEE-7's two symptoms — text appearing in a topic it was not written for, and
19
+ * text lost on returning to the thread it *was* written for — are one bug: the
20
+ * composer's text was not keyed by **destination**. {@link draftKeys} is the
21
+ * only place that decides, and two apps using it cannot disagree about where
22
+ * a draft belongs.
23
+ *
24
+ * ## Injected storage
25
+ *
26
+ * Everything takes its storage rather than reaching for `localStorage`, which
27
+ * is what lets it run with no browser and makes "storage is unavailable" a
28
+ * case rather than a crash. {@link DraftStorage} is the three methods of the
29
+ * Web Storage API this uses, declared here so no DOM type is published.
30
+ */
31
+ /** How long a draft survives. A draft from months ago reappearing surprises more than it helps. */
32
+ export declare const DRAFT_MAX_AGE_MS: number;
33
+ /**
34
+ * The longest draft that is kept. `localStorage` is a shared ~5 MB budget and a
35
+ * runaway paste should not evict everything else; beyond this the draft is not
36
+ * stored, and the text stays in the field in front of the person.
37
+ */
38
+ export declare const DRAFT_MAX_CHARS = 8000;
39
+ /**
40
+ * Where a draft belongs. **A thread is keyed by its own root event, not by the
41
+ * surface showing it**: a reply half-written on an issue page and the same
42
+ * thread seen from a project's Activity are one destination, so one draft.
43
+ */
44
+ export declare const draftKeys: {
45
+ /** Starting a conversation about an object — its address. */
46
+ object: (addr: string) => string;
47
+ /** Replying in a thread — the thread's root event id. */
48
+ thread: (rootEventId: string) => string;
49
+ /** Writing into a container itself — a Peek topic, a DM — by the id a person navigates between. */
50
+ container: (id: string) => string;
51
+ };
52
+ /** The part of the Web Storage API a draft store uses. `window.localStorage` is one. */
53
+ export interface DraftStorage {
54
+ getItem(key: string): string | null;
55
+ setItem(key: string, value: string): void;
56
+ removeItem(key: string): void;
57
+ }
58
+ export interface DraftStore {
59
+ /** The draft for this key, or `undefined` — including when storage is unusable. */
60
+ read(key: string): string | undefined;
61
+ /** Keep this text. An empty or whitespace-only draft is a cleared one. */
62
+ write(key: string, text: string): void;
63
+ /** Forget it — what a successful send calls. */
64
+ clear(key: string): void;
65
+ }
66
+ /** A store that keeps nothing, for when there is no storage to use. */
67
+ export declare const NO_DRAFTS: DraftStore;
68
+ /**
69
+ * A draft store over some storage. **Every operation is guarded**: a private
70
+ * window, cleared site data, blocked storage or a full quota all surface as
71
+ * "no draft" rather than a broken composer.
72
+ */
73
+ export declare function createDraftStore(storage: DraftStorage | undefined, now?: () => number): DraftStore;
74
+ //# sourceMappingURL=drafts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drafts.d.ts","sourceRoot":"","sources":["../src/drafts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,mGAAmG;AACnG,eAAO,MAAM,gBAAgB,QAA2B,CAAA;AAExD;;;;GAIG;AACH,eAAO,MAAM,eAAe,OAAO,CAAA;AAKnC;;;;GAIG;AACH,eAAO,MAAM,SAAS;IACpB,6DAA6D;mBAC9C,MAAM;IACrB,yDAAyD;0BACnC,MAAM;IAC5B,mGAAmG;oBACnF,MAAM;CACvB,CAAA;AAED,wFAAwF;AACxF,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;IACnC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACzC,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;CAC9B;AAQD,MAAM,WAAW,UAAU;IACzB,mFAAmF;IACnF,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;IACrC,0EAA0E;IAC1E,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;IACtC,gDAAgD;IAChD,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;CACzB;AAED,uEAAuE;AACvE,eAAO,MAAM,SAAS,EAAE,UAAwE,CAAA;AAEhG;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,YAAY,GAAG,SAAS,EAAE,GAAG,GAAE,MAAM,MAAiB,GAAG,UAAU,CA8C5G"}