@wirecat/cli-core 0.704.0 → 0.706.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 +116 -0
- package/dist/cli/bot/api-http.d.ts +8 -2
- package/dist/cli/bot/api-http.js +20 -4
- package/dist/cli/bot/api.d.ts +8 -0
- package/dist/cli/bot/api.js +31 -3
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/index.d.ts +4 -4
- package/dist/cli/index.js +3 -3
- package/dist/cli/messenger/lacks.js +3 -0
- package/dist/cli/messenger/messages-delete-command.js +30 -25
- package/dist/cli/messenger/messages-edit-command.js +2 -0
- package/dist/cli/messenger/messages-scheduled-command.js +110 -4
- package/dist/cli/messenger/store-maintenance-command.js +6 -2
- package/dist/cli/service-api-call.d.ts +13 -3
- package/dist/cli/service-api-call.js +19 -9
- package/dist/cli/service-api-manifest.d.ts +11 -2
- package/dist/cli/service-api-manifest.js +30 -9
- package/dist/cli/service-api.d.ts +14 -6
- package/dist/cli/service-api.js +198 -52
- package/dist/cli/token-session.d.ts +5 -2
- package/dist/cli/token-session.js +13 -4
- package/dist/content/cli/index.d.ts +33 -0
- package/dist/content/cli/index.js +368 -0
- package/dist/content/index.d.ts +8 -0
- package/dist/content/index.js +7 -0
- package/dist/content/manifest/index.d.ts +4 -0
- package/dist/content/manifest/index.js +4 -0
- package/dist/content/manifest/manifest.d.ts +92 -0
- package/dist/content/manifest/manifest.js +137 -0
- package/dist/content/manifest/map.d.ts +25 -0
- package/dist/content/manifest/map.js +94 -0
- package/dist/content/manifest/source.d.ts +41 -0
- package/dist/content/manifest/source.js +288 -0
- package/dist/content/manifest/writer.d.ts +26 -0
- package/dist/content/manifest/writer.js +133 -0
- package/dist/content/source.d.ts +64 -0
- package/dist/content/source.js +2 -0
- package/dist/content/sync.d.ts +61 -0
- package/dist/content/sync.js +387 -0
- package/dist/content/testing/index.d.ts +4 -0
- package/dist/content/testing/index.js +4 -0
- package/dist/content/testing/source-contract.d.ts +38 -0
- package/dist/content/testing/source-contract.js +274 -0
- package/dist/content/testing/source.d.ts +55 -0
- package/dist/content/testing/source.js +276 -0
- package/dist/content/testing/sync-contract.d.ts +10 -0
- package/dist/content/testing/sync-contract.js +172 -0
- package/dist/content/testing/write-contract.d.ts +14 -0
- package/dist/content/testing/write-contract.js +102 -0
- package/dist/content/writer.d.ts +64 -0
- package/dist/content/writer.js +38 -0
- package/dist/content/writes.d.ts +58 -0
- package/dist/content/writes.js +129 -0
- package/dist/documents/file-mirror-model.d.ts +3 -1
- package/dist/documents/index.d.ts +3 -3
- package/dist/documents/index.js +1 -1
- package/dist/domain/content.d.ts +102 -0
- package/dist/domain/content.js +11 -0
- package/dist/entities/index.d.ts +8 -2
- package/dist/entities/index.js +4 -2
- package/dist/entities/message-change.d.ts +240 -0
- package/dist/entities/message-change.js +6 -0
- package/dist/errors/reasons.js +14 -0
- package/dist/kit/fake.d.ts +2 -2
- package/dist/kit/fake.js +21 -0
- package/dist/manifest/index.d.ts +21 -7
- package/dist/manifest/index.js +7 -4
- package/dist/mcp/surface.js +6 -3
- package/dist/ports/messenger.d.ts +20 -1
- package/dist/sends/permissions.js +11 -1
- package/dist/services/documents/mirrors.d.ts +13 -1
- package/dist/services/documents/mirrors.js +12 -3
- package/dist/services/files/mirror.d.ts +20 -8
- package/dist/services/files/mirror.js +84 -27
- package/dist/services/files/versions.d.ts +17 -7
- package/dist/services/files/versions.js +56 -21
- package/dist/services/messages.d.ts +31 -0
- package/dist/services/messages.js +62 -0
- package/dist/store/exports.d.ts +1 -0
- package/dist/store/exports.js +1 -0
- package/dist/store/recover.js +2 -0
- package/dist/store/sqlite/account-purge.js +7 -0
- package/dist/store/sqlite/content.d.ts +100 -0
- package/dist/store/sqlite/content.js +375 -0
- package/dist/store/sqlite/entity-types.d.ts +1 -0
- package/dist/store/sqlite/entity-types.js +1 -0
- package/dist/store/sqlite/manifest.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +26 -0
- package/dist/store/sqlite/schema.d.ts +1140 -0
- package/dist/store/sqlite/schema.js +100 -0
- package/dist/store/store.d.ts +3 -0
- package/dist/store/store.js +5 -0
- package/package.json +12 -4
|
@@ -1,12 +1,41 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { appendFileSync, createReadStream, createWriteStream, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
3
|
-
import { readdir, rename, rm, stat, utimes } from "node:fs/promises";
|
|
3
|
+
import { mkdir, readdir, rename, rm, stat, utimes } from "node:fs/promises";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { Transform } from "node:stream";
|
|
6
6
|
import { pipeline } from "node:stream/promises";
|
|
7
7
|
import { CliError } from "@wirecat/cli-base";
|
|
8
|
-
import {
|
|
8
|
+
import { heldCheck } from "../documents/mirrors.js";
|
|
9
|
+
import { assertNoLink, hashFile, replaceLocal, safePath } from "../documents/push.js";
|
|
9
10
|
const RETAIN_MS = 30 * 24 * 60 * 60 * 1000;
|
|
11
|
+
/** Errors of the local file system: a writer that fails with one could not write the store, whatever it was reading. */
|
|
12
|
+
const LOCAL_FAILURES = new Set(["ENOSPC", "EDQUOT", "EROFS", "EACCES", "EPERM", "EIO", "EFBIG", "EMFILE", "ENFILE"]);
|
|
13
|
+
const notKept = (error) => {
|
|
14
|
+
const why = error.code ?? String(error);
|
|
15
|
+
return new CliError("provider_error", `the earlier content could not be kept (${why}); nothing was replaced`, {
|
|
16
|
+
reason: "mirror.version-not-kept",
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
const stored = async (act) => {
|
|
20
|
+
try {
|
|
21
|
+
return await act();
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
throw notKept(error);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
/** Copies `from` into the new file `into` and returns the sha256 of the bytes it copied. */
|
|
28
|
+
const copyHashed = async (from, into) => {
|
|
29
|
+
const hash = createHash("sha256");
|
|
30
|
+
// Hashed while copied: the file may change under the sync, and the store is keyed by what it really holds.
|
|
31
|
+
await pipeline(createReadStream(from), new Transform({
|
|
32
|
+
transform(chunk, _encoding, done) {
|
|
33
|
+
hash.update(chunk);
|
|
34
|
+
done(null, chunk);
|
|
35
|
+
},
|
|
36
|
+
}), createWriteStream(into, { flags: "wx", mode: 0o600 }));
|
|
37
|
+
return hash.digest("hex");
|
|
38
|
+
};
|
|
10
39
|
/**
|
|
11
40
|
* A mirror's local version store: `<dir>/objects/<sha256>` holds each kept content once, `<dir>/log.jsonl` is
|
|
12
41
|
* append-only. The store lives under the profile's state folder, never inside the mirrored folder. Retention, one
|
|
@@ -32,30 +61,35 @@ export const fileVersions = (dir, now = Date.now) => {
|
|
|
32
61
|
};
|
|
33
62
|
const has = (sha256) => existsSync(join(objects, sha256));
|
|
34
63
|
return {
|
|
35
|
-
keep: async (
|
|
36
|
-
|
|
64
|
+
keep: async (source) => {
|
|
65
|
+
await stored(() => mkdir(objects, { recursive: true, mode: 0o700 }));
|
|
37
66
|
const temp = join(objects, `.${randomUUID()}.tmp`);
|
|
38
|
-
const hash = createHash("sha256");
|
|
39
67
|
try {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
await
|
|
51
|
-
|
|
52
|
-
|
|
68
|
+
let sha256;
|
|
69
|
+
if (typeof source === "function") {
|
|
70
|
+
await source(temp).catch((error) => {
|
|
71
|
+
if (LOCAL_FAILURES.has(error.code ?? ""))
|
|
72
|
+
throw notKept(error);
|
|
73
|
+
throw error;
|
|
74
|
+
});
|
|
75
|
+
sha256 = await stored(async () => (await hashFile(temp)).sha256);
|
|
76
|
+
}
|
|
77
|
+
else
|
|
78
|
+
sha256 = await stored(() => copyHashed(source, temp));
|
|
79
|
+
await stored(async () => {
|
|
80
|
+
const object = join(objects, sha256);
|
|
81
|
+
if (!has(sha256))
|
|
82
|
+
await rename(temp, object);
|
|
83
|
+
const at = now() / 1000;
|
|
84
|
+
await utimes(object, at, at);
|
|
85
|
+
});
|
|
53
86
|
return sha256;
|
|
54
87
|
}
|
|
55
88
|
finally {
|
|
56
89
|
await rm(temp, { force: true });
|
|
57
90
|
}
|
|
58
91
|
},
|
|
92
|
+
has,
|
|
59
93
|
log: (event) => {
|
|
60
94
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
61
95
|
appendFileSync(logPath, `${JSON.stringify({ at: new Date(now()).toISOString(), ...event })}\n`, { mode: 0o600 });
|
|
@@ -112,7 +146,8 @@ export const fileVersions = (dir, now = Date.now) => {
|
|
|
112
146
|
/**
|
|
113
147
|
* Puts a kept version of `path` back into the mirrored folder; the file there now is kept first. The next sync sends
|
|
114
148
|
* it like any local change. False when the store no longer has the version or the file changed meanwhile. Runs under
|
|
115
|
-
* the mirrors' sync lock, so a sync never reads the folder halfway
|
|
149
|
+
* the mirrors' sync lock (as `acquireLock` returns it, or any `assertHeld`), so a sync never reads the folder halfway
|
|
150
|
+
* through.
|
|
116
151
|
*/
|
|
117
152
|
export const restoreFileVersion = async (root, path, sha256, versions, lock) => {
|
|
118
153
|
if (!safePath(path))
|
|
@@ -124,14 +159,14 @@ export const restoreFileVersion = async (root, path, sha256, versions, lock) =>
|
|
|
124
159
|
const current = existsSync(absolute) ? await versions.keep(absolute) : undefined;
|
|
125
160
|
if (current === sha256)
|
|
126
161
|
return true;
|
|
127
|
-
held
|
|
162
|
+
await heldCheck(held)();
|
|
128
163
|
if (!(await versions.write(sha256, absolute, current)))
|
|
129
164
|
return false;
|
|
130
165
|
versions.log({
|
|
131
166
|
path,
|
|
132
167
|
event: current ? "changed" : "created",
|
|
133
168
|
direction: "restored",
|
|
134
|
-
...(current ? { old: current } : {}),
|
|
169
|
+
...(current ? { old: current, from: "local" } : {}),
|
|
135
170
|
new: sha256,
|
|
136
171
|
});
|
|
137
172
|
return true;
|
|
@@ -257,6 +257,37 @@ export interface MessagesService {
|
|
|
257
257
|
messages: string[];
|
|
258
258
|
forEveryone: boolean;
|
|
259
259
|
}): Promise<Operated<Deletion>>;
|
|
260
|
+
/**
|
|
261
|
+
* The scheduled messages by the ids `scheduled` lists, each checked against that list first: the same number in the
|
|
262
|
+
* chat's history is another message. Cancelling counts as deleting, sending now as sending.
|
|
263
|
+
*/
|
|
264
|
+
cancelScheduled(request: {
|
|
265
|
+
chat: string;
|
|
266
|
+
ids: string[];
|
|
267
|
+
}): Promise<Operated<{
|
|
268
|
+
chatId: Id;
|
|
269
|
+
cancelled: Id[];
|
|
270
|
+
}>>;
|
|
271
|
+
/** New text, a new time (`at`), or both. */
|
|
272
|
+
editScheduled(request: {
|
|
273
|
+
chat: string;
|
|
274
|
+
id: string;
|
|
275
|
+
text?: string;
|
|
276
|
+
markdown?: boolean;
|
|
277
|
+
html?: boolean;
|
|
278
|
+
at?: string;
|
|
279
|
+
}): Promise<Operated<{
|
|
280
|
+
message: Message;
|
|
281
|
+
scheduledFor: string;
|
|
282
|
+
}>>;
|
|
283
|
+
sendScheduled(request: {
|
|
284
|
+
chat: string;
|
|
285
|
+
ids: string[];
|
|
286
|
+
}): Promise<Operated<{
|
|
287
|
+
chatId: Id;
|
|
288
|
+
sent: Id[];
|
|
289
|
+
messages: Message[];
|
|
290
|
+
}>>;
|
|
260
291
|
/** Guarded against the chat it goes to: that is where somebody new reads it. */
|
|
261
292
|
forward(target: MessageTarget & {
|
|
262
293
|
to: string;
|
|
@@ -22,6 +22,10 @@ import { searchServer, sourceKey, } from "./server-search.js";
|
|
|
22
22
|
import { readThreadContext, threadBounds } from "./thread-context.js";
|
|
23
23
|
/** max-cli's: many at once is what a ban for automation looks like. */
|
|
24
24
|
export const DELETE_AT_ONCE = 10;
|
|
25
|
+
const atOnce = (ids) => {
|
|
26
|
+
if (ids.length > DELETE_AT_ONCE)
|
|
27
|
+
throw new CliError("validation_error", `at most ${DELETE_AT_ONCE} messages at once, got ${ids.length}`);
|
|
28
|
+
};
|
|
25
29
|
/** Every write goes through the guard: asked before it goes, told after, on every outcome. */
|
|
26
30
|
/** Only a message with files says what can be linked of them. */
|
|
27
31
|
const filesOf = ({ attachments }) => {
|
|
@@ -69,6 +73,17 @@ export const messagesService = (deps) => {
|
|
|
69
73
|
})
|
|
70
74
|
.catch(() => undefined);
|
|
71
75
|
};
|
|
76
|
+
/** The chat and its scheduled messages by the ids asked for; an id it does not list is refused before any write. */
|
|
77
|
+
const waiting = async (connection, chat, ids) => {
|
|
78
|
+
const list = capability(connection, "scheduled", "list scheduled messages");
|
|
79
|
+
const { id: chatId } = await connection.resolve(chat);
|
|
80
|
+
const queued = await list(chatId);
|
|
81
|
+
const unknown = ids.filter((id) => !queued.some((message) => message.id === id));
|
|
82
|
+
if (unknown.length > 0)
|
|
83
|
+
throw new CliError("not_found", `nothing scheduled in this chat has the id ${unknown.join(", ")} — a scheduled message has an id of ` +
|
|
84
|
+
`its own, not the one of a sent message; \`messages scheduled ${chat}\` lists them`, { sent: false });
|
|
85
|
+
return { chatId, found: ids.map((id) => queued.find((message) => message.id === id)) };
|
|
86
|
+
};
|
|
72
87
|
const pinning = async ({ chat, message }, pinned, asked, requested) => {
|
|
73
88
|
const allowed = deps.messenger.pinDurations;
|
|
74
89
|
if (requested !== undefined && !allowed?.includes(requested))
|
|
@@ -398,6 +413,53 @@ export const messagesService = (deps) => {
|
|
|
398
413
|
await guardedWrite(guard, { operationId, chatId, kind: "delete", count: messages.length, forEveryone }, () => remove(chatId, messages, { forEveryone }));
|
|
399
414
|
return { operationId, chatId, deleted: messages, forEveryone };
|
|
400
415
|
},
|
|
416
|
+
cancelScheduled: async ({ chat, ids }) => {
|
|
417
|
+
atOnce(ids);
|
|
418
|
+
const connection = await deps.connection();
|
|
419
|
+
const cancel = capability(connection, "cancelScheduled", "cancel a scheduled message");
|
|
420
|
+
const { chatId } = await waiting(connection, chat, ids);
|
|
421
|
+
const operationId = newOperationId();
|
|
422
|
+
await guardedWrite(guard, { operationId, chatId, kind: "delete", key: "messages.scheduled.cancel", count: ids.length }, () => cancel(chatId, ids));
|
|
423
|
+
return { operationId, chatId, cancelled: ids };
|
|
424
|
+
},
|
|
425
|
+
editScheduled: async ({ chat, id, text: typed, markdown, html, at }) => {
|
|
426
|
+
if (typed === undefined && at === undefined)
|
|
427
|
+
throw new CliError("validation_error", "nothing to change — give the new text, --at-time, or both");
|
|
428
|
+
const connection = await deps.connection();
|
|
429
|
+
const edit = capability(connection, "editScheduled", "edit a scheduled message");
|
|
430
|
+
const prepared = typed === undefined ? undefined : await formatted(connection, typed, { markdown, html });
|
|
431
|
+
const change = prepared && (deps.agentText ? visibleFormattedText(prepared) : prepared);
|
|
432
|
+
if (change !== undefined && change.text.trim() === "")
|
|
433
|
+
throw new CliError("validation_error", "no new text — the marks leave nothing");
|
|
434
|
+
const { chatId, found: [current], } = await waiting(connection, chat, [id]);
|
|
435
|
+
const scheduledFor = at ?? current?.scheduledFor ?? current?.timestamp;
|
|
436
|
+
if (scheduledFor === undefined)
|
|
437
|
+
throw new CliError("invalid_response", "the scheduled message has no time");
|
|
438
|
+
const operationId = newOperationId();
|
|
439
|
+
const message = await guardedWrite(guard, {
|
|
440
|
+
operationId,
|
|
441
|
+
chatId,
|
|
442
|
+
kind: "edit",
|
|
443
|
+
key: "messages.scheduled.edit",
|
|
444
|
+
messageId: id,
|
|
445
|
+
scheduledFor,
|
|
446
|
+
...(change === undefined ? {} : { length: change.text.length }),
|
|
447
|
+
}, () => edit(chatId, id, {
|
|
448
|
+
at: scheduledFor,
|
|
449
|
+
...(change === undefined ? {} : { text: change.text }),
|
|
450
|
+
...(change === undefined || change.spans.length === 0 ? {} : { formatting: change.spans }),
|
|
451
|
+
}));
|
|
452
|
+
return { operationId, message, scheduledFor };
|
|
453
|
+
},
|
|
454
|
+
sendScheduled: async ({ chat, ids }) => {
|
|
455
|
+
atOnce(ids);
|
|
456
|
+
const connection = await deps.connection();
|
|
457
|
+
const send = capability(connection, "sendScheduled", "send a scheduled message now");
|
|
458
|
+
const { chatId } = await waiting(connection, chat, ids);
|
|
459
|
+
const operationId = newOperationId();
|
|
460
|
+
const messages = await guardedWrite(guard, { operationId, chatId, kind: "message", key: "messages.scheduled.send", count: ids.length }, () => send(chatId, ids));
|
|
461
|
+
return { operationId, chatId, sent: ids, messages };
|
|
462
|
+
},
|
|
401
463
|
forward: async ({ chat, message, to, silent, sendId, sendAs, threadId: typedThread }) => {
|
|
402
464
|
const connection = await deps.connection();
|
|
403
465
|
const forward = capability(connection, "forward", "forward a message");
|
package/dist/store/exports.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export type { ChatRekey, ChatRekeys } from "./sqlite/chat-rekey.js";
|
|
|
25
25
|
export { CHAT_COLUMNS, TRACKED_CHAT_COLUMNS, titlePart } from "./sqlite/chats.js";
|
|
26
26
|
export { storeClients } from "./sqlite/clients.js";
|
|
27
27
|
export { CHAT_LIST_KEY, type ChatCompleteness, type ChatCoverage, fetchedKey, historyStartKey, } from "./sqlite/completeness.js";
|
|
28
|
+
export { type ContentFilter, type ContentHit, type ContentItem, type ContentPerson, type ContentRevision, type ContentSearch, type ContentStore, contentHashOf, type RevisionReason, resetContentPostWords, } from "./sqlite/content.js";
|
|
28
29
|
export { CONVERSATION_COLUMNS } from "./sqlite/conversations.js";
|
|
29
30
|
export { type CopyViewer, seenBy } from "./sqlite/copy-viewer.js";
|
|
30
31
|
export type { CounterTarget } from "./sqlite/counters.js";
|
package/dist/store/exports.js
CHANGED
|
@@ -7,6 +7,7 @@ export { BUSINESS_FIND_MAX, } from "./sqlite/business-search.js";
|
|
|
7
7
|
export { CHAT_COLUMNS, TRACKED_CHAT_COLUMNS, titlePart } from "./sqlite/chats.js";
|
|
8
8
|
export { storeClients } from "./sqlite/clients.js";
|
|
9
9
|
export { CHAT_LIST_KEY, fetchedKey, historyStartKey, } from "./sqlite/completeness.js";
|
|
10
|
+
export { contentHashOf, resetContentPostWords, } from "./sqlite/content.js";
|
|
10
11
|
export { CONVERSATION_COLUMNS } from "./sqlite/conversations.js";
|
|
11
12
|
export { seenBy } from "./sqlite/copy-viewer.js";
|
|
12
13
|
export { resetCrmFileWords, } from "./sqlite/crm-files.js";
|
package/dist/store/recover.js
CHANGED
|
@@ -11,6 +11,7 @@ import { readHealthMark, recordDeepCheck } from "./marks.js";
|
|
|
11
11
|
import { migrate } from "./migrations.js";
|
|
12
12
|
import { forgetCoverageSince, REFILL_MARGIN_MS } from "./refill.js";
|
|
13
13
|
import { resetAttachmentWords } from "./sqlite/attachment-texts.js";
|
|
14
|
+
import { resetContentPostWords } from "./sqlite/content.js";
|
|
14
15
|
import { resetCrmFileWords } from "./sqlite/crm-files.js";
|
|
15
16
|
import { involvementStoreOver } from "./sqlite/involvements.js";
|
|
16
17
|
import { drainMeetingIndex } from "./sqlite/meetings.js";
|
|
@@ -385,6 +386,7 @@ const FILLERS = {
|
|
|
385
386
|
attachment_words: (database) => void resetAttachmentWords(database),
|
|
386
387
|
crm_file_words: (database) => void resetCrmFileWords(database),
|
|
387
388
|
tracker_item_words: (database) => void resetTrackerItemWords(database),
|
|
389
|
+
content_post_words: (database) => void resetContentPostWords(database),
|
|
388
390
|
attachment_name_words: (database) =>
|
|
389
391
|
// As migration 20 filled it: й and ё fold as the triggers fold them.
|
|
390
392
|
database.exec("INSERT INTO attachment_name_words (rowid, name) SELECT id, " +
|
|
@@ -98,6 +98,13 @@ export const purgeAccount = (context, accountKey) => {
|
|
|
98
98
|
orm.run(sql `DELETE FROM crm_fields WHERE account_id = ${accountKey}`);
|
|
99
99
|
orm.run(sql `DELETE FROM crm_files WHERE account_id = ${accountKey}`);
|
|
100
100
|
orm.run(sql `DELETE FROM tracker_items WHERE account_id = ${accountKey}`);
|
|
101
|
+
const postsOf = sql `(SELECT id FROM content_posts WHERE account_id = ${accountKey})`;
|
|
102
|
+
orm.run(sql `DELETE FROM content_post_revisions WHERE post_id IN ${postsOf}`);
|
|
103
|
+
orm.run(sql `DELETE FROM content_metrics WHERE post_id IN ${postsOf}`);
|
|
104
|
+
orm.run(sql `DELETE FROM content_posts WHERE account_id = ${accountKey}`);
|
|
105
|
+
orm.run(sql `DELETE FROM content_terms WHERE account_id = ${accountKey}`);
|
|
106
|
+
orm.run(sql `DELETE FROM content_authors WHERE account_id = ${accountKey}`);
|
|
107
|
+
orm.run(sql `DELETE FROM content_channels WHERE account_id = ${accountKey}`);
|
|
101
108
|
// An entity made from a record is the owner's and stays; only what the source said goes.
|
|
102
109
|
const recordsOf = sql `(SELECT id FROM records WHERE account_id = ${accountKey})`;
|
|
103
110
|
orm.run(sql `DELETE FROM record_relations WHERE record_id IN ${recordsOf} OR target_id IN ${recordsOf}`);
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { ContentChannel, ContentKind, ContentMetric, ContentPost, ContentRecords, PostStatus, PostType } from "../../domain/content.js";
|
|
2
|
+
import type { CacheDatabase } from "../driver.js";
|
|
3
|
+
import type { AccountKey } from "../types.js";
|
|
4
|
+
import type { StoreContext } from "./open.js";
|
|
5
|
+
/** One stored record with what the store keeps beside it. */
|
|
6
|
+
export interface ContentItem<K extends ContentKind = ContentKind> {
|
|
7
|
+
kind: K;
|
|
8
|
+
record: ContentRecords[K];
|
|
9
|
+
/** Gone from a complete list of the source's ids. A trashed post is not deleted: its status says so. */
|
|
10
|
+
deletedAt: number | null;
|
|
11
|
+
syncedAt: number;
|
|
12
|
+
}
|
|
13
|
+
/** What replaced a stored post's text or status: a sync, a write of the owner's, or the source deleting it. */
|
|
14
|
+
export type RevisionReason = "sync" | "write" | "deleted";
|
|
15
|
+
/** A post as it stood before a change; the newest first. */
|
|
16
|
+
export interface ContentRevision {
|
|
17
|
+
title: string | null;
|
|
18
|
+
body: string | null;
|
|
19
|
+
status: PostStatus;
|
|
20
|
+
contentHash: string;
|
|
21
|
+
version: string | null;
|
|
22
|
+
reason: RevisionReason;
|
|
23
|
+
createdAt: number;
|
|
24
|
+
}
|
|
25
|
+
export interface ContentFilter {
|
|
26
|
+
channel?: string;
|
|
27
|
+
statuses?: readonly PostStatus[];
|
|
28
|
+
type?: PostType;
|
|
29
|
+
/** A term's id at the source. */
|
|
30
|
+
term?: string;
|
|
31
|
+
/** An author's id at the source. */
|
|
32
|
+
author?: string;
|
|
33
|
+
/** Published at or after; epoch ms. */
|
|
34
|
+
since?: number;
|
|
35
|
+
/** Published before; epoch ms. */
|
|
36
|
+
until?: number;
|
|
37
|
+
limit?: number;
|
|
38
|
+
offset?: number;
|
|
39
|
+
}
|
|
40
|
+
export interface ContentHit {
|
|
41
|
+
account: AccountKey;
|
|
42
|
+
item: ContentItem<"post">;
|
|
43
|
+
excerpt: string;
|
|
44
|
+
}
|
|
45
|
+
export interface ContentSearch {
|
|
46
|
+
words: string[];
|
|
47
|
+
anyWord?: boolean;
|
|
48
|
+
exact?: boolean;
|
|
49
|
+
/** Absent: every content account's. */
|
|
50
|
+
accounts?: AccountKey[];
|
|
51
|
+
limit?: number;
|
|
52
|
+
offset?: number;
|
|
53
|
+
}
|
|
54
|
+
/** An author joined to the one person the store knows by the same e-mail. */
|
|
55
|
+
export interface ContentPerson {
|
|
56
|
+
author: string;
|
|
57
|
+
person: string;
|
|
58
|
+
name: string | null;
|
|
59
|
+
}
|
|
60
|
+
export interface ContentStore {
|
|
61
|
+
saveChannels(key: AccountKey, channels: readonly ContentChannel[], now: number): Promise<number>;
|
|
62
|
+
channels(key: AccountKey): Promise<ContentChannel[]>;
|
|
63
|
+
/**
|
|
64
|
+
* Upserts records of one kind; a field a record leaves out keeps its stored value, and a record listed again after
|
|
65
|
+
* it was marked deleted comes back. A post whose title, text or status changes keeps what it had as a revision.
|
|
66
|
+
*/
|
|
67
|
+
save<K extends ContentKind>(key: AccountKey, kind: K, records: readonly ContentRecords[K][], now: number, options?: {
|
|
68
|
+
reason?: Exclude<RevisionReason, "deleted">;
|
|
69
|
+
}): Promise<number>;
|
|
70
|
+
/**
|
|
71
|
+
* After a complete list of the kind's ids: the stored ones the list lacks that were not synced since `before`. Absence
|
|
72
|
+
* alone is no deletion: the sync reads each again, and `markDeleted` marks the ones the source says are gone.
|
|
73
|
+
*/
|
|
74
|
+
unlisted(key: AccountKey, kind: ContentKind, listed: readonly string[], before: number): Promise<string[]>;
|
|
75
|
+
/** Marks one record deleted, as a read that answered "no such post" proves. */
|
|
76
|
+
markDeleted(key: AccountKey, kind: ContentKind, externalId: string, now: number): Promise<boolean>;
|
|
77
|
+
get<K extends ContentKind>(key: AccountKey, kind: K, externalId: string): Promise<ContentItem<K> | undefined>;
|
|
78
|
+
/** Live rows, newest change first; deleted ones never. Post filters apply to posts only. */
|
|
79
|
+
list<K extends ContentKind>(key: AccountKey, kind: K, filter?: ContentFilter): Promise<{
|
|
80
|
+
items: ContentItem<K>[];
|
|
81
|
+
hasMore: boolean;
|
|
82
|
+
}>;
|
|
83
|
+
find(search: ContentSearch): Promise<{
|
|
84
|
+
items: ContentHit[];
|
|
85
|
+
hasMore: boolean;
|
|
86
|
+
}>;
|
|
87
|
+
count(key: AccountKey, kind: ContentKind): Promise<number>;
|
|
88
|
+
/** Scheduled posts whose time has passed, the longest unread first: the source may have published them unmarked. */
|
|
89
|
+
due(key: AccountKey, now: number): Promise<string[]>;
|
|
90
|
+
revisions(key: AccountKey, post: string): Promise<ContentRevision[]>;
|
|
91
|
+
saveMetrics(key: AccountKey, metrics: readonly ContentMetric[]): Promise<number>;
|
|
92
|
+
/** Every reading of a post's numbers, the newest first. */
|
|
93
|
+
metrics(key: AccountKey, post: string, limit?: number): Promise<ContentMetric[]>;
|
|
94
|
+
people(key: AccountKey, authors: readonly string[]): Promise<ContentPerson[]>;
|
|
95
|
+
}
|
|
96
|
+
/** The hash a post's text keeps: a change of either part is a new version. */
|
|
97
|
+
export declare const contentHashOf: (post: Pick<ContentPost, "title" | "body">) => string;
|
|
98
|
+
/** `store rebuild`: the normalized copies follow the current normalizer, then the word index is filled again. */
|
|
99
|
+
export declare const resetContentPostWords: (database: CacheDatabase) => number;
|
|
100
|
+
export declare const contentStoreOver: (context: StoreContext) => ContentStore;
|