@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.
- package/CHANGELOG.md +37 -0
- package/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/builders.d.ts +73 -0
- package/dist/builders.d.ts.map +1 -0
- package/dist/builders.js +110 -0
- package/dist/builders.js.map +1 -0
- package/dist/decorations.d.ts +88 -0
- package/dist/decorations.d.ts.map +1 -0
- package/dist/decorations.js +133 -0
- package/dist/decorations.js.map +1 -0
- package/dist/drafts.d.ts +74 -0
- package/dist/drafts.d.ts.map +1 -0
- package/dist/drafts.js +111 -0
- package/dist/drafts.js.map +1 -0
- package/dist/edits.d.ts +76 -0
- package/dist/edits.d.ts.map +1 -0
- package/dist/edits.js +123 -0
- package/dist/edits.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/kinds.d.ts +17 -0
- package/dist/kinds.d.ts.map +1 -0
- package/dist/kinds.js +22 -0
- package/dist/kinds.js.map +1 -0
- package/dist/order.d.ts +24 -0
- package/dist/order.d.ts.map +1 -0
- package/dist/order.js +21 -0
- package/dist/order.js.map +1 -0
- package/dist/reactions.d.ts +93 -0
- package/dist/reactions.d.ts.map +1 -0
- package/dist/reactions.js +99 -0
- package/dist/reactions.js.map +1 -0
- package/dist/strength.d.ts +104 -0
- package/dist/strength.d.ts.map +1 -0
- package/dist/strength.js +166 -0
- package/dist/strength.js.map +1 -0
- package/dist/threads.d.ts +54 -0
- package/dist/threads.d.ts.map +1 -0
- package/dist/threads.js +70 -0
- package/dist/threads.js.map +1 -0
- package/package.json +56 -0
- package/src/builders.ts +133 -0
- package/src/decorations.ts +187 -0
- package/src/drafts.ts +135 -0
- package/src/edits.ts +139 -0
- package/src/index.ts +74 -0
- package/src/kinds.ts +24 -0
- package/src/order.ts +38 -0
- package/src/reactions.ts +151 -0
- package/src/strength.ts +170 -0
- package/src/threads.ts +104 -0
package/dist/drafts.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 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 const DRAFT_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
|
|
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 const DRAFT_MAX_CHARS = 8000;
|
|
39
|
+
/** Namespaced and versioned, so a shape change can be ignored rather than mis-read. */
|
|
40
|
+
const PREFIX = 'estiva.draft.v1:';
|
|
41
|
+
/**
|
|
42
|
+
* Where a draft belongs. **A thread is keyed by its own root event, not by the
|
|
43
|
+
* surface showing it**: a reply half-written on an issue page and the same
|
|
44
|
+
* thread seen from a project's Activity are one destination, so one draft.
|
|
45
|
+
*/
|
|
46
|
+
export const draftKeys = {
|
|
47
|
+
/** Starting a conversation about an object — its address. */
|
|
48
|
+
object: (addr) => `object:${addr}`,
|
|
49
|
+
/** Replying in a thread — the thread's root event id. */
|
|
50
|
+
thread: (rootEventId) => `thread:${rootEventId}`,
|
|
51
|
+
/** Writing into a container itself — a Peek topic, a DM — by the id a person navigates between. */
|
|
52
|
+
container: (id) => `container:${id}`,
|
|
53
|
+
};
|
|
54
|
+
/** A store that keeps nothing, for when there is no storage to use. */
|
|
55
|
+
export const NO_DRAFTS = { read: () => undefined, write: () => { }, clear: () => { } };
|
|
56
|
+
/**
|
|
57
|
+
* A draft store over some storage. **Every operation is guarded**: a private
|
|
58
|
+
* window, cleared site data, blocked storage or a full quota all surface as
|
|
59
|
+
* "no draft" rather than a broken composer.
|
|
60
|
+
*/
|
|
61
|
+
export function createDraftStore(storage, now = Date.now) {
|
|
62
|
+
if (!storage)
|
|
63
|
+
return NO_DRAFTS;
|
|
64
|
+
const remove = (key) => {
|
|
65
|
+
try {
|
|
66
|
+
storage.removeItem(PREFIX + key);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// The draft is already unreachable as far as anybody reading it is concerned.
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
read(key) {
|
|
74
|
+
try {
|
|
75
|
+
const raw = storage.getItem(PREFIX + key);
|
|
76
|
+
if (!raw)
|
|
77
|
+
return undefined;
|
|
78
|
+
const draft = JSON.parse(raw);
|
|
79
|
+
if (typeof draft?.text !== 'string' || typeof draft?.at !== 'number') {
|
|
80
|
+
// Another version's shape, or corrupted: better dropped than shown.
|
|
81
|
+
remove(key);
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
if (now() - draft.at > DRAFT_MAX_AGE_MS) {
|
|
85
|
+
remove(key);
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
return draft.text;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
write(key, text) {
|
|
95
|
+
// Whitespace is not a draft: emptying the field forgets it.
|
|
96
|
+
if (!text.trim())
|
|
97
|
+
return remove(key);
|
|
98
|
+
if (text.length > DRAFT_MAX_CHARS)
|
|
99
|
+
return remove(key);
|
|
100
|
+
try {
|
|
101
|
+
const draft = { text, at: now() };
|
|
102
|
+
storage.setItem(PREFIX + key, JSON.stringify(draft));
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// Quota, or storage refused. The text is still in the field.
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
clear: remove,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=drafts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drafts.js","sourceRoot":"","sources":["../src/drafts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,mGAAmG;AACnG,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAA;AAExD;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,CAAA;AAEnC,uFAAuF;AACvF,MAAM,MAAM,GAAG,kBAAkB,CAAA;AAEjC;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,6DAA6D;IAC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AE,GAAG,KAAK,CAAC,EAAE,GAAG,gBAAgB,EAAE,CAAC;oBACxC,MAAM,CAAC,GAAG,CAAC,CAAA;oBACX,OAAO,SAAS,CAAA;gBAClB,CAAC;gBACD,OAAO,KAAK,CAAC,IAAI,CAAA;YACnB,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,SAAS,CAAA;YAClB,CAAC;QACH,CAAC;QAED,KAAK,CAAC,GAAG,EAAE,IAAI;YACb,4DAA4D;YAC5D,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;gBAAE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAA;YACpC,IAAI,IAAI,CAAC,MAAM,GAAG,eAAe;gBAAE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAA;YACrD,IAAI,CAAC;gBACH,MAAM,KAAK,GAAgB,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAA;gBAC9C,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAA;YACtD,CAAC;YAAC,MAAM,CAAC;gBACP,6DAA6D;YAC/D,CAAC;QACH,CAAC;QAED,KAAK,EAAE,MAAM;KACd,CAAA;AACH,CAAC"}
|
package/dist/edits.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Edits — SPEC §6.8.
|
|
3
|
+
*
|
|
4
|
+
* An edit is a new `kind:40003`, never a rewrite, and what an app shows as
|
|
5
|
+
* "edited" is a fold over the message and its edits. The latest edit wins, by
|
|
6
|
+
* §6.3's ordering ({@link byOrder}). An edit whose target the reader cannot see
|
|
7
|
+
* is held, not dropped: the target may arrive later, and the caller looks
|
|
8
|
+
* edits up by id.
|
|
9
|
+
*
|
|
10
|
+
* An edit changes the body and the attachments, and nothing else. Attachments
|
|
11
|
+
* fold separately: the set is the `imeta` of the latest edit carrying at least
|
|
12
|
+
* one, and a set replaces rather than appends. The "edited" mark is about the
|
|
13
|
+
* **body**: an edit byte-identical to the body it replaces, judged against the
|
|
14
|
+
* body as it then stood, does not mark the message edited.
|
|
15
|
+
*
|
|
16
|
+
* Nothing here checks who wrote an edit against who wrote the message. The
|
|
17
|
+
* relay adjudicates writes; a `40003` it stored is one it accepted — **for
|
|
18
|
+
* the `e` it checked**, which is why an edit lands only on {@link editTargetOf}.
|
|
19
|
+
*/
|
|
20
|
+
import { type Imeta, type SignedEvent } from '@estiva-app/protocol';
|
|
21
|
+
/**
|
|
22
|
+
* The message a `kind:40003` edits: **the first `e` whose value is 64 hex,
|
|
23
|
+
* marker ignored**, lowercased (SPEC §6.8, PEE-38). That is exactly the event
|
|
24
|
+
* whose ownership the relay checked (`validate_edit_ownership`), and a reader
|
|
25
|
+
* MUST NOT apply an edit to any other `e` — an edit carrying
|
|
26
|
+
* `['e', <own>, '', 'mention'], ['e', <victim>]` was accepted on the writer's
|
|
27
|
+
* own message before CON-21, and must not be drawn on the victim's.
|
|
28
|
+
*
|
|
29
|
+
* Undefined when there is none, which the relay refuses; a reader holding one
|
|
30
|
+
* anyway applies it nowhere.
|
|
31
|
+
*/
|
|
32
|
+
export declare function editTargetOf(event: Pick<SignedEvent, 'tags'>): string | undefined;
|
|
33
|
+
/** One message's edits, folded. */
|
|
34
|
+
export interface EditFold {
|
|
35
|
+
/** The body to show: the latest edit's content. */
|
|
36
|
+
body: string;
|
|
37
|
+
/**
|
|
38
|
+
* Whether any edit changed the body — what a reader MUST mark. With the
|
|
39
|
+
* target's body unknown the first edit counts as a change: the mark errs
|
|
40
|
+
* towards showing, and settles on the next read that holds the target.
|
|
41
|
+
*/
|
|
42
|
+
edited: boolean;
|
|
43
|
+
/** When the body last changed, epoch milliseconds (`ts` when trusted). Absent unless `edited`. */
|
|
44
|
+
editedAt?: number;
|
|
45
|
+
/** Who wrote the edit that last changed the body. Absent unless `edited`. */
|
|
46
|
+
editedBy?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The `imeta` set of the latest edit carrying at least one. Absent when no
|
|
49
|
+
* edit carries one: the message's own attachments stand, because an edit
|
|
50
|
+
* with no `imeta` means "unchanged", never "none".
|
|
51
|
+
*/
|
|
52
|
+
attachments?: Imeta[];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Every `kind:40003` in `events`, grouped by {@link editTargetOf}, oldest first
|
|
56
|
+
* by §6.3. Other kinds are ignored and a repeated id is read once.
|
|
57
|
+
*/
|
|
58
|
+
export declare function editsByTarget(events: readonly SignedEvent[]): Map<string, SignedEvent[]>;
|
|
59
|
+
/**
|
|
60
|
+
* Fold every edit in `events` onto its target (C12, C15).
|
|
61
|
+
*
|
|
62
|
+
* The body each edit replaces comes from `targets` when given, else from a
|
|
63
|
+
* `kind:9` or `kind:1111` with the target's id in `events` itself — so a
|
|
64
|
+
* caller holding the whole stream passes it once, and one holding only the
|
|
65
|
+
* edits passes the bodies beside them. A target nobody edited is absent.
|
|
66
|
+
*/
|
|
67
|
+
export declare function foldEdits(events: readonly SignedEvent[], targets?: readonly {
|
|
68
|
+
id: string;
|
|
69
|
+
body: string;
|
|
70
|
+
}[]): Record<string, EditFold>;
|
|
71
|
+
/**
|
|
72
|
+
* Every message's current attachment set: its own `imeta`, replaced by the
|
|
73
|
+
* latest edit carrying any (§6.8, C15). Messages with none are absent.
|
|
74
|
+
*/
|
|
75
|
+
export declare function foldAttachments(events: readonly SignedEvent[]): Record<string, Imeta[]>;
|
|
76
|
+
//# sourceMappingURL=edits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edits.d.ts","sourceRoot":"","sources":["../src/edits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAW,KAAK,KAAK,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAI5E;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,GAAG,MAAM,GAAG,SAAS,CAGjF;AAED,mCAAmC;AACnC,MAAM,WAAW,QAAQ;IACvB,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAA;IACZ;;;;OAIG;IACH,MAAM,EAAE,OAAO,CAAA;IACf,kGAAkG;IAClG,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,6EAA6E;IAC7E,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;;OAIG;IACH,WAAW,CAAC,EAAE,KAAK,EAAE,CAAA;CACtB;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,WAAW,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,CAAC,CAcxF;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CACvB,MAAM,EAAE,SAAS,WAAW,EAAE,EAC9B,OAAO,GAAE,SAAS;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EAAO,GACpD,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAwB1B;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC,CAgBvF"}
|
package/dist/edits.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Edits — SPEC §6.8.
|
|
3
|
+
*
|
|
4
|
+
* An edit is a new `kind:40003`, never a rewrite, and what an app shows as
|
|
5
|
+
* "edited" is a fold over the message and its edits. The latest edit wins, by
|
|
6
|
+
* §6.3's ordering ({@link byOrder}). An edit whose target the reader cannot see
|
|
7
|
+
* is held, not dropped: the target may arrive later, and the caller looks
|
|
8
|
+
* edits up by id.
|
|
9
|
+
*
|
|
10
|
+
* An edit changes the body and the attachments, and nothing else. Attachments
|
|
11
|
+
* fold separately: the set is the `imeta` of the latest edit carrying at least
|
|
12
|
+
* one, and a set replaces rather than appends. The "edited" mark is about the
|
|
13
|
+
* **body**: an edit byte-identical to the body it replaces, judged against the
|
|
14
|
+
* body as it then stood, does not mark the message edited.
|
|
15
|
+
*
|
|
16
|
+
* Nothing here checks who wrote an edit against who wrote the message. The
|
|
17
|
+
* relay adjudicates writes; a `40003` it stored is one it accepted — **for
|
|
18
|
+
* the `e` it checked**, which is why an edit lands only on {@link editTargetOf}.
|
|
19
|
+
*/
|
|
20
|
+
import { imetaOf } from '@estiva-app/protocol';
|
|
21
|
+
import { isConversationKind, isEventId, KIND_EDIT } from './kinds.js';
|
|
22
|
+
import { byOrder, orderingMs } from './order.js';
|
|
23
|
+
/**
|
|
24
|
+
* The message a `kind:40003` edits: **the first `e` whose value is 64 hex,
|
|
25
|
+
* marker ignored**, lowercased (SPEC §6.8, PEE-38). That is exactly the event
|
|
26
|
+
* whose ownership the relay checked (`validate_edit_ownership`), and a reader
|
|
27
|
+
* MUST NOT apply an edit to any other `e` — an edit carrying
|
|
28
|
+
* `['e', <own>, '', 'mention'], ['e', <victim>]` was accepted on the writer's
|
|
29
|
+
* own message before CON-21, and must not be drawn on the victim's.
|
|
30
|
+
*
|
|
31
|
+
* Undefined when there is none, which the relay refuses; a reader holding one
|
|
32
|
+
* anyway applies it nowhere.
|
|
33
|
+
*/
|
|
34
|
+
export function editTargetOf(event) {
|
|
35
|
+
const tag = event.tags.find((t) => t[0] === 'e' && isEventId(t[1]));
|
|
36
|
+
return tag?.[1].toLowerCase();
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Every `kind:40003` in `events`, grouped by {@link editTargetOf}, oldest first
|
|
40
|
+
* by §6.3. Other kinds are ignored and a repeated id is read once.
|
|
41
|
+
*/
|
|
42
|
+
export function editsByTarget(events) {
|
|
43
|
+
const out = new Map();
|
|
44
|
+
const seen = new Set();
|
|
45
|
+
for (const event of events) {
|
|
46
|
+
if (event.kind !== KIND_EDIT || seen.has(event.id))
|
|
47
|
+
continue;
|
|
48
|
+
seen.add(event.id);
|
|
49
|
+
const target = editTargetOf(event);
|
|
50
|
+
if (!target)
|
|
51
|
+
continue;
|
|
52
|
+
const list = out.get(target);
|
|
53
|
+
if (list)
|
|
54
|
+
list.push(event);
|
|
55
|
+
else
|
|
56
|
+
out.set(target, [event]);
|
|
57
|
+
}
|
|
58
|
+
for (const list of out.values())
|
|
59
|
+
list.sort(byOrder);
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Fold every edit in `events` onto its target (C12, C15).
|
|
64
|
+
*
|
|
65
|
+
* The body each edit replaces comes from `targets` when given, else from a
|
|
66
|
+
* `kind:9` or `kind:1111` with the target's id in `events` itself — so a
|
|
67
|
+
* caller holding the whole stream passes it once, and one holding only the
|
|
68
|
+
* edits passes the bodies beside them. A target nobody edited is absent.
|
|
69
|
+
*/
|
|
70
|
+
export function foldEdits(events, targets = []) {
|
|
71
|
+
const bodies = new Map();
|
|
72
|
+
for (const event of events)
|
|
73
|
+
if (isConversationKind(event.kind))
|
|
74
|
+
bodies.set(event.id.toLowerCase(), event.content);
|
|
75
|
+
for (const t of targets)
|
|
76
|
+
bodies.set(t.id.toLowerCase(), t.body);
|
|
77
|
+
const out = {};
|
|
78
|
+
for (const [target, list] of editsByTarget(events)) {
|
|
79
|
+
let body = bodies.get(target);
|
|
80
|
+
let changed;
|
|
81
|
+
let attachments;
|
|
82
|
+
for (const edit of list) {
|
|
83
|
+
if (body === undefined || edit.content !== body)
|
|
84
|
+
changed = edit;
|
|
85
|
+
body = edit.content;
|
|
86
|
+
const files = imetaOf(edit);
|
|
87
|
+
if (files.length > 0)
|
|
88
|
+
attachments = files;
|
|
89
|
+
}
|
|
90
|
+
out[target] = {
|
|
91
|
+
body: list[list.length - 1].content,
|
|
92
|
+
edited: changed !== undefined,
|
|
93
|
+
...(changed ? { editedAt: orderingMs(changed), editedBy: changed.pubkey } : {}),
|
|
94
|
+
...(attachments ? { attachments } : {}),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Every message's current attachment set: its own `imeta`, replaced by the
|
|
101
|
+
* latest edit carrying any (§6.8, C15). Messages with none are absent.
|
|
102
|
+
*/
|
|
103
|
+
export function foldAttachments(events) {
|
|
104
|
+
const out = {};
|
|
105
|
+
for (const event of events) {
|
|
106
|
+
if (!isConversationKind(event.kind))
|
|
107
|
+
continue;
|
|
108
|
+
const files = imetaOf(event);
|
|
109
|
+
if (files.length > 0)
|
|
110
|
+
out[event.id.toLowerCase()] = files;
|
|
111
|
+
}
|
|
112
|
+
for (const [target, list] of editsByTarget(events)) {
|
|
113
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
114
|
+
const files = imetaOf(list[i]);
|
|
115
|
+
if (files.length === 0)
|
|
116
|
+
continue;
|
|
117
|
+
out[target] = files;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=edits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edits.js","sourceRoot":"","sources":["../src/edits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAE,OAAO,EAAgC,MAAM,sBAAsB,CAAA;AAC5E,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACrE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEhD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAAC,KAAgC;IAC3D,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACnE,OAAO,GAAG,EAA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K,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,IAAI,CAAC;YAAE,SAAQ;QAC7C,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,CAAA;QAC5B,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;YAAE,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,WAAW,EAAE,CAAC,GAAG,KAAK,CAAA;IAC3D,CAAC;IACD,KAAK,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;QACnD,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC1C,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;YAC9B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;gBAAE,SAAQ;YAChC,GAAG,CAAC,MAAM,CAAC,GAAG,KAAK,CAAA;YACnB,MAAK;QACP,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@estiva-app/conversation` — the conversation rules of SPEC §6.2–§6.8, for
|
|
3
|
+
* an app that would rather not implement them itself.
|
|
4
|
+
*
|
|
5
|
+
* **A convenience, never a requirement.** SPEC §9's C10–C16 are written so an
|
|
6
|
+
* app can build conversations from the specification alone, and this
|
|
7
|
+
* package's tests are those checks. If this package became the only place
|
|
8
|
+
* that knows how commenting works, a defect in its fold would be a defect in
|
|
9
|
+
* every app at once and invisible from all of them.
|
|
10
|
+
*
|
|
11
|
+
* It sits above `@estiva-app/protocol`, which carries no fold, and below the
|
|
12
|
+
* apps: it imports no app's data layer, opens no socket, and takes every
|
|
13
|
+
* environment touch — the relay read, the clock, storage — as a parameter.
|
|
14
|
+
*/
|
|
15
|
+
export declare const CONVERSATION_VERSION = "0.1.0";
|
|
16
|
+
export { KIND_MESSAGE, KIND_COMMENT, KIND_EDIT, KIND_REACTION, KIND_DELETION, KIND_ASSERTION, isConversationKind, isEventId, } from './kinds.js';
|
|
17
|
+
export { trustedTs, orderingMs, byOrder } from './order.js';
|
|
18
|
+
export { namedInBody, isReply, anchorsOf, isCommentOn, referencesAddress, strengthOn, threadStrength, referenceTagsFor, mentionTagsFor, MAX_MENTIONED_PEOPLE, type Strength, } from './strength.js';
|
|
19
|
+
export { parentOf, groupThreads, type Threaded } from './threads.js';
|
|
20
|
+
export { editTargetOf, editsByTarget, foldEdits, foldAttachments, type EditFold } from './edits.js';
|
|
21
|
+
export { REACTION_HORIZON, REACTION_EMOJI, reactionTargetOf, reactionEmojiOf, reactionHorizon, reactionEventsOf, foldReactions, groupReactions, type ReactionHorizon, type ReactionEvent, type Reaction, } from './reactions.js';
|
|
22
|
+
export { decorationsOf, type Decoration, type Decorations, type Resolution, type QueryFn } from './decorations.js';
|
|
23
|
+
export { buildComment, buildReply, offersEditAndDelete, type CommentRef } from './builders.js';
|
|
24
|
+
export { createDraftStore, draftKeys, NO_DRAFTS, DRAFT_MAX_AGE_MS, DRAFT_MAX_CHARS, type DraftStore, type DraftStorage, } from './drafts.js';
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,oBAAoB,UAAU,CAAA;AAE3C,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,aAAa,EACb,aAAa,EACb,cAAc,EACd,kBAAkB,EAClB,SAAS,GACV,MAAM,YAAY,CAAA;AAEnB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAE3D,OAAO,EACL,WAAW,EACX,OAAO,EACP,SAAS,EACT,WAAW,EACX,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,KAAK,QAAQ,GACd,MAAM,eAAe,CAAA;AAEtB,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEpE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,eAAe,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAA;AAEnG,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,QAAQ,GACd,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EAAE,aAAa,EAAE,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,KAAK,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAElH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,mBAAmB,EAAE,KAAK,UAAU,EAAE,MAAM,eAAe,CAAA;AAE9F,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,gBAAgB,EAChB,eAAe,EACf,KAAK,UAAU,EACf,KAAK,YAAY,GAClB,MAAM,aAAa,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@estiva-app/conversation` — the conversation rules of SPEC §6.2–§6.8, for
|
|
3
|
+
* an app that would rather not implement them itself.
|
|
4
|
+
*
|
|
5
|
+
* **A convenience, never a requirement.** SPEC §9's C10–C16 are written so an
|
|
6
|
+
* app can build conversations from the specification alone, and this
|
|
7
|
+
* package's tests are those checks. If this package became the only place
|
|
8
|
+
* that knows how commenting works, a defect in its fold would be a defect in
|
|
9
|
+
* every app at once and invisible from all of them.
|
|
10
|
+
*
|
|
11
|
+
* It sits above `@estiva-app/protocol`, which carries no fold, and below the
|
|
12
|
+
* apps: it imports no app's data layer, opens no socket, and takes every
|
|
13
|
+
* environment touch — the relay read, the clock, storage — as a parameter.
|
|
14
|
+
*/
|
|
15
|
+
export const CONVERSATION_VERSION = '0.1.0';
|
|
16
|
+
export { KIND_MESSAGE, KIND_COMMENT, KIND_EDIT, KIND_REACTION, KIND_DELETION, KIND_ASSERTION, isConversationKind, isEventId, } from './kinds.js';
|
|
17
|
+
export { trustedTs, orderingMs, byOrder } from './order.js';
|
|
18
|
+
export { namedInBody, isReply, anchorsOf, isCommentOn, referencesAddress, strengthOn, threadStrength, referenceTagsFor, mentionTagsFor, MAX_MENTIONED_PEOPLE, } from './strength.js';
|
|
19
|
+
export { parentOf, groupThreads } from './threads.js';
|
|
20
|
+
export { editTargetOf, editsByTarget, foldEdits, foldAttachments } from './edits.js';
|
|
21
|
+
export { REACTION_HORIZON, REACTION_EMOJI, reactionTargetOf, reactionEmojiOf, reactionHorizon, reactionEventsOf, foldReactions, groupReactions, } from './reactions.js';
|
|
22
|
+
export { decorationsOf } from './decorations.js';
|
|
23
|
+
export { buildComment, buildReply, offersEditAndDelete } from './builders.js';
|
|
24
|
+
export { createDraftStore, draftKeys, NO_DRAFTS, DRAFT_MAX_AGE_MS, DRAFT_MAX_CHARS, } from './drafts.js';
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,OAAO,CAAA;AAE3C,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,aAAa,EACb,aAAa,EACb,cAAc,EACd,kBAAkB,EAClB,SAAS,GACV,MAAM,YAAY,CAAA;AAEnB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAE3D,OAAO,EACL,WAAW,EACX,OAAO,EACP,SAAS,EACT,WAAW,EACX,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,oBAAoB,GAErB,MAAM,eAAe,CAAA;AAEtB,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAiB,MAAM,cAAc,CAAA;AAEpE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,eAAe,EAAiB,MAAM,YAAY,CAAA;AAEnG,OAAO,EACL,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,aAAa,EACb,cAAc,GAIf,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EAAE,aAAa,EAAoE,MAAM,kBAAkB,CAAA;AAElH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,mBAAmB,EAAmB,MAAM,eAAe,CAAA;AAE9F,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,gBAAgB,EAChB,eAAe,GAGhB,MAAM,aAAa,CAAA"}
|
package/dist/kinds.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** A message in a channel — chat (SPEC §6.4, §6.7). */
|
|
2
|
+
export declare const KIND_MESSAGE: 9;
|
|
3
|
+
/** A NIP-22 comment on an object, and a reply to one (§6.4). */
|
|
4
|
+
export declare const KIND_COMMENT: 1111;
|
|
5
|
+
/** An edit of a message or comment (§6.8). */
|
|
6
|
+
export declare const KIND_EDIT: 40003;
|
|
7
|
+
/** A NIP-25 reaction (§6.6). */
|
|
8
|
+
export declare const KIND_REACTION: 7;
|
|
9
|
+
/** A NIP-09 deletion request (§6.5). */
|
|
10
|
+
export declare const KIND_DELETION: 5;
|
|
11
|
+
/** An assertion; with `t=resolution` it resolves or reopens a comment (PEEK-128). */
|
|
12
|
+
export declare const KIND_ASSERTION: 9101;
|
|
13
|
+
/** Whether the event is a message or a comment — what a conversation lists. */
|
|
14
|
+
export declare const isConversationKind: (kind: number) => boolean;
|
|
15
|
+
/** A 64-hex event id. */
|
|
16
|
+
export declare const isEventId: (value: unknown) => value is string;
|
|
17
|
+
//# sourceMappingURL=kinds.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kinds.d.ts","sourceRoot":"","sources":["../src/kinds.ts"],"names":[],"mappings":"AAMA,uDAAuD;AACvD,eAAO,MAAM,YAAY,GAAsB,CAAA;AAC/C,gEAAgE;AAChE,eAAO,MAAM,YAAY,MAAe,CAAA;AACxC,8CAA8C;AAC9C,eAAO,MAAM,SAAS,OAAoB,CAAA;AAC1C,gCAAgC;AAChC,eAAO,MAAM,aAAa,GAAgB,CAAA;AAC1C,wCAAwC;AACxC,eAAO,MAAM,aAAa,GAAgB,CAAA;AAC1C,qFAAqF;AACrF,eAAO,MAAM,cAAc,MAAiB,CAAA;AAE5C,+EAA+E;AAC/E,eAAO,MAAM,kBAAkB,GAAI,MAAM,MAAM,KAAG,OAAyD,CAAA;AAE3G,yBAAyB;AACzB,eAAO,MAAM,SAAS,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,MAAsE,CAAA"}
|
package/dist/kinds.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kinds a conversation is made of. Numbers from the protocol, named here
|
|
3
|
+
* so every module reads the same one.
|
|
4
|
+
*/
|
|
5
|
+
import { KIND } from '@estiva-app/protocol';
|
|
6
|
+
/** A message in a channel — chat (SPEC §6.4, §6.7). */
|
|
7
|
+
export const KIND_MESSAGE = KIND.STREAM_MESSAGE;
|
|
8
|
+
/** A NIP-22 comment on an object, and a reply to one (§6.4). */
|
|
9
|
+
export const KIND_COMMENT = KIND.COMMENT;
|
|
10
|
+
/** An edit of a message or comment (§6.8). */
|
|
11
|
+
export const KIND_EDIT = KIND.MESSAGE_EDIT;
|
|
12
|
+
/** A NIP-25 reaction (§6.6). */
|
|
13
|
+
export const KIND_REACTION = KIND.REACTION;
|
|
14
|
+
/** A NIP-09 deletion request (§6.5). */
|
|
15
|
+
export const KIND_DELETION = KIND.DELETION;
|
|
16
|
+
/** An assertion; with `t=resolution` it resolves or reopens a comment (PEEK-128). */
|
|
17
|
+
export const KIND_ASSERTION = KIND.ASSERTION;
|
|
18
|
+
/** Whether the event is a message or a comment — what a conversation lists. */
|
|
19
|
+
export const isConversationKind = (kind) => kind === KIND_MESSAGE || kind === KIND_COMMENT;
|
|
20
|
+
/** A 64-hex event id. */
|
|
21
|
+
export const isEventId = (value) => typeof value === 'string' && /^[0-9a-fA-F]{64}$/.test(value);
|
|
22
|
+
//# sourceMappingURL=kinds.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kinds.js","sourceRoot":"","sources":["../src/kinds.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAE3C,uDAAuD;AACvD,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC,cAAc,CAAA;AAC/C,gEAAgE;AAChE,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAA;AACxC,8CAA8C;AAC9C,MAAM,CAAC,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAA;AAC1C,gCAAgC;AAChC,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,QAAQ,CAAA;AAC1C,wCAAwC;AACxC,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,QAAQ,CAAA;AAC1C,qFAAqF;AACrF,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC,SAAS,CAAA;AAE5C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAAY,EAAW,EAAE,CAAC,IAAI,KAAK,YAAY,IAAI,IAAI,KAAK,YAAY,CAAA;AAE3G,yBAAyB;AACzB,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAc,EAAmB,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA"}
|
package/dist/order.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one ordering — SPEC §6.3, with §6.2's `ts` rule.
|
|
3
|
+
*
|
|
4
|
+
* Ordering, most significant first: `ts` (when trusted), then `created_at`,
|
|
5
|
+
* then event `id`. Edits (§6.8) fold by it on purpose, because a second
|
|
6
|
+
* ordering rule in the same protocol is a second thing to get wrong.
|
|
7
|
+
*
|
|
8
|
+
* **`ts` is trusted only when `floor(ts / 1000) == created_at`, exactly**
|
|
9
|
+
* (§6.2, clarified 2026-09-29, CON-5). Peek's edit fold and interop's change
|
|
10
|
+
* and edit folds accepted ±1 s; that lets a `ts` claim the neighbouring second,
|
|
11
|
+
* which is the one thing the rule exists to prevent. Every writer floors, and
|
|
12
|
+
* all 3,470 events carrying `ts` on production agreed exactly, so the strict
|
|
13
|
+
* reading changes no fold that exists.
|
|
14
|
+
*/
|
|
15
|
+
import type { SignedEvent } from '@estiva-app/protocol';
|
|
16
|
+
type Ordered = Pick<SignedEvent, 'id' | 'created_at' | 'tags'>;
|
|
17
|
+
/** The event's `ts` in epoch milliseconds when §6.2 lets a reader trust it, else undefined. */
|
|
18
|
+
export declare function trustedTs(event: Pick<SignedEvent, 'created_at' | 'tags'>): number | undefined;
|
|
19
|
+
/** Epoch milliseconds to order by: the trusted `ts`, else `created_at` as milliseconds. */
|
|
20
|
+
export declare function orderingMs(event: Pick<SignedEvent, 'created_at' | 'tags'>): number;
|
|
21
|
+
/** Oldest first by §6.3: `ts` when trusted, then `created_at`, then the lower id. */
|
|
22
|
+
export declare function byOrder(a: Ordered, b: Ordered): number;
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=order.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order.d.ts","sourceRoot":"","sources":["../src/order.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAEvD,KAAK,OAAO,GAAG,IAAI,CAAC,WAAW,EAAE,IAAI,GAAG,YAAY,GAAG,MAAM,CAAC,CAAA;AAE9D,+FAA+F;AAC/F,wBAAgB,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,WAAW,EAAE,YAAY,GAAG,MAAM,CAAC,GAAG,MAAM,GAAG,SAAS,CAK7F;AAED,2FAA2F;AAC3F,wBAAgB,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,WAAW,EAAE,YAAY,GAAG,MAAM,CAAC,GAAG,MAAM,CAElF;AAED,qFAAqF;AACrF,wBAAgB,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,CAKtD"}
|
package/dist/order.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** The event's `ts` in epoch milliseconds when §6.2 lets a reader trust it, else undefined. */
|
|
2
|
+
export function trustedTs(event) {
|
|
3
|
+
const value = event.tags.find((t) => t[0] === 'ts')?.[1];
|
|
4
|
+
if (value === undefined || !/^\d+$/.test(value))
|
|
5
|
+
return undefined;
|
|
6
|
+
const ms = Number(value);
|
|
7
|
+
return Number.isSafeInteger(ms) && Math.floor(ms / 1000) === event.created_at ? ms : undefined;
|
|
8
|
+
}
|
|
9
|
+
/** Epoch milliseconds to order by: the trusted `ts`, else `created_at` as milliseconds. */
|
|
10
|
+
export function orderingMs(event) {
|
|
11
|
+
return trustedTs(event) ?? event.created_at * 1000;
|
|
12
|
+
}
|
|
13
|
+
/** Oldest first by §6.3: `ts` when trusted, then `created_at`, then the lower id. */
|
|
14
|
+
export function byOrder(a, b) {
|
|
15
|
+
const at = orderingMs(a);
|
|
16
|
+
const bt = orderingMs(b);
|
|
17
|
+
if (at !== bt)
|
|
18
|
+
return at - bt;
|
|
19
|
+
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=order.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order.js","sourceRoot":"","sources":["../src/order.ts"],"names":[],"mappings":"AAkBA,+FAA+F;AAC/F,MAAM,UAAU,SAAS,CAAC,KAA+C;IACvE,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IACxD,IAAI,KAAK,KAAK,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAA;IACjE,MAAM,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,CAAA;IACxB,OAAO,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAA;AAChG,CAAC;AAED,2FAA2F;AAC3F,MAAM,UAAU,UAAU,CAAC,KAA+C;IACxE,OAAO,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,UAAU,GAAG,IAAI,CAAA;AACpD,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,OAAO,CAAC,CAAU,EAAE,CAAU;IAC5C,MAAM,EAAE,GAAG,UAAU,CAAC,CAAC,CAAC,CAAA;IACxB,MAAM,EAAE,GAAG,UAAU,CAAC,CAAC,CAAC,CAAA;IACxB,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,EAAE,GAAG,EAAE,CAAA;IAC7B,OAAO,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,CAAA;AAC/C,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactions — SPEC §6.6.
|
|
3
|
+
*
|
|
4
|
+
* A reaction is a NIP-25 `kind:7` naming its target by `e`. **It carries no
|
|
5
|
+
* `h`**, so a channel read never returns one: reactions are fetched addressed
|
|
6
|
+
* by target, which forces a horizon every reader must share.
|
|
7
|
+
*
|
|
8
|
+
* Two apps must agree on a **count**, so what counts is specified (decided
|
|
9
|
+
* 2026-09-29, CON-5):
|
|
10
|
+
*
|
|
11
|
+
* - **Target**: the **last** `e` whose value is 64 hex, as NIP-25 says — the
|
|
12
|
+
* one the relay files the reaction under (`handlers/ingest.rs`).
|
|
13
|
+
* - **Emoji**: the content as stored, **not trimmed**; an empty content is
|
|
14
|
+
* `+`, NIP-25's "like".
|
|
15
|
+
* - **Count**: one per `(target, pubkey, emoji)`. The relay refuses an active
|
|
16
|
+
* duplicate; a reader still deduplicates, because a copy need not.
|
|
17
|
+
* - **Retraction** is a `kind:5` on the reaction, and the relay stops
|
|
18
|
+
* returning it, so a reader re-reads and does not reconcile.
|
|
19
|
+
* - **Order** is presentation. This keeps first-reaction order; an app MAY
|
|
20
|
+
* sort by count, and two apps that differ there disagree about nothing.
|
|
21
|
+
*/
|
|
22
|
+
import type { SignedEvent } from '@estiva-app/protocol';
|
|
23
|
+
/**
|
|
24
|
+
* How many targets a reaction read asks about — SPEC §6.6, decided by CON-1.
|
|
25
|
+
* The value is the specification's; two apps with different N disagree about
|
|
26
|
+
* a count, legitimately and unfixably.
|
|
27
|
+
*/
|
|
28
|
+
export declare const REACTION_HORIZON = 100;
|
|
29
|
+
/**
|
|
30
|
+
* The emoji a person can pick, and what each one means — the same five in
|
|
31
|
+
* both apps, in the same order, because a reaction you can read is one you
|
|
32
|
+
* should be able to answer. The name is the tooltip and the accessible name.
|
|
33
|
+
*/
|
|
34
|
+
export declare const REACTION_EMOJI: readonly {
|
|
35
|
+
emoji: string;
|
|
36
|
+
name: string;
|
|
37
|
+
}[];
|
|
38
|
+
/** The event a `kind:7` reacts to: the **last** 64-hex `e`, lowercased. */
|
|
39
|
+
export declare function reactionTargetOf(event: Pick<SignedEvent, 'tags'>): string | undefined;
|
|
40
|
+
/** The emoji a `kind:7` carries: its content untrimmed, `+` when empty. */
|
|
41
|
+
export declare function reactionEmojiOf(event: Pick<SignedEvent, 'content'>): string;
|
|
42
|
+
/** Which targets a reaction read asks about, and how many it leaves out. */
|
|
43
|
+
export interface ReactionHorizon {
|
|
44
|
+
/** The ids to ask about, newest first. */
|
|
45
|
+
asked: string[];
|
|
46
|
+
/** How many were left out — what §6.6 makes reporting a MUST. */
|
|
47
|
+
omitted: number;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The horizon applied: **the newest {@link REACTION_HORIZON} targets by
|
|
51
|
+
* `created_at`, ties broken on the higher id**, in one budget across every id
|
|
52
|
+
* space the caller mixes (roots and replies alike). `at` is the target's
|
|
53
|
+
* `created_at` in seconds; a repeated id counts once.
|
|
54
|
+
*/
|
|
55
|
+
export declare function reactionHorizon(targets: readonly {
|
|
56
|
+
id: string;
|
|
57
|
+
at: number;
|
|
58
|
+
}[], limit?: number): ReactionHorizon;
|
|
59
|
+
/** One reaction event, read. */
|
|
60
|
+
export interface ReactionEvent {
|
|
61
|
+
id: string;
|
|
62
|
+
target: string;
|
|
63
|
+
emoji: string;
|
|
64
|
+
by: string;
|
|
65
|
+
at: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Every `kind:7` in `events`, read by §6.6 and deduplicated per
|
|
69
|
+
* `(target, pubkey, emoji)` — the earliest wins, so a retraction names it.
|
|
70
|
+
* Oldest first by §6.3. A reaction with no 64-hex `e` is nobody's.
|
|
71
|
+
*/
|
|
72
|
+
export declare function reactionEventsOf(events: readonly SignedEvent[]): ReactionEvent[];
|
|
73
|
+
/** One emoji on one target, and who put it there. */
|
|
74
|
+
export interface Reaction {
|
|
75
|
+
emoji: string;
|
|
76
|
+
/** Distinct reactors — one per pubkey. */
|
|
77
|
+
count: number;
|
|
78
|
+
/** Reactor pubkeys, in the order they reacted. */
|
|
79
|
+
by: string[];
|
|
80
|
+
/**
|
|
81
|
+
* The viewer's own reaction event id, when they are among `by` — what a
|
|
82
|
+
* retraction (`kind:5` on the reaction) names.
|
|
83
|
+
*/
|
|
84
|
+
mine?: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Fold `kind:7` events into per-target counts (C13): target id → one entry per
|
|
88
|
+
* emoji, in first-reaction order. `viewer` fills `mine`.
|
|
89
|
+
*/
|
|
90
|
+
export declare function foldReactions(events: readonly SignedEvent[], viewer?: string): Record<string, Reaction[]>;
|
|
91
|
+
/** {@link foldReactions} over reactions already read by {@link reactionEventsOf}. */
|
|
92
|
+
export declare function groupReactions(reactions: readonly ReactionEvent[], viewer?: string): Record<string, Reaction[]>;
|
|
93
|
+
//# sourceMappingURL=reactions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactions.d.ts","sourceRoot":"","sources":["../src/reactions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAIvD;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,MAAM,CAAA;AAEnC;;;;GAIG;AACH,eAAO,MAAM,cAAc,EAAE,SAAS;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EAMpE,CAAA;AAED,2EAA2E;AAC3E,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,GAAG,MAAM,GAAG,SAAS,CAMrF;AA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