@wirecat/cli-core 0.704.0 → 0.705.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 +86 -0
- package/dist/cli/bot/api-http.d.ts +8 -2
- package/dist/cli/bot/api-http.js +20 -4
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/index.d.ts +3 -3
- package/dist/cli/index.js +3 -3
- 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 +137 -29
- 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/errors/reasons.js +14 -0
- package/dist/manifest/index.d.ts +21 -7
- package/dist/manifest/index.js +7 -4
- 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/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
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { runBudget } from "../../background/budget.js";
|
|
3
|
+
import { syncContent } from "../sync.js";
|
|
4
|
+
import { exampleContentRows, fakeContentSource, } from "./source.js";
|
|
5
|
+
const DAY = 86_400_000;
|
|
6
|
+
/** What the sync engine does over any store, whatever the source: core runs it, and a CLI may run it over its store. */
|
|
7
|
+
export const contentSyncContract = (make) => {
|
|
8
|
+
let now = Date.UTC(2026, 10, 18);
|
|
9
|
+
const test = (name, check, fake = {}) => ({
|
|
10
|
+
name,
|
|
11
|
+
async run() {
|
|
12
|
+
now = Date.UTC(2026, 10, 18);
|
|
13
|
+
const fixture = await make();
|
|
14
|
+
const source = fakeContentSource({ rows: exampleContentRows(), clock: () => now, ...fake });
|
|
15
|
+
const sync = (options = {}, other = source) => syncContent(other, fixture.store, fixture.account, { now, ...options });
|
|
16
|
+
const posts = async () => (await fixture.store.content.list(fixture.account, "post")).items.map((one) => one.record.externalId).sort();
|
|
17
|
+
const status = async (id) => (await fixture.store.content.get(fixture.account, "post", id))?.record.status;
|
|
18
|
+
await check({ ...fixture, source, sync, posts, status });
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
const later = (days = 2) => {
|
|
22
|
+
now += days * DAY;
|
|
23
|
+
};
|
|
24
|
+
return [
|
|
25
|
+
test("copies channels, authors, terms and posts on the first run, and only what changed on the next", async (c) => {
|
|
26
|
+
const first = await c.sync();
|
|
27
|
+
assert.equal(first.state, "complete");
|
|
28
|
+
assert.equal(first.channels, 1);
|
|
29
|
+
assert.deepEqual(await c.posts(), ["10", "11", "12"]);
|
|
30
|
+
c.source.touch("post", "11");
|
|
31
|
+
const second = await c.sync();
|
|
32
|
+
const posts = second.synced.find((one) => one.kind === "post");
|
|
33
|
+
assert.equal(posts?.round, "changes");
|
|
34
|
+
assert.equal(posts?.saved, 1, "only the changed post is read again");
|
|
35
|
+
}),
|
|
36
|
+
test("a post moved to the trash stays in the copy with the status trashed; it is not deleted", async (c) => {
|
|
37
|
+
await c.sync();
|
|
38
|
+
c.source.trashed("11");
|
|
39
|
+
later();
|
|
40
|
+
await c.sync();
|
|
41
|
+
assert.deepEqual(await c.posts(), ["10", "11", "12"]);
|
|
42
|
+
assert.equal(await c.status("11"), "trashed");
|
|
43
|
+
}),
|
|
44
|
+
test("a post deleted for good is marked deleted by a complete id pass, its last text kept as a version", async (c) => {
|
|
45
|
+
await c.sync();
|
|
46
|
+
c.source.remove("post", "11");
|
|
47
|
+
later();
|
|
48
|
+
const report = await c.sync();
|
|
49
|
+
assert.equal(report.synced.find((one) => one.kind === "post")?.unlisted, 1);
|
|
50
|
+
assert.deepEqual(await c.posts(), ["10", "12"]);
|
|
51
|
+
const [kept] = await c.store.content.revisions(c.account, "11");
|
|
52
|
+
assert.equal(kept?.reason, "deleted");
|
|
53
|
+
assert.equal(kept?.title, "Draft about pricing");
|
|
54
|
+
}),
|
|
55
|
+
test("a changed post keeps what it said before as a version", async (c) => {
|
|
56
|
+
await c.sync();
|
|
57
|
+
await c.source.writer.update("10", { body: "We shipped version 2, and 2.1." }, { operationId: "x" });
|
|
58
|
+
await c.sync();
|
|
59
|
+
const [kept] = await c.store.content.revisions(c.account, "10");
|
|
60
|
+
assert.equal(kept?.reason, "sync");
|
|
61
|
+
assert.equal(kept?.body, "We shipped **version 2**.");
|
|
62
|
+
}),
|
|
63
|
+
test("a scheduled post whose time passed is read again until it is published, with no change reported", async (c) => {
|
|
64
|
+
await c.sync();
|
|
65
|
+
await c.source.writer.schedule("11", now + 60_000, { operationId: "x" });
|
|
66
|
+
later();
|
|
67
|
+
await c.sync();
|
|
68
|
+
assert.equal(await c.status("11"), "scheduled", "its time passed, but the source has not sent it out yet");
|
|
69
|
+
c.source.elapse("11");
|
|
70
|
+
later();
|
|
71
|
+
const report = await c.sync();
|
|
72
|
+
assert.equal(report.due, 1);
|
|
73
|
+
assert.equal(await c.status("11"), "published");
|
|
74
|
+
}),
|
|
75
|
+
test("a scheduled post deleted for good before its time is marked deleted when read again", async (c) => {
|
|
76
|
+
await c.sync();
|
|
77
|
+
await c.source.writer.schedule("11", now + 60_000, { operationId: "x" });
|
|
78
|
+
await c.sync();
|
|
79
|
+
c.source.remove("post", "11");
|
|
80
|
+
later(0.01);
|
|
81
|
+
await c.sync({ passEvery: 10 * DAY });
|
|
82
|
+
assert.deepEqual(await c.posts(), ["10", "12"]);
|
|
83
|
+
}),
|
|
84
|
+
test("a sign-in that sees only its own posts says so, and the posts it cannot see are not marked deleted", async (c) => {
|
|
85
|
+
await c.sync({}, fakeContentSource({ rows: c.source.rows }));
|
|
86
|
+
later();
|
|
87
|
+
const report = await c.sync();
|
|
88
|
+
const posts = report.synced.find((one) => one.kind === "post");
|
|
89
|
+
assert.equal(report.gaps[0]?.scope, "edit_others_posts");
|
|
90
|
+
assert.equal(posts?.pass, "held");
|
|
91
|
+
assert.deepEqual(await c.posts(), ["10", "11", "12"]);
|
|
92
|
+
}, { only: { author: "1" } }),
|
|
93
|
+
test("a list of no ids marks nothing deleted; --rescan lets it through", async (c) => {
|
|
94
|
+
await c.sync();
|
|
95
|
+
for (const id of ["10", "11", "12"])
|
|
96
|
+
c.source.remove("post", id);
|
|
97
|
+
later();
|
|
98
|
+
const held = await c.sync();
|
|
99
|
+
assert.equal(held.synced.find((one) => one.kind === "post")?.pass, "held");
|
|
100
|
+
assert.deepEqual(await c.posts(), ["10", "11", "12"]);
|
|
101
|
+
const rescan = await c.sync({ rescan: true });
|
|
102
|
+
assert.equal(rescan.synced.find((one) => one.kind === "post")?.pass, "complete");
|
|
103
|
+
assert.deepEqual(await c.posts(), []);
|
|
104
|
+
}),
|
|
105
|
+
test("a site whose address changed holds its deletions until a rescan", async (c) => {
|
|
106
|
+
await c.sync();
|
|
107
|
+
c.source.move("https://other.example.com");
|
|
108
|
+
c.source.remove("post", "11");
|
|
109
|
+
later();
|
|
110
|
+
const report = await c.sync();
|
|
111
|
+
assert.deepEqual(report.moved, [
|
|
112
|
+
{ channel: "site", from: "https://blog.example.com", to: "https://other.example.com" },
|
|
113
|
+
]);
|
|
114
|
+
assert.equal(report.synced.find((one) => one.kind === "post")?.pass, "held");
|
|
115
|
+
assert.deepEqual(await c.posts(), ["10", "11", "12"]);
|
|
116
|
+
later();
|
|
117
|
+
const next = await c.sync();
|
|
118
|
+
assert.equal(next.synced.find((one) => one.kind === "post")?.pass, "held", "a plain run after it still holds");
|
|
119
|
+
assert.deepEqual(await c.posts(), ["10", "11", "12"]);
|
|
120
|
+
await c.sync({ rescan: true });
|
|
121
|
+
assert.deepEqual(await c.posts(), ["10", "12"]);
|
|
122
|
+
later();
|
|
123
|
+
assert.equal((await c.sync()).moved, undefined, "a rescan takes the new address as the site's");
|
|
124
|
+
}),
|
|
125
|
+
test("an author whose e-mail a read leaves out keeps the stored one; nothing is erased by omission", async (c) => {
|
|
126
|
+
await c.sync();
|
|
127
|
+
const [author] = c.source.rows.author ?? [];
|
|
128
|
+
assert.ok(author, "the fake holds an author");
|
|
129
|
+
delete author.email;
|
|
130
|
+
c.source.touch("author", "1");
|
|
131
|
+
await c.sync();
|
|
132
|
+
assert.equal((await c.store.content.get(c.account, "author", "1"))?.record.email, "ada@example.com");
|
|
133
|
+
}),
|
|
134
|
+
test("a renamed term keeps its id; a post that loses a term loses its tag", async (c) => {
|
|
135
|
+
await c.sync();
|
|
136
|
+
const tags = async () => (await c.store.content.get(c.account, "post", "10"))?.record.terms ?? [];
|
|
137
|
+
assert.deepEqual(await tags(), ["tag-7", "cat-1"]);
|
|
138
|
+
const rows = c.source.rows;
|
|
139
|
+
const term = rows.term?.find((one) => one.externalId === "tag-7");
|
|
140
|
+
if (term)
|
|
141
|
+
term.name = "Release notes";
|
|
142
|
+
c.source.touch("term", "tag-7");
|
|
143
|
+
await c.source.writer.update("10", { terms: ["cat-1"] }, { operationId: "x" });
|
|
144
|
+
await c.sync();
|
|
145
|
+
assert.equal((await c.store.content.get(c.account, "term", "tag-7"))?.record.name, "Release notes");
|
|
146
|
+
assert.deepEqual(await tags(), ["cat-1"]);
|
|
147
|
+
}),
|
|
148
|
+
test("two profiles on one site sync into one copy without a post twice", async (c) => {
|
|
149
|
+
await c.sync();
|
|
150
|
+
const other = fakeContentSource({ rows: c.source.rows });
|
|
151
|
+
await c.sync({}, other);
|
|
152
|
+
assert.deepEqual(await c.posts(), ["10", "11", "12"]);
|
|
153
|
+
assert.equal(await c.store.content.count(c.account, "post"), 3);
|
|
154
|
+
}),
|
|
155
|
+
test("a run that stops part way goes on from its page in the next one, nothing lost", async (c) => {
|
|
156
|
+
const first = await c.sync({ budget: runBudget({ requests: 3 }) });
|
|
157
|
+
assert.equal(first.state, "partial");
|
|
158
|
+
for (let run = 0; run < 10; run += 1)
|
|
159
|
+
if ((await c.sync({ budget: runBudget({ requests: 3 }) })).complete)
|
|
160
|
+
break;
|
|
161
|
+
assert.deepEqual(await c.posts(), ["10", "11", "12"]);
|
|
162
|
+
}),
|
|
163
|
+
test("numbers are new readings each run, never overwritten; a source without them reads none", async (c) => {
|
|
164
|
+
await c.sync();
|
|
165
|
+
later(0.01);
|
|
166
|
+
await c.sync();
|
|
167
|
+
const readings = await c.store.content.metrics(c.account, "10");
|
|
168
|
+
assert.equal(readings.length, 2);
|
|
169
|
+
assert.equal(readings[0]?.views, 42);
|
|
170
|
+
}, { metrics: { "10": { views: 42, comments: 3 } } }),
|
|
171
|
+
];
|
|
172
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ContractCase } from "../../meetings/index.js";
|
|
2
|
+
import type { ContentWriter } from "../writer.js";
|
|
3
|
+
/** One writer over recorded or faked answers, fresh for each case. */
|
|
4
|
+
export interface ContentWriteFixture {
|
|
5
|
+
writer: ContentWriter;
|
|
6
|
+
/** Makes the next write's answer lost after the source got it, as a dropped connection does. */
|
|
7
|
+
loseNextAnswer: () => void;
|
|
8
|
+
/** How many write requests reached the source so far. */
|
|
9
|
+
requests: () => number;
|
|
10
|
+
/** Changes a post at the source behind the copy's back, so the version a write sends is old. */
|
|
11
|
+
changeElsewhere?: (externalId: string) => void | Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
/** What every content writer does, whatever the source: run each case in the adapter's tests. */
|
|
14
|
+
export declare const contentWriteContract: (make: () => ContentWriteFixture | Promise<ContentWriteFixture>) => ContractCase[];
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { isCliError } from "@wirecat/cli-base";
|
|
3
|
+
let operations = 0;
|
|
4
|
+
const operation = (version) => {
|
|
5
|
+
operations += 1;
|
|
6
|
+
return { operationId: `contract-${Date.now()}-${operations}`, ...(version === undefined ? {} : { version }) };
|
|
7
|
+
};
|
|
8
|
+
const refusedWith = async (check, act) => {
|
|
9
|
+
try {
|
|
10
|
+
await act();
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
assert.ok(isCliError(error), `a CliError, not ${String(error)}`);
|
|
14
|
+
check(error);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
assert.fail("expected a refusal");
|
|
18
|
+
};
|
|
19
|
+
/** What every content writer does, whatever the source: run each case in the adapter's tests. */
|
|
20
|
+
export const contentWriteContract = (make) => {
|
|
21
|
+
const drafted = async () => {
|
|
22
|
+
const fixture = await make();
|
|
23
|
+
const made = await fixture.writer.createDraft({ title: "Example contract post", body: "A *contract* body.\n\n- one\n- two" }, operation());
|
|
24
|
+
return { ...fixture, made };
|
|
25
|
+
};
|
|
26
|
+
return [
|
|
27
|
+
{
|
|
28
|
+
name: "createDraft answers a draft with its new id, never a published post",
|
|
29
|
+
async run() {
|
|
30
|
+
const { made } = await drafted();
|
|
31
|
+
assert.ok(made.externalId, "a new id");
|
|
32
|
+
assert.equal(made.status, "draft");
|
|
33
|
+
assert.equal(made.title, "Example contract post");
|
|
34
|
+
assert.match(made.body ?? "", /contract/);
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: "update of one field answers the whole post, every other field as it was",
|
|
39
|
+
async run() {
|
|
40
|
+
const { writer, made } = await drafted();
|
|
41
|
+
const updated = await writer.update(made.externalId, { title: "Renamed" }, operation(made.version));
|
|
42
|
+
assert.equal(updated.title, "Renamed");
|
|
43
|
+
assert.equal(updated.body, made.body);
|
|
44
|
+
assert.equal(updated.status, "draft");
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "a write with an old version is refused as content.conflict, and the post stays as it was",
|
|
49
|
+
async run() {
|
|
50
|
+
const { writer, made, changeElsewhere } = await drafted();
|
|
51
|
+
if (!changeElsewhere)
|
|
52
|
+
return;
|
|
53
|
+
await changeElsewhere(made.externalId);
|
|
54
|
+
await refusedWith((error) => assert.equal(error.details.reason, "content.conflict"), () => writer.update(made.externalId, { title: "Overwritten" }, operation(made.version)));
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "publish publishes once: a second publish answers the post as it is and sends nothing out again",
|
|
59
|
+
async run() {
|
|
60
|
+
const { writer, made, requests } = await drafted();
|
|
61
|
+
const published = await writer.publish(made.externalId, operation(made.version));
|
|
62
|
+
assert.equal(published.status, "published");
|
|
63
|
+
const before = requests();
|
|
64
|
+
const again = await writer.publish(made.externalId, operation(published.version));
|
|
65
|
+
assert.equal(again.status, "published");
|
|
66
|
+
assert.ok(requests() - before <= 1, "a repeated publish makes at most a read");
|
|
67
|
+
assert.equal(again.publishedAt, published.publishedAt, "the first publish time stays");
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "schedule answers a scheduled post with its time; unpublish brings it back to a draft",
|
|
72
|
+
async run() {
|
|
73
|
+
const { writer, made } = await drafted();
|
|
74
|
+
const at = Date.UTC(2030, 0, 1, 9);
|
|
75
|
+
const scheduled = await writer.schedule(made.externalId, at, operation(made.version));
|
|
76
|
+
assert.equal(scheduled.status, "scheduled");
|
|
77
|
+
assert.equal(scheduled.scheduledFor, at);
|
|
78
|
+
const back = await writer.unpublish(made.externalId, operation(scheduled.version));
|
|
79
|
+
assert.equal(back.status, "draft");
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: "trash keeps the post, trashed; force deletes it for good",
|
|
84
|
+
async run() {
|
|
85
|
+
const { writer, made } = await drafted();
|
|
86
|
+
const trashed = await writer.trash(made.externalId, operation(made.version));
|
|
87
|
+
assert.equal(trashed?.status, "trashed");
|
|
88
|
+
assert.equal(await writer.trash(made.externalId, { ...operation(trashed?.version), force: true }), null);
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: "a lost answer is outcome_unknown after one request, never retried",
|
|
93
|
+
async run() {
|
|
94
|
+
const { writer, made, loseNextAnswer, requests } = await drafted();
|
|
95
|
+
loseNextAnswer();
|
|
96
|
+
const before = requests();
|
|
97
|
+
await refusedWith((error) => assert.equal(error.code, "outcome_unknown"), () => writer.update(made.externalId, { title: "Lost" }, operation(made.version)));
|
|
98
|
+
assert.equal(requests() - before, 1, "one request, not a retry");
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import type { ConditionalKey } from "../cli/permission-keys.js";
|
|
3
|
+
import type { ContentPost, PostType } from "../domain/content.js";
|
|
4
|
+
import type { Level, PermissionKey } from "../sends/permissions.js";
|
|
5
|
+
/** A post's fields as a write sends them; `null` clears one. The body is Markdown. */
|
|
6
|
+
export interface ContentFields {
|
|
7
|
+
title?: string | null;
|
|
8
|
+
body?: string;
|
|
9
|
+
excerpt?: string | null;
|
|
10
|
+
slug?: string | null;
|
|
11
|
+
/** Terms' ids at the source; replaces the whole list. */
|
|
12
|
+
terms?: string[];
|
|
13
|
+
}
|
|
14
|
+
export type ContentDraft = ContentFields & {
|
|
15
|
+
type?: PostType;
|
|
16
|
+
channel?: string;
|
|
17
|
+
};
|
|
18
|
+
export interface ContentWriteOptions {
|
|
19
|
+
/** Journaled before the call. */
|
|
20
|
+
operationId: string;
|
|
21
|
+
/**
|
|
22
|
+
* The version the copy holds. A source whose post has another one now refuses with `content.conflict`: someone
|
|
23
|
+
* changed it since the last sync, and the write would overwrite that.
|
|
24
|
+
*/
|
|
25
|
+
version?: string | null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Writes to one content account. Each answers the whole post as the source holds it after the write, which the store
|
|
29
|
+
* saves over its copy. None of them retries: a write whose answer was lost throws `outcome_unknown`.
|
|
30
|
+
*/
|
|
31
|
+
export interface ContentWriter {
|
|
32
|
+
readonly provider: string;
|
|
33
|
+
/** Always a draft: publishing is its own step, with its own key. */
|
|
34
|
+
createDraft(draft: ContentDraft, options: ContentWriteOptions): Promise<ContentPost>;
|
|
35
|
+
update(externalId: string, patch: ContentFields, options: ContentWriteOptions): Promise<ContentPost>;
|
|
36
|
+
/** A post already published answers as it is: a second publish never sends it out again. */
|
|
37
|
+
publish(externalId: string, options: ContentWriteOptions): Promise<ContentPost>;
|
|
38
|
+
/** At a time in the future; a past time would publish at once. */
|
|
39
|
+
schedule(externalId: string, at: number, options: ContentWriteOptions): Promise<ContentPost>;
|
|
40
|
+
/** Back to a draft. */
|
|
41
|
+
unpublish(externalId: string, options: ContentWriteOptions): Promise<ContentPost>;
|
|
42
|
+
/** Into the source's trash, from which it comes back; `force` deletes it for good. */
|
|
43
|
+
trash(externalId: string, options: ContentWriteOptions & {
|
|
44
|
+
force?: boolean;
|
|
45
|
+
}): Promise<ContentPost | null>;
|
|
46
|
+
}
|
|
47
|
+
/** What a writer throws when the post changed at the source since the copy's version. */
|
|
48
|
+
export declare const contentConflict: (message?: string) => CliError;
|
|
49
|
+
/** The key publishing checks, the same the `api` group checks for a `status` that publishes (cli-core #3113). */
|
|
50
|
+
export declare const POSTS_PUBLISH_KEY: PermissionKey;
|
|
51
|
+
/** Deleting for good, past the trash. */
|
|
52
|
+
export declare const POSTS_DELETE_FORCE_KEY: PermissionKey;
|
|
53
|
+
/** The keys the content writes check beyond their command paths, for `commandPermissions({ extra })`. */
|
|
54
|
+
export declare const CONTENT_WRITE_KEYS: readonly PermissionKey[];
|
|
55
|
+
/**
|
|
56
|
+
* Built in, and only ever tightening (WC-PERM-7). Every write asks: a draft is not public, but publishing is, and the
|
|
57
|
+
* owner chose `ask` for it too (NEED-2720 B). A deletion for good asks as well.
|
|
58
|
+
*/
|
|
59
|
+
export declare const CONTENT_WRITE_DEFAULTS: Readonly<Record<PermissionKey, Level>>;
|
|
60
|
+
/**
|
|
61
|
+
* Every key a content write command may check beyond its own path, for `commandPermissions({ alsoChecks })`, so its
|
|
62
|
+
* `--dry-run` names each.
|
|
63
|
+
*/
|
|
64
|
+
export declare const contentAlsoChecks: (path: readonly string[]) => readonly (PermissionKey | ConditionalKey)[];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
/** What a writer throws when the post changed at the source since the copy's version. */
|
|
3
|
+
export const contentConflict = (message = "the post changed at the source since the last sync") => new CliError("validation_error", `${message}; sync and try again`, { reason: "content.conflict" });
|
|
4
|
+
/** The key publishing checks, the same the `api` group checks for a `status` that publishes (cli-core #3113). */
|
|
5
|
+
export const POSTS_PUBLISH_KEY = "posts.publish";
|
|
6
|
+
/** Deleting for good, past the trash. */
|
|
7
|
+
export const POSTS_DELETE_FORCE_KEY = "posts.delete.force";
|
|
8
|
+
/** The keys the content writes check beyond their command paths, for `commandPermissions({ extra })`. */
|
|
9
|
+
export const CONTENT_WRITE_KEYS = [POSTS_PUBLISH_KEY, POSTS_DELETE_FORCE_KEY];
|
|
10
|
+
/**
|
|
11
|
+
* Built in, and only ever tightening (WC-PERM-7). Every write asks: a draft is not public, but publishing is, and the
|
|
12
|
+
* owner chose `ask` for it too (NEED-2720 B). A deletion for good asks as well.
|
|
13
|
+
*/
|
|
14
|
+
export const CONTENT_WRITE_DEFAULTS = {
|
|
15
|
+
"posts.create": "ask",
|
|
16
|
+
"posts.update": "ask",
|
|
17
|
+
[POSTS_PUBLISH_KEY]: "ask",
|
|
18
|
+
"posts.schedule": "ask",
|
|
19
|
+
"posts.unpublish": "ask",
|
|
20
|
+
"posts.delete": "ask",
|
|
21
|
+
[POSTS_DELETE_FORCE_KEY]: "ask",
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Every key a content write command may check beyond its own path, for `commandPermissions({ alsoChecks })`, so its
|
|
25
|
+
* `--dry-run` names each.
|
|
26
|
+
*/
|
|
27
|
+
export const contentAlsoChecks = (path) => {
|
|
28
|
+
const [noun, command] = path;
|
|
29
|
+
if (path.length !== 2 || noun !== "posts")
|
|
30
|
+
return [];
|
|
31
|
+
if (command === "schedule")
|
|
32
|
+
return [POSTS_PUBLISH_KEY];
|
|
33
|
+
if (command === "update")
|
|
34
|
+
return [{ key: POSTS_PUBLISH_KEY, when: "the post is published or scheduled" }];
|
|
35
|
+
if (command === "delete")
|
|
36
|
+
return [{ key: POSTS_DELETE_FORCE_KEY, when: "--force" }];
|
|
37
|
+
return [];
|
|
38
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ContentPost } from "../domain/content.js";
|
|
2
|
+
import type { AppIdentity } from "../ports/app.js";
|
|
3
|
+
import type { Warn } from "../runs/events.js";
|
|
4
|
+
import { type Asker, type GuardSettings } from "../sends/guard.js";
|
|
5
|
+
import type { MessageStore } from "../store/store.js";
|
|
6
|
+
import type { AccountKey } from "../store/types.js";
|
|
7
|
+
import type { ContentSource } from "./source.js";
|
|
8
|
+
import { type ContentDraft, type ContentFields, type ContentWriter } from "./writer.js";
|
|
9
|
+
/** What a CLI hands the content writes for one profile. */
|
|
10
|
+
export interface ContentWrites {
|
|
11
|
+
writer: ContentWriter;
|
|
12
|
+
app: AppIdentity;
|
|
13
|
+
/** The profile's levels, read on every write so a config change applies at once. */
|
|
14
|
+
settings: () => Omit<GuardSettings, "sendsPerHour"> & Partial<Pick<GuardSettings, "sendsPerHour">>;
|
|
15
|
+
env?: NodeJS.ProcessEnv;
|
|
16
|
+
warn?: Warn;
|
|
17
|
+
}
|
|
18
|
+
export interface ContentWriteContext {
|
|
19
|
+
store: MessageStore;
|
|
20
|
+
account: AccountKey;
|
|
21
|
+
writes: ContentWrites;
|
|
22
|
+
/** A terminal's question, or an agent's go-ahead. */
|
|
23
|
+
ask: Asker;
|
|
24
|
+
/**
|
|
25
|
+
* Reads the post again before a write, so the keys follow the source's status and not a stale copy: a scheduled post
|
|
26
|
+
* whose time passed is published by any edit, and a copy may still call it scheduled.
|
|
27
|
+
*/
|
|
28
|
+
source?: () => ContentSource;
|
|
29
|
+
now?: () => number;
|
|
30
|
+
}
|
|
31
|
+
export interface ContentWritten {
|
|
32
|
+
operationId: string;
|
|
33
|
+
post: ContentPost;
|
|
34
|
+
/** The write was not sent: the post already stood that way. */
|
|
35
|
+
unchanged?: true;
|
|
36
|
+
}
|
|
37
|
+
/** A new draft. Publishing is its own step. */
|
|
38
|
+
export declare const createPost: (context: ContentWriteContext, draft: ContentDraft) => Promise<ContentWritten>;
|
|
39
|
+
/**
|
|
40
|
+
* Changes a post's fields. A change to a published or scheduled post is public, so it also checks `posts.publish`. A
|
|
41
|
+
* body written over a post whose markup Markdown cannot keep needs `replaceBody`: it would flatten the blocks.
|
|
42
|
+
*/
|
|
43
|
+
export declare const updatePost: (context: ContentWriteContext, externalId: string, fields: ContentFields, options?: {
|
|
44
|
+
replaceBody?: boolean;
|
|
45
|
+
}) => Promise<ContentWritten>;
|
|
46
|
+
/** Publishes now. A post the copy holds as published is answered as it is, and nothing is sent. */
|
|
47
|
+
export declare const publishPost: (context: ContentWriteContext, externalId: string) => Promise<ContentWritten>;
|
|
48
|
+
/** Publishes at `at`, which must be ahead: a source publishes a post dated in the past at once. */
|
|
49
|
+
export declare const schedulePost: (context: ContentWriteContext, externalId: string, at: number) => Promise<ContentWritten>;
|
|
50
|
+
export declare const unpublishPost: (context: ContentWriteContext, externalId: string) => Promise<ContentWritten>;
|
|
51
|
+
/** Into the trash; `force` deletes for good and checks its own key. The copy keeps the last text as a revision. */
|
|
52
|
+
export declare const deletePost: (context: ContentWriteContext, externalId: string, options?: {
|
|
53
|
+
force?: boolean;
|
|
54
|
+
}) => Promise<{
|
|
55
|
+
operationId: string;
|
|
56
|
+
post: ContentPost | null;
|
|
57
|
+
deleted: boolean;
|
|
58
|
+
}>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { CliError, isCliError } from "@wirecat/cli-base";
|
|
2
|
+
import { guardFor } from "../sends/guard.js";
|
|
3
|
+
import { guardedWrite } from "../sends/guarded.js";
|
|
4
|
+
import { keyedGuard } from "../sends/keyed-guard.js";
|
|
5
|
+
import { DEFAULT_PERMISSIONS, levelFor } from "../sends/permissions.js";
|
|
6
|
+
import { newOperationId } from "../sends/send-id.js";
|
|
7
|
+
import { CONTENT_WRITE_DEFAULTS, contentConflict, POSTS_DELETE_FORCE_KEY, POSTS_PUBLISH_KEY, } from "./writer.js";
|
|
8
|
+
const PUBLIC = new Set(["published", "scheduled"]);
|
|
9
|
+
const guardOf = (context, extra) => {
|
|
10
|
+
const settings = context.writes.settings();
|
|
11
|
+
const defaults = { ...DEFAULT_PERMISSIONS, ...CONTENT_WRITE_DEFAULTS, ...settings.permissionDefaults };
|
|
12
|
+
return keyedGuard((ask) => guardFor(context.writes.app, { ...settings, sendsPerHour: Number.MAX_SAFE_INTEGER, permissionDefaults: defaults }, context.writes.warn ?? (() => { }), context.writes.env, ask), context.ask, (key) => levelFor(settings.permissions, key, defaults).level, extra);
|
|
13
|
+
};
|
|
14
|
+
/** Checked and journaled once; a lost answer is `outcome_unknown`, and the owner syncs before writing again. */
|
|
15
|
+
const written = async (context, key, extra, operationId, act) => {
|
|
16
|
+
try {
|
|
17
|
+
return await guardedWrite(guardOf(context, extra), { chatId: null, kind: "edit", key, operationId }, act);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
if (!isCliError(error) || error.code !== "outcome_unknown")
|
|
21
|
+
throw error;
|
|
22
|
+
throw new CliError("outcome_unknown", `${error.message} — sync and look at the post before writing again: a repeat may write it twice`, { ...error.details, operation_id: operationId });
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The post as the copy holds it, read again from the source first when it can be: the keys then follow its status
|
|
27
|
+
* there. A post changed at the source since the copy's version is refused, its new state kept for the owner to look at.
|
|
28
|
+
*/
|
|
29
|
+
const copyOf = async (context, externalId) => {
|
|
30
|
+
const stored = await context.store.content.get(context.account, "post", externalId);
|
|
31
|
+
if (context.source && stored?.deletedAt === null) {
|
|
32
|
+
const now = context.now?.() ?? Date.now();
|
|
33
|
+
const live = await context.source().get("post", externalId);
|
|
34
|
+
if (live)
|
|
35
|
+
await context.store.content.save(context.account, "post", [live], now);
|
|
36
|
+
else
|
|
37
|
+
await context.store.content.markDeleted(context.account, "post", externalId, now);
|
|
38
|
+
const was = stored.record.version;
|
|
39
|
+
if (live && was != null && live.version != null && live.version !== was)
|
|
40
|
+
throw contentConflict();
|
|
41
|
+
}
|
|
42
|
+
const found = await context.store.content.get(context.account, "post", externalId);
|
|
43
|
+
if (!found || found.deletedAt !== null)
|
|
44
|
+
throw new CliError("not_found", `no post ${externalId} in this profile's copy; run sync, then try again`);
|
|
45
|
+
return found;
|
|
46
|
+
};
|
|
47
|
+
/** The source's answer into the copy; what it replaced stays as a revision. */
|
|
48
|
+
const kept = async (context, post, operationId) => {
|
|
49
|
+
try {
|
|
50
|
+
await context.store.content.save(context.account, "post", [post], context.now?.() ?? Date.now(), {
|
|
51
|
+
reason: "write",
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
throw new CliError("invalid_response", `the source wrote post ${post.externalId}, but the local store did not keep it ` +
|
|
56
|
+
`(${error instanceof Error ? error.message : String(error)}); sync to store it — do not write it again`, { operation_id: operationId, external_id: post.externalId, sent: true });
|
|
57
|
+
}
|
|
58
|
+
return { operationId, post };
|
|
59
|
+
};
|
|
60
|
+
const blank = (fields) => Object.values(fields).every((one) => one === undefined);
|
|
61
|
+
/** A new draft. Publishing is its own step. */
|
|
62
|
+
export const createPost = async (context, draft) => {
|
|
63
|
+
if (!draft.title?.trim() && !draft.body?.trim())
|
|
64
|
+
throw new CliError("validation_error", "a new post needs a title or a body", { reason: "content.empty" });
|
|
65
|
+
const operationId = newOperationId();
|
|
66
|
+
const post = await written(context, "posts.create", [], operationId, () => context.writes.writer.createDraft(draft, { operationId }));
|
|
67
|
+
return kept(context, post, operationId);
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Changes a post's fields. A change to a published or scheduled post is public, so it also checks `posts.publish`. A
|
|
71
|
+
* body written over a post whose markup Markdown cannot keep needs `replaceBody`: it would flatten the blocks.
|
|
72
|
+
*/
|
|
73
|
+
export const updatePost = async (context, externalId, fields, options = {}) => {
|
|
74
|
+
if (blank(fields))
|
|
75
|
+
throw new CliError("validation_error", "nothing to change: name a field", { reason: "content.empty" });
|
|
76
|
+
const copy = await copyOf(context, externalId);
|
|
77
|
+
if (fields.body !== undefined && copy.record.lossy && !options.replaceBody)
|
|
78
|
+
throw new CliError("validation_error", `post ${externalId} holds blocks or embeds that Markdown cannot keep; writing its body would flatten them. ` +
|
|
79
|
+
"Change only its title, excerpt or tags, or add --replace-body to replace the body anyway", { reason: "content.lossy-body", param: "--replace-body" });
|
|
80
|
+
const operationId = newOperationId();
|
|
81
|
+
const extra = PUBLIC.has(copy.record.status) ? [POSTS_PUBLISH_KEY] : [];
|
|
82
|
+
const post = await written(context, "posts.update", extra, operationId, () => context.writes.writer.update(externalId, fields, { operationId, version: copy.record.version ?? null }));
|
|
83
|
+
return kept(context, post, operationId);
|
|
84
|
+
};
|
|
85
|
+
/** Publishes now. A post the copy holds as published is answered as it is, and nothing is sent. */
|
|
86
|
+
export const publishPost = async (context, externalId) => {
|
|
87
|
+
const copy = await copyOf(context, externalId);
|
|
88
|
+
if (copy.record.status === "published")
|
|
89
|
+
return { operationId: "", post: copy.record, unchanged: true };
|
|
90
|
+
const operationId = newOperationId();
|
|
91
|
+
const post = await written(context, POSTS_PUBLISH_KEY, [], operationId, () => context.writes.writer.publish(externalId, { operationId, version: copy.record.version ?? null }));
|
|
92
|
+
return kept(context, post, operationId);
|
|
93
|
+
};
|
|
94
|
+
/** Publishes at `at`, which must be ahead: a source publishes a post dated in the past at once. */
|
|
95
|
+
export const schedulePost = async (context, externalId, at) => {
|
|
96
|
+
const now = context.now?.() ?? Date.now();
|
|
97
|
+
if (!Number.isFinite(at) || at <= now)
|
|
98
|
+
throw new CliError("validation_error", "--at is in the past, and the source would publish the post at once; give a later time, or use posts publish", { reason: "content.schedule-past", param: "--at" });
|
|
99
|
+
const copy = await copyOf(context, externalId);
|
|
100
|
+
if (copy.record.status === "published")
|
|
101
|
+
throw new CliError("validation_error", `post ${externalId} is already published`, {
|
|
102
|
+
reason: "content.already-published",
|
|
103
|
+
});
|
|
104
|
+
const operationId = newOperationId();
|
|
105
|
+
const post = await written(context, "posts.schedule", [POSTS_PUBLISH_KEY], operationId, () => context.writes.writer.schedule(externalId, at, { operationId, version: copy.record.version ?? null }));
|
|
106
|
+
return kept(context, post, operationId);
|
|
107
|
+
};
|
|
108
|
+
export const unpublishPost = async (context, externalId) => {
|
|
109
|
+
const copy = await copyOf(context, externalId);
|
|
110
|
+
if (copy.record.status === "draft")
|
|
111
|
+
return { operationId: "", post: copy.record, unchanged: true };
|
|
112
|
+
const operationId = newOperationId();
|
|
113
|
+
const post = await written(context, "posts.unpublish", [], operationId, () => context.writes.writer.unpublish(externalId, { operationId, version: copy.record.version ?? null }));
|
|
114
|
+
return kept(context, post, operationId);
|
|
115
|
+
};
|
|
116
|
+
/** Into the trash; `force` deletes for good and checks its own key. The copy keeps the last text as a revision. */
|
|
117
|
+
export const deletePost = async (context, externalId, options = {}) => {
|
|
118
|
+
const copy = await copyOf(context, externalId);
|
|
119
|
+
const operationId = newOperationId();
|
|
120
|
+
const post = await written(context, "posts.delete", options.force ? [POSTS_DELETE_FORCE_KEY] : [], operationId, () => context.writes.writer.trash(externalId, {
|
|
121
|
+
operationId,
|
|
122
|
+
version: copy.record.version ?? null,
|
|
123
|
+
...(options.force ? { force: true } : {}),
|
|
124
|
+
}));
|
|
125
|
+
if (post && !options.force)
|
|
126
|
+
return { ...(await kept(context, post, operationId)), deleted: false };
|
|
127
|
+
await context.store.content.markDeleted(context.account, "post", externalId, context.now?.() ?? Date.now());
|
|
128
|
+
return { operationId, post: null, deleted: true };
|
|
129
|
+
};
|
|
@@ -74,11 +74,13 @@ export interface FileMirrorState {
|
|
|
74
74
|
entries: Record<string, FileEntry>;
|
|
75
75
|
/**
|
|
76
76
|
* Saved before each write. After a crash the next listing settles the write (same content on both sides is
|
|
77
|
-
* adopted); this only names the temporary file to remove and the operation to look up.
|
|
77
|
+
* adopted); this only names the temporary file to remove and the operation to look up. `created` is false for a
|
|
78
|
+
* download that replaces a file, which is renamed into place whole and so never left partial.
|
|
78
79
|
*/
|
|
79
80
|
pending: Record<string, {
|
|
80
81
|
op: FileStepKind;
|
|
81
82
|
operationId: string;
|
|
83
|
+
created?: boolean;
|
|
82
84
|
}>;
|
|
83
85
|
}
|
|
84
86
|
export interface FileMirrorStatePort {
|
|
@@ -5,12 +5,12 @@ export { documentIngestion, type IngestionOptions } from "../services/documents/
|
|
|
5
5
|
export { type DocumentKnowledge, documentKnowledge } from "../services/documents/knowledge.js";
|
|
6
6
|
export { blocksMarkdown, inlineMarkdown, spliceMarkdown } from "../services/documents/markdown-splice.js";
|
|
7
7
|
export { type MirrorLocalCheck, type MirrorTrigger, type MirrorWatchEvent, type MirrorWatchOptions, watchMirrors, } from "../services/documents/mirror-watch.js";
|
|
8
|
-
export { conflictReport, fileMirrorState, type Mirror, type MirrorSyncOptions, mirrorId, mirrorStatus, syncMirrors, } from "../services/documents/mirrors.js";
|
|
8
|
+
export { conflictReport, fileMirrorState, type HeldLock, type Mirror, type MirrorSyncOptions, mirrorId, mirrorStatus, syncMirrors, } from "../services/documents/mirrors.js";
|
|
9
9
|
export type { SyncOptions } from "../services/documents/ports.js";
|
|
10
10
|
export { extractDocumentMemories, type ImportedSource, type ProposalOptions } from "../services/documents/proposals.js";
|
|
11
|
-
export { type MirrorAction, type PushConflict, type PushOptions, type PushResult, planPush, pushDocuments, scanLocal, } from "../services/documents/push.js";
|
|
11
|
+
export { type MirrorAction, type PushConflict, type PushOptions, type PushResult, planPush, pushDocuments, scanLocal, skippedName, } from "../services/documents/push.js";
|
|
12
12
|
export { type SyncResult, syncDocuments } from "../services/documents/sync.js";
|
|
13
|
-
export { type FileMirrorOptions, type FileMirrorResult, type FileMirrorSyncOptions, type FileStep, fileMirrorStatus, fileRemoteChanges, fileSyncState, syncFileMirror, syncFileMirrors, } from "../services/files/mirror.js";
|
|
13
|
+
export { type FileMirrorOptions, type FileMirrorResult, type FileMirrorSyncOptions, type FileStep, fileMirrorStatus, fileRemoteChanges, fileSyncState, type MirrorHints, syncFileMirror, syncFileMirrors, } from "../services/files/mirror.js";
|
|
14
14
|
export { type FileVersion, type FileVersions, fileVersions, restoreFileVersion, type VersionDirection, type VersionEvent, } from "../services/files/versions.js";
|
|
15
15
|
export { type Block, blockKey, blockShape, type Inline, type ListKind, normalizeInlines } from "./blocks.js";
|
|
16
16
|
export { catalogFiles } from "./catalog.js";
|
package/dist/documents/index.js
CHANGED
|
@@ -7,7 +7,7 @@ export { blocksMarkdown, inlineMarkdown, spliceMarkdown } from "../services/docu
|
|
|
7
7
|
export { watchMirrors, } from "../services/documents/mirror-watch.js";
|
|
8
8
|
export { conflictReport, fileMirrorState, mirrorId, mirrorStatus, syncMirrors, } from "../services/documents/mirrors.js";
|
|
9
9
|
export { extractDocumentMemories } from "../services/documents/proposals.js";
|
|
10
|
-
export { planPush, pushDocuments, scanLocal, } from "../services/documents/push.js";
|
|
10
|
+
export { planPush, pushDocuments, scanLocal, skippedName, } from "../services/documents/push.js";
|
|
11
11
|
export { syncDocuments } from "../services/documents/sync.js";
|
|
12
12
|
export { fileMirrorStatus, fileRemoteChanges, fileSyncState, syncFileMirror, syncFileMirrors, } from "../services/files/mirror.js";
|
|
13
13
|
export { fileVersions, restoreFileVersion, } from "../services/files/versions.js";
|