@estiva-app/conversation 0.31.0 → 0.33.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 +57 -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 +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -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/refusals.d.ts +59 -0
- package/dist/refusals.d.ts.map +1 -0
- package/dist/refusals.js +168 -0
- package/dist/refusals.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 +27 -2
- package/src/lines.ts +166 -0
- package/src/refusals.ts +176 -0
- package/src/resolution.ts +112 -0
- package/src/shape.ts +228 -0
package/src/shape.ts
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
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 {
|
|
14
|
+
BLOCK_ANCHOR_TAG,
|
|
15
|
+
imetaOf,
|
|
16
|
+
partsOf,
|
|
17
|
+
quoteOf,
|
|
18
|
+
readRunIds,
|
|
19
|
+
type Embed,
|
|
20
|
+
type Imeta,
|
|
21
|
+
type PartPointer,
|
|
22
|
+
type Quote,
|
|
23
|
+
type SignedEvent,
|
|
24
|
+
} from '@estiva-app/protocol'
|
|
25
|
+
import { editsByTarget, foldEdits, foldEmbeds } from './edits.js'
|
|
26
|
+
import { isConversationKind, isEventId, KIND_COMMENT, KIND_DELETION, KIND_REACTION } from './kinds.js'
|
|
27
|
+
import { orderingMs } from './order.js'
|
|
28
|
+
import { groupReactions, reactionEventsOf, type Reaction } from './reactions.js'
|
|
29
|
+
import { resolutionOf, resolutionsByTarget, type Resolution } from './resolution.js'
|
|
30
|
+
import { groupThreads } from './threads.js'
|
|
31
|
+
|
|
32
|
+
/** One message — a top-level comment, a channel message or a reply — as it stands after its edits. */
|
|
33
|
+
export interface Message {
|
|
34
|
+
id: string
|
|
35
|
+
kind: number
|
|
36
|
+
/** Who wrote it, by key. */
|
|
37
|
+
author: string
|
|
38
|
+
/** Its `h`: the Folder it was filed in. */
|
|
39
|
+
folder?: string
|
|
40
|
+
/** When it was written, epoch milliseconds (§6.3: the trusted `ts`, else `created_at` × 1000). */
|
|
41
|
+
at: number
|
|
42
|
+
/** The text to show: the latest edit's, else its own (§6.8). */
|
|
43
|
+
body: string
|
|
44
|
+
/** Whether an edit changed the body — what a reader MUST mark (§6.8). */
|
|
45
|
+
edited: boolean
|
|
46
|
+
/** When the body last changed, epoch milliseconds. Absent unless `edited`. */
|
|
47
|
+
editedAt?: number
|
|
48
|
+
/** Its files: the latest edit's `imeta` set, else its own (§6.8, C15). */
|
|
49
|
+
files: Imeta[]
|
|
50
|
+
/** The cards its writer carried (`embed`, §13.6.2), after edits (C20). Which of them the body still names is the drawer's (`cardsIn`). */
|
|
51
|
+
cards: Embed[]
|
|
52
|
+
/** The blocks drawn live under it (`part`, §13.6.1). An edit carries none, so these are the message's own. */
|
|
53
|
+
parts: PartPointer[]
|
|
54
|
+
/** 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). */
|
|
55
|
+
links: string[]
|
|
56
|
+
/**
|
|
57
|
+
* What a comment on part of a document is about (§13.6): the run of blocks
|
|
58
|
+
* in its `block` tag, and the words in its `quote` when it quotes some. An
|
|
59
|
+
* edit never moves it.
|
|
60
|
+
*/
|
|
61
|
+
anchor?: { blocks: string[]; quote?: Quote }
|
|
62
|
+
/** Its reactions by emoji, each reactor by key (§6.6). */
|
|
63
|
+
reactions: Reaction[]
|
|
64
|
+
/** The event itself, for what this shape does not carry. */
|
|
65
|
+
event: SignedEvent
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The thread's top-level message, in one of §6.5's three states:
|
|
70
|
+
* - `present` — returned, and not deleted;
|
|
71
|
+
* - `deleted` — its author's `kind:5` came back: drawn as "This comment was
|
|
72
|
+
* deleted", its replies under it;
|
|
73
|
+
* - `missing` — only its replies were read. Drawn as their thread, never as
|
|
74
|
+
* deleted: it may only be outside the read.
|
|
75
|
+
*/
|
|
76
|
+
export type RootState = 'present' | 'deleted' | 'missing'
|
|
77
|
+
|
|
78
|
+
/** A thread: a top-level message, its replies, and its state. */
|
|
79
|
+
export interface Thread {
|
|
80
|
+
/** The top-level message's id, whether or not it was returned. */
|
|
81
|
+
id: string
|
|
82
|
+
state: RootState
|
|
83
|
+
/** The top-level message, when `present`. */
|
|
84
|
+
root?: Message
|
|
85
|
+
/** Its replies, oldest first by §6.3; a deleted reply is left out. */
|
|
86
|
+
replies: Message[]
|
|
87
|
+
/** The thread's Folder: its top-level message's `h`, else its replies'. */
|
|
88
|
+
folder?: string
|
|
89
|
+
/** The resolve or reopen that counts (§6.4, C51); absent when none does. `resolution.action` is the state. */
|
|
90
|
+
resolution?: Resolution
|
|
91
|
+
/**
|
|
92
|
+
* Whether Resolve or Reopen may be offered, as far as the thread can say:
|
|
93
|
+
* only on a top-level message that is `present`. Whether the reader may
|
|
94
|
+
* write in its Folder, and whether its file is archived, is the app's to add.
|
|
95
|
+
*/
|
|
96
|
+
resolvable: boolean
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const values = (event: Pick<SignedEvent, 'tags'>, name: string) =>
|
|
100
|
+
event.tags.filter((t) => t[0] === name && typeof t[1] === 'string' && t[1] !== '').map((t) => t[1])
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* What its author took back: the ids a `kind:5` names by `e`, kept only for
|
|
104
|
+
* the event it names when its signer wrote that event (§6.5). A `kind:5` for
|
|
105
|
+
* an event the read does not hold is kept by its signer, for a top-level
|
|
106
|
+
* message known only from its replies.
|
|
107
|
+
*/
|
|
108
|
+
function deletionsOf(events: readonly SignedEvent[]): Map<string, Set<string>> {
|
|
109
|
+
const by = new Map<string, Set<string>>()
|
|
110
|
+
for (const event of events) {
|
|
111
|
+
if (event.kind !== KIND_DELETION) continue
|
|
112
|
+
for (const id of values(event, 'e')) {
|
|
113
|
+
if (!isEventId(id)) continue
|
|
114
|
+
const key = id.toLowerCase()
|
|
115
|
+
if (!by.has(key)) by.set(key, new Set())
|
|
116
|
+
by.get(key)!.add(event.pubkey)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return by
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Fold a conversation into its threads (§6.3–§6.8).
|
|
124
|
+
*
|
|
125
|
+
* `events` is everything read for it: its `kind:9` or `kind:1111` messages,
|
|
126
|
+
* and the `kind:40003` edits, `kind:7` reactions, `kind:5` deletions and
|
|
127
|
+
* `kind:9101` resolutions read by `#e` on them. Duplicates are read once.
|
|
128
|
+
* `viewer` marks the viewer's own reactions (`Reaction.mine`).
|
|
129
|
+
*
|
|
130
|
+
* Threads come oldest first by their top-level message, or by their first
|
|
131
|
+
* reply when it was not returned. A reply to a reply is filed under the
|
|
132
|
+
* thread's top-level message (§6.4).
|
|
133
|
+
*/
|
|
134
|
+
export function foldThreads(events: readonly SignedEvent[], options: { viewer?: string } = {}): Thread[] {
|
|
135
|
+
const unique = [...new Map(events.map((e) => [e.id, e])).values()]
|
|
136
|
+
const deletions = deletionsOf(unique)
|
|
137
|
+
const deletedBy = (id: string, author: string | undefined) => author !== undefined && deletions.get(id.toLowerCase())?.has(author) === true
|
|
138
|
+
/*
|
|
139
|
+
A top-level message the read did not return is known deleted only by its
|
|
140
|
+
author's `kind:5`, and its author only from its replies: a reply's
|
|
141
|
+
lowercase `p` names the comment's author (§6.4). Every `kind:1111` reply
|
|
142
|
+
must name the deletion's signer, so one reply's tags cannot speak for a
|
|
143
|
+
thread others answered; a channel `kind:9` names no author, and stays
|
|
144
|
+
`missing`.
|
|
145
|
+
*/
|
|
146
|
+
/** 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). */
|
|
147
|
+
const replyFolderOf = (answers: readonly SignedEvent[]): string | undefined => {
|
|
148
|
+
const count = new Map<string, number>()
|
|
149
|
+
for (const r of answers) {
|
|
150
|
+
const h = values(r, 'h')[0]
|
|
151
|
+
if (h) count.set(h, (count.get(h) ?? 0) + 1)
|
|
152
|
+
}
|
|
153
|
+
const ranked = [...count].sort((a, b) => b[1] - a[1])
|
|
154
|
+
return ranked.length > 0 && (ranked.length === 1 || ranked[0][1] > ranked[1][1]) ? ranked[0][0] : undefined
|
|
155
|
+
}
|
|
156
|
+
const deletedUnseen = (id: string, answers: readonly SignedEvent[]) => {
|
|
157
|
+
const comments = answers.filter((r) => r.kind === KIND_COMMENT)
|
|
158
|
+
const signers = deletions.get(id.toLowerCase())
|
|
159
|
+
if (!signers || comments.length === 0) return false
|
|
160
|
+
return [...signers].some((signer) => comments.every((r) => r.tags.some((t) => t[0] === 'p' && t[1] === signer)))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const messages = unique.filter((e) => isConversationKind(e.kind))
|
|
164
|
+
const edits = foldEdits(unique)
|
|
165
|
+
const edited = editsByTarget(unique)
|
|
166
|
+
const cardsOf = foldEmbeds(unique)
|
|
167
|
+
// A taken-back reaction leaves before the one-per-person fold, so the same person's later one counts.
|
|
168
|
+
const reactions = groupReactions(
|
|
169
|
+
reactionEventsOf(unique.filter((e) => e.kind !== KIND_REACTION || !deletedBy(e.id, e.pubkey))),
|
|
170
|
+
options.viewer,
|
|
171
|
+
)
|
|
172
|
+
const resolutions = resolutionsByTarget(unique)
|
|
173
|
+
|
|
174
|
+
const messageOf = (event: SignedEvent): Message => {
|
|
175
|
+
const id = event.id.toLowerCase()
|
|
176
|
+
const fold = edits[id]
|
|
177
|
+
// An edit is the whole new message (§6.8): what its words link is the latest edit's `a` and `q`.
|
|
178
|
+
const linkSource = edited.get(id)?.at(-1) ?? event
|
|
179
|
+
const blockTag = event.tags.find((t) => t[0] === BLOCK_ANCHOR_TAG)
|
|
180
|
+
const blocks = blockTag ? readRunIds(blockTag.slice(1)) : undefined
|
|
181
|
+
const quote = blocks ? quoteOf(event) : undefined
|
|
182
|
+
const folder = values(event, 'h')[0]
|
|
183
|
+
return {
|
|
184
|
+
id,
|
|
185
|
+
kind: event.kind,
|
|
186
|
+
author: event.pubkey,
|
|
187
|
+
...(folder ? { folder } : {}),
|
|
188
|
+
at: orderingMs(event),
|
|
189
|
+
body: fold?.body ?? event.content,
|
|
190
|
+
edited: fold?.edited ?? false,
|
|
191
|
+
...(fold?.editedAt !== undefined ? { editedAt: fold.editedAt } : {}),
|
|
192
|
+
files: fold ? fold.attachments : imetaOf(event),
|
|
193
|
+
cards: cardsOf[id] ?? [],
|
|
194
|
+
parts: partsOf(event),
|
|
195
|
+
links: [...new Set([...values(linkSource, 'a'), ...values(linkSource, 'q')])],
|
|
196
|
+
...(blocks && event.kind === KIND_COMMENT ? { anchor: { blocks, ...(quote ? { quote } : {}) } } : {}),
|
|
197
|
+
reactions: reactions[id] ?? [],
|
|
198
|
+
event,
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const { roots, replies, missingRoots } = groupThreads(messages)
|
|
203
|
+
const threads: Thread[] = []
|
|
204
|
+
const thread = (id: string, root: SignedEvent | undefined): Thread => {
|
|
205
|
+
const all = replies[id] ?? []
|
|
206
|
+
const kept = all.filter((r) => !deletedBy(r.id, r.pubkey)).map(messageOf)
|
|
207
|
+
const folder = root ? values(root, 'h')[0] : replyFolderOf(all)
|
|
208
|
+
// Not returned, only the replies filed in the thread's Folder speak for its comment: one filed elsewhere neither deletes nor clears.
|
|
209
|
+
const speaking = root ? all : all.filter((r) => values(r, 'h')[0] === folder)
|
|
210
|
+
const state: RootState = (root ? deletedBy(id, root.pubkey) : folder !== undefined && deletedUnseen(id, speaking)) ? 'deleted' : root ? 'present' : 'missing'
|
|
211
|
+
const resolution = resolutionOf(resolutions[id.toLowerCase()] ?? [], { folder, replies: kept.map((r) => r.id) })
|
|
212
|
+
return {
|
|
213
|
+
id: id.toLowerCase(),
|
|
214
|
+
state,
|
|
215
|
+
...(state === 'present' && root ? { root: messageOf(root) } : {}),
|
|
216
|
+
replies: kept,
|
|
217
|
+
...(folder ? { folder } : {}),
|
|
218
|
+
...(resolution ? { resolution } : {}),
|
|
219
|
+
resolvable: state === 'present',
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
for (const root of roots) threads.push(thread(root.id, root))
|
|
223
|
+
for (const id of missingRoots) threads.push(thread(id, undefined))
|
|
224
|
+
// A thread whose top-level message is gone and has no reply left has nothing to draw.
|
|
225
|
+
const drawn = threads.filter((t) => t.root || t.replies.length > 0)
|
|
226
|
+
const firstAt = (t: Thread) => t.root?.at ?? t.replies[0].at
|
|
227
|
+
return drawn.sort((a, b) => firstAt(a) - firstAt(b) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
|
|
228
|
+
}
|