@crombolo/content 0.76.0 → 0.77.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/package.json +3 -3
- package/src/actor.ts +4 -0
- package/src/events.ts +20 -6
- package/src/operations.ts +27 -4
- package/src/workspace-merge-run.ts +195 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/content",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.77.0",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. The rules every page, media and Library write follows, whoever starts it: who may, where it may go, approvals, and what it announces. No transport.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@crombolo/core": "0.
|
|
33
|
-
"@crombolo/data": "0.
|
|
32
|
+
"@crombolo/core": "0.77.0",
|
|
33
|
+
"@crombolo/data": "0.77.0"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/src/actor.ts
CHANGED
|
@@ -57,6 +57,8 @@ export type Actor =
|
|
|
57
57
|
user?: { id: string; name: string }
|
|
58
58
|
/** Said with what it did, in notifications and the audit: `approved through "Legal"`. */
|
|
59
59
|
note?: string
|
|
60
|
+
/** Story 414: the change whose schedule it carries out. */
|
|
61
|
+
change?: { key: string; name: string | null }
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
/**
|
|
@@ -112,6 +114,7 @@ export const systemActor = (
|
|
|
112
114
|
source?: ActorOrigin['source']
|
|
113
115
|
user?: { id: string; name: string }
|
|
114
116
|
note?: string
|
|
117
|
+
change?: { key: string; name: string | null }
|
|
115
118
|
} = {},
|
|
116
119
|
): Actor => ({
|
|
117
120
|
kind: 'system',
|
|
@@ -119,6 +122,7 @@ export const systemActor = (
|
|
|
119
122
|
origin: { source: options.source ?? 'job' },
|
|
120
123
|
user: options.user,
|
|
121
124
|
note: options.note,
|
|
125
|
+
...(options.change ? { change: options.change } : {}),
|
|
122
126
|
})
|
|
123
127
|
|
|
124
128
|
/** The name an audit entry or a notification gives the actor. */
|
package/src/events.ts
CHANGED
|
@@ -20,14 +20,18 @@ export type DocumentEvent =
|
|
|
20
20
|
| { type: 'branchPublished'; key: string; published: readonly string[] }
|
|
21
21
|
| { type: 'unpublished'; key: string; cultures: readonly string[] | null }
|
|
22
22
|
| { type: 'trashed'; key: string }
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* `name` is read before it went, since it cannot be read after. `was`: the page as it
|
|
25
|
+
* stood, when it was deleted from outside the recycle bin (story 411).
|
|
26
|
+
*/
|
|
27
|
+
| { type: 'deleted'; key: string; name: string | null; was?: object }
|
|
25
28
|
| { type: 'restored'; key: string }
|
|
26
29
|
| { type: 'binEmptied' }
|
|
27
|
-
|
|
30
|
+
/** `from`: where it was before, as its change carries it (story 411). */
|
|
31
|
+
| { type: 'moved'; key: string; from?: { parentKey: string | null; trashed: boolean } }
|
|
28
32
|
| { type: 'copied'; key: string; copyKey: string }
|
|
29
|
-
/** `parentKey` null: the top level. */
|
|
30
|
-
| { type: 'sorted'; parentKey: string | null }
|
|
33
|
+
/** `parentKey` null: the top level. `before`: the children's order before (story 411). */
|
|
34
|
+
| { type: 'sorted'; parentKey: string | null; before?: readonly string[] }
|
|
31
35
|
| { type: 'rolledBack'; key: string; versionId: string }
|
|
32
36
|
| { type: 'domainsAssigned'; key: string }
|
|
33
37
|
| { type: 'publicAccessChanged'; key: string; change: 'added' | 'set' | 'removed' }
|
|
@@ -42,7 +46,17 @@ export interface EventSink {
|
|
|
42
46
|
record(tx: Db, event: DocumentEvent, actor: Actor): Promise<void>
|
|
43
47
|
afterCommit?(event: DocumentEvent, actor: Actor): void
|
|
44
48
|
/** Story 397: a publish in a person's own workspace merged its page, recorded as `changeId`. */
|
|
45
|
-
ownPublished?(
|
|
49
|
+
ownPublished?(
|
|
50
|
+
tx: Db,
|
|
51
|
+
published: {
|
|
52
|
+
changeId: string
|
|
53
|
+
key: string
|
|
54
|
+
/** Story 411: what the page's merge did, other than a publish. */
|
|
55
|
+
kind?: 'unpublish' | 'move' | 'reorder' | 'delete'
|
|
56
|
+
page?: { key: string; name: string | null }
|
|
57
|
+
},
|
|
58
|
+
actor: Actor,
|
|
59
|
+
): Promise<void>
|
|
46
60
|
}
|
|
47
61
|
|
|
48
62
|
/** A sink that hears nothing: for tests and tools that announce nothing. */
|
package/src/operations.ts
CHANGED
|
@@ -464,6 +464,16 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
|
|
|
464
464
|
try {
|
|
465
465
|
const befores: unknown[] = []
|
|
466
466
|
for (const node of writes) befores.push(await options.redirects?.capture(node.key))
|
|
467
|
+
// Story 411: where each was, for its change to carry the move.
|
|
468
|
+
const from = new Map<string, { parentKey: string | null; trashed: boolean }>()
|
|
469
|
+
for (const node of writes)
|
|
470
|
+
from.set(node.key, {
|
|
471
|
+
parentKey:
|
|
472
|
+
node.parentId === SystemNodes.Root
|
|
473
|
+
? null
|
|
474
|
+
: ((await nodes.byId(node.parentId))?.key ?? null),
|
|
475
|
+
trashed: node.trashed,
|
|
476
|
+
})
|
|
467
477
|
const done = await commit(
|
|
468
478
|
actor,
|
|
469
479
|
async (tx) => {
|
|
@@ -472,7 +482,11 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
|
|
|
472
482
|
if ((await tx.move(node.key, targetKey)) === 'moved') out.push(node.key)
|
|
473
483
|
return out
|
|
474
484
|
},
|
|
475
|
-
(moved) =>
|
|
485
|
+
(moved) =>
|
|
486
|
+
moved.map((key): DocumentEvent => {
|
|
487
|
+
const was = from.get(key)
|
|
488
|
+
return was ? { type: 'moved', key, from: was } : { type: 'moved', key }
|
|
489
|
+
}),
|
|
476
490
|
)
|
|
477
491
|
changed()
|
|
478
492
|
for (const before of befores) await options.redirects?.commit(before)
|
|
@@ -482,6 +496,10 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
|
|
|
482
496
|
}
|
|
483
497
|
}
|
|
484
498
|
|
|
499
|
+
/** The keys of a parent's children in their order (story 411): what a reorder changes. */
|
|
500
|
+
const childOrder = async (parentKey: string | null): Promise<string[]> =>
|
|
501
|
+
(await repo.children(parentKey, 0, 10_000)).items.map((n) => normaliseUuid(n.key))
|
|
502
|
+
|
|
485
503
|
/**
|
|
486
504
|
* Moves pages to the recycle bin in one transaction, every one checked first.
|
|
487
505
|
* One already there is unchanged; one below another in the selection goes
|
|
@@ -979,10 +997,13 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
|
|
|
979
997
|
if (options_.fromBin && !node.trashed)
|
|
980
998
|
return refuse('invalid', 400, 'The document is not in the recycle bin')
|
|
981
999
|
if (!(await permits(actor, 'delete', options_.fromBin ? 'bin' : key))) return forbidden()
|
|
1000
|
+
// Story 411: one deleted from outside the bin, as it stood, for its change to carry.
|
|
1001
|
+
const was = node.trashed ? undefined : await repo.byKey(key)
|
|
982
1002
|
const removed = await commit(
|
|
983
1003
|
actor,
|
|
984
1004
|
(tx) => tx.delete(key),
|
|
985
|
-
(done)
|
|
1005
|
+
(done): DocumentEvent[] =>
|
|
1006
|
+
done ? [{ type: 'deleted', key, name: node.text ?? null, ...(was ? { was } : {}) }] : [],
|
|
986
1007
|
)
|
|
987
1008
|
if (!removed) return refuse('notFound', 404, NOT_FOUND)
|
|
988
1009
|
changed()
|
|
@@ -1110,10 +1131,11 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
|
|
|
1110
1131
|
}
|
|
1111
1132
|
try {
|
|
1112
1133
|
const before = await options.redirects?.captureChildren(parentKey)
|
|
1134
|
+
const order = await childOrder(parentKey)
|
|
1113
1135
|
const result = await commit(
|
|
1114
1136
|
actor,
|
|
1115
1137
|
(tx) => tx.sort(parentKey, sorting),
|
|
1116
|
-
(done) => (done === 'notFound' ? [] : [{ type: 'sorted', parentKey }]),
|
|
1138
|
+
(done) => (done === 'notFound' ? [] : [{ type: 'sorted', parentKey, before: order }]),
|
|
1117
1139
|
)
|
|
1118
1140
|
if (result === 'notFound')
|
|
1119
1141
|
return refuse(
|
|
@@ -1138,10 +1160,11 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
|
|
|
1138
1160
|
if (!(await permits(actor, 'sort', parentKey ?? 'root'))) return forbidden()
|
|
1139
1161
|
try {
|
|
1140
1162
|
const before = await options.redirects?.captureChildren(parentKey)
|
|
1163
|
+
const order = await childOrder(parentKey)
|
|
1141
1164
|
const result = await commit(
|
|
1142
1165
|
actor,
|
|
1143
1166
|
(tx) => tx.sortChildrenBy(parentKey, field, direction),
|
|
1144
|
-
(done) => (done === 'notFound' ? [] : [{ type: 'sorted', parentKey }]),
|
|
1167
|
+
(done) => (done === 'notFound' ? [] : [{ type: 'sorted', parentKey, before: order }]),
|
|
1145
1168
|
)
|
|
1146
1169
|
if (result === 'notFound') return refuse('notFound', 404, NOT_FOUND)
|
|
1147
1170
|
changed()
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* reorder and delete is done; then the workspace is closed. Anything refused
|
|
6
6
|
* undoes all of it. A rehearsal runs the same and undoes it on purpose.
|
|
7
7
|
*/
|
|
8
|
-
import { type DocumentValue, ObjectTypes } from '@crombolo/core'
|
|
8
|
+
import { type DocumentValue, normaliseUuid, ObjectTypes } from '@crombolo/core'
|
|
9
9
|
import {
|
|
10
10
|
type ChangeItemInput,
|
|
11
11
|
ChangeRepository,
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
DocumentRepository,
|
|
15
15
|
type DocumentRepositoryOptions,
|
|
16
16
|
Locks,
|
|
17
|
+
type ScheduleEntryAt,
|
|
17
18
|
SystemNodes,
|
|
18
19
|
TransferRunRepository,
|
|
19
20
|
type Workspace,
|
|
@@ -100,23 +101,58 @@ export type WorkspaceMergeOutcome =
|
|
|
100
101
|
}
|
|
101
102
|
| { ok: false; blockers: string[]; plan: MergePlan }
|
|
102
103
|
|
|
104
|
+
/** A page whose schedule a workspace set, as its merge carries it (story 414). */
|
|
105
|
+
interface ScheduledPage {
|
|
106
|
+
nodeId: number
|
|
107
|
+
key: string
|
|
108
|
+
name: string
|
|
109
|
+
entries: ScheduleEntryAt[]
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Whether two schedules are the same, entry by entry. Pure. */
|
|
113
|
+
const sameSchedule = (a: unknown, b: unknown) => JSON.stringify(a ?? []) === JSON.stringify(b ?? [])
|
|
114
|
+
|
|
103
115
|
/** Where a deleted page's state is kept among its change's items (story 383). */
|
|
104
116
|
export const REMOVED_PAGE_PREFIX = 'pages/'
|
|
105
117
|
|
|
118
|
+
/** Story 411: what a merge of a person's own page alone does, as its change is named and announced. */
|
|
119
|
+
export type OnlyKind = 'publish' | 'unpublish' | 'move' | 'reorder' | 'delete'
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* `only`: the pages of a person's own workspace that go alone (story 380), with what waits
|
|
123
|
+
* for a merge of them: a move, a reorder or a delete (story 411).
|
|
124
|
+
*/
|
|
125
|
+
export interface OnlyOptions {
|
|
126
|
+
only?: string | readonly string[]
|
|
127
|
+
onlyKind?: OnlyKind
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const onlyKeys = (only: OnlyOptions['only']): ReadonlySet<string> | undefined =>
|
|
131
|
+
only === undefined ? undefined : new Set(typeof only === 'string' ? [only] : only)
|
|
132
|
+
|
|
133
|
+
const ONLY_NAMES: Record<OnlyKind, string> = {
|
|
134
|
+
publish: 'Published',
|
|
135
|
+
unpublish: 'Unpublished',
|
|
136
|
+
move: 'Moved',
|
|
137
|
+
reorder: 'Reordered',
|
|
138
|
+
delete: 'Deleted',
|
|
139
|
+
}
|
|
140
|
+
|
|
106
141
|
/** Records a merge as a change (story 381): its name, who made and merged it, its files and pages. */
|
|
107
142
|
async function recordChange(
|
|
108
143
|
tx: Db,
|
|
109
144
|
list: readonly Workspace[],
|
|
110
|
-
options: WorkspaceMergeOptions &
|
|
145
|
+
options: WorkspaceMergeOptions & OnlyOptions,
|
|
111
146
|
items: readonly ChangeItemInput[],
|
|
112
147
|
pages: readonly PageChange[],
|
|
113
148
|
merged: Done['merged'],
|
|
149
|
+
done: Done['done'] = [],
|
|
114
150
|
): Promise<string> {
|
|
115
151
|
const changes = new ChangeRepository(tx)
|
|
116
152
|
const first = list[0] as Workspace
|
|
117
153
|
const name =
|
|
118
154
|
options.only !== undefined
|
|
119
|
-
?
|
|
155
|
+
? `${ONLY_NAMES[options.onlyKind ?? 'publish']} "${merged[0]?.name ?? done[0]?.name ?? 'a page'}"`
|
|
120
156
|
: list.map((w) => w.name).join(', ')
|
|
121
157
|
const id = await changes.begin({
|
|
122
158
|
kind: 'merge',
|
|
@@ -268,7 +304,7 @@ function betweenThem(plans: ReadonlyArray<{ workspace: Workspace; plan: MergePla
|
|
|
268
304
|
*/
|
|
269
305
|
export async function runWorkspaceMerge(
|
|
270
306
|
chosen: Workspace | readonly Workspace[],
|
|
271
|
-
options: WorkspaceMergeOptions &
|
|
307
|
+
options: WorkspaceMergeOptions & OnlyOptions,
|
|
272
308
|
): Promise<WorkspaceMergeOutcome> {
|
|
273
309
|
const list = Array.isArray(chosen) ? chosen : [chosen as Workspace]
|
|
274
310
|
const documents = new DocumentRepository(options.db, options.repository)
|
|
@@ -287,14 +323,19 @@ export async function runWorkspaceMerge(
|
|
|
287
323
|
await locked(() =>
|
|
288
324
|
documents.atomically(async (shared, tx) => {
|
|
289
325
|
const workspaces = new WorkspaceRepository(tx)
|
|
326
|
+
const only = onlyKeys(options.only)
|
|
290
327
|
const narrowed = (plan: MergePlan): MergePlan =>
|
|
291
|
-
|
|
328
|
+
only === undefined
|
|
292
329
|
? plan
|
|
293
330
|
: {
|
|
294
|
-
pages: plan.pages.filter((p) => p.page.key
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
331
|
+
pages: plan.pages.filter((p) => only.has(p.page.key)),
|
|
332
|
+
// Story 411: what waits for the merge of those pages goes with them.
|
|
333
|
+
pending:
|
|
334
|
+
options.onlyKind && options.onlyKind !== 'publish'
|
|
335
|
+
? plan.pending.filter((p) => p.pending.key !== null && only.has(p.pending.key))
|
|
336
|
+
: [],
|
|
337
|
+
conflicts: plan.conflicts.filter((c) => c.key !== null && only.has(c.key)),
|
|
338
|
+
findings: plan.findings.filter((f) => f.key !== null && only.has(f.key)),
|
|
298
339
|
}
|
|
299
340
|
const first: Array<{ workspace: Workspace; plan: MergePlan }> = []
|
|
300
341
|
for (const workspace of list)
|
|
@@ -348,7 +389,29 @@ export async function runWorkspaceMerge(
|
|
|
348
389
|
fileBlockers.push(
|
|
349
390
|
`"${page.name}" is of a type made in the workspace: merge the workspace to bring both.`,
|
|
350
391
|
)
|
|
392
|
+
// Story 414: the schedules each workspace set, and any Shared changed since.
|
|
393
|
+
const schedules: ScheduledPage[][] = []
|
|
394
|
+
const scheduleBlockers: string[] = []
|
|
395
|
+
for (const workspace of list) {
|
|
396
|
+
const planned: ScheduledPage[] = []
|
|
397
|
+
for (const row of await workspaces.scheduled(workspace.id)) {
|
|
398
|
+
if (only !== undefined && !only.has(row.key)) continue
|
|
399
|
+
const entries = await shared.inWorkspace(workspace.id).scheduleEntries(row.key)
|
|
400
|
+
const now = await shared.scheduleEntries(row.key)
|
|
401
|
+
if (
|
|
402
|
+
!sameSchedule(now, row.base) &&
|
|
403
|
+
!sameSchedule(now, entries) &&
|
|
404
|
+
!(options.accepted ?? []).includes('schedule-changed')
|
|
405
|
+
)
|
|
406
|
+
scheduleBlockers.push(
|
|
407
|
+
`"${row.name}": its schedule was changed in Shared since. Accept "schedule-changed" to set it as the workspace has it.`,
|
|
408
|
+
)
|
|
409
|
+
planned.push({ nodeId: row.nodeId, key: row.key, name: row.name, entries })
|
|
410
|
+
}
|
|
411
|
+
schedules.push(planned)
|
|
412
|
+
}
|
|
351
413
|
const blockers = [
|
|
414
|
+
...scheduleBlockers,
|
|
352
415
|
...fileBlockers,
|
|
353
416
|
...betweenThem(first),
|
|
354
417
|
...first.flatMap(({ workspace, plan }) =>
|
|
@@ -366,6 +429,7 @@ export async function runWorkspaceMerge(
|
|
|
366
429
|
await options.events.record(tx, event, options.actor)
|
|
367
430
|
}
|
|
368
431
|
const merged: Done['merged'] = []
|
|
432
|
+
const scheduled: string[] = []
|
|
369
433
|
const forApproval: Done['forApproval'] = []
|
|
370
434
|
const done: Done['done'] = []
|
|
371
435
|
const pages: PageChange[] = []
|
|
@@ -389,14 +453,59 @@ export async function runWorkspaceMerge(
|
|
|
389
453
|
pages,
|
|
390
454
|
removed: items,
|
|
391
455
|
})
|
|
456
|
+
// Story 414: its schedules set in Shared, and held by its change.
|
|
457
|
+
for (const page of schedules[n] ?? []) {
|
|
458
|
+
const had = await shared.scheduleEntries(page.key)
|
|
459
|
+
await shared.setSchedule(page.key, [
|
|
460
|
+
...had
|
|
461
|
+
.filter((h) => !page.entries.some((e) => e.culture === h.culture))
|
|
462
|
+
.map((h) => ({ culture: h.culture, publishTime: null, unpublishTime: null })),
|
|
463
|
+
...page.entries.map((e) => ({
|
|
464
|
+
culture: e.culture,
|
|
465
|
+
publishTime: e.publishTime ? new Date(e.publishTime) : null,
|
|
466
|
+
unpublishTime: e.unpublishTime ? new Date(e.unpublishTime) : null,
|
|
467
|
+
})),
|
|
468
|
+
])
|
|
469
|
+
await record({ type: 'scheduled', key: page.key })
|
|
470
|
+
items.push({
|
|
471
|
+
path: `schedules/${page.key}`,
|
|
472
|
+
kind: 'page',
|
|
473
|
+
class: 'none',
|
|
474
|
+
before: null,
|
|
475
|
+
after: JSON.stringify(page.entries),
|
|
476
|
+
})
|
|
477
|
+
if (!pages.some((p) => p.key === page.key)) {
|
|
478
|
+
const at = await shared.eventMarkers(page.key)
|
|
479
|
+
pages.push({
|
|
480
|
+
key: page.key,
|
|
481
|
+
kind: 'document',
|
|
482
|
+
action: 'update',
|
|
483
|
+
beforeEventId: at?.head ?? null,
|
|
484
|
+
beforePublishedEventId: at?.published ?? null,
|
|
485
|
+
eventId: at?.head ?? null,
|
|
486
|
+
})
|
|
487
|
+
}
|
|
488
|
+
if (!merged.some((m) => m.key === page.key))
|
|
489
|
+
merged.push({ key: page.key, name: page.name, published: false, unpublished: false })
|
|
490
|
+
scheduled.push(page.key)
|
|
491
|
+
if (options.only !== undefined) await workspaces.dropSchedule(workspace.id, page.nodeId)
|
|
492
|
+
}
|
|
392
493
|
if (options.only === undefined)
|
|
393
494
|
await workspaces.closeMerged(workspace.id, options.userKey)
|
|
394
|
-
else
|
|
495
|
+
else {
|
|
395
496
|
for (const page of plan.pages)
|
|
396
497
|
if (!page.page.made) await workspaces.dropPage(workspace.id, page.page.nodeId)
|
|
498
|
+
for (const { pending } of plan.pending)
|
|
499
|
+
await workspaces.dropPending(workspace.id, pending.nodeId)
|
|
500
|
+
}
|
|
397
501
|
}
|
|
398
502
|
// Story 381: the merge is a change, its files and pages each with before and after.
|
|
399
|
-
const changeId = await recordChange(tx, list, options, items, pages, merged)
|
|
503
|
+
const changeId = await recordChange(tx, list, options, items, pages, merged, done)
|
|
504
|
+
// Story 414: each schedule it set names it, for when it fires.
|
|
505
|
+
if (scheduled.length > 0) {
|
|
506
|
+
const made = await new ChangeRepository(tx).byId(changeId)
|
|
507
|
+
if (made) for (const key of scheduled) await shared.scheduleSetBy(key, made.key)
|
|
508
|
+
}
|
|
400
509
|
// Story 400: each workspace the merge closed, announced with the change it made.
|
|
401
510
|
if (options.only === undefined) {
|
|
402
511
|
const made = await new ChangeRepository(tx).byId(changeId)
|
|
@@ -409,7 +518,23 @@ export async function runWorkspaceMerge(
|
|
|
409
518
|
)
|
|
410
519
|
}
|
|
411
520
|
const alone = options.only === undefined ? undefined : merged[0]
|
|
412
|
-
|
|
521
|
+
const kind = options.onlyKind ?? 'publish'
|
|
522
|
+
if (options.only !== undefined && kind !== 'publish') {
|
|
523
|
+
// Story 411: announced as what it did, naming the page a delete took.
|
|
524
|
+
const first = done.find((d) => d.key !== null) ?? merged[0]
|
|
525
|
+
if (first?.key)
|
|
526
|
+
await options.events.ownPublished?.(
|
|
527
|
+
tx,
|
|
528
|
+
{
|
|
529
|
+
changeId,
|
|
530
|
+
key: first.key,
|
|
531
|
+
kind,
|
|
532
|
+
...(kind === 'delete' ? { page: { key: first.key, name: first.name } } : {}),
|
|
533
|
+
},
|
|
534
|
+
options.actor,
|
|
535
|
+
)
|
|
536
|
+
} else if (alone && (alone.published || scheduled.includes(alone.key)))
|
|
537
|
+
// Story 414: a page only scheduled is announced as one published is.
|
|
413
538
|
await options.events.ownPublished?.(tx, { changeId, key: alone.key }, options.actor)
|
|
414
539
|
outcome = {
|
|
415
540
|
ok: true,
|
|
@@ -474,6 +599,16 @@ async function apply(
|
|
|
474
599
|
await shared.unpublish(page.key, cultures)
|
|
475
600
|
await record({ type: 'unpublished', key: page.key, cultures })
|
|
476
601
|
unpublished = true
|
|
602
|
+
// Story 411: its change says it was taken down, as the page read back cannot.
|
|
603
|
+
const now = await shared.byKey(page.key)
|
|
604
|
+
if (now)
|
|
605
|
+
out.removed.push(
|
|
606
|
+
placeItem(
|
|
607
|
+
page.key,
|
|
608
|
+
{ parentKey: now.parentKey, published: true },
|
|
609
|
+
{ parentKey: now.parentKey, published: now.published },
|
|
610
|
+
),
|
|
611
|
+
)
|
|
477
612
|
}
|
|
478
613
|
}
|
|
479
614
|
out.merged.push({ key: page.key, name: pagePlan.name, published, unpublished })
|
|
@@ -490,13 +625,18 @@ async function apply(
|
|
|
490
625
|
|
|
491
626
|
const keyOf = async (id: number | null) =>
|
|
492
627
|
id === null || id === SystemNodes.Root ? null : ((await sharedAll.nodes.byId(id))?.key ?? null)
|
|
628
|
+
// Story 411: each parent's children's order before its first reorder, for the change.
|
|
629
|
+
const orders = new Map<string, { parentKey: string | null; before: string[] }>()
|
|
630
|
+
const childKeys = async (parentKey: string | null) =>
|
|
631
|
+
(await sharedAll.children(parentKey, 0, 10_000)).items.map((n) => normaliseUuid(n.key))
|
|
493
632
|
for (const { pending } of plan.pending) {
|
|
494
633
|
const key = pending.key
|
|
495
634
|
if (pending.kind === 'gone' || key === null) continue
|
|
496
635
|
const shared = sharedAll.ofKind(pending.objectKind ?? 'document')
|
|
497
636
|
const before = await shared.eventMarkers(key)
|
|
498
637
|
// A delete takes the page's history with it: its change keeps the page as it stood (story 383).
|
|
499
|
-
|
|
638
|
+
// Story 411: so does one moved to the bin, which the bin may empty before the change is taken.
|
|
639
|
+
if (pending.kind === 'delete' || pending.kind === 'trash') {
|
|
500
640
|
const doc = await shared.byKey(key)
|
|
501
641
|
if (doc)
|
|
502
642
|
out.removed.push({
|
|
@@ -508,11 +648,27 @@ async function apply(
|
|
|
508
648
|
})
|
|
509
649
|
}
|
|
510
650
|
if (pending.kind === 'move') {
|
|
511
|
-
await shared.
|
|
512
|
-
|
|
651
|
+
const node = await shared.nodes.byId(pending.nodeId)
|
|
652
|
+
const from = {
|
|
653
|
+
parentKey: await keyOf(node?.parentId ?? null),
|
|
654
|
+
trashed: node?.trashed ?? false,
|
|
655
|
+
}
|
|
656
|
+
const to = await keyOf(pending.targetParentId)
|
|
657
|
+
await shared.move(key, to)
|
|
658
|
+
await record({ type: 'moved', key, from })
|
|
659
|
+
const published = (await shared.byKey(key))?.published ?? false
|
|
660
|
+
out.removed.push(
|
|
661
|
+
placeItem(
|
|
662
|
+
key,
|
|
663
|
+
{ ...from, published, ...(from.trashed ? { trashed: true } : {}) },
|
|
664
|
+
{ parentKey: to, published },
|
|
665
|
+
),
|
|
666
|
+
)
|
|
513
667
|
} else if (pending.kind === 'sort' && pending.sortOrder !== null) {
|
|
514
668
|
const node = await shared.nodes.byId(pending.nodeId)
|
|
515
669
|
const parentKey = await keyOf(node?.parentId ?? null)
|
|
670
|
+
const slot = parentKey ?? ''
|
|
671
|
+
if (!orders.has(slot)) orders.set(slot, { parentKey, before: await childKeys(parentKey) })
|
|
516
672
|
await shared.sort(parentKey, [{ key, sortOrder: pending.sortOrder }])
|
|
517
673
|
await record({ type: 'sorted', parentKey })
|
|
518
674
|
} else if (pending.kind === 'trash') {
|
|
@@ -540,4 +696,28 @@ async function apply(
|
|
|
540
696
|
eventId: after?.head ?? null,
|
|
541
697
|
})
|
|
542
698
|
}
|
|
699
|
+
for (const { parentKey, before } of orders.values()) {
|
|
700
|
+
const after = await childKeys(parentKey)
|
|
701
|
+
if (after.join() !== before.join())
|
|
702
|
+
out.removed.push({
|
|
703
|
+
path: `orders/${parentKey ? normaliseUuid(parentKey) : 'root'}`,
|
|
704
|
+
kind: 'page',
|
|
705
|
+
class: 'none',
|
|
706
|
+
before: JSON.stringify(before),
|
|
707
|
+
after: JSON.stringify(after),
|
|
708
|
+
})
|
|
709
|
+
}
|
|
543
710
|
}
|
|
711
|
+
|
|
712
|
+
/** Story 411: a page's place before and after, as its change keeps it. */
|
|
713
|
+
const placeItem = (
|
|
714
|
+
key: string,
|
|
715
|
+
before: { parentKey: string | null; published: boolean; trashed?: boolean },
|
|
716
|
+
after: { parentKey: string | null; published: boolean },
|
|
717
|
+
): ChangeItemInput => ({
|
|
718
|
+
path: `places/${normaliseUuid(key)}`,
|
|
719
|
+
kind: 'page',
|
|
720
|
+
class: 'none',
|
|
721
|
+
before: JSON.stringify(before),
|
|
722
|
+
after: JSON.stringify(after),
|
|
723
|
+
})
|