@crombolo/content 0.34.0 → 0.36.2
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 +39 -4
- package/src/media-operations.ts +152 -35
- package/src/operations.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/content",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.2",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. The rules every page and media 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.36.2",
|
|
33
|
+
"@crombolo/data": "0.36.2"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/src/actor.ts
CHANGED
|
@@ -49,6 +49,35 @@ export type Actor =
|
|
|
49
49
|
note?: string
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* A signed-in member, acting through a server bundle (story 198): media
|
|
54
|
+
* operations only, with the media permissions deciding what they may do. A
|
|
55
|
+
* document operation never takes one, which `Actor` leaving it out enforces.
|
|
56
|
+
*/
|
|
57
|
+
export interface MemberActor {
|
|
58
|
+
kind: 'member'
|
|
59
|
+
member: { key: string; name: string; groups: readonly string[] }
|
|
60
|
+
/** What they acted through, for the audit log: the bundle's name. */
|
|
61
|
+
via?: string
|
|
62
|
+
origin: ActorOrigin
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Who a media write is for: anyone a document write may be for, or a member. */
|
|
66
|
+
export type MediaActor = Actor | MemberActor
|
|
67
|
+
|
|
68
|
+
export const memberActor = (
|
|
69
|
+
member: MemberActor['member'],
|
|
70
|
+
options: { via?: string } & Omit<ActorOrigin, 'source'> = {},
|
|
71
|
+
): MemberActor => {
|
|
72
|
+
const { via, ...origin } = options
|
|
73
|
+
return {
|
|
74
|
+
kind: 'member',
|
|
75
|
+
member: { key: member.key, name: member.name, groups: [...member.groups] },
|
|
76
|
+
via,
|
|
77
|
+
origin: { source: 'request', ...origin },
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
52
81
|
export const userActor = (
|
|
53
82
|
principal: ContentPrincipal,
|
|
54
83
|
origin: Omit<ActorOrigin, 'source'> = {},
|
|
@@ -77,21 +106,27 @@ export const systemActor = (
|
|
|
77
106
|
})
|
|
78
107
|
|
|
79
108
|
/** The name an audit entry or a notification gives the actor. */
|
|
80
|
-
export const actorName = (actor:
|
|
109
|
+
export const actorName = (actor: MediaActor): string =>
|
|
81
110
|
actor.kind === 'user'
|
|
82
111
|
? actor.principal.name
|
|
83
112
|
: actor.kind === 'system'
|
|
84
113
|
? (actor.user?.name ?? actor.name)
|
|
85
|
-
: actor.
|
|
114
|
+
: actor.kind === 'member'
|
|
115
|
+
? actor.member.name
|
|
116
|
+
: actor.name
|
|
86
117
|
|
|
87
118
|
/** The user's key, when the actor is one; versions record it as their author. */
|
|
88
|
-
export const actorUserKey = (actor:
|
|
119
|
+
export const actorUserKey = (actor: MediaActor): string | null =>
|
|
89
120
|
actor.kind === 'user'
|
|
90
121
|
? actor.principal.id
|
|
91
122
|
: actor.kind === 'system'
|
|
92
123
|
? (actor.user?.id ?? null)
|
|
93
124
|
: null
|
|
94
125
|
|
|
126
|
+
/** The member's key, when the actor is one. */
|
|
127
|
+
export const actorMemberKey = (actor: MediaActor): string | null =>
|
|
128
|
+
actor.kind === 'member' ? actor.member.key : null
|
|
129
|
+
|
|
95
130
|
/** What the actor says alongside the change, if anything. */
|
|
96
|
-
export const actorNote = (actor:
|
|
131
|
+
export const actorNote = (actor: MediaActor): string | undefined =>
|
|
97
132
|
actor.kind === 'system' ? actor.note : undefined
|
package/src/media-operations.ts
CHANGED
|
@@ -8,8 +8,10 @@
|
|
|
8
8
|
import {
|
|
9
9
|
type DocumentValue,
|
|
10
10
|
mediaFileOf,
|
|
11
|
+
memberMay,
|
|
11
12
|
normaliseUuid,
|
|
12
13
|
ObjectTypes,
|
|
14
|
+
SYSTEM_MEDIA_TYPE_KEYS,
|
|
13
15
|
SystemNodes,
|
|
14
16
|
} from '@crombolo/core'
|
|
15
17
|
import {
|
|
@@ -19,11 +21,13 @@ import {
|
|
|
19
21
|
DocumentRepository,
|
|
20
22
|
type DocumentRepositoryOptions,
|
|
21
23
|
InvalidParentError,
|
|
24
|
+
MediaAccessRepository,
|
|
22
25
|
NodeRepository,
|
|
23
26
|
type NodeRow,
|
|
27
|
+
ruleOf,
|
|
24
28
|
WriteRejectedError,
|
|
25
29
|
} from '@crombolo/data'
|
|
26
|
-
import {
|
|
30
|
+
import { actorName, actorUserKey, type MediaActor } from './actor.ts'
|
|
27
31
|
import type { Refusal, Result, SelectionResult } from './operations.ts'
|
|
28
32
|
import { mayChangeMedia } from './policy.ts'
|
|
29
33
|
|
|
@@ -34,6 +38,13 @@ export interface MediaInput {
|
|
|
34
38
|
parentKey: string | null
|
|
35
39
|
values: DocumentValue[]
|
|
36
40
|
variants: Array<{ culture: string | null; segment: string | null; name: string }>
|
|
41
|
+
/** The member who created it, recorded as its creator (story 196); a member actor is. */
|
|
42
|
+
creatorMemberKey?: string | null
|
|
43
|
+
/**
|
|
44
|
+
* False leaves it following the rule above rather than stamping the defaults
|
|
45
|
+
* that rule sets for new files: for a folder made to hold them (story 198).
|
|
46
|
+
*/
|
|
47
|
+
stamp?: boolean
|
|
37
48
|
}
|
|
38
49
|
|
|
39
50
|
export type MediaEvent =
|
|
@@ -50,8 +61,8 @@ export type MediaEvent =
|
|
|
50
61
|
|
|
51
62
|
/** Where a media operation records what it did, inside the change's transaction. */
|
|
52
63
|
export interface MediaEventSink {
|
|
53
|
-
record(tx: Db, event: MediaEvent, actor:
|
|
54
|
-
afterCommit?(event: MediaEvent, actor:
|
|
64
|
+
record(tx: Db, event: MediaEvent, actor: MediaActor): Promise<void>
|
|
65
|
+
afterCommit?(event: MediaEvent, actor: MediaActor): void
|
|
55
66
|
}
|
|
56
67
|
|
|
57
68
|
/** The audit entry a media event writes: the entries the backoffice has always written. Pure. */
|
|
@@ -101,7 +112,7 @@ export function mediaWebhookFor(event: MediaEvent): { alias: string; key: string
|
|
|
101
112
|
}
|
|
102
113
|
|
|
103
114
|
/** The whole audit entry a media event writes, with who acted and from where. Pure. */
|
|
104
|
-
export function mediaAuditEntryFor(event: MediaEvent, actor:
|
|
115
|
+
export function mediaAuditEntryFor(event: MediaEvent, actor: MediaActor) {
|
|
105
116
|
return {
|
|
106
117
|
...mediaAuditFor(event),
|
|
107
118
|
source: actor.origin.source,
|
|
@@ -109,22 +120,41 @@ export function mediaAuditEntryFor(event: MediaEvent, actor: Actor) {
|
|
|
109
120
|
userName: actorName(actor),
|
|
110
121
|
ipAddress: actor.origin.ipAddress ?? null,
|
|
111
122
|
forwardedFor: actor.origin.forwardedFor ?? null,
|
|
112
|
-
detail:
|
|
123
|
+
detail:
|
|
124
|
+
actor.kind === 'member'
|
|
125
|
+
? `A member${actor.via ? `, through ${actor.via}` : ''} (${actor.member.key})`
|
|
126
|
+
: null,
|
|
113
127
|
}
|
|
114
128
|
}
|
|
115
129
|
|
|
130
|
+
/** The cache instruction a media change appends: `{ media: [keys], moved?: [paths] }`. Pure. */
|
|
131
|
+
export function mediaChangePayload(change: MediaChange): Record<string, unknown> {
|
|
132
|
+
const keys = [...new Set(change.keys.map(normaliseUuid))]
|
|
133
|
+
const moved = [...new Set(change.moved)]
|
|
134
|
+
return moved.length > 0 ? { media: keys, moved } : { media: keys }
|
|
135
|
+
}
|
|
136
|
+
|
|
116
137
|
export interface MediaOperationsOptions {
|
|
117
138
|
db: Db
|
|
118
139
|
repository?: Omit<DocumentRepositoryOptions, 'kind'>
|
|
119
140
|
events: MediaEventSink
|
|
120
141
|
/** Where an item's file goes when the item is deleted for good. */
|
|
121
142
|
files?: { remove(path: string): Promise<void> }
|
|
122
|
-
/**
|
|
123
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Told after every committed change, with the items it touched and the old
|
|
145
|
+
* paths of any that moved, for the media cache held in this process.
|
|
146
|
+
*/
|
|
147
|
+
onChange?(change: MediaChange): void | Promise<void>
|
|
124
148
|
/** Errors a value's intake throws that are the sender's to fix, as the reason to give. */
|
|
125
149
|
refusalReason?(error: unknown): string | undefined
|
|
126
150
|
}
|
|
127
151
|
|
|
152
|
+
/** What a media change touched: the items, and the old paths of any whose branch moved with them. */
|
|
153
|
+
export interface MediaChange {
|
|
154
|
+
keys: string[]
|
|
155
|
+
moved: string[]
|
|
156
|
+
}
|
|
157
|
+
|
|
128
158
|
const NOT_FOUND = 'The media item could not be found.'
|
|
129
159
|
|
|
130
160
|
const refuse = (kind: Refusal['kind'], status: number, reason: string): Refusal => ({
|
|
@@ -150,26 +180,34 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
150
180
|
const nodes = new NodeRepository(db)
|
|
151
181
|
const types = new ContentTypeRepository(db, { kind: 'media' })
|
|
152
182
|
const nodeId = options.repository?.nodeId
|
|
153
|
-
const changed = async () => {
|
|
154
|
-
await options.onChange?.()
|
|
155
|
-
}
|
|
156
183
|
|
|
157
|
-
/**
|
|
184
|
+
/**
|
|
185
|
+
* The change, its events and the cache instruction in one transaction
|
|
186
|
+
* (stories 171, 174). The instruction names the items touched, and the old
|
|
187
|
+
* paths of any that moved, so each server drops only those (story 197).
|
|
188
|
+
*/
|
|
158
189
|
const commit = async <T>(
|
|
159
|
-
actor:
|
|
190
|
+
actor: MediaActor,
|
|
160
191
|
write: (tx: DocumentRepository, txDb: Db) => Promise<T>,
|
|
161
192
|
announce: (result: T) => readonly MediaEvent[],
|
|
193
|
+
touched: (result: T, events: readonly MediaEvent[]) => MediaChange = (_, said) => ({
|
|
194
|
+
keys: said.flatMap((e) => ('key' in e ? [e.key] : [])),
|
|
195
|
+
moved: [],
|
|
196
|
+
}),
|
|
162
197
|
): Promise<T> => {
|
|
163
198
|
const recorded: MediaEvent[] = []
|
|
199
|
+
let change: MediaChange | undefined
|
|
164
200
|
const result = await repo.atomically(async (tx, txDb) => {
|
|
165
201
|
const written = await write(tx, txDb)
|
|
166
202
|
const said = announce(written)
|
|
167
|
-
if (said.length > 0)
|
|
203
|
+
if (said.length > 0) {
|
|
204
|
+
change = touched(written, said)
|
|
168
205
|
await appendCacheInstruction(txDb, {
|
|
169
206
|
kind: 'content',
|
|
170
|
-
payload:
|
|
207
|
+
payload: mediaChangePayload(change),
|
|
171
208
|
by: nodeId,
|
|
172
209
|
})
|
|
210
|
+
}
|
|
173
211
|
for (const event of said) {
|
|
174
212
|
await events.record(txDb, event, actor)
|
|
175
213
|
recorded.push(event)
|
|
@@ -177,11 +215,16 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
177
215
|
return written
|
|
178
216
|
})
|
|
179
217
|
for (const event of recorded) events.afterCommit?.(event, actor)
|
|
180
|
-
if (
|
|
218
|
+
if (change) await options.onChange?.(change)
|
|
181
219
|
return result
|
|
182
220
|
}
|
|
221
|
+
/** The keys, and the old paths of branches that moved: what a move, trash or delete touched. */
|
|
222
|
+
const branches = (rows: readonly NodeRow[]): MediaChange => ({
|
|
223
|
+
keys: rows.map((row) => row.key),
|
|
224
|
+
moved: rows.map((row) => row.path),
|
|
225
|
+
})
|
|
183
226
|
|
|
184
|
-
const userIdOf = async (actor:
|
|
227
|
+
const userIdOf = async (actor: MediaActor): Promise<number | undefined> => {
|
|
185
228
|
if (actor.kind !== 'user') return undefined
|
|
186
229
|
const rows = await db.query<{ id: number }>('SELECT id FROM user_account WHERE key = ?', [
|
|
187
230
|
actor.principal.id,
|
|
@@ -205,8 +248,54 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
205
248
|
return { node, chain }
|
|
206
249
|
}
|
|
207
250
|
|
|
208
|
-
/**
|
|
209
|
-
const
|
|
251
|
+
/** A parent's media children, by key: what sorting them all touched. */
|
|
252
|
+
const childKeys = async (parentKey: string | null): Promise<string[]> => {
|
|
253
|
+
const parentId = parentKey ? (await nodes.byKey(parentKey))?.id : SystemNodes.Root
|
|
254
|
+
if (parentId === undefined) return []
|
|
255
|
+
const rows = await db.query<{ unique_id: string }>(
|
|
256
|
+
'SELECT unique_id FROM node WHERE parent_id = ? AND node_object_type = ?',
|
|
257
|
+
[parentId, ObjectTypes.Media],
|
|
258
|
+
)
|
|
259
|
+
return rows.map((row) => normaliseUuid(String(row.unique_id)))
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Whether a member may change `key` under the media permissions (story 196):
|
|
264
|
+
* its own write policy, else the nearest rule's write groups. Never the root
|
|
265
|
+
* or the bin. With `branch`, every item below it must pass too, so trashing
|
|
266
|
+
* or moving a folder cannot take another member's files with it.
|
|
267
|
+
*/
|
|
268
|
+
const memberPermits = async (
|
|
269
|
+
actor: MediaActor & { kind: 'member' },
|
|
270
|
+
key: string | 'root' | 'bin',
|
|
271
|
+
branch: boolean,
|
|
272
|
+
): Promise<boolean> => {
|
|
273
|
+
if (key === 'root' || key === 'bin') return false
|
|
274
|
+
const node = await nodes.byKey(key)
|
|
275
|
+
if (!node || node.objectType !== ObjectTypes.Media || node.trashed) return false
|
|
276
|
+
const access = new MediaAccessRepository(db)
|
|
277
|
+
const writable = async (itemKey: string) => {
|
|
278
|
+
const item = await access.itemAccess(itemKey)
|
|
279
|
+
const rule = await access.ruleFor(itemKey)
|
|
280
|
+
return item ? memberMay('write', item, rule ? ruleOf(rule) : undefined, actor.member) : false
|
|
281
|
+
}
|
|
282
|
+
if (!(await writable(key))) return false
|
|
283
|
+
if (!branch) return true
|
|
284
|
+
for (const below of await nodes.descendants(node, [ObjectTypes.Media]))
|
|
285
|
+
if (!(await writable(below.key))) return false
|
|
286
|
+
return true
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Whether the actor may change media at `key`, the root or the bin; a user or
|
|
291
|
+
* a member is checked. `branch` when the change carries everything below `key`.
|
|
292
|
+
*/
|
|
293
|
+
const permits = async (
|
|
294
|
+
actor: MediaActor,
|
|
295
|
+
key: string | 'root' | 'bin',
|
|
296
|
+
{ branch = false }: { branch?: boolean } = {},
|
|
297
|
+
): Promise<boolean> => {
|
|
298
|
+
if (actor.kind === 'member') return memberPermits(actor, key, branch)
|
|
210
299
|
if (actor.kind !== 'user') return true
|
|
211
300
|
if (key === 'root' || key === 'bin') return mayChangeMedia(actor.principal, key)
|
|
212
301
|
const place = await placeOf(key)
|
|
@@ -216,7 +305,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
216
305
|
const forbidden = () => refuse('forbidden', 403, 'You may not do that to this media item.')
|
|
217
306
|
|
|
218
307
|
/** Where an item of this type may go; the system acts on what was already decided. */
|
|
219
|
-
const placement = async (actor:
|
|
308
|
+
const placement = async (actor: MediaActor, typeKey: string, parentKey: string | null) =>
|
|
220
309
|
actor.kind === 'system' ? undefined : types.placementRefusal(typeKey, parentKey)
|
|
221
310
|
const typeOf = async (key: string): Promise<string | undefined> => {
|
|
222
311
|
const rows = await db.query<{ unique_id: string }>(
|
|
@@ -247,7 +336,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
247
336
|
|
|
248
337
|
/** Every item checked before any is written: the reasons each one cannot be moved there. */
|
|
249
338
|
const moveBlockers = async (
|
|
250
|
-
actor:
|
|
339
|
+
actor: MediaActor,
|
|
251
340
|
targets: readonly NodeRow[],
|
|
252
341
|
target: NodeRow | undefined,
|
|
253
342
|
) => {
|
|
@@ -266,13 +355,14 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
266
355
|
|
|
267
356
|
/** The items the actor may not change, as one refusal naming each. */
|
|
268
357
|
const deniedOf = async (
|
|
269
|
-
actor:
|
|
358
|
+
actor: MediaActor,
|
|
270
359
|
found: readonly NodeRow[],
|
|
271
360
|
reason: string,
|
|
272
361
|
): Promise<(Refusal & { blocked: Array<{ key: string; reasons: string[] }> }) | undefined> => {
|
|
273
362
|
const blocked: Array<{ key: string; reasons: string[] }> = []
|
|
274
363
|
for (const node of found)
|
|
275
|
-
if (!(await permits(actor, node.key
|
|
364
|
+
if (!(await permits(actor, node.key, { branch: true })))
|
|
365
|
+
blocked.push({ key: node.key, reasons: [reason] })
|
|
276
366
|
if (blocked.length === 0) return undefined
|
|
277
367
|
return {
|
|
278
368
|
...refuse(
|
|
@@ -295,15 +385,28 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
295
385
|
}
|
|
296
386
|
|
|
297
387
|
const operations = {
|
|
298
|
-
async create(actor:
|
|
388
|
+
async create(actor: MediaActor, input: MediaInput): Promise<Result<{ key: string }>> {
|
|
299
389
|
if (!(await permits(actor, input.parentKey ?? 'root'))) return forbidden()
|
|
300
390
|
const misplaced = await placement(actor, input.contentTypeKey, input.parentKey)
|
|
301
391
|
if (misplaced) return refuse('invalid', 400, misplaced)
|
|
302
392
|
try {
|
|
303
393
|
const userId = await userIdOf(actor)
|
|
394
|
+
const { creatorMemberKey, stamp, ...fields } = input
|
|
395
|
+
const creator = actor.kind === 'member' ? actor.member.key : (creatorMemberKey ?? null)
|
|
304
396
|
const created = await commit(
|
|
305
397
|
actor,
|
|
306
|
-
(tx) =>
|
|
398
|
+
async (tx, txDb) => {
|
|
399
|
+
const doc = await tx.create({ ...fields, componentKey: null, userId })
|
|
400
|
+
// What the folder above says files created in it get.
|
|
401
|
+
const access = new MediaAccessRepository(txDb)
|
|
402
|
+
if (
|
|
403
|
+
stamp === false ||
|
|
404
|
+
input.contentTypeKey.toLowerCase() === SYSTEM_MEDIA_TYPE_KEYS.Folder
|
|
405
|
+
)
|
|
406
|
+
await access.setCreator(doc.key, creator)
|
|
407
|
+
else await access.stampNew(doc.key, creator)
|
|
408
|
+
return doc
|
|
409
|
+
},
|
|
307
410
|
(doc) => [{ type: 'created', key: doc.key }],
|
|
308
411
|
)
|
|
309
412
|
return { ok: true, key: created.key }
|
|
@@ -313,7 +416,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
313
416
|
},
|
|
314
417
|
|
|
315
418
|
/** Saves an item's values and names; its type and place stay as they are. */
|
|
316
|
-
async save(actor:
|
|
419
|
+
async save(actor: MediaActor, key: string, input: Omit<MediaInput, 'key'>): Promise<Result> {
|
|
317
420
|
const before = await repo.byKey(key)
|
|
318
421
|
if (!before) return refuse('notFound', 404, NOT_FOUND)
|
|
319
422
|
if (!(await permits(actor, key))) return forbidden()
|
|
@@ -342,7 +445,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
342
445
|
}
|
|
343
446
|
},
|
|
344
447
|
|
|
345
|
-
async trash(actor:
|
|
448
|
+
async trash(actor: MediaActor, key: string): Promise<Result> {
|
|
346
449
|
const result = await operations.trashMany(actor, [key])
|
|
347
450
|
return result.ok ? { ok: true } : result
|
|
348
451
|
},
|
|
@@ -353,7 +456,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
353
456
|
* selection goes with it.
|
|
354
457
|
*/
|
|
355
458
|
async trashMany(
|
|
356
|
-
actor:
|
|
459
|
+
actor: MediaActor,
|
|
357
460
|
keys: readonly string[],
|
|
358
461
|
many: { dryRun?: boolean } = {},
|
|
359
462
|
): Promise<SelectionResult> {
|
|
@@ -375,29 +478,36 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
375
478
|
return out
|
|
376
479
|
},
|
|
377
480
|
(done) => done.map((key): MediaEvent => ({ type: 'trashed', key })),
|
|
481
|
+
() => branches(writes),
|
|
378
482
|
)
|
|
379
483
|
return { ok: true, changed: changedKeys, unchanged }
|
|
380
484
|
},
|
|
381
485
|
|
|
382
486
|
/** Deletes an item and its branch for good, with their files; `fromBin` only one in the bin. */
|
|
383
|
-
async delete(
|
|
487
|
+
async delete(
|
|
488
|
+
actor: MediaActor,
|
|
489
|
+
key: string,
|
|
490
|
+
deleting: { fromBin?: boolean } = {},
|
|
491
|
+
): Promise<Result> {
|
|
384
492
|
const node = await nodes.byKey(key)
|
|
385
493
|
if (!node || node.objectType !== ObjectTypes.Media) return refuse('notFound', 404, NOT_FOUND)
|
|
386
494
|
if (deleting.fromBin && !node.trashed)
|
|
387
495
|
return refuse('invalid', 400, 'The media item is not in the recycle bin')
|
|
388
|
-
if (!(await permits(actor, deleting.fromBin ? 'bin' : key)))
|
|
496
|
+
if (!(await permits(actor, deleting.fromBin ? 'bin' : key, { branch: true })))
|
|
497
|
+
return forbidden()
|
|
389
498
|
const paths = await filesIn(node)
|
|
390
499
|
const removed = await commit(
|
|
391
500
|
actor,
|
|
392
501
|
(tx) => tx.delete(key),
|
|
393
502
|
(done) => (done ? [{ type: 'deleted', key, name: node.text ?? null }] : []),
|
|
503
|
+
() => branches([node]),
|
|
394
504
|
)
|
|
395
505
|
if (!removed) return refuse('notFound', 404, NOT_FOUND)
|
|
396
506
|
await removeFiles(paths)
|
|
397
507
|
return { ok: true }
|
|
398
508
|
},
|
|
399
509
|
|
|
400
|
-
async emptyBin(actor:
|
|
510
|
+
async emptyBin(actor: MediaActor): Promise<Result<{ deleted: number }>> {
|
|
401
511
|
if (!(await permits(actor, 'bin'))) return forbidden()
|
|
402
512
|
const roots = await repo.trashedRoots()
|
|
403
513
|
const paths: string[] = []
|
|
@@ -414,13 +524,14 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
414
524
|
...done.map((node): MediaEvent => ({ type: 'deleted', ...node })),
|
|
415
525
|
{ type: 'binEmptied' },
|
|
416
526
|
],
|
|
527
|
+
() => branches(roots),
|
|
417
528
|
)
|
|
418
529
|
await removeFiles(paths)
|
|
419
530
|
return { ok: true, deleted: gone.length }
|
|
420
531
|
},
|
|
421
532
|
|
|
422
533
|
async restore(
|
|
423
|
-
actor:
|
|
534
|
+
actor: MediaActor,
|
|
424
535
|
key: string,
|
|
425
536
|
targetKey: string | null | undefined,
|
|
426
537
|
): Promise<Result> {
|
|
@@ -430,10 +541,12 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
430
541
|
const misplaced = type ? await placement(actor, type, targetKey) : undefined
|
|
431
542
|
if (misplaced) return refuse('invalid', 400, misplaced)
|
|
432
543
|
}
|
|
544
|
+
const before = await nodes.byKey(key)
|
|
433
545
|
const result = await commit(
|
|
434
546
|
actor,
|
|
435
547
|
(tx) => tx.restore(key, targetKey),
|
|
436
548
|
(done) => (done === 'restored' ? [{ type: 'restored', key }] : []),
|
|
549
|
+
() => (before ? branches([before]) : { keys: [key], moved: [] }),
|
|
437
550
|
)
|
|
438
551
|
if (result === 'notFound')
|
|
439
552
|
return refuse('notFound', 404, 'The media item is not in the recycle bin.')
|
|
@@ -442,7 +555,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
442
555
|
return { ok: true }
|
|
443
556
|
},
|
|
444
557
|
|
|
445
|
-
async move(actor:
|
|
558
|
+
async move(actor: MediaActor, key: string, targetKey: string | null): Promise<Result> {
|
|
446
559
|
const result = await operations.moveMany(actor, [key], targetKey)
|
|
447
560
|
if (result.ok) return { ok: true }
|
|
448
561
|
const first = 'blocked' in result ? result.blocked?.[0]?.reasons[0] : undefined
|
|
@@ -454,7 +567,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
454
567
|
* transaction, every one checked first. One already there is unchanged.
|
|
455
568
|
*/
|
|
456
569
|
async moveMany(
|
|
457
|
-
actor:
|
|
570
|
+
actor: MediaActor,
|
|
458
571
|
keys: readonly string[],
|
|
459
572
|
targetKey: string | null,
|
|
460
573
|
many: { dryRun?: boolean } = {},
|
|
@@ -494,6 +607,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
494
607
|
return out
|
|
495
608
|
},
|
|
496
609
|
(moved) => moved.map((key): MediaEvent => ({ type: 'moved', key })),
|
|
610
|
+
(moved) => branches(writes.filter((node) => moved.includes(node.key))),
|
|
497
611
|
)
|
|
498
612
|
return { ok: true, changed: done, unchanged }
|
|
499
613
|
} catch (error) {
|
|
@@ -502,7 +616,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
502
616
|
},
|
|
503
617
|
|
|
504
618
|
async sort(
|
|
505
|
-
actor:
|
|
619
|
+
actor: MediaActor,
|
|
506
620
|
parentKey: string | null,
|
|
507
621
|
sorting: ReadonlyArray<{ key: string; sortOrder: number }>,
|
|
508
622
|
sortOptions: { dryRun?: boolean } = {},
|
|
@@ -526,6 +640,7 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
526
640
|
actor,
|
|
527
641
|
(tx) => tx.sort(parentKey, sorting),
|
|
528
642
|
(done) => (done === 'sorted' ? [{ type: 'sorted', parentKey }] : []),
|
|
643
|
+
() => ({ keys: sorting.map((item) => item.key), moved: [] }),
|
|
529
644
|
)
|
|
530
645
|
return result === 'sorted'
|
|
531
646
|
? { ok: true }
|
|
@@ -533,16 +648,18 @@ export function createMediaOperations(options: MediaOperationsOptions) {
|
|
|
533
648
|
},
|
|
534
649
|
|
|
535
650
|
async sortChildrenBy(
|
|
536
|
-
actor:
|
|
651
|
+
actor: MediaActor,
|
|
537
652
|
parentKey: string | null,
|
|
538
653
|
field: 'Name' | 'CreateDate' | 'UpdateDate',
|
|
539
654
|
direction: 'Ascending' | 'Descending',
|
|
540
655
|
): Promise<Result> {
|
|
541
656
|
if (!(await permits(actor, parentKey ?? 'root'))) return forbidden()
|
|
657
|
+
const keys = await childKeys(parentKey)
|
|
542
658
|
const result = await commit(
|
|
543
659
|
actor,
|
|
544
660
|
(tx) => tx.sortChildrenBy(parentKey, field, direction),
|
|
545
661
|
(done) => (done === 'sorted' ? [{ type: 'sorted', parentKey }] : []),
|
|
662
|
+
() => ({ keys, moved: [] }),
|
|
546
663
|
)
|
|
547
664
|
return result === 'sorted' ? { ok: true } : refuse('notFound', 404, NOT_FOUND)
|
|
548
665
|
},
|
package/src/operations.ts
CHANGED
|
@@ -261,6 +261,8 @@ export function createDocumentOperations(options: DocumentOperationsOptions) {
|
|
|
261
261
|
kind: WriteKind,
|
|
262
262
|
key: string | 'root' | 'bin',
|
|
263
263
|
): Promise<boolean> => {
|
|
264
|
+
// A member acts on media only (story 198); the type says so, and this holds without it.
|
|
265
|
+
if ((actor as { kind: string }).kind === 'member') return false
|
|
264
266
|
if (actor.kind !== 'user') return true
|
|
265
267
|
if (key === 'root' || key === 'bin') return mayAt(actor.principal, WRITE_VERBS[kind], key)
|
|
266
268
|
const place = await placeOf(key)
|