@milesroxas/design-hub 0.6.2 → 0.8.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/README.md +16 -6
- package/core/review/model.ts +124 -6
- package/core/review/policy.test.ts +103 -24
- package/core/review/policy.ts +43 -7
- package/core/review/service.test.ts +569 -33
- package/core/review/service.ts +272 -76
- package/core/review/store.ts +24 -4
- package/dist/core/review/model.d.ts +59 -4
- package/dist/core/review/model.js +98 -4
- package/dist/core/review/policy.d.ts +21 -2
- package/dist/core/review/policy.js +34 -2
- package/dist/core/review/service.d.ts +39 -11
- package/dist/core/review/service.js +237 -77
- package/dist/core/review/store.d.ts +17 -4
- package/dist/core/review/store.js +21 -3
- package/dist/scripts/bootstrap.d.ts +16 -14
- package/dist/scripts/bootstrap.js +56 -39
- package/dist/scripts/comments.d.ts +10 -2
- package/dist/scripts/comments.js +18 -7
- package/dist/scripts/lib/deploy.d.ts +12 -4
- package/dist/scripts/lib/deploy.js +47 -13
- package/dist/scripts/lib/vercel.d.ts +8 -2
- package/dist/scripts/lib/vercel.js +23 -9
- package/dist/scripts/people.d.ts +13 -26
- package/dist/scripts/people.js +68 -35
- package/dist/scripts/preflight.js +2 -2
- package/dist/scripts/share.d.ts +2 -3
- package/dist/scripts/share.js +18 -10
- package/dist/server/api.d.ts +6 -2
- package/dist/server/api.js +65 -24
- package/dist/server/fake-upstash.js +3 -2
- package/dist/server/local.js +2 -0
- package/dist/server/session.d.ts +25 -10
- package/dist/server/session.js +69 -52
- package/dist/server/upstash.js +4 -3
- package/dist/vercel/proxy.d.ts +5 -3
- package/dist/vercel/proxy.js +183 -157
- package/dist/vite/plugin.js +5 -5
- package/kit/prefs.ts +19 -2
- package/package.json +2 -2
- package/shell/review/Bar.tsx +97 -4
- package/shell/review/People.tsx +178 -45
- package/shell/review/Pins.tsx +39 -24
- package/shell/review/Review.tsx +113 -29
- package/shell/review/ThreadCard.tsx +25 -13
- package/shell/review/client.ts +64 -23
- package/shell/review/review.css +16 -2
- package/shell/review/view.test.ts +73 -1
- package/shell/review/view.ts +49 -3
- package/skills/miles-deploy/SKILL.md +44 -16
- package/skills/miles-design-hub/SKILL.md +5 -5
- package/skills/miles-new-feature/SKILL.md +1 -1
- package/skills/miles-review/SKILL.md +10 -6
- package/skills/miles-worktrees/SKILL.md +1 -1
- package/templates/conductor/settings.toml +1 -1
- package/templates/guide.md +1 -1
package/core/review/service.ts
CHANGED
|
@@ -6,20 +6,32 @@
|
|
|
6
6
|
// two edits of the same comment at once keep the last.
|
|
7
7
|
import { base64url, hex, sha256 } from '../bytes.ts';
|
|
8
8
|
import {
|
|
9
|
+
type Access,
|
|
10
|
+
type Anchor,
|
|
11
|
+
type Audience,
|
|
12
|
+
accessEnd,
|
|
9
13
|
authorOf,
|
|
10
14
|
type Checked,
|
|
11
15
|
type Comment,
|
|
16
|
+
checkAccess,
|
|
12
17
|
checkAnchor,
|
|
18
|
+
checkAudience,
|
|
13
19
|
checkBody,
|
|
14
20
|
checkPersonInput,
|
|
15
21
|
checkStates,
|
|
16
22
|
checkTriage,
|
|
17
|
-
|
|
23
|
+
checkViewAs,
|
|
24
|
+
DEFAULT_ACCESS,
|
|
25
|
+
hasAccess,
|
|
26
|
+
isProfile,
|
|
18
27
|
isRoot,
|
|
28
|
+
LINK_DAYS,
|
|
29
|
+
type Link,
|
|
19
30
|
type Person,
|
|
31
|
+
readComment,
|
|
20
32
|
type Triage,
|
|
21
33
|
} from './model.ts';
|
|
22
|
-
import { type Action, can } from './policy.ts';
|
|
34
|
+
import { type Action, audienceFor, can, viewOf, visibleTo } from './policy.ts';
|
|
23
35
|
import { KEY, type KV } from './store.ts';
|
|
24
36
|
|
|
25
37
|
export type ErrorCode = 'unauthorized' | 'forbidden' | 'not_found' | 'invalid';
|
|
@@ -52,7 +64,9 @@ const defaults: Deps = {
|
|
|
52
64
|
triage: null,
|
|
53
65
|
};
|
|
54
66
|
|
|
55
|
-
|
|
67
|
+
/** What a link's token looks like: 32 random bytes, base64url. Anything else is never looked up. */
|
|
68
|
+
const LINK_TOKEN = /^[\w-]{20,100}$/;
|
|
69
|
+
const linkKey = async (token: string) => hex(await sha256(token));
|
|
56
70
|
|
|
57
71
|
export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
58
72
|
const deps = { ...defaults, ...overrides };
|
|
@@ -61,10 +75,23 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
61
75
|
const newId = () => `${deps.now().getTime().toString(36).padStart(9, '0')}${hex(deps.random(5))}`;
|
|
62
76
|
|
|
63
77
|
const parse = <T>(json: string | null): T | null => (json === null ? null : (JSON.parse(json) as T));
|
|
78
|
+
/** A stored value that may not be JSON at all: null rather than a throw. */
|
|
79
|
+
const safeParse = (json: string | null): unknown => {
|
|
80
|
+
try {
|
|
81
|
+
return json === null ? null : JSON.parse(json);
|
|
82
|
+
} catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
64
86
|
|
|
65
|
-
function allow(
|
|
66
|
-
|
|
67
|
-
|
|
87
|
+
function allow(
|
|
88
|
+
actor: Person | null,
|
|
89
|
+
action: Action,
|
|
90
|
+
root?: Comment,
|
|
91
|
+
subject?: Comment,
|
|
92
|
+
): asserts actor is Person {
|
|
93
|
+
if (!actor) throw new ReviewError('unauthorized', 'Sign in with your link to do that.');
|
|
94
|
+
if (!can(actor, action, root, subject)) throw new ReviewError('forbidden', 'You cannot do that.');
|
|
68
95
|
}
|
|
69
96
|
|
|
70
97
|
function valid<T>(checked: Checked<T>): T {
|
|
@@ -73,30 +100,84 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
73
100
|
}
|
|
74
101
|
|
|
75
102
|
// Every field checked before any throws, so one save names every problem.
|
|
76
|
-
function validAll<
|
|
77
|
-
const problems =
|
|
78
|
-
if (
|
|
79
|
-
return
|
|
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;
|
|
80
107
|
}
|
|
81
108
|
|
|
82
|
-
|
|
83
|
-
|
|
109
|
+
// A person is three records. The profile (KEY.people) is read-modify-write, so
|
|
110
|
+
// a stale write of it can land after anything. Revocation and the epoch live
|
|
111
|
+
// outside it: revocation is written once and never cleared, the epoch only
|
|
112
|
+
// rises (HINCRBY), so no profile write, however stale, can revive a session.
|
|
113
|
+
const compose = (
|
|
114
|
+
json: string | null,
|
|
115
|
+
revoked: string | null | undefined,
|
|
116
|
+
epoch: string | null | undefined,
|
|
117
|
+
) => {
|
|
118
|
+
const profile = safeParse(json);
|
|
119
|
+
if (!isProfile(profile)) return null;
|
|
120
|
+
// Any revocation record means revoked, an empty one included: it reads as the start of time.
|
|
121
|
+
const revokedAt = revoked == null ? null : revoked || new Date(0).toISOString();
|
|
122
|
+
return { ...profile, revokedAt, epoch: Number(epoch ?? 0) };
|
|
123
|
+
};
|
|
124
|
+
const person = async (id: string): Promise<Person | null> => {
|
|
125
|
+
const [json, revoked, epoch] = await Promise.all([
|
|
126
|
+
kv.hget(KEY.people, id),
|
|
127
|
+
kv.hget(KEY.revoked, id),
|
|
128
|
+
kv.hget(KEY.epochs, id),
|
|
129
|
+
]);
|
|
130
|
+
return compose(json, revoked, epoch);
|
|
131
|
+
};
|
|
132
|
+
/** Writes the profile alone: revocation and epoch are never part of what it saves. */
|
|
133
|
+
const savePerson = ({ revokedAt: _r, epoch: _e, ...profile }: Person) =>
|
|
134
|
+
kv.hset(KEY.people, profile.id, JSON.stringify(profile));
|
|
135
|
+
/** Not revoked, and their access has not ended. */
|
|
136
|
+
const active = (p: Person | null): p is Person => !!p && hasAccess(p, deps.now());
|
|
84
137
|
|
|
85
|
-
async function
|
|
138
|
+
async function issueLink(p: Person): Promise<string> {
|
|
86
139
|
// The link carries the token; the store keeps only its hash, so a leaked store signs nobody in.
|
|
87
140
|
const token = base64url(deps.random(32));
|
|
88
|
-
const
|
|
141
|
+
const link: Link = {
|
|
89
142
|
person: p.id,
|
|
90
|
-
expiresAt: new Date(deps.now().getTime() +
|
|
143
|
+
expiresAt: new Date(deps.now().getTime() + LINK_DAYS * DAY_MS).toISOString(),
|
|
91
144
|
};
|
|
92
|
-
await kv.hset(KEY.
|
|
145
|
+
await kv.hset(KEY.links, await linkKey(token), JSON.stringify(link));
|
|
93
146
|
return token;
|
|
94
147
|
}
|
|
95
148
|
|
|
96
|
-
async function
|
|
97
|
-
const all = await kv.hgetall(KEY.
|
|
149
|
+
async function dropLinks(personId: string): Promise<void> {
|
|
150
|
+
const all = await kv.hgetall(KEY.links);
|
|
98
151
|
for (const [hash, json] of Object.entries(all))
|
|
99
|
-
if ((JSON.parse(json) as
|
|
152
|
+
if ((JSON.parse(json) as Link).person === personId) await kv.hdel(KEY.links, hash);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The link a token names, or null for a malformed or unknown token. */
|
|
156
|
+
async function findLink(token: string): Promise<{ key: string; link: Link } | null> {
|
|
157
|
+
if (!LINK_TOKEN.test(token)) return null;
|
|
158
|
+
const key = await linkKey(token);
|
|
159
|
+
const link = parse<Link>(await kv.hget(KEY.links, key));
|
|
160
|
+
return link ? { key, link } : null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The person a current link signs in, while they may sign in. */
|
|
164
|
+
async function linkPerson(link: Link): Promise<Person | null> {
|
|
165
|
+
if (link.expiresAt <= now()) return null;
|
|
166
|
+
const p = await person(link.person);
|
|
167
|
+
return active(p) ? p : null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function readDefault(): Promise<Access> {
|
|
171
|
+
const stored = await kv.hget(KEY.settings, 'access');
|
|
172
|
+
return checkAccess(stored).ok ? (stored as Access) : DEFAULT_ACCESS;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Someone Studio can still give a link to. */
|
|
176
|
+
async function needActive(id: string): Promise<Person> {
|
|
177
|
+
const p = await needPerson(id);
|
|
178
|
+
if (p.revokedAt) throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
179
|
+
if (!active(p)) throw new ReviewError('invalid', 'Their access has ended; change their access first.');
|
|
180
|
+
return p;
|
|
100
181
|
}
|
|
101
182
|
|
|
102
183
|
async function needPerson(id: string): Promise<Person> {
|
|
@@ -105,24 +186,56 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
105
186
|
return p;
|
|
106
187
|
}
|
|
107
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.
|
|
108
191
|
const commentsOf = async (prototype: string) =>
|
|
109
192
|
Object.values(await kv.hgetall(KEY.comments(prototype)))
|
|
110
|
-
.map(
|
|
193
|
+
.map(readComment)
|
|
111
194
|
.sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id));
|
|
112
195
|
|
|
196
|
+
const commentAt = async (prototype: string, id: string): Promise<Comment | null> => {
|
|
197
|
+
const json = await kv.hget(KEY.comments(prototype), id);
|
|
198
|
+
return json === null ? null : readComment(json);
|
|
199
|
+
};
|
|
200
|
+
|
|
113
201
|
const saveComment = (prototype: string, c: Comment) =>
|
|
114
202
|
kv.hset(KEY.comments(prototype), c.id, JSON.stringify(c));
|
|
115
203
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
204
|
+
/**
|
|
205
|
+
* A comment and its thread's root, if `actor` may see both; else null. A
|
|
206
|
+
* reply whose root is gone is not there either.
|
|
207
|
+
*/
|
|
208
|
+
async function seen(
|
|
209
|
+
actor: Person,
|
|
210
|
+
prototype: string,
|
|
211
|
+
id: string,
|
|
212
|
+
): Promise<{ comment: Comment; root: Comment } | null> {
|
|
213
|
+
const comment = await commentAt(prototype, id);
|
|
214
|
+
if (!comment) return null;
|
|
215
|
+
const root = isRoot(comment) ? comment : await commentAt(prototype, comment.thread);
|
|
216
|
+
return root && can(actor, 'read', root, comment) ? { comment, root } : null;
|
|
120
217
|
}
|
|
121
218
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
219
|
+
/**
|
|
220
|
+
* The comment an op names, and its root. One that `actor` may not see gets
|
|
221
|
+
* the same answer as one that does not exist, so an id never tells a Client
|
|
222
|
+
* an internal thread is there.
|
|
223
|
+
*/
|
|
224
|
+
async function needThread(
|
|
225
|
+
actor: Person | null,
|
|
226
|
+
prototype: string,
|
|
227
|
+
id: string,
|
|
228
|
+
): Promise<{ comment: Comment; root: Comment }> {
|
|
229
|
+
allow(actor, 'read');
|
|
230
|
+
const found = await seen(actor, prototype, id);
|
|
231
|
+
if (!found) throw new ReviewError('not_found', 'No such comment.');
|
|
232
|
+
return found;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async function needRoot(actor: Person | null, prototype: string, id: string): Promise<Comment> {
|
|
236
|
+
const { comment } = await needThread(actor, prototype, id);
|
|
237
|
+
if (!isRoot(comment)) throw new ReviewError('invalid', 'That is a reply; act on its thread.');
|
|
238
|
+
return comment;
|
|
126
239
|
}
|
|
127
240
|
|
|
128
241
|
return {
|
|
@@ -131,75 +244,151 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
131
244
|
/** The person a signed session names, if they still may sign in with it. */
|
|
132
245
|
async sessionPerson(id: string, epoch: number): Promise<Person | null> {
|
|
133
246
|
const p = await person(id);
|
|
134
|
-
return p &&
|
|
247
|
+
return active(p) && p.epoch === epoch ? p : null;
|
|
135
248
|
},
|
|
136
249
|
|
|
137
250
|
async listPeople(actor: Person | null): Promise<Person[]> {
|
|
138
251
|
allow(actor, 'people');
|
|
139
|
-
const
|
|
252
|
+
const [profiles, revoked, epochs] = await Promise.all([
|
|
253
|
+
kv.hgetall(KEY.people),
|
|
254
|
+
kv.hgetall(KEY.revoked),
|
|
255
|
+
kv.hgetall(KEY.epochs),
|
|
256
|
+
]);
|
|
257
|
+
// A profile that is not a well-formed person is skipped, never a throw.
|
|
258
|
+
const all = Object.entries(profiles)
|
|
259
|
+
.map(([id, json]) => compose(json, revoked[id], epochs[id]))
|
|
260
|
+
.filter((p): p is Person => p !== null);
|
|
140
261
|
return all.sort((a, b) => a.team.localeCompare(b.team) || a.name.localeCompare(b.name));
|
|
141
262
|
},
|
|
142
263
|
|
|
143
|
-
|
|
264
|
+
/** A new person and their first link. Without an access length, a Client gets the project default and Studio is open-ended. */
|
|
265
|
+
async addPerson(actor: Person | null, input: unknown): Promise<{ person: Person; token: string }> {
|
|
144
266
|
allow(actor, 'people');
|
|
267
|
+
const { access, ...fields } = valid(checkPersonInput(input));
|
|
268
|
+
const length = access ?? (fields.kind === 'studio' ? 'open' : await readDefault());
|
|
145
269
|
const p: Person = {
|
|
146
270
|
id: newId(),
|
|
147
|
-
...
|
|
271
|
+
...fields,
|
|
272
|
+
email: null,
|
|
273
|
+
accessEndsAt: accessEnd(length, deps.now()),
|
|
148
274
|
createdAt: now(),
|
|
149
275
|
revokedAt: null,
|
|
150
276
|
epoch: 0,
|
|
151
277
|
};
|
|
152
278
|
await savePerson(p);
|
|
153
|
-
return { person: p, token: await
|
|
279
|
+
return { person: p, token: await issueLink(p) };
|
|
154
280
|
},
|
|
155
281
|
|
|
156
|
-
/**
|
|
157
|
-
async
|
|
282
|
+
/** Another one-time link, for another device. Their sessions and other unused links stay. */
|
|
283
|
+
async newLink(actor: Person | null, id: string): Promise<{ person: Person; token: string }> {
|
|
158
284
|
allow(actor, 'people');
|
|
159
|
-
const p = await
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
285
|
+
const p = await needActive(id);
|
|
286
|
+
return { person: p, token: await issueLink(p) };
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
/** Ends every session they hold and every unused link, and gives one new link. */
|
|
290
|
+
async signOutEverywhere(actor: Person | null, id: string): Promise<{ person: Person; token: string }> {
|
|
291
|
+
allow(actor, 'people');
|
|
292
|
+
const p = await needActive(id);
|
|
293
|
+
// The epoch first: a failure after it has still ended every session. Atomic, so two at once raise it twice.
|
|
294
|
+
const next = { ...p, epoch: await kv.hincrby(KEY.epochs, id, 1) };
|
|
295
|
+
await dropLinks(id);
|
|
296
|
+
return { person: next, token: await issueLink(next) };
|
|
165
297
|
},
|
|
166
298
|
|
|
167
299
|
async revoke(actor: Person | null, id: string): Promise<Person> {
|
|
168
300
|
allow(actor, 'people');
|
|
169
301
|
if (actor.id === id) throw new ReviewError('invalid', 'You cannot revoke yourself.');
|
|
170
302
|
const p = await needPerson(id);
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
303
|
+
const revokedAt = p.revokedAt ?? now();
|
|
304
|
+
// Revocation and the epoch before the links go, so a failure part-way never leaves an active
|
|
305
|
+
// person; a leftover link opens nothing for someone revoked.
|
|
306
|
+
if (!p.revokedAt) await kv.hset(KEY.revoked, id, revokedAt);
|
|
307
|
+
const epoch = await kv.hincrby(KEY.epochs, id, 1);
|
|
308
|
+
await dropLinks(id);
|
|
309
|
+
return { ...p, revokedAt, epoch };
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
/** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
|
|
313
|
+
async setAccess(actor: Person | null, id: string, access: unknown): Promise<Person> {
|
|
314
|
+
allow(actor, 'people');
|
|
315
|
+
const length = valid(checkAccess(access));
|
|
316
|
+
const p = await needPerson(id);
|
|
317
|
+
if (p.revokedAt) throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
318
|
+
await savePerson({ ...p, accessEndsAt: accessEnd(length, deps.now()) });
|
|
319
|
+
// Read back, so the answer carries a revocation or epoch that landed meanwhile.
|
|
320
|
+
return (await person(id)) as Person;
|
|
321
|
+
},
|
|
322
|
+
|
|
323
|
+
/** How long a new Client keeps access unless Studio says otherwise. */
|
|
324
|
+
async defaultAccess(actor: Person | null): Promise<Access> {
|
|
325
|
+
allow(actor, 'people');
|
|
326
|
+
return readDefault();
|
|
175
327
|
},
|
|
176
328
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const p = await person(invite.person);
|
|
183
|
-
return p && !p.revokedAt ? p : null;
|
|
329
|
+
async setDefaultAccess(actor: Person | null, access: unknown): Promise<Access> {
|
|
330
|
+
allow(actor, 'people');
|
|
331
|
+
const length = valid(checkAccess(access));
|
|
332
|
+
await kv.hset(KEY.settings, 'access', length);
|
|
333
|
+
return length;
|
|
184
334
|
},
|
|
185
335
|
|
|
186
|
-
|
|
336
|
+
/** Who a link signs in, without using it: mail and chat previews open links too. */
|
|
337
|
+
async peekLink(token: string): Promise<Person | null> {
|
|
338
|
+
const found = await findLink(token);
|
|
339
|
+
return found ? linkPerson(found.link) : null;
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Uses a link up and returns who it signs in. The delete is the claim: of
|
|
344
|
+
* two requests with one token, only the one whose delete removed the link
|
|
345
|
+
* gets a person, so a link signs in one device at most. Who it signs in is
|
|
346
|
+
* read and checked first, so the delete is the last store call.
|
|
347
|
+
*/
|
|
348
|
+
async useLink(token: string): Promise<Person | null> {
|
|
349
|
+
const found = await findLink(token);
|
|
350
|
+
// Every read and check comes before the delete, so a store failure before it leaves the link usable.
|
|
351
|
+
const p = found && (await linkPerson(found.link));
|
|
352
|
+
if (!found || !p) return null;
|
|
353
|
+
if ((await kv.hdel(KEY.links, found.key)) !== 1) return null;
|
|
354
|
+
// A revoke or sign out everywhere can land between the check and the claim, before its
|
|
355
|
+
// dropLinks reaches this link: read again, and sign in only someone still active on the same epoch.
|
|
356
|
+
const after = await person(p.id);
|
|
357
|
+
return active(after) && after.epoch === p.epoch ? after : null;
|
|
358
|
+
},
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* A prototype's comments as the actor's view sees them. `viewAs: 'client'`
|
|
362
|
+
* is Studio's Client view; it never widens a Client's.
|
|
363
|
+
*/
|
|
364
|
+
async listComments(actor: Person | null, prototype: string, viewAs?: unknown): Promise<Comment[]> {
|
|
187
365
|
allow(actor, 'read');
|
|
188
|
-
|
|
366
|
+
const view = viewOf(actor, valid(checkViewAs(viewAs)));
|
|
367
|
+
return visibleTo(view, await commentsOf(prototype));
|
|
189
368
|
},
|
|
190
369
|
|
|
191
|
-
/** Every comment on every prototype, for the CLI
|
|
192
|
-
async allComments(): Promise<Comment[]> {
|
|
370
|
+
/** Every comment on every prototype the actor may see, for the CLI (which acts as Studio). */
|
|
371
|
+
async allComments(actor: Person | null): Promise<Comment[]> {
|
|
372
|
+
allow(actor, 'read');
|
|
193
373
|
const prototypes = await kv.smembers(KEY.prototypes);
|
|
194
|
-
return (await Promise.all(prototypes.map(commentsOf))).flat();
|
|
374
|
+
return visibleTo(viewOf(actor), (await Promise.all(prototypes.map(commentsOf))).flat());
|
|
195
375
|
},
|
|
196
376
|
|
|
377
|
+
/**
|
|
378
|
+
* A new thread. A Client's is client-facing whatever the input says;
|
|
379
|
+
* Studio's is internal unless `audience` is 'client'. Its number is the
|
|
380
|
+
* prototype's next, never one used before.
|
|
381
|
+
*/
|
|
197
382
|
async add(
|
|
198
383
|
actor: Person | null,
|
|
199
|
-
input: { body?: unknown; anchor?: unknown; states?: unknown },
|
|
384
|
+
input: { body?: unknown; anchor?: unknown; states?: unknown; audience?: unknown },
|
|
200
385
|
): Promise<Comment> {
|
|
201
386
|
allow(actor, 'comment');
|
|
202
|
-
const [body, anchor] = validAll
|
|
387
|
+
const [body, anchor, asked] = validAll<[string, Anchor, Audience | null]>(
|
|
388
|
+
checkBody(input.body),
|
|
389
|
+
checkAnchor(input.anchor),
|
|
390
|
+
checkAudience(input.audience),
|
|
391
|
+
);
|
|
203
392
|
const id = newId();
|
|
204
393
|
const triage = deps.triage
|
|
205
394
|
? await deps
|
|
@@ -218,6 +407,9 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
218
407
|
status: 'open',
|
|
219
408
|
resolvedBy: null,
|
|
220
409
|
triage,
|
|
410
|
+
audience: audienceFor(actor, asked),
|
|
411
|
+
// Atomic and only raised: two threads at once get two numbers, and a failed save skips one.
|
|
412
|
+
number: await kv.hincrby(KEY.counters, anchor.prototype, 1),
|
|
221
413
|
};
|
|
222
414
|
await kv.sadd(KEY.prototypes, anchor.prototype);
|
|
223
415
|
await saveComment(anchor.prototype, c);
|
|
@@ -228,8 +420,8 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
228
420
|
actor: Person | null,
|
|
229
421
|
input: { prototype: string; thread: string; body?: unknown },
|
|
230
422
|
): Promise<Comment> {
|
|
231
|
-
|
|
232
|
-
|
|
423
|
+
const root = await needRoot(actor, input.prototype, input.thread);
|
|
424
|
+
allow(actor, 'comment', root);
|
|
233
425
|
const id = newId();
|
|
234
426
|
const c: Comment = {
|
|
235
427
|
id,
|
|
@@ -242,6 +434,9 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
242
434
|
status: 'open',
|
|
243
435
|
resolvedBy: null,
|
|
244
436
|
triage: null,
|
|
437
|
+
// A reply follows its thread; nobody sets it.
|
|
438
|
+
audience: root.audience,
|
|
439
|
+
number: root.number,
|
|
245
440
|
};
|
|
246
441
|
await saveComment(input.prototype, c);
|
|
247
442
|
return c;
|
|
@@ -251,8 +446,8 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
251
446
|
actor: Person | null,
|
|
252
447
|
input: { prototype: string; id: string; body?: unknown },
|
|
253
448
|
): Promise<Comment> {
|
|
254
|
-
const c = await
|
|
255
|
-
allow(actor, 'edit', c);
|
|
449
|
+
const { comment: c, root } = await needThread(actor, input.prototype, input.id);
|
|
450
|
+
allow(actor, 'edit', root, c);
|
|
256
451
|
const next = { ...c, body: valid(checkBody(input.body)), editedAt: now() };
|
|
257
452
|
await saveComment(input.prototype, next);
|
|
258
453
|
return next;
|
|
@@ -260,20 +455,20 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
260
455
|
|
|
261
456
|
/** Deletes a comment; deleting a thread's root deletes its replies with it. */
|
|
262
457
|
async remove(actor: Person | null, input: { prototype: string; id: string }): Promise<string[]> {
|
|
263
|
-
const c = await
|
|
264
|
-
allow(actor, 'delete', c);
|
|
265
|
-
const gone = isRoot(c)
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return gone;
|
|
458
|
+
const { comment: c, root } = await needThread(actor, input.prototype, input.id);
|
|
459
|
+
allow(actor, 'delete', root, c);
|
|
460
|
+
const gone = isRoot(c) ? (await commentsOf(input.prototype)).filter((x) => x.thread === c.id) : [c];
|
|
461
|
+
for (const x of gone) await kv.hdel(KEY.comments(input.prototype), x.id);
|
|
462
|
+
// The answer names only what the actor may see, judged with the thread's root beside it.
|
|
463
|
+
const shown = new Set(visibleTo(viewOf(actor), [root, ...gone]).map((x) => x.id));
|
|
464
|
+
return gone.filter((x) => shown.has(x.id)).map((x) => x.id);
|
|
270
465
|
},
|
|
271
466
|
|
|
272
467
|
async resolve(
|
|
273
468
|
actor: Person | null,
|
|
274
469
|
input: { prototype: string; id: string; resolved: boolean },
|
|
275
470
|
): Promise<Comment> {
|
|
276
|
-
const c = await needRoot(input.prototype, input.id);
|
|
471
|
+
const c = await needRoot(actor, input.prototype, input.id);
|
|
277
472
|
allow(actor, 'resolve', c);
|
|
278
473
|
const next: Comment = input.resolved
|
|
279
474
|
? { ...c, status: 'resolved', resolvedBy: authorOf(actor) }
|
|
@@ -287,7 +482,7 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
287
482
|
input: { prototype: string; id: string; triage?: unknown; states?: unknown },
|
|
288
483
|
): Promise<Comment> {
|
|
289
484
|
allow(actor, 'triage');
|
|
290
|
-
const c = await needRoot(input.prototype, input.id);
|
|
485
|
+
const c = await needRoot(actor, input.prototype, input.id);
|
|
291
486
|
const states = checkStates(input.states);
|
|
292
487
|
const { kind, state } = valid(checkTriage(input.triage, states.length ? states : null));
|
|
293
488
|
const next: Comment = { ...c, triage: { kind, state, by: 'person', confidence: null } };
|
|
@@ -295,11 +490,12 @@ export function createReview(kv: KV, overrides: Partial<Deps> = {}) {
|
|
|
295
490
|
return next;
|
|
296
491
|
},
|
|
297
492
|
|
|
298
|
-
/** Where a comment lives, for the CLI, which has an id and no prototype. */
|
|
299
|
-
async locate(id: string): Promise<{ prototype: string; comment: Comment } | null> {
|
|
493
|
+
/** Where a comment lives, for the CLI, which has an id and no prototype; null when the actor may not see it. */
|
|
494
|
+
async locate(actor: Person | null, id: string): Promise<{ prototype: string; comment: Comment } | null> {
|
|
495
|
+
allow(actor, 'read');
|
|
300
496
|
for (const prototype of await kv.smembers(KEY.prototypes)) {
|
|
301
|
-
const
|
|
302
|
-
if (
|
|
497
|
+
const found = await seen(actor, prototype, id);
|
|
498
|
+
if (found) return { prototype, comment: found.comment };
|
|
303
499
|
}
|
|
304
500
|
return null;
|
|
305
501
|
},
|
package/core/review/store.ts
CHANGED
|
@@ -6,8 +6,11 @@
|
|
|
6
6
|
export interface KV {
|
|
7
7
|
hget(key: string, field: string): Promise<string | null>;
|
|
8
8
|
hset(key: string, field: string, value: string): Promise<void>;
|
|
9
|
-
|
|
9
|
+
/** How many fields it removed: 1, or 0 when the field was not there. Atomic, so it can claim a record. */
|
|
10
|
+
hdel(key: string, field: string): Promise<number>;
|
|
10
11
|
hgetall(key: string): Promise<Record<string, string>>;
|
|
12
|
+
/** Adds `by` to a field's integer (absent is 0) and returns the result. Atomic: two calls always add both. */
|
|
13
|
+
hincrby(key: string, field: string, by: number): Promise<number>;
|
|
11
14
|
sadd(key: string, member: string): Promise<void>;
|
|
12
15
|
smembers(key: string): Promise<string[]>;
|
|
13
16
|
}
|
|
@@ -17,12 +20,22 @@ export interface KV {
|
|
|
17
20
|
* another app's data. Bumped when the stored shapes change without a
|
|
18
21
|
* migration: records under an older prefix are simply never read.
|
|
19
22
|
*/
|
|
20
|
-
const PREFIX = '
|
|
23
|
+
const PREFIX = 'hub3:';
|
|
21
24
|
|
|
22
25
|
export const KEY = {
|
|
26
|
+
/** A person's profile: everything but revocation and epoch, which nothing here can overwrite. */
|
|
23
27
|
people: `${PREFIX}people`,
|
|
24
|
-
|
|
28
|
+
/** When a person was revoked. Written once by revoke, never cleared. */
|
|
29
|
+
revoked: `${PREFIX}revoked`,
|
|
30
|
+
/** A person's session epoch, only ever raised, by HINCRBY. Absent is 0. */
|
|
31
|
+
epochs: `${PREFIX}epochs`,
|
|
32
|
+
/** One-time sign-in links: hex(sha256(token)) to a Link. */
|
|
33
|
+
links: `${PREFIX}links`,
|
|
34
|
+
/** Project settings, one field each: `access` is the default access length. */
|
|
35
|
+
settings: `${PREFIX}settings`,
|
|
25
36
|
prototypes: `${PREFIX}prototypes`,
|
|
37
|
+
/** The last thread number given on each prototype, one field per prototype, only ever raised by HINCRBY. */
|
|
38
|
+
counters: `${PREFIX}counters`,
|
|
26
39
|
comments: (prototype: string) => `${PREFIX}comments:${prototype}`,
|
|
27
40
|
} as const;
|
|
28
41
|
|
|
@@ -44,10 +57,17 @@ export function memoryKV(initial: Snapshot = { hashes: {}, sets: {} }, onChange?
|
|
|
44
57
|
changed();
|
|
45
58
|
},
|
|
46
59
|
hdel: async (key, field) => {
|
|
47
|
-
hashes.get(key)?.delete(field);
|
|
60
|
+
if (!hashes.get(key)?.delete(field)) return 0;
|
|
48
61
|
changed();
|
|
62
|
+
return 1;
|
|
49
63
|
},
|
|
50
64
|
hgetall: async (key) => Object.fromEntries(hashes.get(key) ?? []),
|
|
65
|
+
hincrby: async (key, field, by) => {
|
|
66
|
+
const n = Number(hash(key).get(field) ?? 0) + by;
|
|
67
|
+
hash(key).set(field, String(n));
|
|
68
|
+
changed();
|
|
69
|
+
return n;
|
|
70
|
+
},
|
|
51
71
|
sadd: async (key, member) => {
|
|
52
72
|
(sets.get(key) ?? sets.set(key, new Set()).get(key)!).add(member);
|
|
53
73
|
changed();
|