@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/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.33.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
|
|
|
@@ -242,6 +255,18 @@ export { messageWords, openingWords, referenceWord, type OpeningWordsOptions } f
|
|
|
242
255
|
|
|
243
256
|
export { archivedLine } from './archivedLine.js'
|
|
244
257
|
|
|
258
|
+
export {
|
|
259
|
+
refusalCause,
|
|
260
|
+
refusalReason,
|
|
261
|
+
REFUSAL_REASONS,
|
|
262
|
+
tooLarge,
|
|
263
|
+
uploadLimit,
|
|
264
|
+
uploadRefusal,
|
|
265
|
+
UPLOAD_LIMITS,
|
|
266
|
+
type RefusalCause,
|
|
267
|
+
type UploadCause,
|
|
268
|
+
} from './refusals.js'
|
|
269
|
+
|
|
245
270
|
export {
|
|
246
271
|
createDraftStore,
|
|
247
272
|
draftKeys,
|
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
|
+
}
|
package/src/refusals.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* refusals, the one set of words every app says when a send, an edit, a
|
|
3
|
+
* reaction or a file is refused (ONE-4).
|
|
4
|
+
*
|
|
5
|
+
* A refusal comes from the relay (`invalid: …`, `restricted: …`, an HTTP body),
|
|
6
|
+
* from Estiva ID, from the builders' own checks (`content is N bytes, max M`),
|
|
7
|
+
* from the network, or from the app itself. Whatever it was, a person reads
|
|
8
|
+
* one plain reason for its cause, the same in Peek, Ship and Leaf, and never
|
|
9
|
+
* the relay's words: those go to the console. The comment box and the cards
|
|
10
|
+
* put the reason in their sentence ("Not sent: <reason>. Your text is back in
|
|
11
|
+
* the box."), so a reason is lower case and ends with no full stop.
|
|
12
|
+
*
|
|
13
|
+
* An app with a cause of its own hands `{ cause }` rather than a sentence, so
|
|
14
|
+
* its words are these too. A file's own words (`uploadRefusal`) are a whole
|
|
15
|
+
* line on its card, and `tooLarge` stops a file over the relay's limit before
|
|
16
|
+
* it uploads (Miky, 4 Nov: the server's limits, checked in every app first).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** Why something was refused, as far as a person needs to know. */
|
|
20
|
+
export type RefusalCause =
|
|
21
|
+
| 'too-long'
|
|
22
|
+
| 'gone'
|
|
23
|
+
| 'no-access'
|
|
24
|
+
| 'archived'
|
|
25
|
+
| 'busy'
|
|
26
|
+
| 'clock'
|
|
27
|
+
| 'signed-out'
|
|
28
|
+
| 'unknown'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Each cause's reason, put in a sentence by the box or the card. `gone` is
|
|
32
|
+
* what the relay says for an edit or a reaction on a message that was deleted
|
|
33
|
+
* or that you can no longer write beside: it does not say which.
|
|
34
|
+
*/
|
|
35
|
+
export const REFUSAL_REASONS: Readonly<Record<RefusalCause, string>> = {
|
|
36
|
+
'too-long': 'it is too long',
|
|
37
|
+
gone: 'this message was deleted, or you can no longer write here',
|
|
38
|
+
'no-access': 'you can no longer write here',
|
|
39
|
+
archived: 'this team is archived',
|
|
40
|
+
busy: 'too many at once, try again in a moment',
|
|
41
|
+
clock: 'your computer’s clock is wrong; set it and try again',
|
|
42
|
+
'signed-out': 'you are signed out; sign in again',
|
|
43
|
+
unknown: 'something went wrong, try again',
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The text a refusal carries: a string, an `Error`'s message, or `{ error }` / `{ message }` / `{ reason }`, one JSON body deep. */
|
|
47
|
+
function textOf(reason: unknown): string {
|
|
48
|
+
if (reason == null) return ''
|
|
49
|
+
if (typeof reason === 'string') {
|
|
50
|
+
const json = reason.trim()
|
|
51
|
+
if (json.startsWith('{')) {
|
|
52
|
+
try {
|
|
53
|
+
return textOf(JSON.parse(json)) || reason
|
|
54
|
+
} catch {
|
|
55
|
+
return reason
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return reason
|
|
59
|
+
}
|
|
60
|
+
if (reason instanceof Error) return textOf(reason.message)
|
|
61
|
+
if (typeof reason === 'object') {
|
|
62
|
+
const r = reason as Record<string, unknown>
|
|
63
|
+
for (const key of ['error', 'message', 'reason']) {
|
|
64
|
+
const value = r[key]
|
|
65
|
+
if (typeof value === 'string') return textOf(value)
|
|
66
|
+
if (value && typeof value === 'object') return textOf(value)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return String(reason)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const CAUSES = new Set<string>(Object.keys(REFUSAL_REASONS))
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
In order: the first that matches is the cause. Scope and identity before
|
|
76
|
+
`restricted:`, which also prefixes "insufficient scope". Each reason above
|
|
77
|
+
maps back to its own cause, so a reason passed on twice stays the same.
|
|
78
|
+
|
|
79
|
+
Words, because the relay sends no reason code yet (SPEC: "until then a
|
|
80
|
+
refusal is its sentence alone"). When the TEA issue "When the server says
|
|
81
|
+
no, every app knows why" adds them, `refusalCause` reads the code first and
|
|
82
|
+
these rules stay only for a refusal that carries none.
|
|
83
|
+
why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-refusal-starts-with-a-reason-code-every-app-reads.md
|
|
84
|
+
*/
|
|
85
|
+
const RULES: readonly [RegExp, RefusalCause][] = [
|
|
86
|
+
[/content (is \d+ bytes, max|exceeds maximum size)|too long/i, 'too-long'],
|
|
87
|
+
[/rate-limited|quota|too many|too busy|relay is busy|admission|\b429\b|concurrency/i, 'busy'],
|
|
88
|
+
[/timestamp too far|clock/i, 'clock'],
|
|
89
|
+
[/auth-required|insufficient scope|does not match authenticated|signed out|sign in|\b401\b|token_rejected|session_expired/i, 'signed-out'],
|
|
90
|
+
[/archived/i, 'archived'],
|
|
91
|
+
[/target (event )?not found|not (editable|deletable) by you|was deleted|no longer on this page/i, 'gone'],
|
|
92
|
+
[/^(restricted|blocked):|not a (channel |relay )?member|only members|no longer (post|write)|banned|timed out until|writes are fenced|\b403\b/i, 'no-access'],
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
/** What a refusal comes to; `{ cause }` is taken as it is. */
|
|
96
|
+
export function refusalCause(reason: unknown): RefusalCause {
|
|
97
|
+
if (reason && typeof reason === 'object' && 'cause' in reason) {
|
|
98
|
+
const cause = (reason as { cause: unknown }).cause
|
|
99
|
+
if (typeof cause === 'string' && CAUSES.has(cause)) return cause as RefusalCause
|
|
100
|
+
}
|
|
101
|
+
const text = textOf(reason)
|
|
102
|
+
for (const [rule, cause] of RULES) if (rule.test(text)) return cause
|
|
103
|
+
return 'unknown'
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The plain reason for any refusal, to say after "Not sent:", "Not saved:" or "Not reacted:". */
|
|
107
|
+
export function refusalReason(reason: unknown): string {
|
|
108
|
+
return REFUSAL_REASONS[refusalCause(reason)]
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Why a file did not upload, as far as a person needs to know. */
|
|
112
|
+
export type UploadCause = 'too-large' | 'type' | 'unreadable' | 'busy' | 'signed-out' | 'no-access' | 'unknown'
|
|
113
|
+
|
|
114
|
+
const MB = 1024 * 1024
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The relay's size limits (buzz `BUZZ_MAX_*_BYTES`, read back on production on
|
|
118
|
+
* 4 Nov): a GIF 10 MB, another picture 50 MB, anything else 100 MB, since
|
|
119
|
+
* Cloudflare refuses a larger body before the relay sees it (a video's 500 MB
|
|
120
|
+
* is never reached). Cloudflare's 100 MB is taken as decimal, the lower
|
|
121
|
+
* reading, so nothing that passes here is turned away there.
|
|
122
|
+
*/
|
|
123
|
+
export const UPLOAD_LIMITS = { gif: 10 * MB, image: 50 * MB, other: 100_000_000 } as const
|
|
124
|
+
|
|
125
|
+
/** The most a file of this type may be, in bytes. */
|
|
126
|
+
export function uploadLimit(type: string | undefined): number {
|
|
127
|
+
const t = (type ?? '').toLowerCase()
|
|
128
|
+
if (t === 'image/gif') return UPLOAD_LIMITS.gif
|
|
129
|
+
if (t.startsWith('image/')) return UPLOAD_LIMITS.image
|
|
130
|
+
return UPLOAD_LIMITS.other
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Whole MB either way: 10,485,760 bytes and 100,000,000 bytes both say what the limit is called.
|
|
134
|
+
const tooLargeLine = (bytes: number) => `Too large: the most is ${bytes % MB === 0 ? bytes / MB : Math.round(bytes / 1e6)} MB`
|
|
135
|
+
|
|
136
|
+
/** The line for a file over its limit, said before it uploads; undefined when it fits. */
|
|
137
|
+
export function tooLarge(file: { size: number; type?: string }): string | undefined {
|
|
138
|
+
const limit = uploadLimit(file.type)
|
|
139
|
+
return file.size > limit ? tooLargeLine(limit) : undefined
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const UPLOAD_LINES: Readonly<Record<Exclude<UploadCause, 'too-large'>, string>> = {
|
|
143
|
+
type: 'This type of file can’t be added',
|
|
144
|
+
unreadable: 'This file could not be read',
|
|
145
|
+
busy: 'Too many at once, try again in a moment',
|
|
146
|
+
'signed-out': 'Sign in again',
|
|
147
|
+
'no-access': 'You can no longer add files here',
|
|
148
|
+
unknown: 'Could not upload, try again',
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The line on a file's card when its upload was refused: from protocol's
|
|
153
|
+
* `upload failed: <status> <body>`, any other error, or `{ cause }`. A 413
|
|
154
|
+
* names the limit the relay gave, or the type's own when Cloudflare answered.
|
|
155
|
+
*/
|
|
156
|
+
export function uploadRefusal(reason: unknown, file?: { type?: string }): string {
|
|
157
|
+
if (reason && typeof reason === 'object' && 'cause' in reason) {
|
|
158
|
+
const cause = (reason as { cause: unknown }).cause
|
|
159
|
+
if (cause === 'too-large') return tooLargeLine(uploadLimit(file?.type))
|
|
160
|
+
if (typeof cause === 'string' && Object.hasOwn(UPLOAD_LINES, cause)) return UPLOAD_LINES[cause as keyof typeof UPLOAD_LINES]
|
|
161
|
+
}
|
|
162
|
+
const raw = reason instanceof Error ? reason.message : typeof reason === 'string' ? reason : textOf(reason)
|
|
163
|
+
const status = Number(/upload failed:\s*(\d{3})/i.exec(raw)?.[1] ?? 0)
|
|
164
|
+
const said = textOf(raw.replace(/^.*?upload failed:\s*\d{3}\s*/is, ''))
|
|
165
|
+
if (status === 413 || /file too large/i.test(said)) {
|
|
166
|
+
const max = Number(/max (\d+)\)?/i.exec(said)?.[1] ?? 0)
|
|
167
|
+
if (/dimensions/i.test(said)) return UPLOAD_LINES.unreadable
|
|
168
|
+
return tooLargeLine(max || uploadLimit(file?.type))
|
|
169
|
+
}
|
|
170
|
+
if (status === 415 || /content type|unsupported container/i.test(said)) return UPLOAD_LINES.type
|
|
171
|
+
if (status === 422 || /metadata|invalid image|duration|resolution/i.test(said)) return UPLOAD_LINES.unreadable
|
|
172
|
+
if (status === 429 || /rate limit|concurrency/i.test(said)) return UPLOAD_LINES.busy
|
|
173
|
+
if (status === 401 || /authentication failed|signed out/i.test(said)) return UPLOAD_LINES['signed-out']
|
|
174
|
+
if (status === 403) return UPLOAD_LINES['no-access']
|
|
175
|
+
return UPLOAD_LINES.unknown
|
|
176
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolving a thread — SPEC §6.4, "Resolving a thread" (ONE-2), and C51.
|
|
3
|
+
*
|
|
4
|
+
* A `kind:9101` with `t=resolution` resolves or reopens the thread of the
|
|
5
|
+
* top-level message its first unmarked `e` names. Anyone who may write in that
|
|
6
|
+
* message's Folder may write one, and **the newest counts, in §6.3's order**
|
|
7
|
+
* (`byOrder`): `ts` when trusted, then `created_at`, then the higher id.
|
|
8
|
+
* A reader ignores one whose `h` is not its target's Folder, one about a reply
|
|
9
|
+
* (never walked to its root), and drops a `support` outside the thread.
|
|
10
|
+
*
|
|
11
|
+
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/anyone-who-may-comment-resolves-or-reopens-a-thread-and-the-newest-counts.md
|
|
12
|
+
*/
|
|
13
|
+
import type { SignedEvent } from '@estiva-app/protocol'
|
|
14
|
+
import { isEventId, KIND_ASSERTION } from './kinds.js'
|
|
15
|
+
import { orderingMs } from './order.js'
|
|
16
|
+
|
|
17
|
+
/** A `kind:9101` resolution assertion on a message (PEEK-128, SPEC §6.4). */
|
|
18
|
+
export interface Resolution {
|
|
19
|
+
id: string
|
|
20
|
+
action: 'resolved' | 'reopened'
|
|
21
|
+
by: string
|
|
22
|
+
/** `created_at`, seconds. */
|
|
23
|
+
at: number
|
|
24
|
+
/** Where it sorts, epoch milliseconds: the trusted `ts`, else `at` × 1000 (§6.3). */
|
|
25
|
+
orderMs: number
|
|
26
|
+
/** Its `h`: the Folder it was filed in, which must be its target's. */
|
|
27
|
+
folder?: string
|
|
28
|
+
/** The assertion's free text, when it carried one. */
|
|
29
|
+
message?: string
|
|
30
|
+
/** The reply that carried the resolution, when the writer named one. */
|
|
31
|
+
supportingEventId?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const tagValue = (event: Pick<SignedEvent, 'tags'>, name: string) => event.tags.find((t) => t[0] === name)?.[1]
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The message a `kind:9101` is about: its **first unmarked `e`**, lowercased.
|
|
38
|
+
* Not the first one a reader happens to hold — a resolve listing a stranger
|
|
39
|
+
* first and a thread second is about the stranger.
|
|
40
|
+
*/
|
|
41
|
+
export function resolutionTargetOf(event: Pick<SignedEvent, 'tags'>): string | undefined {
|
|
42
|
+
const tag = event.tags.find((t) => t[0] === 'e' && !t[3])
|
|
43
|
+
return tag && isEventId(tag[1]) ? tag[1].toLowerCase() : undefined
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The event read as a {@link Resolution} with its target, or undefined when it is not one. */
|
|
47
|
+
export function resolutionOfEvent(event: SignedEvent): { target: string; resolution: Resolution } | undefined {
|
|
48
|
+
if (event.kind !== KIND_ASSERTION || tagValue(event, 't') !== 'resolution') return undefined
|
|
49
|
+
const action = tagValue(event, 'action')
|
|
50
|
+
if (action !== 'resolved' && action !== 'reopened') return undefined
|
|
51
|
+
const target = resolutionTargetOf(event)
|
|
52
|
+
if (!target) return undefined
|
|
53
|
+
const support = event.tags.find((t) => t[0] === 'e' && t[3] === 'support')?.[1]
|
|
54
|
+
const folder = tagValue(event, 'h')
|
|
55
|
+
return {
|
|
56
|
+
target,
|
|
57
|
+
resolution: {
|
|
58
|
+
id: event.id,
|
|
59
|
+
action,
|
|
60
|
+
by: event.pubkey,
|
|
61
|
+
at: event.created_at,
|
|
62
|
+
orderMs: orderingMs(event),
|
|
63
|
+
...(folder ? { folder } : {}),
|
|
64
|
+
...(event.content ? { message: event.content } : {}),
|
|
65
|
+
...(isEventId(support) ? { supportingEventId: support.toLowerCase() } : {}),
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Oldest first by §6.3: `ts` when trusted, then `created_at`, then the lower id. */
|
|
71
|
+
export const byResolutionOrder = (a: Resolution, b: Resolution): number =>
|
|
72
|
+
a.orderMs - b.orderMs || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A thread's state: the newest of `resolutions` that counts, or undefined when
|
|
76
|
+
* none does (never resolved, or only ones a reader ignores).
|
|
77
|
+
*
|
|
78
|
+
* `resolutions` are those whose target is the thread's **top-level** message;
|
|
79
|
+
* one about a reply is never passed here. `folder` is that message's Folder
|
|
80
|
+
* (for a top-level message the read did not return, its replies'), and
|
|
81
|
+
* `replies` the thread's reply ids: a `support` naming anything else is
|
|
82
|
+
* dropped and the resolve stands.
|
|
83
|
+
*/
|
|
84
|
+
export function resolutionOf(
|
|
85
|
+
resolutions: readonly Resolution[],
|
|
86
|
+
thread: { folder: string | undefined; replies: readonly string[] },
|
|
87
|
+
): Resolution | undefined {
|
|
88
|
+
if (!thread.folder) return undefined
|
|
89
|
+
const newest = resolutions
|
|
90
|
+
.filter((r) => r.folder === thread.folder)
|
|
91
|
+
.sort(byResolutionOrder)
|
|
92
|
+
.at(-1)
|
|
93
|
+
if (!newest?.supportingEventId) return newest
|
|
94
|
+
const replies = new Set(thread.replies.map((id) => id.toLowerCase()))
|
|
95
|
+
if (replies.has(newest.supportingEventId)) return newest
|
|
96
|
+
const { supportingEventId: _dropped, ...kept } = newest
|
|
97
|
+
return kept
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Every resolution in `events`, by target id, oldest first (§6.3). */
|
|
101
|
+
export function resolutionsByTarget(events: readonly SignedEvent[]): Record<string, Resolution[]> {
|
|
102
|
+
const out: Record<string, Resolution[]> = {}
|
|
103
|
+
const seen = new Set<string>()
|
|
104
|
+
for (const event of events) {
|
|
105
|
+
if (seen.has(event.id)) continue
|
|
106
|
+
seen.add(event.id)
|
|
107
|
+
const read = resolutionOfEvent(event)
|
|
108
|
+
if (read) (out[read.target] ??= []).push(read.resolution)
|
|
109
|
+
}
|
|
110
|
+
for (const list of Object.values(out)) list.sort(byResolutionOrder)
|
|
111
|
+
return out
|
|
112
|
+
}
|