@milesroxas/design-hub 0.7.0 → 0.8.1

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.
@@ -7,16 +7,20 @@
7
7
  import { base64url, hex, sha256 } from '../bytes.ts';
8
8
  import {
9
9
  type Access,
10
+ type Anchor,
11
+ type Audience,
10
12
  accessEnd,
11
13
  authorOf,
12
14
  type Checked,
13
15
  type Comment,
14
16
  checkAccess,
15
17
  checkAnchor,
18
+ checkAudience,
16
19
  checkBody,
17
20
  checkPersonInput,
18
21
  checkStates,
19
22
  checkTriage,
23
+ checkViewAs,
20
24
  DEFAULT_ACCESS,
21
25
  hasAccess,
22
26
  isProfile,
@@ -24,9 +28,10 @@ import {
24
28
  LINK_DAYS,
25
29
  type Link,
26
30
  type Person,
31
+ readComment,
27
32
  type Triage,
28
33
  } from './model.ts';
29
- import { type Action, can } from './policy.ts';
34
+ import { type Action, audienceFor, can, viewOf, visibleTo } from './policy.ts';
30
35
  import { KEY, type KV } from './store.ts';
31
36
 
32
37
  export type ErrorCode = 'unauthorized' | 'forbidden' | 'not_found' | 'invalid';
@@ -79,9 +84,14 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
79
84
  }
80
85
  };
81
86
 
82
- function allow(actor: Person | null, action: Action, subject?: Comment): asserts actor is Person {
87
+ function allow(
88
+ actor: Person | null,
89
+ action: Action,
90
+ root?: Comment,
91
+ subject?: Comment,
92
+ ): asserts actor is Person {
83
93
  if (!actor) throw new ReviewError('unauthorized', 'Sign in with your link to do that.');
84
- if (!can(actor, action, subject)) throw new ReviewError('forbidden', 'You cannot do that.');
94
+ if (!can(actor, action, root, subject)) throw new ReviewError('forbidden', 'You cannot do that.');
85
95
  }
86
96
 
87
97
  function valid<T>(checked: Checked<T>): T {
@@ -90,10 +100,10 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
90
100
  }
91
101
 
92
102
  // Every field checked before any throws, so one save names every problem.
93
- function validAll<A, B>(a: Checked<A>, b: Checked<B>): [A, B] {
94
- const problems = [...(a.ok ? [] : a.problems), ...(b.ok ? [] : b.problems)];
95
- if (!a.ok || !b.ok) throw new ReviewError('invalid', 'Some fields are not valid.', problems);
96
- return [a.value, b.value];
103
+ function validAll<T extends unknown[]>(...checks: { [K in keyof T]: Checked<T[K]> }): T {
104
+ const problems = checks.flatMap((c) => (c.ok ? [] : c.problems));
105
+ if (problems.length) throw new ReviewError('invalid', 'Some fields are not valid.', problems);
106
+ return checks.map((c) => (c as { value: unknown }).value) as T;
97
107
  }
98
108
 
99
109
  // A person is three records. The profile (KEY.people) is read-modify-write, so
@@ -176,24 +186,74 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
176
186
  return p;
177
187
  }
178
188
 
189
+ // Every comment read goes through readComment, so a record with no audience reads as internal.
190
+ // Unfiltered: nothing below hands these out without visibleTo or seen.
191
+ /**
192
+ * The next thread number for an audience on a prototype. Atomic and only raised: two threads at
193
+ * once get two numbers, and a failed save skips one. Client-facing threads keep the field
194
+ * numbers always had. Internal threads count in their own, which starts past any internal
195
+ * thread numbered before the two counted apart, so no internal number is given twice.
196
+ */
197
+ const nextNumber = async (prototype: string, audience: Audience): Promise<number> => {
198
+ if (audience === 'client') return kv.hincrby(KEY.counters, prototype, 1);
199
+ const field = `${prototype}:internal`;
200
+ const n = await kv.hincrby(KEY.counters, field, 1);
201
+ if (n !== 1) return n;
202
+ const before = Math.max(
203
+ 0,
204
+ ...(await commentsOf(prototype)).filter((c) => c.audience === 'internal').map((c) => c.number),
205
+ );
206
+ return before ? kv.hincrby(KEY.counters, field, before) : n;
207
+ };
208
+
179
209
  const commentsOf = async (prototype: string) =>
180
210
  Object.values(await kv.hgetall(KEY.comments(prototype)))
181
- .map((json) => JSON.parse(json) as Comment)
211
+ .map(readComment)
182
212
  .sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id));
183
213
 
214
+ const commentAt = async (prototype: string, id: string): Promise<Comment | null> => {
215
+ const json = await kv.hget(KEY.comments(prototype), id);
216
+ return json === null ? null : readComment(json);
217
+ };
218
+
184
219
  const saveComment = (prototype: string, c: Comment) =>
185
220
  kv.hset(KEY.comments(prototype), c.id, JSON.stringify(c));
186
221
 
187
- async function needComment(prototype: string, id: string): Promise<Comment> {
188
- const c = parse<Comment>(await kv.hget(KEY.comments(prototype), id));
189
- if (!c) throw new ReviewError('not_found', 'No such comment.');
190
- return c;
222
+ /**
223
+ * A comment and its thread's root, if `actor` may see both; else null. A
224
+ * reply whose root is gone is not there either.
225
+ */
226
+ async function seen(
227
+ actor: Person,
228
+ prototype: string,
229
+ id: string,
230
+ ): Promise<{ comment: Comment; root: Comment } | null> {
231
+ const comment = await commentAt(prototype, id);
232
+ if (!comment) return null;
233
+ const root = isRoot(comment) ? comment : await commentAt(prototype, comment.thread);
234
+ return root && can(actor, 'read', root, comment) ? { comment, root } : null;
191
235
  }
192
236
 
193
- async function needRoot(prototype: string, id: string): Promise<Comment> {
194
- const c = await needComment(prototype, id);
195
- if (!isRoot(c)) throw new ReviewError('invalid', 'That is a reply; act on its thread.');
196
- return c;
237
+ /**
238
+ * The comment an op names, and its root. One that `actor` may not see gets
239
+ * the same answer as one that does not exist, so an id never tells a Client
240
+ * an internal thread is there.
241
+ */
242
+ async function needThread(
243
+ actor: Person | null,
244
+ prototype: string,
245
+ id: string,
246
+ ): Promise<{ comment: Comment; root: Comment }> {
247
+ allow(actor, 'read');
248
+ const found = await seen(actor, prototype, id);
249
+ if (!found) throw new ReviewError('not_found', 'No such comment.');
250
+ return found;
251
+ }
252
+
253
+ async function needRoot(actor: Person | null, prototype: string, id: string): Promise<Comment> {
254
+ const { comment } = await needThread(actor, prototype, id);
255
+ if (!isRoot(comment)) throw new ReviewError('invalid', 'That is a reply; act on its thread.');
256
+ return comment;
197
257
  }
198
258
 
199
259
  return {
@@ -315,23 +375,38 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
315
375
  return active(after) && after.epoch === p.epoch ? after : null;
316
376
  },
317
377
 
318
- async listComments(actor: Person | null, prototype: string): Promise<Comment[]> {
378
+ /**
379
+ * A prototype's comments as the actor's view sees them. `viewAs: 'client'`
380
+ * is Studio's Client view; it never widens a Client's.
381
+ */
382
+ async listComments(actor: Person | null, prototype: string, viewAs?: unknown): Promise<Comment[]> {
319
383
  allow(actor, 'read');
320
- return commentsOf(prototype);
384
+ const view = viewOf(actor, valid(checkViewAs(viewAs)));
385
+ return visibleTo(view, await commentsOf(prototype));
321
386
  },
322
387
 
323
- /** Every comment on every prototype, for the CLI, which holds the store's own credentials. */
324
- async allComments(): Promise<Comment[]> {
388
+ /** Every comment on every prototype the actor may see, for the CLI (which acts as Studio). */
389
+ async allComments(actor: Person | null): Promise<Comment[]> {
390
+ allow(actor, 'read');
325
391
  const prototypes = await kv.smembers(KEY.prototypes);
326
- return (await Promise.all(prototypes.map(commentsOf))).flat();
392
+ return visibleTo(viewOf(actor), (await Promise.all(prototypes.map(commentsOf))).flat());
327
393
  },
328
394
 
395
+ /**
396
+ * A new thread. A Client's is client-facing whatever the input says;
397
+ * Studio's is internal unless `audience` is 'client'. Its number is the
398
+ * next of its audience on the prototype, never one used before.
399
+ */
329
400
  async add(
330
401
  actor: Person | null,
331
- input: { body?: unknown; anchor?: unknown; states?: unknown },
402
+ input: { body?: unknown; anchor?: unknown; states?: unknown; audience?: unknown },
332
403
  ): Promise<Comment> {
333
404
  allow(actor, 'comment');
334
- const [body, anchor] = validAll(checkBody(input.body), checkAnchor(input.anchor));
405
+ const [body, anchor, asked] = validAll<[string, Anchor, Audience | null]>(
406
+ checkBody(input.body),
407
+ checkAnchor(input.anchor),
408
+ checkAudience(input.audience),
409
+ );
335
410
  const id = newId();
336
411
  const triage = deps.triage
337
412
  ? await deps
@@ -350,7 +425,10 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
350
425
  status: 'open',
351
426
  resolvedBy: null,
352
427
  triage,
428
+ audience: audienceFor(actor, asked),
429
+ number: 0,
353
430
  };
431
+ c.number = await nextNumber(anchor.prototype, c.audience);
354
432
  await kv.sadd(KEY.prototypes, anchor.prototype);
355
433
  await saveComment(anchor.prototype, c);
356
434
  return c;
@@ -360,8 +438,8 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
360
438
  actor: Person | null,
361
439
  input: { prototype: string; thread: string; body?: unknown },
362
440
  ): Promise<Comment> {
363
- allow(actor, 'comment');
364
- const root = await needRoot(input.prototype, input.thread);
441
+ const root = await needRoot(actor, input.prototype, input.thread);
442
+ allow(actor, 'comment', root);
365
443
  const id = newId();
366
444
  const c: Comment = {
367
445
  id,
@@ -374,6 +452,9 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
374
452
  status: 'open',
375
453
  resolvedBy: null,
376
454
  triage: null,
455
+ // A reply follows its thread; nobody sets it.
456
+ audience: root.audience,
457
+ number: root.number,
377
458
  };
378
459
  await saveComment(input.prototype, c);
379
460
  return c;
@@ -383,8 +464,8 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
383
464
  actor: Person | null,
384
465
  input: { prototype: string; id: string; body?: unknown },
385
466
  ): Promise<Comment> {
386
- const c = await needComment(input.prototype, input.id);
387
- allow(actor, 'edit', c);
467
+ const { comment: c, root } = await needThread(actor, input.prototype, input.id);
468
+ allow(actor, 'edit', root, c);
388
469
  const next = { ...c, body: valid(checkBody(input.body)), editedAt: now() };
389
470
  await saveComment(input.prototype, next);
390
471
  return next;
@@ -392,20 +473,20 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
392
473
 
393
474
  /** Deletes a comment; deleting a thread's root deletes its replies with it. */
394
475
  async remove(actor: Person | null, input: { prototype: string; id: string }): Promise<string[]> {
395
- const c = await needComment(input.prototype, input.id);
396
- allow(actor, 'delete', c);
397
- const gone = isRoot(c)
398
- ? (await commentsOf(input.prototype)).filter((x) => x.thread === c.id).map((x) => x.id)
399
- : [c.id];
400
- for (const id of gone) await kv.hdel(KEY.comments(input.prototype), id);
401
- return gone;
476
+ const { comment: c, root } = await needThread(actor, input.prototype, input.id);
477
+ allow(actor, 'delete', root, c);
478
+ const gone = isRoot(c) ? (await commentsOf(input.prototype)).filter((x) => x.thread === c.id) : [c];
479
+ for (const x of gone) await kv.hdel(KEY.comments(input.prototype), x.id);
480
+ // The answer names only what the actor may see, judged with the thread's root beside it.
481
+ const shown = new Set(visibleTo(viewOf(actor), [root, ...gone]).map((x) => x.id));
482
+ return gone.filter((x) => shown.has(x.id)).map((x) => x.id);
402
483
  },
403
484
 
404
485
  async resolve(
405
486
  actor: Person | null,
406
487
  input: { prototype: string; id: string; resolved: boolean },
407
488
  ): Promise<Comment> {
408
- const c = await needRoot(input.prototype, input.id);
489
+ const c = await needRoot(actor, input.prototype, input.id);
409
490
  allow(actor, 'resolve', c);
410
491
  const next: Comment = input.resolved
411
492
  ? { ...c, status: 'resolved', resolvedBy: authorOf(actor) }
@@ -419,7 +500,7 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
419
500
  input: { prototype: string; id: string; triage?: unknown; states?: unknown },
420
501
  ): Promise<Comment> {
421
502
  allow(actor, 'triage');
422
- const c = await needRoot(input.prototype, input.id);
503
+ const c = await needRoot(actor, input.prototype, input.id);
423
504
  const states = checkStates(input.states);
424
505
  const { kind, state } = valid(checkTriage(input.triage, states.length ? states : null));
425
506
  const next: Comment = { ...c, triage: { kind, state, by: 'person', confidence: null } };
@@ -427,11 +508,12 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
427
508
  return next;
428
509
  },
429
510
 
430
- /** Where a comment lives, for the CLI, which has an id and no prototype. */
431
- async locate(id: string): Promise<{ prototype: string; comment: Comment } | null> {
511
+ /** Where a comment lives, for the CLI, which has an id and no prototype; null when the actor may not see it. */
512
+ async locate(actor: Person | null, id: string): Promise<{ prototype: string; comment: Comment } | null> {
513
+ allow(actor, 'read');
432
514
  for (const prototype of await kv.smembers(KEY.prototypes)) {
433
- const c = parse<Comment>(await kv.hget(KEY.comments(prototype), id));
434
- if (c) return { prototype, comment: c };
515
+ const found = await seen(actor, prototype, id);
516
+ if (found) return { prototype, comment: found.comment };
435
517
  }
436
518
  return null;
437
519
  },
@@ -34,6 +34,11 @@ export const KEY = {
34
34
  /** Project settings, one field each: `access` is the default access length. */
35
35
  settings: `${PREFIX}settings`,
36
36
  prototypes: `${PREFIX}prototypes`,
37
+ /**
38
+ * The last thread number given on each prototype, only ever raised by HINCRBY: field
39
+ * `<prototype>` counts client-facing threads, `<prototype>:internal` internal ones.
40
+ */
41
+ counters: `${PREFIX}counters`,
37
42
  comments: (prototype: string) => `${PREFIX}comments:${prototype}`,
38
43
  } as const;
39
44
 
@@ -1,6 +1,12 @@
1
1
  /** Which side a person is on: the studio that makes the work, or the client it is for. */
2
2
  export declare const PERSON_KINDS: readonly ['studio', 'client'];
3
3
  export type PersonKind = (typeof PERSON_KINDS)[number];
4
+ /**
5
+ * Who a thread is for. Internal stays inside the studio; client-facing is
6
+ * read by everyone. Fixed when the thread is made: no op changes it.
7
+ */
8
+ declare const AUDIENCES: readonly ['internal', 'client'];
9
+ export type Audience = (typeof AUDIENCES)[number];
4
10
  /** How a comment is triaged: something broken, a design opinion, or the words. */
5
11
  export declare const KINDS: readonly ['bug', 'taste', 'copy'];
6
12
  export type Kind = (typeof KINDS)[number];
@@ -87,7 +93,29 @@ export type Comment = {
87
93
  status: 'open' | 'resolved';
88
94
  resolvedBy: Author | null;
89
95
  triage: Triage | null;
96
+ /** The thread's audience. Set on the root when it is made and copied onto every reply; never changed. */
97
+ audience: Audience;
98
+ /**
99
+ * The thread's number on its prototype and audience, from 1, copied onto every reply.
100
+ * Internal and client-facing threads count apart, so a Client's numbers run 1, 2, 3 with no
101
+ * gap where an internal thread sits; `threadLabel` tells the two apart. Never reused, so a
102
+ * deleted thread moves no other number. 0 for a thread stored before numbers existed.
103
+ */
104
+ number: number;
90
105
  };
106
+ /**
107
+ * How a thread is named in words, for a screen reader and an agent: `I2` for internal thread 2,
108
+ * `2` for client-facing thread 2, '' for a thread stored before numbers. The pin, the card
109
+ * and `comments pull` use it, so "fix I2" names one thread. The pin and the card's badge show
110
+ * the bare number; their amber says internal.
111
+ */
112
+ export declare const threadLabel: (c: Pick<Comment, 'audience' | 'number'>) => string;
113
+ /**
114
+ * A stored comment as the rules read it. A record with no audience, or one
115
+ * that is not a known value, is internal: a missing field never shows a
116
+ * Client anything. A missing or broken number reads as 0, no number.
117
+ */
118
+ export declare function readComment(json: string): Comment;
91
119
  /** Whether a stored profile (a person without revocation and epoch) is well formed. */
92
120
  export declare function isProfile(v: unknown): v is Omit<Person, 'revokedAt' | 'epoch'>;
93
121
  /**
@@ -110,8 +138,13 @@ export type PersonInput = Pick<Person, 'name' | 'team' | 'kind'> & {
110
138
  access: Access | null;
111
139
  };
112
140
  export declare function checkPersonInput(input: unknown): Checked<PersonInput>;
141
+ /** A new thread's audience as asked: one of AUDIENCES, or null when none is given. */
142
+ export declare function checkAudience(input: unknown): Checked<Audience | null>;
143
+ /** Whose view of the comments is asked for: a person kind, or null for the caller's own. */
144
+ export declare function checkViewAs(input: unknown): Checked<PersonKind | null>;
113
145
  export declare function checkBody(body: unknown): Checked<string>;
114
146
  export declare function checkAnchor(input: unknown): Checked<Anchor>;
115
147
  /** The README states a client offers for triage: trimmed, capped, de-duplicated. */
116
148
  export declare function checkStates(input: unknown): string[];
117
149
  export declare function checkTriage(input: unknown, states: string[] | null): Checked<Pick<Triage, 'kind' | 'state'>>;
150
+ export {};
@@ -6,6 +6,11 @@
6
6
  import { SLUG } from '../feature.js';
7
7
  /** Which side a person is on: the studio that makes the work, or the client it is for. */
8
8
  export const PERSON_KINDS = ['studio', 'client'];
9
+ /**
10
+ * Who a thread is for. Internal stays inside the studio; client-facing is
11
+ * read by everyone. Fixed when the thread is made: no op changes it.
12
+ */
13
+ const AUDIENCES = ['internal', 'client'];
9
14
  /** How a comment is triaged: something broken, a design opinion, or the words. */
10
15
  export const KINDS = ['bug', 'taste', 'copy'];
11
16
  /** How long an unused sign-in link lasts. A link signs in one device, then stops working. */
@@ -41,6 +46,24 @@ export const LIMITS = {
41
46
  states: 50,
42
47
  stateName: 120,
43
48
  };
49
+ /**
50
+ * How a thread is named in words, for a screen reader and an agent: `I2` for internal thread 2,
51
+ * `2` for client-facing thread 2, '' for a thread stored before numbers. The pin, the card
52
+ * and `comments pull` use it, so "fix I2" names one thread. The pin and the card's badge show
53
+ * the bare number; their amber says internal.
54
+ */
55
+ export const threadLabel = (c) => !c.number ? '' : c.audience === 'internal' ? `I${c.number}` : String(c.number);
56
+ /**
57
+ * A stored comment as the rules read it. A record with no audience, or one
58
+ * that is not a known value, is internal: a missing field never shows a
59
+ * Client anything. A missing or broken number reads as 0, no number.
60
+ */
61
+ export function readComment(json) {
62
+ const c = JSON.parse(json);
63
+ const audience = c.audience === 'client' ? 'client' : 'internal';
64
+ const number = Number.isInteger(c.number) && c.number > 0 ? c.number : 0;
65
+ return { ...c, audience, number };
66
+ }
44
67
  const isText = (v) => typeof v === 'string';
45
68
  const isTextOrNull = (v) => v === null || typeof v === 'string';
46
69
  /** What a stored profile must hold to be a person; anything else is no person at all. */
@@ -109,6 +132,22 @@ export function checkPersonInput(input) {
109
132
  ? { ok: false, problems }
110
133
  : { ok: true, value: { name, team, kind: kind, access: access } };
111
134
  }
135
+ /** A new thread's audience as asked: one of AUDIENCES, or null when none is given. */
136
+ export function checkAudience(input) {
137
+ if (input === undefined || input === null)
138
+ return { ok: true, value: null };
139
+ return AUDIENCES.includes(input)
140
+ ? { ok: true, value: input }
141
+ : { ok: false, problems: [`audience: one of ${AUDIENCES.join(', ')}`] };
142
+ }
143
+ /** Whose view of the comments is asked for: a person kind, or null for the caller's own. */
144
+ export function checkViewAs(input) {
145
+ if (input === undefined || input === null)
146
+ return { ok: true, value: null };
147
+ return PERSON_KINDS.includes(input)
148
+ ? { ok: true, value: input }
149
+ : { ok: false, problems: [`viewAs: one of ${PERSON_KINDS.join(', ')}`] };
150
+ }
112
151
  export function checkBody(body) {
113
152
  const problems = [];
114
153
  const value = text(body, 'body', LIMITS.body, problems);
@@ -1,6 +1,25 @@
1
- import type { Comment, Person } from './model.ts';
1
+ import type { Audience, Comment, Person, PersonKind } from './model.ts';
2
2
  export type Action = 'read' | 'comment' | 'edit' | 'delete' | 'resolve' | 'triage' | 'people';
3
3
  /** Only what the rules read, so the browser's signed-in person (no epoch) is checked by the same rules. */
4
4
  type Actor = Pick<Person, 'id' | 'kind' | 'revokedAt'>;
5
- export declare function can(actor: Actor | null, action: Action, subject?: Comment): boolean;
5
+ /**
6
+ * `root` is the thread acted on, `subject` the comment in it when that is a
7
+ * reply (edit or delete one); without `subject`, the root is the subject.
8
+ * Without a root, the action is on no thread: listing, or a new thread.
9
+ */
10
+ export declare function can(actor: Actor | null, action: Action, root?: Comment, subject?: Comment): boolean;
11
+ /**
12
+ * The view a person gets: their own kind, or a narrower one they ask for.
13
+ * Studio may look as a Client does (Client view); a Client asking for
14
+ * Studio's still gets a Client's, so the view is never wider than the person.
15
+ */
16
+ export declare function viewOf(actor: Pick<Person, 'kind'> | null, viewAs?: PersonKind | null): PersonKind;
17
+ /**
18
+ * The comments a view may see. Studio sees all. A Client sees a client-facing
19
+ * thread and its client-facing replies; a reply whose thread is not among
20
+ * them is not seen either.
21
+ */
22
+ export declare function visibleTo(viewAs: PersonKind, comments: Comment[]): Comment[];
23
+ /** A new thread's audience: a Client's is always client-facing; Studio's is internal unless Studio asks otherwise. */
24
+ export declare function audienceFor(actor: Pick<Person, 'kind'>, asked: Audience | null): Audience;
6
25
  export {};
@@ -1,8 +1,17 @@
1
- export function can(actor, action, subject) {
1
+ /** Whether a comment reaches a Client. Only the exact value counts, so a record without one stays inside. */
2
+ const clientFacing = (c) => c.audience === 'client';
3
+ /**
4
+ * `root` is the thread acted on, `subject` the comment in it when that is a
5
+ * reply (edit or delete one); without `subject`, the root is the subject.
6
+ * Without a root, the action is on no thread: listing, or a new thread.
7
+ */
8
+ export function can(actor, action, root, subject) {
2
9
  if (!actor || actor.revokedAt)
3
10
  return false;
4
11
  const studio = actor.kind === 'studio';
5
- const own = subject?.author.id === actor.id;
12
+ if (!studio && [root, subject].some((c) => c && !clientFacing(c)))
13
+ return false;
14
+ const own = (subject ?? root)?.author.id === actor.id;
6
15
  switch (action) {
7
16
  case 'read':
8
17
  case 'comment':
@@ -17,3 +26,26 @@ export function can(actor, action, subject) {
17
26
  return studio;
18
27
  }
19
28
  }
29
+ /**
30
+ * The view a person gets: their own kind, or a narrower one they ask for.
31
+ * Studio may look as a Client does (Client view); a Client asking for
32
+ * Studio's still gets a Client's, so the view is never wider than the person.
33
+ */
34
+ export function viewOf(actor, viewAs) {
35
+ return actor?.kind === 'studio' && viewAs !== 'client' ? 'studio' : 'client';
36
+ }
37
+ /**
38
+ * The comments a view may see. Studio sees all. A Client sees a client-facing
39
+ * thread and its client-facing replies; a reply whose thread is not among
40
+ * them is not seen either.
41
+ */
42
+ export function visibleTo(viewAs, comments) {
43
+ if (viewAs === 'studio')
44
+ return comments;
45
+ const roots = new Set(comments.filter((c) => c.thread === c.id && clientFacing(c)).map((c) => c.id));
46
+ return comments.filter((c) => clientFacing(c) && roots.has(c.thread));
47
+ }
48
+ /** A new thread's audience: a Client's is always client-facing; Studio's is internal unless Studio asks otherwise. */
49
+ export function audienceFor(actor, asked) {
50
+ return actor.kind === 'studio' ? (asked ?? 'internal') : 'client';
51
+ }
@@ -54,13 +54,23 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
54
54
  * read and checked first, so the delete is the last store call.
55
55
  */
56
56
  useLink(token: string): Promise<Person | null>;
57
- listComments(actor: Person | null, prototype: string): Promise<Comment[]>;
58
- /** Every comment on every prototype, for the CLI, which holds the store's own credentials. */
59
- allComments(): Promise<Comment[]>;
57
+ /**
58
+ * A prototype's comments as the actor's view sees them. `viewAs: 'client'`
59
+ * is Studio's Client view; it never widens a Client's.
60
+ */
61
+ listComments(actor: Person | null, prototype: string, viewAs?: unknown): Promise<Comment[]>;
62
+ /** Every comment on every prototype the actor may see, for the CLI (which acts as Studio). */
63
+ allComments(actor: Person | null): Promise<Comment[]>;
64
+ /**
65
+ * A new thread. A Client's is client-facing whatever the input says;
66
+ * Studio's is internal unless `audience` is 'client'. Its number is the
67
+ * next of its audience on the prototype, never one used before.
68
+ */
60
69
  add(actor: Person | null, input: {
61
70
  body?: unknown;
62
71
  anchor?: unknown;
63
72
  states?: unknown;
73
+ audience?: unknown;
64
74
  }): Promise<Comment>;
65
75
  reply(actor: Person | null, input: {
66
76
  prototype: string;
@@ -88,8 +98,8 @@ export declare function createReview(kv: KV, overrides?: Partial<Deps>): {
88
98
  triage?: unknown;
89
99
  states?: unknown;
90
100
  }): Promise<Comment>;
91
- /** Where a comment lives, for the CLI, which has an id and no prototype. */
92
- locate(id: string): Promise<{
101
+ /** Where a comment lives, for the CLI, which has an id and no prototype; null when the actor may not see it. */
102
+ locate(actor: Person | null, id: string): Promise<{
93
103
  prototype: string;
94
104
  comment: Comment;
95
105
  } | null>;