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