@estiva-app/conversation 0.31.0 → 0.32.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.
- package/CHANGELOG.md +33 -0
- package/README.md +3 -0
- package/dist/decorations.d.ts +9 -14
- package/dist/decorations.d.ts.map +1 -1
- package/dist/decorations.js +7 -25
- package/dist/decorations.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/lines.d.ts +76 -0
- package/dist/lines.d.ts.map +1 -0
- package/dist/lines.js +126 -0
- package/dist/lines.js.map +1 -0
- package/dist/resolution.d.ts +59 -0
- package/dist/resolution.d.ts.map +1 -0
- package/dist/resolution.js +82 -0
- package/dist/resolution.js.map +1 -0
- package/dist/shape.d.ts +98 -0
- package/dist/shape.d.ts.map +1 -0
- package/dist/shape.js +148 -0
- package/dist/shape.js.map +1 -0
- package/package.json +2 -2
- package/src/decorations.ts +13 -34
- package/src/index.ts +15 -2
- package/src/lines.ts +166 -0
- package/src/resolution.ts +112 -0
- package/src/shape.ts +228 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { isEventId, KIND_ASSERTION } from './kinds.js';
|
|
2
|
+
import { orderingMs } from './order.js';
|
|
3
|
+
const tagValue = (event, name) => event.tags.find((t) => t[0] === name)?.[1];
|
|
4
|
+
/**
|
|
5
|
+
* The message a `kind:9101` is about: its **first unmarked `e`**, lowercased.
|
|
6
|
+
* Not the first one a reader happens to hold — a resolve listing a stranger
|
|
7
|
+
* first and a thread second is about the stranger.
|
|
8
|
+
*/
|
|
9
|
+
export function resolutionTargetOf(event) {
|
|
10
|
+
const tag = event.tags.find((t) => t[0] === 'e' && !t[3]);
|
|
11
|
+
return tag && isEventId(tag[1]) ? tag[1].toLowerCase() : undefined;
|
|
12
|
+
}
|
|
13
|
+
/** The event read as a {@link Resolution} with its target, or undefined when it is not one. */
|
|
14
|
+
export function resolutionOfEvent(event) {
|
|
15
|
+
if (event.kind !== KIND_ASSERTION || tagValue(event, 't') !== 'resolution')
|
|
16
|
+
return undefined;
|
|
17
|
+
const action = tagValue(event, 'action');
|
|
18
|
+
if (action !== 'resolved' && action !== 'reopened')
|
|
19
|
+
return undefined;
|
|
20
|
+
const target = resolutionTargetOf(event);
|
|
21
|
+
if (!target)
|
|
22
|
+
return undefined;
|
|
23
|
+
const support = event.tags.find((t) => t[0] === 'e' && t[3] === 'support')?.[1];
|
|
24
|
+
const folder = tagValue(event, 'h');
|
|
25
|
+
return {
|
|
26
|
+
target,
|
|
27
|
+
resolution: {
|
|
28
|
+
id: event.id,
|
|
29
|
+
action,
|
|
30
|
+
by: event.pubkey,
|
|
31
|
+
at: event.created_at,
|
|
32
|
+
orderMs: orderingMs(event),
|
|
33
|
+
...(folder ? { folder } : {}),
|
|
34
|
+
...(event.content ? { message: event.content } : {}),
|
|
35
|
+
...(isEventId(support) ? { supportingEventId: support.toLowerCase() } : {}),
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** Oldest first by §6.3: `ts` when trusted, then `created_at`, then the lower id. */
|
|
40
|
+
export const byResolutionOrder = (a, b) => a.orderMs - b.orderMs || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
41
|
+
/**
|
|
42
|
+
* A thread's state: the newest of `resolutions` that counts, or undefined when
|
|
43
|
+
* none does (never resolved, or only ones a reader ignores).
|
|
44
|
+
*
|
|
45
|
+
* `resolutions` are those whose target is the thread's **top-level** message;
|
|
46
|
+
* one about a reply is never passed here. `folder` is that message's Folder
|
|
47
|
+
* (for a top-level message the read did not return, its replies'), and
|
|
48
|
+
* `replies` the thread's reply ids: a `support` naming anything else is
|
|
49
|
+
* dropped and the resolve stands.
|
|
50
|
+
*/
|
|
51
|
+
export function resolutionOf(resolutions, thread) {
|
|
52
|
+
if (!thread.folder)
|
|
53
|
+
return undefined;
|
|
54
|
+
const newest = resolutions
|
|
55
|
+
.filter((r) => r.folder === thread.folder)
|
|
56
|
+
.sort(byResolutionOrder)
|
|
57
|
+
.at(-1);
|
|
58
|
+
if (!newest?.supportingEventId)
|
|
59
|
+
return newest;
|
|
60
|
+
const replies = new Set(thread.replies.map((id) => id.toLowerCase()));
|
|
61
|
+
if (replies.has(newest.supportingEventId))
|
|
62
|
+
return newest;
|
|
63
|
+
const { supportingEventId: _dropped, ...kept } = newest;
|
|
64
|
+
return kept;
|
|
65
|
+
}
|
|
66
|
+
/** Every resolution in `events`, by target id, oldest first (§6.3). */
|
|
67
|
+
export function resolutionsByTarget(events) {
|
|
68
|
+
const out = {};
|
|
69
|
+
const seen = new Set();
|
|
70
|
+
for (const event of events) {
|
|
71
|
+
if (seen.has(event.id))
|
|
72
|
+
continue;
|
|
73
|
+
seen.add(event.id);
|
|
74
|
+
const read = resolutionOfEvent(event);
|
|
75
|
+
if (read)
|
|
76
|
+
(out[read.target] ??= []).push(read.resolution);
|
|
77
|
+
}
|
|
78
|
+
for (const list of Object.values(out))
|
|
79
|
+
list.sort(byResolutionOrder);
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=resolution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolution.js","sourceRoot":"","sources":["../src/resolution.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAmBvC,MAAM,QAAQ,GAAG,CAAC,KAAgC,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;AAE/G;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAgC;IACjE,MAAM,GAAG,GAAG,KAA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AAW,EAAE,CAAC,CAAC,CAAA;IACrE,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,iBAAiB,CAAC;QAAE,OAAO,MAAM,CAAA;IACxD,MAAM,EAAE,iBAAiB,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAA;IACvD,OAAO,IAAI,CAAA;AACb,CAAC;AAED,uEAAuE;AACvE,MAAM,UAAU,mBAAmB,CAAC,MAA8B;IAChE,MAAM,GAAG,GAAiC,EAAE,CAAA;IAC5C,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAA;IAC9B,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YAAE,SAAQ;QAChC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;QAClB,MAAM,IAAI,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAA;QACrC,IAAI,IAAI;YAAE,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IAC3D,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC;QAAE,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAA;IACnE,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
package/dist/shape.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One shape for a message, a reply and a thread — SPEC §6.3–§6.8 and §6.4's
|
|
3
|
+
* "Resolving a thread", folded once for every app (ONE-2).
|
|
4
|
+
*
|
|
5
|
+
* Peek, Ship and Leaf each read a conversation into their own shape until
|
|
6
|
+
* now, and they disagreed: on which resolve counts, on what a deleted
|
|
7
|
+
* comment's thread shows, and on whether a reaction is known by its person's
|
|
8
|
+
* key or name. {@link foldThreads} is the one answer. It draws nothing — the
|
|
9
|
+
* kit in `@estiva-app/ui` takes words and nodes, and this sits below it.
|
|
10
|
+
*
|
|
11
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-conversation-is-one-shared-thing-and-only-its-colours-are-the-apps.md
|
|
12
|
+
*/
|
|
13
|
+
import { type Embed, type Imeta, type PartPointer, type Quote, type SignedEvent } from '@estiva-app/protocol';
|
|
14
|
+
import { type Reaction } from './reactions.js';
|
|
15
|
+
import { type Resolution } from './resolution.js';
|
|
16
|
+
/** One message — a top-level comment, a channel message or a reply — as it stands after its edits. */
|
|
17
|
+
export interface Message {
|
|
18
|
+
id: string;
|
|
19
|
+
kind: number;
|
|
20
|
+
/** Who wrote it, by key. */
|
|
21
|
+
author: string;
|
|
22
|
+
/** Its `h`: the Folder it was filed in. */
|
|
23
|
+
folder?: string;
|
|
24
|
+
/** When it was written, epoch milliseconds (§6.3: the trusted `ts`, else `created_at` × 1000). */
|
|
25
|
+
at: number;
|
|
26
|
+
/** The text to show: the latest edit's, else its own (§6.8). */
|
|
27
|
+
body: string;
|
|
28
|
+
/** Whether an edit changed the body — what a reader MUST mark (§6.8). */
|
|
29
|
+
edited: boolean;
|
|
30
|
+
/** When the body last changed, epoch milliseconds. Absent unless `edited`. */
|
|
31
|
+
editedAt?: number;
|
|
32
|
+
/** Its files: the latest edit's `imeta` set, else its own (§6.8, C15). */
|
|
33
|
+
files: Imeta[];
|
|
34
|
+
/** The cards its writer carried (`embed`, §13.6.2), after edits (C20). Which of them the body still names is the drawer's (`cardsIn`). */
|
|
35
|
+
cards: Embed[];
|
|
36
|
+
/** The blocks drawn live under it (`part`, §13.6.1). An edit carries none, so these are the message's own. */
|
|
37
|
+
parts: PartPointer[];
|
|
38
|
+
/** What its words link: its `a` and `q` values, or the latest edit's when it was edited, since an edit is the whole new message (§6.4, §6.8). */
|
|
39
|
+
links: string[];
|
|
40
|
+
/**
|
|
41
|
+
* What a comment on part of a document is about (§13.6): the run of blocks
|
|
42
|
+
* in its `block` tag, and the words in its `quote` when it quotes some. An
|
|
43
|
+
* edit never moves it.
|
|
44
|
+
*/
|
|
45
|
+
anchor?: {
|
|
46
|
+
blocks: string[];
|
|
47
|
+
quote?: Quote;
|
|
48
|
+
};
|
|
49
|
+
/** Its reactions by emoji, each reactor by key (§6.6). */
|
|
50
|
+
reactions: Reaction[];
|
|
51
|
+
/** The event itself, for what this shape does not carry. */
|
|
52
|
+
event: SignedEvent;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The thread's top-level message, in one of §6.5's three states:
|
|
56
|
+
* - `present` — returned, and not deleted;
|
|
57
|
+
* - `deleted` — its author's `kind:5` came back: drawn as "This comment was
|
|
58
|
+
* deleted", its replies under it;
|
|
59
|
+
* - `missing` — only its replies were read. Drawn as their thread, never as
|
|
60
|
+
* deleted: it may only be outside the read.
|
|
61
|
+
*/
|
|
62
|
+
export type RootState = 'present' | 'deleted' | 'missing';
|
|
63
|
+
/** A thread: a top-level message, its replies, and its state. */
|
|
64
|
+
export interface Thread {
|
|
65
|
+
/** The top-level message's id, whether or not it was returned. */
|
|
66
|
+
id: string;
|
|
67
|
+
state: RootState;
|
|
68
|
+
/** The top-level message, when `present`. */
|
|
69
|
+
root?: Message;
|
|
70
|
+
/** Its replies, oldest first by §6.3; a deleted reply is left out. */
|
|
71
|
+
replies: Message[];
|
|
72
|
+
/** The thread's Folder: its top-level message's `h`, else its replies'. */
|
|
73
|
+
folder?: string;
|
|
74
|
+
/** The resolve or reopen that counts (§6.4, C51); absent when none does. `resolution.action` is the state. */
|
|
75
|
+
resolution?: Resolution;
|
|
76
|
+
/**
|
|
77
|
+
* Whether Resolve or Reopen may be offered, as far as the thread can say:
|
|
78
|
+
* only on a top-level message that is `present`. Whether the reader may
|
|
79
|
+
* write in its Folder, and whether its file is archived, is the app's to add.
|
|
80
|
+
*/
|
|
81
|
+
resolvable: boolean;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Fold a conversation into its threads (§6.3–§6.8).
|
|
85
|
+
*
|
|
86
|
+
* `events` is everything read for it: its `kind:9` or `kind:1111` messages,
|
|
87
|
+
* and the `kind:40003` edits, `kind:7` reactions, `kind:5` deletions and
|
|
88
|
+
* `kind:9101` resolutions read by `#e` on them. Duplicates are read once.
|
|
89
|
+
* `viewer` marks the viewer's own reactions (`Reaction.mine`).
|
|
90
|
+
*
|
|
91
|
+
* Threads come oldest first by their top-level message, or by their first
|
|
92
|
+
* reply when it was not returned. A reply to a reply is filed under the
|
|
93
|
+
* thread's top-level message (§6.4).
|
|
94
|
+
*/
|
|
95
|
+
export declare function foldThreads(events: readonly SignedEvent[], options?: {
|
|
96
|
+
viewer?: string;
|
|
97
|
+
}): Thread[];
|
|
98
|
+
//# sourceMappingURL=shape.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape.d.ts","sourceRoot":"","sources":["../src/shape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAML,KAAK,KAAK,EACV,KAAK,KAAK,EACV,KAAK,WAAW,EAChB,KAAK,KAAK,EACV,KAAK,WAAW,EACjB,MAAM,sBAAsB,CAAA;AAI7B,OAAO,EAAoC,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAChF,OAAO,EAAqC,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAGpF,sGAAsG;AACtG,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,4BAA4B;IAC5B,MAAM,EAAE,MAAM,CAAA;IACd,2CAA2C;IAC3C,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,kGAAkG;IAClG,EAAE,EAAE,MAAM,CAAA;IACV,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAA;IACZ,yEAAyE;IACzE,MAAM,EAAE,OAAO,CAAA;IACf,8EAA8E;IAC9E,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,0EAA0E;IAC1E,KAAK,EAAE,KAAK,EAAE,CAAA;IACd,0IAA0I;IAC1I,KAAK,EAAE,KAAK,EAAE,CAAA;IACd,8GAA8G;IAC9G,KAAK,EAAE,WAAW,EAAE,CAAA;IACpB,iJAAiJ;IACjJ,KAAK,EAAE,MAAM,EAAE,CAAA;IACf;;;;OAIG;IACH,MAAM,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,EAAE,CAAC;QAAC,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE,CAAA;IAC5C,0DAA0D;IAC1D,SAAS,EAAE,QAAQ,EAAE,CAAA;IACrB,4DAA4D;IAC5D,KAAK,EAAE,WAAW,CAAA;CACnB;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,SAAS,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAAA;AAEzD,iEAAiE;AACjE,MAAM,WAAW,MAAM;IACrB,kEAAkE;IAClE,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,SAAS,CAAA;IAChB,6CAA6C;IAC7C,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,sEAAsE;IACtE,OAAO,EAAE,OAAO,EAAE,CAAA;IAClB,2EAA2E;IAC3E,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,8GAA8G;IAC9G,UAAU,CAAC,EAAE,UAAU,CAAA;IACvB;;;;OAIG;IACH,UAAU,EAAE,OAAO,CAAA;CACpB;AAyBD;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,SAAS,WAAW,EAAE,EAAE,OAAO,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,MAAM,EAAE,CA8FvG"}
|
package/dist/shape.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One shape for a message, a reply and a thread — SPEC §6.3–§6.8 and §6.4's
|
|
3
|
+
* "Resolving a thread", folded once for every app (ONE-2).
|
|
4
|
+
*
|
|
5
|
+
* Peek, Ship and Leaf each read a conversation into their own shape until
|
|
6
|
+
* now, and they disagreed: on which resolve counts, on what a deleted
|
|
7
|
+
* comment's thread shows, and on whether a reaction is known by its person's
|
|
8
|
+
* key or name. {@link foldThreads} is the one answer. It draws nothing — the
|
|
9
|
+
* kit in `@estiva-app/ui` takes words and nodes, and this sits below it.
|
|
10
|
+
*
|
|
11
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-conversation-is-one-shared-thing-and-only-its-colours-are-the-apps.md
|
|
12
|
+
*/
|
|
13
|
+
import { BLOCK_ANCHOR_TAG, imetaOf, partsOf, quoteOf, readRunIds, } from '@estiva-app/protocol';
|
|
14
|
+
import { editsByTarget, foldEdits, foldEmbeds } from './edits.js';
|
|
15
|
+
import { isConversationKind, isEventId, KIND_COMMENT, KIND_DELETION, KIND_REACTION } from './kinds.js';
|
|
16
|
+
import { orderingMs } from './order.js';
|
|
17
|
+
import { groupReactions, reactionEventsOf } from './reactions.js';
|
|
18
|
+
import { resolutionOf, resolutionsByTarget } from './resolution.js';
|
|
19
|
+
import { groupThreads } from './threads.js';
|
|
20
|
+
const values = (event, name) => event.tags.filter((t) => t[0] === name && typeof t[1] === 'string' && t[1] !== '').map((t) => t[1]);
|
|
21
|
+
/**
|
|
22
|
+
* What its author took back: the ids a `kind:5` names by `e`, kept only for
|
|
23
|
+
* the event it names when its signer wrote that event (§6.5). A `kind:5` for
|
|
24
|
+
* an event the read does not hold is kept by its signer, for a top-level
|
|
25
|
+
* message known only from its replies.
|
|
26
|
+
*/
|
|
27
|
+
function deletionsOf(events) {
|
|
28
|
+
const by = new Map();
|
|
29
|
+
for (const event of events) {
|
|
30
|
+
if (event.kind !== KIND_DELETION)
|
|
31
|
+
continue;
|
|
32
|
+
for (const id of values(event, 'e')) {
|
|
33
|
+
if (!isEventId(id))
|
|
34
|
+
continue;
|
|
35
|
+
const key = id.toLowerCase();
|
|
36
|
+
if (!by.has(key))
|
|
37
|
+
by.set(key, new Set());
|
|
38
|
+
by.get(key).add(event.pubkey);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return by;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Fold a conversation into its threads (§6.3–§6.8).
|
|
45
|
+
*
|
|
46
|
+
* `events` is everything read for it: its `kind:9` or `kind:1111` messages,
|
|
47
|
+
* and the `kind:40003` edits, `kind:7` reactions, `kind:5` deletions and
|
|
48
|
+
* `kind:9101` resolutions read by `#e` on them. Duplicates are read once.
|
|
49
|
+
* `viewer` marks the viewer's own reactions (`Reaction.mine`).
|
|
50
|
+
*
|
|
51
|
+
* Threads come oldest first by their top-level message, or by their first
|
|
52
|
+
* reply when it was not returned. A reply to a reply is filed under the
|
|
53
|
+
* thread's top-level message (§6.4).
|
|
54
|
+
*/
|
|
55
|
+
export function foldThreads(events, options = {}) {
|
|
56
|
+
const unique = [...new Map(events.map((e) => [e.id, e])).values()];
|
|
57
|
+
const deletions = deletionsOf(unique);
|
|
58
|
+
const deletedBy = (id, author) => author !== undefined && deletions.get(id.toLowerCase())?.has(author) === true;
|
|
59
|
+
/*
|
|
60
|
+
A top-level message the read did not return is known deleted only by its
|
|
61
|
+
author's `kind:5`, and its author only from its replies: a reply's
|
|
62
|
+
lowercase `p` names the comment's author (§6.4). Every `kind:1111` reply
|
|
63
|
+
must name the deletion's signer, so one reply's tags cannot speak for a
|
|
64
|
+
thread others answered; a channel `kind:9` names no author, and stays
|
|
65
|
+
`missing`.
|
|
66
|
+
*/
|
|
67
|
+
/** A comment the read did not return is in the Folder most of its replies were filed in, or none on a tie (§6.5). */
|
|
68
|
+
const replyFolderOf = (answers) => {
|
|
69
|
+
const count = new Map();
|
|
70
|
+
for (const r of answers) {
|
|
71
|
+
const h = values(r, 'h')[0];
|
|
72
|
+
if (h)
|
|
73
|
+
count.set(h, (count.get(h) ?? 0) + 1);
|
|
74
|
+
}
|
|
75
|
+
const ranked = [...count].sort((a, b) => b[1] - a[1]);
|
|
76
|
+
return ranked.length > 0 && (ranked.length === 1 || ranked[0][1] > ranked[1][1]) ? ranked[0][0] : undefined;
|
|
77
|
+
};
|
|
78
|
+
const deletedUnseen = (id, answers) => {
|
|
79
|
+
const comments = answers.filter((r) => r.kind === KIND_COMMENT);
|
|
80
|
+
const signers = deletions.get(id.toLowerCase());
|
|
81
|
+
if (!signers || comments.length === 0)
|
|
82
|
+
return false;
|
|
83
|
+
return [...signers].some((signer) => comments.every((r) => r.tags.some((t) => t[0] === 'p' && t[1] === signer)));
|
|
84
|
+
};
|
|
85
|
+
const messages = unique.filter((e) => isConversationKind(e.kind));
|
|
86
|
+
const edits = foldEdits(unique);
|
|
87
|
+
const edited = editsByTarget(unique);
|
|
88
|
+
const cardsOf = foldEmbeds(unique);
|
|
89
|
+
// A taken-back reaction leaves before the one-per-person fold, so the same person's later one counts.
|
|
90
|
+
const reactions = groupReactions(reactionEventsOf(unique.filter((e) => e.kind !== KIND_REACTION || !deletedBy(e.id, e.pubkey))), options.viewer);
|
|
91
|
+
const resolutions = resolutionsByTarget(unique);
|
|
92
|
+
const messageOf = (event) => {
|
|
93
|
+
const id = event.id.toLowerCase();
|
|
94
|
+
const fold = edits[id];
|
|
95
|
+
// An edit is the whole new message (§6.8): what its words link is the latest edit's `a` and `q`.
|
|
96
|
+
const linkSource = edited.get(id)?.at(-1) ?? event;
|
|
97
|
+
const blockTag = event.tags.find((t) => t[0] === BLOCK_ANCHOR_TAG);
|
|
98
|
+
const blocks = blockTag ? readRunIds(blockTag.slice(1)) : undefined;
|
|
99
|
+
const quote = blocks ? quoteOf(event) : undefined;
|
|
100
|
+
const folder = values(event, 'h')[0];
|
|
101
|
+
return {
|
|
102
|
+
id,
|
|
103
|
+
kind: event.kind,
|
|
104
|
+
author: event.pubkey,
|
|
105
|
+
...(folder ? { folder } : {}),
|
|
106
|
+
at: orderingMs(event),
|
|
107
|
+
body: fold?.body ?? event.content,
|
|
108
|
+
edited: fold?.edited ?? false,
|
|
109
|
+
...(fold?.editedAt !== undefined ? { editedAt: fold.editedAt } : {}),
|
|
110
|
+
files: fold ? fold.attachments : imetaOf(event),
|
|
111
|
+
cards: cardsOf[id] ?? [],
|
|
112
|
+
parts: partsOf(event),
|
|
113
|
+
links: [...new Set([...values(linkSource, 'a'), ...values(linkSource, 'q')])],
|
|
114
|
+
...(blocks && event.kind === KIND_COMMENT ? { anchor: { blocks, ...(quote ? { quote } : {}) } } : {}),
|
|
115
|
+
reactions: reactions[id] ?? [],
|
|
116
|
+
event,
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
const { roots, replies, missingRoots } = groupThreads(messages);
|
|
120
|
+
const threads = [];
|
|
121
|
+
const thread = (id, root) => {
|
|
122
|
+
const all = replies[id] ?? [];
|
|
123
|
+
const kept = all.filter((r) => !deletedBy(r.id, r.pubkey)).map(messageOf);
|
|
124
|
+
const folder = root ? values(root, 'h')[0] : replyFolderOf(all);
|
|
125
|
+
// Not returned, only the replies filed in the thread's Folder speak for its comment: one filed elsewhere neither deletes nor clears.
|
|
126
|
+
const speaking = root ? all : all.filter((r) => values(r, 'h')[0] === folder);
|
|
127
|
+
const state = (root ? deletedBy(id, root.pubkey) : folder !== undefined && deletedUnseen(id, speaking)) ? 'deleted' : root ? 'present' : 'missing';
|
|
128
|
+
const resolution = resolutionOf(resolutions[id.toLowerCase()] ?? [], { folder, replies: kept.map((r) => r.id) });
|
|
129
|
+
return {
|
|
130
|
+
id: id.toLowerCase(),
|
|
131
|
+
state,
|
|
132
|
+
...(state === 'present' && root ? { root: messageOf(root) } : {}),
|
|
133
|
+
replies: kept,
|
|
134
|
+
...(folder ? { folder } : {}),
|
|
135
|
+
...(resolution ? { resolution } : {}),
|
|
136
|
+
resolvable: state === 'present',
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
for (const root of roots)
|
|
140
|
+
threads.push(thread(root.id, root));
|
|
141
|
+
for (const id of missingRoots)
|
|
142
|
+
threads.push(thread(id, undefined));
|
|
143
|
+
// A thread whose top-level message is gone and has no reply left has nothing to draw.
|
|
144
|
+
const drawn = threads.filter((t) => t.root || t.replies.length > 0);
|
|
145
|
+
const firstAt = (t) => t.root?.at ?? t.replies[0].at;
|
|
146
|
+
return drawn.sort((a, b) => firstAt(a) - firstAt(b) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=shape.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape.js","sourceRoot":"","sources":["../src/shape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EACL,gBAAgB,EAChB,OAAO,EACP,OAAO,EACP,OAAO,EACP,UAAU,GAMX,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACjE,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AACtG,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAiB,MAAM,gBAAgB,CAAA;AAChF,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAmB,MAAM,iBAAiB,CAAA;AACpF,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAqE3C,MAAM,MAAM,GAAG,CAAC,KAAgC,EAAE,IAAY,EAAE,EAAE,CAChE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAEr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M,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACnE,MAAM,OAAO,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IAC5D,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAClG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@estiva-app/conversation",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The conversation rules of the Estiva SPEC (§6.2–§6.8, §11): comment and mention strengths, threading, the edit, reaction and attachment folds, NIP-22 comment and reply builders, drafts, NIP-RS read state, membership and unread. A convenience an app adopts, never a requirement.",
|
|
3
|
+
"version": "0.32.0",
|
|
4
|
+
"description": "The conversation rules of the Estiva SPEC (§6.2–§6.8, §11): comment and mention strengths, threading, one shape for a message and a thread, resolving a thread, the line rule, the edit, reaction and attachment folds, NIP-22 comment and reply builders, drafts, NIP-RS read state, membership and unread. A convenience an app adopts, never a requirement.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
package/src/decorations.ts
CHANGED
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
import { MAX_FILTERS_PER_QUERY, RELAY_PAGE_CEILING, type SignedEvent } from '@estiva-app/protocol'
|
|
12
12
|
import { editTargetOf, foldEdits, type EditFold } from './edits.js'
|
|
13
13
|
import { KIND_ASSERTION, KIND_EDIT, KIND_REACTION } from './kinds.js'
|
|
14
|
+
import { resolutionsByTarget, resolutionTargetOf, type Resolution } from './resolution.js'
|
|
15
|
+
|
|
16
|
+
export type { Resolution }
|
|
14
17
|
import { reactionEventsOf, reactionHorizon, reactionTargetOf, type ReactionEvent } from './reactions.js'
|
|
15
18
|
|
|
16
19
|
/**
|
|
@@ -20,25 +23,17 @@ import { reactionEventsOf, reactionHorizon, reactionTargetOf, type ReactionEvent
|
|
|
20
23
|
*/
|
|
21
24
|
export type QueryFn = (filters: Record<string, unknown>[]) => Promise<SignedEvent[]>
|
|
22
25
|
|
|
23
|
-
/** A `kind:9101` resolution assertion on a message (PEEK-128). */
|
|
24
|
-
export interface Resolution {
|
|
25
|
-
id: string
|
|
26
|
-
action: 'resolved' | 'reopened'
|
|
27
|
-
by: string
|
|
28
|
-
at: number
|
|
29
|
-
/** The assertion's free text, when it carried one. */
|
|
30
|
-
message?: string
|
|
31
|
-
/** The reply that carried the resolution, when the writer named one. */
|
|
32
|
-
supportingEventId?: string
|
|
33
|
-
}
|
|
34
|
-
|
|
35
26
|
/** What has been done to one message since it was written. */
|
|
36
27
|
export interface Decoration {
|
|
37
28
|
/** Its edits, folded (§6.8). Absent when nobody edited it. */
|
|
38
29
|
edit?: EditFold
|
|
39
30
|
/** Its reactions, one per `(pubkey, emoji)`, oldest first. Empty inside the horizon means none. */
|
|
40
31
|
reactions: ReactionEvent[]
|
|
41
|
-
/**
|
|
32
|
+
/**
|
|
33
|
+
* Every resolve and reopen naming it, oldest first by §6.3. Which one counts
|
|
34
|
+
* — `h` checked, a `support` outside the thread dropped — is
|
|
35
|
+
* `resolutionOf`'s: the last one here is not the state on its own.
|
|
36
|
+
*/
|
|
42
37
|
resolutions: Resolution[]
|
|
43
38
|
}
|
|
44
39
|
|
|
@@ -74,7 +69,6 @@ const chunked = (list: readonly string[]) => {
|
|
|
74
69
|
return out
|
|
75
70
|
}
|
|
76
71
|
|
|
77
|
-
const tagValue = (event: SignedEvent, name: string) => event.tags.find((t) => t[0] === name)?.[1]
|
|
78
72
|
|
|
79
73
|
/**
|
|
80
74
|
* Edits, reactions and resolutions for `targets`, in as few requests as the
|
|
@@ -89,8 +83,8 @@ const tagValue = (event: SignedEvent, name: string) => event.tags.find((t) => t[
|
|
|
89
83
|
* one per `(target, pubkey, emoji)`.
|
|
90
84
|
* - **Resolutions** are `kind:9101` carrying `t=resolution`; the `action` tag
|
|
91
85
|
* is the state, the content the rationale, and an `e` marked `support` names
|
|
92
|
-
* the reply that carried it. The target is the first unmarked `e`
|
|
93
|
-
*
|
|
86
|
+
* the reply that carried it. The target is the first unmarked `e`
|
|
87
|
+
* ({@link resolutionTargetOf}), and a thread's state is `resolutionOf`'s.
|
|
94
88
|
*
|
|
95
89
|
* Edits and resolutions have no horizon: they are one event per change, not
|
|
96
90
|
* one per reader. A refused read is whatever `query` does with one.
|
|
@@ -155,7 +149,7 @@ export async function decorationsOf(
|
|
|
155
149
|
}
|
|
156
150
|
const reactionEventsCut = cut(reactionChunks, [KIND_REACTION], reactionTargetOf)
|
|
157
151
|
const editEventsCut = cut(editChunks, [KIND_EDIT, KIND_ASSERTION], (e) =>
|
|
158
|
-
e.kind === KIND_EDIT ? editTargetOf(e) :
|
|
152
|
+
e.kind === KIND_EDIT ? editTargetOf(e) : resolutionTargetOf(e),
|
|
159
153
|
)
|
|
160
154
|
|
|
161
155
|
const inHorizon = new Set(asked)
|
|
@@ -164,24 +158,9 @@ export async function decorationsOf(
|
|
|
164
158
|
if (ours(reaction.target) && inHorizon.has(reaction.target)) byId[reaction.target].reactions.push(reaction)
|
|
165
159
|
}
|
|
166
160
|
|
|
167
|
-
for (const
|
|
168
|
-
if (
|
|
169
|
-
const action = tagValue(event, 'action')
|
|
170
|
-
if (action !== 'resolved' && action !== 'reopened') continue
|
|
171
|
-
const target = event.tags.find((t) => t[0] === 'e' && ours(t[1]) && !t[3])?.[1]
|
|
172
|
-
if (!ours(target)) continue
|
|
173
|
-
const support = event.tags.find((t) => t[0] === 'e' && t[3] === 'support')?.[1]
|
|
174
|
-
byId[target].resolutions.push({
|
|
175
|
-
id: event.id,
|
|
176
|
-
action,
|
|
177
|
-
by: event.pubkey,
|
|
178
|
-
at: event.created_at,
|
|
179
|
-
...(event.content ? { message: event.content } : {}),
|
|
180
|
-
...(support ? { supportingEventId: support } : {}),
|
|
181
|
-
})
|
|
161
|
+
for (const [target, list] of Object.entries(resolutionsByTarget(events))) {
|
|
162
|
+
if (ours(target)) byId[target].resolutions = list
|
|
182
163
|
}
|
|
183
|
-
const oldestFirst = (a: { at: number; id: string }, b: { at: number; id: string }) => a.at - b.at || (a.id < b.id ? -1 : 1)
|
|
184
|
-
for (const decoration of Object.values(byId)) decoration.resolutions.sort(oldestFirst)
|
|
185
164
|
|
|
186
165
|
return { byId, reactionTargetsOmitted: omitted, reactionEventsCut, editEventsCut }
|
|
187
166
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* apps: it imports no app's data layer, opens no socket, and takes every
|
|
13
13
|
* environment touch — the relay read, the clock, storage — as a parameter.
|
|
14
14
|
*/
|
|
15
|
-
export const CONVERSATION_VERSION = '0.
|
|
15
|
+
export const CONVERSATION_VERSION = '0.32.0'
|
|
16
16
|
|
|
17
17
|
export {
|
|
18
18
|
KIND_MESSAGE,
|
|
@@ -64,7 +64,20 @@ export {
|
|
|
64
64
|
type Reaction,
|
|
65
65
|
} from './reactions.js'
|
|
66
66
|
|
|
67
|
-
export { decorationsOf, type Decoration, type Decorations, type
|
|
67
|
+
export { decorationsOf, type Decoration, type Decorations, type QueryFn } from './decorations.js'
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
resolutionOf,
|
|
71
|
+
resolutionOfEvent,
|
|
72
|
+
resolutionTargetOf,
|
|
73
|
+
resolutionsByTarget,
|
|
74
|
+
byResolutionOrder,
|
|
75
|
+
type Resolution,
|
|
76
|
+
} from './resolution.js'
|
|
77
|
+
|
|
78
|
+
export { foldThreads, type Message, type Thread, type RootState } from './shape.js'
|
|
79
|
+
|
|
80
|
+
export { placeLines, writtenUrlOf, urlNames, ROW_BLOCK, type RowLine, type PlacedLines, type LineContext } from './lines.js'
|
|
68
81
|
|
|
69
82
|
export { buildComment, buildReply, offersEditAndDelete, placeOf, type CommentRef } from './builders.js'
|
|
70
83
|
|
package/src/lines.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a message's lines draw — the one line rule (SPEC §13.2, §13.6.2, C21),
|
|
3
|
+
* shared since ONE-2. It replaces Peek's and Ship's `messageLines.ts`, Leaf's
|
|
4
|
+
* `commentLines.ts` and Peek's `urlNames`, three copies that had drifted.
|
|
5
|
+
*
|
|
6
|
+
* A sentence keeps its chips; a line that is only a link to a file or a
|
|
7
|
+
* message is that file's row; and one whose file has a card or a part under
|
|
8
|
+
* the message draws nothing, since the card stands for it (C26). A person or a
|
|
9
|
+
* Folder alone on its line stays a chip, and a quote's or a heading's line is a
|
|
10
|
+
* sentence (protocol `lineDrawOf`).
|
|
11
|
+
*
|
|
12
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-link-on-words-counts-toward-the-card.md
|
|
13
|
+
*/
|
|
14
|
+
import {
|
|
15
|
+
lineDrawOf,
|
|
16
|
+
linesOf,
|
|
17
|
+
referenceOfLink,
|
|
18
|
+
type Embed,
|
|
19
|
+
type LinkResolver,
|
|
20
|
+
type PartPointer,
|
|
21
|
+
type RenderBlock,
|
|
22
|
+
type RenderInline,
|
|
23
|
+
} from '@estiva-app/protocol'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A line of a message that is only a link to a file or a message, drawn as
|
|
27
|
+
* that file's row in place. `reference` is the `nostr:` reference the line
|
|
28
|
+
* names; `unreadable` is one that names no address or event id, drawn as the
|
|
29
|
+
* not-available row.
|
|
30
|
+
*/
|
|
31
|
+
export interface RowLine {
|
|
32
|
+
reference: string
|
|
33
|
+
/** The file's value (an address or an id), when the line names one. */
|
|
34
|
+
value?: string
|
|
35
|
+
unreadable?: true
|
|
36
|
+
/** The URL the line was written as, when it names that same file: the row opens it as written (LTX-6). */
|
|
37
|
+
url?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A paragraph's line that is a row, lifted out as a block of its own, so the row is not drawn inside a `<p>`. */
|
|
41
|
+
export const ROW_BLOCK = 'linkRow'
|
|
42
|
+
|
|
43
|
+
/** The answer of {@link placeLines}. */
|
|
44
|
+
export interface PlacedLines {
|
|
45
|
+
blocks: RenderBlock[]
|
|
46
|
+
/** The row a block of type {@link ROW_BLOCK} draws. */
|
|
47
|
+
rowOf: (block: { type: string; attrs?: Record<string, unknown> }) => RowLine | undefined
|
|
48
|
+
/** The row a list item's only run draws, by that run: a bullet that is only a link keeps its bullet. */
|
|
49
|
+
rowRuns: ReadonlyMap<object, RowLine>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** What a line's drawing reads beyond the line. */
|
|
53
|
+
export interface LineContext {
|
|
54
|
+
/** The message's cards (`foldEmbeds`, `cardsIn`): a line whose file has one draws nothing. */
|
|
55
|
+
cards?: readonly Embed[]
|
|
56
|
+
/** The `part`s drawn under the message: a line whose file has one draws nothing. */
|
|
57
|
+
parts?: readonly PartPointer[]
|
|
58
|
+
/** What an app link names (§7.7), as answered so far. */
|
|
59
|
+
resolveLink?: LinkResolver
|
|
60
|
+
/**
|
|
61
|
+
* Values whose lines stay sentences, their chip only: the file the message
|
|
62
|
+
* is shown on draws no row of itself there (Miky, WID-3).
|
|
63
|
+
*/
|
|
64
|
+
stayChip?: (value: string) => boolean
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The URL a run wrote for its reference, when the URL names that same file:
|
|
69
|
+
* what its chip or row opens (LTX-6). A marker or a block comment can set any
|
|
70
|
+
* URL's text on a reference to another file, so the text must itself read as
|
|
71
|
+
* it; without `resolveLink` nothing can be checked and there is none.
|
|
72
|
+
*/
|
|
73
|
+
export function writtenUrlOf(run: Pick<RenderInline, 'text' | 'reference'>, resolveLink: LinkResolver | undefined): string | undefined {
|
|
74
|
+
if (!run.reference || !resolveLink || !/^https?:\/\//i.test(run.text)) return undefined
|
|
75
|
+
return referenceOfLink(run.text, resolveLink) === run.reference ? run.text : undefined
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Whether a pasted app link names this address: its path ends in the
|
|
80
|
+
* address's `d` (§7.2 puts the identity last, `…/<slug>-<d>`), and that `d` is
|
|
81
|
+
* long enough to be one. A `d` comes from somebody's tag, so a short one would
|
|
82
|
+
* otherwise match half the links in a body.
|
|
83
|
+
*/
|
|
84
|
+
export function urlNames(url: string, address: string): boolean {
|
|
85
|
+
const d = address.split(':')[2] ?? ''
|
|
86
|
+
if (d.length < 8) return false
|
|
87
|
+
const path = url.split(/[?#]/)[0].replace(/\/+$/, '')
|
|
88
|
+
return path.endsWith(`-${d}`) || path.endsWith(`/${d}`)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const BREAK: RenderInline = { text: '\n', marks: [] }
|
|
92
|
+
|
|
93
|
+
function rowLineOf(line: readonly RenderInline[], block: Pick<RenderBlock, 'type'>, context: LineContext): RowLine | 'nothing' | undefined {
|
|
94
|
+
const drawn = lineDrawOf(line, block, context)
|
|
95
|
+
if ((drawn.draw === 'row' || drawn.draw === 'nothing') && context.stayChip?.(drawn.value)) return undefined
|
|
96
|
+
if (drawn.draw === 'nothing') return 'nothing'
|
|
97
|
+
if (drawn.draw === 'unreadable') return { reference: drawn.reference, unreadable: true }
|
|
98
|
+
// A URL whose file is not read yet stays the link it was; once read, `linksAsReferences` made it a reference.
|
|
99
|
+
if (drawn.draw !== 'row' || drawn.url) return undefined
|
|
100
|
+
const run = line.find((r) => r.reference)
|
|
101
|
+
if (!run?.reference) return undefined
|
|
102
|
+
const url = writtenUrlOf(run, context.resolveLink)
|
|
103
|
+
return { reference: run.reference, value: drawn.value, ...(url ? { url } : {}) }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Where each of a message's lines draws. `blocks` is its tree after
|
|
108
|
+
* `linksAsReferences`, so a lone app URL this reader can read is already its
|
|
109
|
+
* reference, and one it cannot stays the URL as written.
|
|
110
|
+
*/
|
|
111
|
+
export function placeLines(blocks: readonly RenderBlock[], context: LineContext = {}): PlacedLines {
|
|
112
|
+
const rows = new Map<object, RowLine>()
|
|
113
|
+
const rowRuns = new Map<RenderInline, RowLine>()
|
|
114
|
+
const out: RenderBlock[] = []
|
|
115
|
+
for (const block of blocks) {
|
|
116
|
+
if (block.type === 'paragraph') {
|
|
117
|
+
const lines = linesOf(block)
|
|
118
|
+
const drawn = lines.map((line) => rowLineOf(line, block, context))
|
|
119
|
+
if (drawn.every((d) => d === undefined)) {
|
|
120
|
+
out.push(block)
|
|
121
|
+
continue
|
|
122
|
+
}
|
|
123
|
+
let sentence: RenderInline[][] = []
|
|
124
|
+
let first = true
|
|
125
|
+
const flush = () => {
|
|
126
|
+
if (!sentence.length) return
|
|
127
|
+
const inline = sentence.flatMap((line, i) => (i > 0 ? [BREAK, ...line] : line))
|
|
128
|
+
out.push({ ...block, inline, ...(first ? {} : { id: undefined }) })
|
|
129
|
+
first = false
|
|
130
|
+
sentence = []
|
|
131
|
+
}
|
|
132
|
+
lines.forEach((line, i) => {
|
|
133
|
+
const d = drawn[i]
|
|
134
|
+
if (d === undefined) {
|
|
135
|
+
sentence.push(line)
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
flush()
|
|
139
|
+
if (d === 'nothing') return
|
|
140
|
+
// Not a protocol block: a drawer hands a type it has no design for to its own renderer.
|
|
141
|
+
const row = { type: ROW_BLOCK, attrs: {} } as unknown as RenderBlock
|
|
142
|
+
rows.set(row, d)
|
|
143
|
+
out.push(row)
|
|
144
|
+
})
|
|
145
|
+
flush()
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
if (block.type === 'bulletList' || block.type === 'orderedList') {
|
|
149
|
+
const items = (block.children ?? []).flatMap((item) => {
|
|
150
|
+
const lines = linesOf(item)
|
|
151
|
+
if (lines.length !== 1) return [item]
|
|
152
|
+
const d = rowLineOf(lines[0], item, context)
|
|
153
|
+
if (d === undefined) return [item]
|
|
154
|
+
if (d === 'nothing') return []
|
|
155
|
+
// A fresh run, so the map names this line and no other drawing of the same body.
|
|
156
|
+
const run = { ...lines[0].find((r) => r.reference || r.text.trim())! }
|
|
157
|
+
rowRuns.set(run, d)
|
|
158
|
+
return [{ ...item, inline: [run] }]
|
|
159
|
+
})
|
|
160
|
+
if (items.length) out.push({ ...block, children: items })
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
out.push(block)
|
|
164
|
+
}
|
|
165
|
+
return { blocks: out, rowOf: (block) => rows.get(block), rowRuns }
|
|
166
|
+
}
|