@milesroxas/design-hub 0.10.0 → 0.11.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.
- package/README.md +13 -11
- package/core/files.test.ts +76 -0
- package/core/files.ts +131 -0
- package/core/prototype.test.ts +124 -7
- package/core/prototype.ts +133 -24
- package/core/review/model.ts +119 -2
- package/core/review/policy.test.ts +79 -22
- package/core/review/policy.ts +36 -8
- package/core/review/service.test.ts +1464 -340
- package/core/review/service.ts +777 -191
- package/core/review/store.test.ts +91 -0
- package/core/review/store.ts +143 -33
- package/dist/core/files.d.ts +39 -0
- package/dist/core/files.js +107 -0
- package/dist/core/prototype.d.ts +61 -12
- package/dist/core/prototype.js +105 -26
- package/dist/core/review/model.d.ts +76 -2
- package/dist/core/review/model.js +76 -1
- package/dist/core/review/policy.d.ts +11 -4
- package/dist/core/review/policy.js +14 -2
- package/dist/core/review/service.d.ts +197 -45
- package/dist/core/review/service.js +702 -192
- package/dist/core/review/store.d.ts +84 -31
- package/dist/core/review/store.js +66 -33
- package/dist/scripts/bootstrap.d.ts +4 -34
- package/dist/scripts/bootstrap.js +9 -244
- package/dist/scripts/cli.js +6 -0
- package/dist/scripts/client.d.ts +45 -0
- package/dist/scripts/client.js +320 -0
- package/dist/scripts/commands.js +6 -0
- package/dist/scripts/comments.d.ts +8 -26
- package/dist/scripts/comments.js +27 -14
- package/dist/scripts/lib/builds.d.ts +40 -0
- package/dist/scripts/lib/builds.js +198 -0
- package/dist/scripts/lib/cli.js +3 -1
- package/dist/scripts/lib/deploy.d.ts +6 -90
- package/dist/scripts/lib/deploy.js +24 -137
- package/dist/scripts/lib/dns.d.ts +49 -0
- package/dist/scripts/lib/dns.js +141 -0
- package/dist/scripts/lib/fake-host.d.ts +168 -0
- package/dist/scripts/lib/fake-host.js +244 -0
- package/dist/scripts/lib/host.d.ts +87 -0
- package/dist/scripts/lib/host.js +362 -0
- package/dist/scripts/lib/linked.d.ts +51 -0
- package/dist/scripts/lib/linked.js +124 -0
- package/dist/scripts/lib/names.d.ts +35 -0
- package/dist/scripts/lib/names.js +77 -0
- package/dist/scripts/lib/review.d.ts +23 -2
- package/dist/scripts/lib/review.js +32 -15
- package/dist/scripts/lib/studio-deps.d.ts +35 -0
- package/dist/scripts/lib/studio-deps.js +79 -0
- package/dist/scripts/lib/studios.d.ts +70 -0
- package/dist/scripts/lib/studios.js +187 -0
- package/dist/scripts/lib/update.d.ts +33 -0
- package/dist/scripts/lib/update.js +40 -0
- package/dist/scripts/lib/vercel.d.ts +1 -23
- package/dist/scripts/lib/vercel.js +6 -81
- package/dist/scripts/people.d.ts +7 -11
- package/dist/scripts/people.js +51 -46
- package/dist/scripts/preflight.d.ts +8 -1
- package/dist/scripts/preflight.js +36 -19
- package/dist/scripts/project.d.ts +86 -0
- package/dist/scripts/project.js +141 -0
- package/dist/scripts/share.d.ts +105 -30
- package/dist/scripts/share.js +82 -154
- package/dist/scripts/skills.d.ts +18 -2
- package/dist/scripts/skills.js +22 -3
- package/dist/scripts/studio.d.ts +75 -0
- package/dist/scripts/studio.js +401 -0
- package/dist/scripts/upgrade.d.ts +10 -2
- package/dist/scripts/upgrade.js +44 -14
- package/dist/server/api.d.ts +22 -11
- package/dist/server/api.js +116 -47
- package/dist/server/blob.d.ts +10 -0
- package/dist/server/blob.js +53 -0
- package/dist/server/checks.js +2 -2
- package/dist/server/dev.js +4 -4
- package/dist/server/env.d.ts +2 -0
- package/dist/server/env.js +9 -0
- package/dist/server/fake-upstash.js +52 -3
- package/dist/server/hosts.d.ts +11 -0
- package/dist/server/hosts.js +31 -0
- package/dist/server/local.d.ts +7 -1
- package/dist/server/local.js +10 -1
- package/dist/server/mark.d.ts +41 -0
- package/dist/server/mark.js +111 -0
- package/dist/server/session.d.ts +17 -9
- package/dist/server/session.js +22 -45
- package/dist/server/upstash.js +20 -0
- package/dist/vercel/files.d.ts +11 -0
- package/dist/vercel/files.js +68 -0
- package/dist/vercel/index.d.ts +4 -3
- package/dist/vercel/index.js +12 -8
- package/dist/vercel/proxy.d.ts +69 -4
- package/dist/vercel/proxy.js +171 -107
- package/dist/vite/plugin.d.ts +24 -1
- package/dist/vite/plugin.js +46 -2
- package/kit/dials.tsx +2 -2
- package/package.json +2 -1
- package/shell/Guide.tsx +11 -5
- package/shell/Index.tsx +32 -10
- package/shell/Notice.tsx +1 -1
- package/shell/controls.ts +1 -1
- package/shell/hub.ts +5 -1
- package/shell/main.tsx +18 -3
- package/shell/review/Review.tsx +6 -4
- package/shell/review/ThreadCard.tsx +20 -7
- package/shell/review/client.ts +42 -14
- package/shell/review/view.test.ts +19 -0
- package/shell/review/view.ts +25 -0
- package/shell/shell.css +4 -0
- package/shell/studio/ClientHub.tsx +98 -0
- package/shell/studio/hub.css +48 -0
- package/shell/studio/index.html +14 -0
- package/shell/studio/main.tsx +17 -0
- package/skills/miles-deploy/SKILL.md +120 -65
- package/skills/miles-design-hub/SKILL.md +11 -9
- package/skills/miles-review/SKILL.md +5 -7
- package/skills/miles-worktrees/SKILL.md +3 -3
- package/templates/conductor/settings.toml +1 -1
- package/templates/studio/.env.example +9 -0
- package/templates/studio/.vercelignore +5 -0
- package/templates/studio/README.md +84 -0
- package/templates/studio/_gitignore +7 -0
- package/templates/studio/api/files.ts +4 -0
- package/templates/studio/api/review.ts +4 -0
- package/templates/studio/build.mjs +4 -0
- package/templates/studio/package.json +16 -0
- package/templates/studio/proxy.ts +4 -0
- package/templates/studio/studio.json +9 -0
- package/templates/studio/vercel.json +27 -0
|
@@ -4,12 +4,18 @@
|
|
|
4
4
|
// policy.ts hold everywhere. Writes are per record (one hash field per
|
|
5
5
|
// person, per comment), so two people commenting at once never collide;
|
|
6
6
|
// two edits of the same comment at once keep the last.
|
|
7
|
+
//
|
|
8
|
+
// Every method takes the Scope it runs in first, and every key it reads or
|
|
9
|
+
// writes is built from that scope (keyOf in store.ts). A person outside the
|
|
10
|
+
// scope gets the answer an unknown id gets, never forbidden, so no answer
|
|
11
|
+
// says what another client holds.
|
|
7
12
|
import { base64url, hex, sha256 } from '../bytes.js';
|
|
8
13
|
import { controlField, readMarks } from '../controls.js';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
14
|
+
import { isBuildId } from '../files.js';
|
|
15
|
+
import { hostName, isClientSlug, isProjectSlug, isPrototypeSlug } from '../prototype.js';
|
|
16
|
+
import { accessEnd, authorOf, checkAccess, checkAnchor, checkAudience, checkBody, checkClientInput, checkControl, checkPersonInput, checkProjectInput, checkPrototypeAudience, checkStates, checkTriage, checkViewAs, DEFAULT_ACCESS, HOST_SECONDS, hasAccess, isProfile, isRoot, isStudioClient, LINK_DAYS, LOCAL_CLIENT, readClient, readComment, readProject, } from './model.js';
|
|
17
|
+
import { audienceFor, can, inScope, viewOf, visibleTo } from './policy.js';
|
|
18
|
+
import { keyOf, sweepPrefix } from './store.js';
|
|
13
19
|
export class ReviewError extends Error {
|
|
14
20
|
code;
|
|
15
21
|
problems;
|
|
@@ -28,6 +34,31 @@ const defaults = {
|
|
|
28
34
|
/** What a link's token looks like: 32 random bytes, base64url. Anything else is never looked up. */
|
|
29
35
|
const LINK_TOKEN = /^[\w-]{20,100}$/;
|
|
30
36
|
const linkKey = async (token) => hex(await sha256(token));
|
|
37
|
+
/**
|
|
38
|
+
* How long a slug or host stays out of reach after its client is archived:
|
|
39
|
+
* twice the time a studio site's instance keeps a host's answer. An instance
|
|
40
|
+
* that cached the archived client's record keeps accepting that client's
|
|
41
|
+
* fresh tokens until its answer runs out; if the slug came back sooner, it
|
|
42
|
+
* would show them the new client's projects.
|
|
43
|
+
*/
|
|
44
|
+
export const REUSE_SECONDS = 2 * HOST_SECONDS;
|
|
45
|
+
function readBuildEntry(json) {
|
|
46
|
+
try {
|
|
47
|
+
const v = json === null ? null : JSON.parse(json);
|
|
48
|
+
return v && Number.isSafeInteger(v.n) && typeof v.live === 'boolean' ? { n: v.n, live: v.live } : null;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const tooSoon = (seconds) => new ReviewError('invalid', `That client or host was archived moments ago: add it again in ${seconds} seconds.`, [`wait: ${seconds} seconds`]);
|
|
55
|
+
/** What an op answers when what it names is not there, or is outside the actor's scope. */
|
|
56
|
+
const NO_COMMENT = 'No such comment.';
|
|
57
|
+
const NO_PERSON = 'No such person.';
|
|
58
|
+
const NO_PROTOTYPE = 'No such prototype.';
|
|
59
|
+
const NO_CLIENT = 'No such client.';
|
|
60
|
+
export const NO_PROJECT = 'No such project.';
|
|
61
|
+
const homeOf = ({ client }) => (client === null ? 'studio' : { client });
|
|
31
62
|
export function createReview(kv, overrides = {}) {
|
|
32
63
|
const deps = { ...defaults, ...overrides };
|
|
33
64
|
const now = () => deps.now().toISOString();
|
|
@@ -43,10 +74,56 @@ export function createReview(kv, overrides = {}) {
|
|
|
43
74
|
return null;
|
|
44
75
|
}
|
|
45
76
|
};
|
|
46
|
-
|
|
77
|
+
/** Signed in, then in scope. Out of scope answers `missing`, what the op answers an unknown id. */
|
|
78
|
+
function admit(scope, actor, missing) {
|
|
47
79
|
if (!actor)
|
|
48
80
|
throw new ReviewError('unauthorized', 'Sign in with your link to do that.');
|
|
49
|
-
if (!
|
|
81
|
+
if (!inScope(actor, scope))
|
|
82
|
+
throw new ReviewError('not_found', missing);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Whether ops may run in a client: its record is there and not archived,
|
|
86
|
+
* or it is the local store's, which never has one (LOCAL_CLIENT).
|
|
87
|
+
*/
|
|
88
|
+
const isLive = async (client) => client === LOCAL_CLIENT || (await clientAt(client))?.archivedAt === null;
|
|
89
|
+
/**
|
|
90
|
+
* The first check of every op with an actor that runs in a client: signed
|
|
91
|
+
* in, in scope, the scope's client live, then allowed. Out of scope, or a
|
|
92
|
+
* client archived or gone, answers `missing`, what the op answers an
|
|
93
|
+
* unknown id, so it never tells a Client whether another client holds
|
|
94
|
+
* anything. The client is read here, from the store, on every op: a host's
|
|
95
|
+
* answer cached by an instance can name a client archived since, and
|
|
96
|
+
* nothing may write into the keys its archive swept.
|
|
97
|
+
*/
|
|
98
|
+
async function allow(scope, actor, action, missing) {
|
|
99
|
+
admit(scope, actor, missing);
|
|
100
|
+
if (!(await isLive(scope.client)))
|
|
101
|
+
throw new ReviewError('not_found', missing);
|
|
102
|
+
permit(scope, actor, action);
|
|
103
|
+
return actor;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The first check of an op on a client's record itself (add, read,
|
|
107
|
+
* archive, forget), which runs while the record is absent or archived:
|
|
108
|
+
* signed in, in scope, then Studio. Nothing it does writes in the client.
|
|
109
|
+
*/
|
|
110
|
+
function allowRecord(scope, actor) {
|
|
111
|
+
admit(scope, actor, NO_CLIENT);
|
|
112
|
+
permit(scope, actor, 'setup');
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The first check of an op on the studio as a whole, which names no client:
|
|
116
|
+
* signed in, then Studio. A Client is refused; no answer names a client.
|
|
117
|
+
*/
|
|
118
|
+
function allowStudio(actor) {
|
|
119
|
+
if (!actor)
|
|
120
|
+
throw new ReviewError('unauthorized', 'Sign in with your link to do that.');
|
|
121
|
+
if (actor.kind !== 'studio' || actor.client !== null || actor.revokedAt)
|
|
122
|
+
throw new ReviewError('forbidden', 'You cannot do that.');
|
|
123
|
+
}
|
|
124
|
+
/** Whether the actor may act on this thread, or on `subject`, a reply in it. */
|
|
125
|
+
function permit(scope, actor, action, root, subject) {
|
|
126
|
+
if (!can(actor, scope, action, root, subject))
|
|
50
127
|
throw new ReviewError('forbidden', 'You cannot do that.');
|
|
51
128
|
}
|
|
52
129
|
function valid(checked) {
|
|
@@ -61,28 +138,46 @@ export function createReview(kv, overrides = {}) {
|
|
|
61
138
|
throw new ReviewError('invalid', 'Some fields are not valid.', problems);
|
|
62
139
|
return checks.map((c) => c.value);
|
|
63
140
|
}
|
|
64
|
-
// A person is three records. The profile (
|
|
141
|
+
// A person is three records. The profile (`people`) is read-modify-write, so
|
|
65
142
|
// a stale write of it can land after anything. Revocation and the epoch live
|
|
66
143
|
// outside it: revocation is written once and never cleared, the epoch only
|
|
67
144
|
// rises (HINCRBY), so no profile write, however stale, can revive a session.
|
|
68
|
-
|
|
145
|
+
// A person's client is where the three live, never a field anyone writes.
|
|
146
|
+
const compose = (home, json, revoked, epoch) => {
|
|
69
147
|
const profile = safeParse(json);
|
|
70
148
|
if (!isProfile(profile))
|
|
71
149
|
return null;
|
|
150
|
+
// Studio lives with the studio and a Client with their client: a profile anywhere else is no person.
|
|
151
|
+
if ((profile.kind === 'studio') !== (home === 'studio'))
|
|
152
|
+
return null;
|
|
72
153
|
// Any revocation record means revoked, an empty one included: it reads as the start of time.
|
|
73
154
|
const revokedAt = revoked == null ? null : revoked || new Date(0).toISOString();
|
|
74
|
-
|
|
155
|
+
const client = home === 'studio' ? null : home.client;
|
|
156
|
+
return { ...profile, client, revokedAt, epoch: Number(epoch ?? 0) };
|
|
75
157
|
};
|
|
76
|
-
const
|
|
158
|
+
const personAt = async (home, id) => {
|
|
77
159
|
const [json, revoked, epoch] = await Promise.all([
|
|
78
|
-
kv.hget(
|
|
79
|
-
kv.hget(
|
|
80
|
-
kv.hget(
|
|
160
|
+
kv.hget(keyOf(home, 'people'), id),
|
|
161
|
+
kv.hget(keyOf(home, 'revoked'), id),
|
|
162
|
+
kv.hget(keyOf(home, 'epochs'), id),
|
|
81
163
|
]);
|
|
82
|
-
return compose(json, revoked, epoch);
|
|
164
|
+
return compose(home, json, revoked, epoch);
|
|
83
165
|
};
|
|
84
|
-
/**
|
|
85
|
-
const
|
|
166
|
+
/** Someone the scope reaches: a Client of its client, else a Studio person. */
|
|
167
|
+
const person = async (scope, id) => (await personAt(scope, id)) ?? personAt('studio', id);
|
|
168
|
+
/** Every person at one home, skipping any profile that is not a well-formed person. */
|
|
169
|
+
const peopleAt = async (home) => {
|
|
170
|
+
const [profiles, revoked, epochs] = await Promise.all([
|
|
171
|
+
kv.hgetall(keyOf(home, 'people')),
|
|
172
|
+
kv.hgetall(keyOf(home, 'revoked')),
|
|
173
|
+
kv.hgetall(keyOf(home, 'epochs')),
|
|
174
|
+
]);
|
|
175
|
+
return Object.entries(profiles)
|
|
176
|
+
.map(([id, json]) => compose(home, json, revoked[id], epochs[id]))
|
|
177
|
+
.filter((p) => p !== null);
|
|
178
|
+
};
|
|
179
|
+
/** Writes the profile alone, at the person's home: revocation, epoch and client are never part of it. */
|
|
180
|
+
const savePerson = ({ revokedAt: _r, epoch: _e, client, ...profile }) => kv.hset(keyOf(homeOf({ client }), 'people'), profile.id, JSON.stringify(profile));
|
|
86
181
|
/** Not revoked, and their access has not ended. */
|
|
87
182
|
const active = (p) => !!p && hasAccess(p, deps.now());
|
|
88
183
|
async function issueLink(p) {
|
|
@@ -92,47 +187,116 @@ export function createReview(kv, overrides = {}) {
|
|
|
92
187
|
person: p.id,
|
|
93
188
|
expiresAt: new Date(deps.now().getTime() + LINK_DAYS * DAY_MS).toISOString(),
|
|
94
189
|
};
|
|
95
|
-
|
|
190
|
+
// A link lives with its person, so it signs in only where that person's scope reaches.
|
|
191
|
+
await kv.hset(keyOf(homeOf(p), 'links'), await linkKey(token), JSON.stringify(link));
|
|
96
192
|
return token;
|
|
97
193
|
}
|
|
98
|
-
async function dropLinks(
|
|
99
|
-
const
|
|
100
|
-
for (const [hash, json] of Object.entries(
|
|
101
|
-
if (JSON.parse(json).person ===
|
|
102
|
-
await kv.hdel(
|
|
194
|
+
async function dropLinks(p) {
|
|
195
|
+
const links = keyOf(homeOf(p), 'links');
|
|
196
|
+
for (const [hash, json] of Object.entries(await kv.hgetall(links)))
|
|
197
|
+
if (JSON.parse(json).person === p.id)
|
|
198
|
+
await kv.hdel(links, hash);
|
|
103
199
|
}
|
|
104
|
-
/** The link a token names, or null for a malformed or unknown token. */
|
|
105
|
-
async function findLink(token) {
|
|
200
|
+
/** The link a token names in the scope's client or the studio, or null for a malformed or unknown token. */
|
|
201
|
+
async function findLink(scope, token) {
|
|
106
202
|
if (!LINK_TOKEN.test(token))
|
|
107
203
|
return null;
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
|
|
204
|
+
const hash = await linkKey(token);
|
|
205
|
+
for (const home of [scope, 'studio']) {
|
|
206
|
+
const link = parse(await kv.hget(keyOf(home, 'links'), hash));
|
|
207
|
+
if (link)
|
|
208
|
+
return { home, hash, link };
|
|
209
|
+
}
|
|
210
|
+
return null;
|
|
111
211
|
}
|
|
112
|
-
/** The person a current link signs in, while they may sign in. */
|
|
113
|
-
async function linkPerson(link) {
|
|
212
|
+
/** The person a current link signs in, while they may sign in here. */
|
|
213
|
+
async function linkPerson(scope, { home, link }) {
|
|
114
214
|
if (link.expiresAt <= now())
|
|
115
215
|
return null;
|
|
116
|
-
const p = await
|
|
117
|
-
return active(p) ? p : null;
|
|
216
|
+
const p = await personAt(home, link.person);
|
|
217
|
+
return active(p) && inScope(p, scope) ? p : null;
|
|
118
218
|
}
|
|
119
|
-
async function
|
|
120
|
-
const stored = await kv.hget(
|
|
219
|
+
async function studioDefault() {
|
|
220
|
+
const stored = await kv.hget(keyOf('studio', 'settings'), 'access');
|
|
121
221
|
return checkAccess(stored).ok ? stored : DEFAULT_ACCESS;
|
|
122
222
|
}
|
|
223
|
+
/** How long a new Client of the scope's client keeps access: the client's default, else the studio's, else 3m. */
|
|
224
|
+
async function readDefault(scope) {
|
|
225
|
+
const own = isClientSlug(scope.client) ? (await clientAt(scope.client))?.access : null;
|
|
226
|
+
return own && checkAccess(own).ok ? own : studioDefault();
|
|
227
|
+
}
|
|
228
|
+
/** A new Studio person, with no link yet; their records live with the studio. */
|
|
229
|
+
async function newStudioPerson(input) {
|
|
230
|
+
const { access, ...fields } = valid(checkPersonInput({ ...input, kind: 'studio' }));
|
|
231
|
+
const p = {
|
|
232
|
+
id: newId(),
|
|
233
|
+
...fields,
|
|
234
|
+
client: null,
|
|
235
|
+
email: null,
|
|
236
|
+
accessEndsAt: accessEnd(access ?? 'open', deps.now()),
|
|
237
|
+
createdAt: now(),
|
|
238
|
+
revokedAt: null,
|
|
239
|
+
epoch: 0,
|
|
240
|
+
};
|
|
241
|
+
await savePerson(p);
|
|
242
|
+
return p;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Seconds until a slug or host archived lately (`held`) may be added
|
|
246
|
+
* again; 0 when it was not, or long enough ago.
|
|
247
|
+
*/
|
|
248
|
+
async function heldFor(field) {
|
|
249
|
+
const at = Date.parse((await kv.hget(keyOf('studio', 'held'), field)) ?? '');
|
|
250
|
+
if (Number.isNaN(at))
|
|
251
|
+
return 0;
|
|
252
|
+
return Math.max(0, Math.ceil((at + REUSE_SECONDS * 1000 - deps.now().getTime()) / 1000));
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Whether no client but `client` has the host: none serves it and no live
|
|
256
|
+
* client's record names it. Only then is its claim this client's to free.
|
|
257
|
+
*/
|
|
258
|
+
async function hostIsFree(client, host) {
|
|
259
|
+
const served = await kv.hget(keyOf('studio', 'hosts'), host);
|
|
260
|
+
if (served !== null && served !== client)
|
|
261
|
+
return false;
|
|
262
|
+
const records = Object.values(await kv.hgetall(keyOf('studio', 'clients'))).map(readClient);
|
|
263
|
+
return !records.some((o) => o && o.slug !== client && o.host === host && o.archivedAt === null);
|
|
264
|
+
}
|
|
265
|
+
/** Raises a claim counter; true when this call raised it to 1 and so holds it. */
|
|
266
|
+
const claim = async (field) => (await kv.hincrby(keyOf('studio', 'claims'), field, 1)) === 1;
|
|
267
|
+
const release = (field) => kv.hdel(keyOf('studio', 'claims'), field);
|
|
268
|
+
/**
|
|
269
|
+
* Removes everything stored under a client's keys, or one project's, by
|
|
270
|
+
* their prefix: whatever an index names and whatever none does, such as a
|
|
271
|
+
* write that passed the live check before an archive and landed after it.
|
|
272
|
+
* Never the local store's (sweepPrefix refuses it). Run only on a client
|
|
273
|
+
* that is not live: one being added, once its slug is claimed, or one
|
|
274
|
+
* archived; or on a project not yet added.
|
|
275
|
+
*/
|
|
276
|
+
const sweep = (scope) => kv.delPrefix(sweepPrefix(scope));
|
|
277
|
+
/**
|
|
278
|
+
* The client an op on a client's record names (archive, forget): a slug a
|
|
279
|
+
* studio client can have. Never the local store's, which has no record and
|
|
280
|
+
* is never archived; it answers as a client that is not there.
|
|
281
|
+
*/
|
|
282
|
+
function recordSlug(scope) {
|
|
283
|
+
if (!isStudioClient(scope.client))
|
|
284
|
+
throw new ReviewError('not_found', NO_CLIENT);
|
|
285
|
+
return scope.client;
|
|
286
|
+
}
|
|
123
287
|
/** Someone Studio can still give a link to. */
|
|
124
|
-
async function needActive(id) {
|
|
125
|
-
const p = await needPerson(id);
|
|
288
|
+
async function needActive(scope, id) {
|
|
289
|
+
const p = await needPerson(scope, id);
|
|
126
290
|
if (p.revokedAt)
|
|
127
291
|
throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
128
292
|
if (!active(p))
|
|
129
293
|
throw new ReviewError('invalid', 'Their access has ended; change their access first.');
|
|
130
294
|
return p;
|
|
131
295
|
}
|
|
132
|
-
async function needPerson(id) {
|
|
133
|
-
const p = await person(id);
|
|
296
|
+
async function needPerson(scope, id) {
|
|
297
|
+
const p = await person(scope, id);
|
|
134
298
|
if (!p)
|
|
135
|
-
throw new ReviewError('not_found',
|
|
299
|
+
throw new ReviewError('not_found', NO_PERSON);
|
|
136
300
|
return p;
|
|
137
301
|
}
|
|
138
302
|
// Every comment read goes through readComment, so a record with no audience reads as internal.
|
|
@@ -143,90 +307,383 @@ export function createReview(kv, overrides = {}) {
|
|
|
143
307
|
* numbers always had. Internal threads count in their own, which starts past any internal
|
|
144
308
|
* thread numbered before the two counted apart, so no internal number is given twice.
|
|
145
309
|
*/
|
|
146
|
-
const nextNumber = async (prototype, audience) => {
|
|
310
|
+
const nextNumber = async (scope, prototype, audience) => {
|
|
311
|
+
const counters = keyOf(scope, 'counters');
|
|
147
312
|
if (audience === 'client')
|
|
148
|
-
return kv.hincrby(
|
|
313
|
+
return kv.hincrby(counters, prototype, 1);
|
|
149
314
|
const field = `${prototype}:internal`;
|
|
150
|
-
const n = await kv.hincrby(
|
|
315
|
+
const n = await kv.hincrby(counters, field, 1);
|
|
151
316
|
if (n !== 1)
|
|
152
317
|
return n;
|
|
153
|
-
const before = Math.max(0, ...(await commentsOf(prototype)).filter((c) => c.audience === 'internal').map((c) => c.number));
|
|
154
|
-
return before ? kv.hincrby(
|
|
318
|
+
const before = Math.max(0, ...(await commentsOf(scope, prototype)).filter((c) => c.audience === 'internal').map((c) => c.number));
|
|
319
|
+
return before ? kv.hincrby(counters, field, before) : n;
|
|
155
320
|
};
|
|
156
|
-
const commentsOf = async (prototype) => Object.values(await kv.hgetall(
|
|
321
|
+
const commentsOf = async (scope, prototype) => Object.values(await kv.hgetall(keyOf(scope, 'comments', prototype)))
|
|
157
322
|
.map(readComment)
|
|
158
323
|
.sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id));
|
|
159
|
-
const commentAt = async (prototype, id) => {
|
|
160
|
-
const json = await kv.hget(
|
|
324
|
+
const commentAt = async (scope, prototype, id) => {
|
|
325
|
+
const json = await kv.hget(keyOf(scope, 'comments', prototype), id);
|
|
161
326
|
return json === null ? null : readComment(json);
|
|
162
327
|
};
|
|
163
|
-
const saveComment = (prototype, c) => kv.hset(
|
|
164
|
-
const isClientFacing = async (prototype) => isPrototypeSlug(prototype) && (await kv.hget(
|
|
328
|
+
const saveComment = (scope, prototype, c) => kv.hset(keyOf(scope, 'comments', prototype), c.id, JSON.stringify(c));
|
|
329
|
+
const isClientFacing = async (scope, prototype) => isPrototypeSlug(prototype) && (await kv.hget(keyOf(scope, 'audiences'), prototype)) === 'client';
|
|
165
330
|
/**
|
|
166
331
|
* Whether a view reaches a prototype's comments at all. A Client's view
|
|
167
332
|
* reaches only a client-facing prototype: on any other, every op answers
|
|
168
333
|
* as if it had no comments, so no answer confirms an internal slug.
|
|
169
334
|
*/
|
|
170
|
-
const reachable = async (view, prototype) => view === 'studio' || (await isClientFacing(prototype));
|
|
335
|
+
const reachable = async (scope, view, prototype) => view === 'studio' || (await isClientFacing(scope, prototype));
|
|
171
336
|
/**
|
|
172
337
|
* A comment and its thread's root, if `actor` may see both; else null. A
|
|
173
338
|
* reply whose root is gone is not there either.
|
|
174
339
|
*/
|
|
175
|
-
async function seen(actor, prototype, id) {
|
|
176
|
-
if (!(await reachable(viewOf(actor), prototype)))
|
|
340
|
+
async function seen(scope, actor, prototype, id) {
|
|
341
|
+
if (!(await reachable(scope, viewOf(actor), prototype)))
|
|
177
342
|
return null;
|
|
178
|
-
const comment = await commentAt(prototype, id);
|
|
343
|
+
const comment = await commentAt(scope, prototype, id);
|
|
179
344
|
if (!comment)
|
|
180
345
|
return null;
|
|
181
|
-
const root = isRoot(comment) ? comment : await commentAt(prototype, comment.thread);
|
|
182
|
-
return root && can(actor, 'read', root, comment) ? { comment, root } : null;
|
|
346
|
+
const root = isRoot(comment) ? comment : await commentAt(scope, prototype, comment.thread);
|
|
347
|
+
return root && can(actor, scope, 'read', root, comment) ? { comment, root } : null;
|
|
183
348
|
}
|
|
184
349
|
/**
|
|
185
350
|
* The comment an op names, and its root. One that `actor` may not see gets
|
|
186
351
|
* the same answer as one that does not exist, so an id never tells a Client
|
|
187
352
|
* an internal thread is there.
|
|
188
353
|
*/
|
|
189
|
-
async function needThread(actor, prototype, id) {
|
|
190
|
-
allow(actor, 'read');
|
|
191
|
-
const found = await seen(
|
|
354
|
+
async function needThread(scope, actor, prototype, id) {
|
|
355
|
+
const by = await allow(scope, actor, 'read', NO_COMMENT);
|
|
356
|
+
const found = await seen(scope, by, prototype, id);
|
|
192
357
|
if (!found)
|
|
193
|
-
throw new ReviewError('not_found',
|
|
194
|
-
return found;
|
|
358
|
+
throw new ReviewError('not_found', NO_COMMENT);
|
|
359
|
+
return { actor: by, ...found };
|
|
195
360
|
}
|
|
196
|
-
async function needRoot(actor, prototype, id) {
|
|
197
|
-
const
|
|
198
|
-
if (!isRoot(comment))
|
|
361
|
+
async function needRoot(scope, actor, prototype, id) {
|
|
362
|
+
const found = await needThread(scope, actor, prototype, id);
|
|
363
|
+
if (!isRoot(found.comment))
|
|
199
364
|
throw new ReviewError('invalid', 'That is a reply; act on its thread.');
|
|
200
|
-
return comment;
|
|
365
|
+
return { actor: found.actor, root: found.comment };
|
|
201
366
|
}
|
|
367
|
+
const clientAt = async (slug) => {
|
|
368
|
+
const c = readClient(await kv.hget(keyOf('studio', 'clients'), slug));
|
|
369
|
+
return c?.slug === slug ? c : null;
|
|
370
|
+
};
|
|
371
|
+
/** The scope's project, when its record is well formed and its own. */
|
|
372
|
+
const projectAt = async ({ client, project }) => {
|
|
373
|
+
if (!isClientSlug(client) || !isProjectSlug(project))
|
|
374
|
+
return null;
|
|
375
|
+
const p = readProject(await kv.hget(keyOf({ client }, 'projects'), project));
|
|
376
|
+
return p?.client === client && p.slug === project ? p : null;
|
|
377
|
+
};
|
|
378
|
+
const saveProject = (p) => kv.hset(keyOf({ client: p.client }, 'projects'), p.slug, JSON.stringify(p));
|
|
202
379
|
return {
|
|
203
380
|
person,
|
|
204
|
-
/**
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
async
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
381
|
+
/**
|
|
382
|
+
* The client a host serves, asked by the gate and the studio's functions
|
|
383
|
+
* with no actor: null for a host no client has, an archived client, or
|
|
384
|
+
* records that disagree, so an unknown host is never a scope.
|
|
385
|
+
*/
|
|
386
|
+
async clientOfHost(host) {
|
|
387
|
+
const name = hostName(host);
|
|
388
|
+
if (!name)
|
|
389
|
+
return null;
|
|
390
|
+
const slug = await kv.hget(keyOf('studio', 'hosts'), name);
|
|
391
|
+
const c = isClientSlug(slug) ? await clientAt(slug) : null;
|
|
392
|
+
return c && c.host === name && c.archivedAt === null ? c : null;
|
|
393
|
+
},
|
|
394
|
+
/** One project of the scope's client, asked with no actor: null when the client has none by that name. */
|
|
395
|
+
projectOf: projectAt,
|
|
396
|
+
/** Every project of the scope's client that has a build to open, by name. */
|
|
397
|
+
async listProjects(scope, actor) {
|
|
398
|
+
await allow(scope, actor, 'read', NO_PROJECT);
|
|
399
|
+
const stored = Object.values(await kv.hgetall(keyOf({ client: scope.client }, 'projects')));
|
|
400
|
+
return stored
|
|
401
|
+
.map(readProject)
|
|
402
|
+
.filter((p) => p?.client === scope.client && p.build !== null)
|
|
403
|
+
.sort((a, b) => a.name.localeCompare(b.name) || a.slug.localeCompare(b.slug));
|
|
404
|
+
},
|
|
405
|
+
/**
|
|
406
|
+
* A new client and its host. Studio only. The slug and the host are each
|
|
407
|
+
* claimed (`claims` in store.ts), so of two adds at once only one gets
|
|
408
|
+
* either, and a slug or a host already taken is refused. The record goes
|
|
409
|
+
* in between the two claims, so a host's claim is only ever held while a
|
|
410
|
+
* record names the host: an add that stops part way leaves a record that
|
|
411
|
+
* `archiveClient` and `forgetClient` find, and so free. A slug or host
|
|
412
|
+
* archived less than REUSE_SECONDS ago is refused, with how long to wait.
|
|
413
|
+
* Once the slug is claimed, everything stored under the slug's prefix is
|
|
414
|
+
* removed before the record goes in, so the client starts clean whatever
|
|
415
|
+
* was written under the slug before, and whether or not a record named it.
|
|
416
|
+
*/
|
|
417
|
+
async addClient(actor, input) {
|
|
418
|
+
const fields = valid(checkClientInput(input));
|
|
419
|
+
allowRecord({ client: fields.slug }, actor);
|
|
420
|
+
const slug = `client:${fields.slug}`;
|
|
421
|
+
const host = `host:${fields.host}`;
|
|
422
|
+
const slugHeld = await claim(slug);
|
|
423
|
+
if (!slugHeld || (await clientAt(fields.slug))) {
|
|
424
|
+
if (slugHeld)
|
|
425
|
+
await release(slug);
|
|
426
|
+
throw new ReviewError('invalid', 'That client exists, or an add or archive of it stopped part way.', [
|
|
427
|
+
'slug: archive the client to free it',
|
|
428
|
+
]);
|
|
429
|
+
}
|
|
430
|
+
const slugWait = await heldFor(slug);
|
|
431
|
+
if (slugWait) {
|
|
432
|
+
await release(slug);
|
|
433
|
+
throw tooSoon(slugWait);
|
|
434
|
+
}
|
|
435
|
+
// A new client starts clean: whatever an earlier client of the slug left, or a write that landed
|
|
436
|
+
// after its archive swept, goes before the record makes the client live.
|
|
437
|
+
await sweep({ client: fields.slug });
|
|
438
|
+
const c = { ...fields, access: null, createdAt: now(), archivedAt: null, provenAt: null };
|
|
439
|
+
const clients = keyOf('studio', 'clients');
|
|
440
|
+
await kv.hset(clients, c.slug, JSON.stringify(c));
|
|
441
|
+
const hostHeld = await claim(host);
|
|
442
|
+
const hostWait = hostHeld ? await heldFor(host) : 0;
|
|
443
|
+
if (!hostHeld || hostWait || (await kv.hget(keyOf('studio', 'hosts'), fields.host))) {
|
|
444
|
+
if (hostHeld)
|
|
445
|
+
await release(host);
|
|
446
|
+
await kv.hdel(clients, c.slug);
|
|
447
|
+
await release(slug);
|
|
448
|
+
if (hostWait)
|
|
449
|
+
throw tooSoon(hostWait);
|
|
450
|
+
throw new ReviewError('invalid', 'That host already serves a client.', ['host: another host']);
|
|
451
|
+
}
|
|
452
|
+
await kv.hset(keyOf('studio', 'hosts'), c.host, c.slug);
|
|
453
|
+
await kv.hdel(keyOf('studio', 'held'), slug);
|
|
454
|
+
await kv.hdel(keyOf('studio', 'held'), host);
|
|
455
|
+
return c;
|
|
456
|
+
},
|
|
457
|
+
/**
|
|
458
|
+
* How many seconds until this slug and host may be added again: 0 when
|
|
459
|
+
* neither was archived in the last REUSE_SECONDS. `client add` asks
|
|
460
|
+
* before it changes any DNS or domain. Studio only.
|
|
461
|
+
*/
|
|
462
|
+
async clientWait(actor, input) {
|
|
463
|
+
allowStudio(actor);
|
|
464
|
+
return Math.max(await heldFor(`client:${input.slug}`), await heldFor(`host:${input.host}`));
|
|
465
|
+
},
|
|
466
|
+
/**
|
|
467
|
+
* A new host proof for the scope's client, in place of any before it:
|
|
468
|
+
* `ask` goes to the client's host and `answer` must come back, which only
|
|
469
|
+
* the gate with this store can give (PROOF_PATH). The store keeps the ask
|
|
470
|
+
* as its hash, so a leaked store answers nothing. Studio only.
|
|
471
|
+
*/
|
|
472
|
+
async startProof(scope, actor) {
|
|
473
|
+
await allow(scope, actor, 'setup', NO_CLIENT);
|
|
474
|
+
const ask = base64url(deps.random(32));
|
|
475
|
+
const answer = base64url(deps.random(32));
|
|
476
|
+
const key = keyOf({ client: scope.client }, 'proof');
|
|
477
|
+
await kv.del(key);
|
|
478
|
+
await kv.hset(key, await linkKey(ask), answer);
|
|
479
|
+
return { ask, answer };
|
|
480
|
+
},
|
|
481
|
+
/**
|
|
482
|
+
* Ends the scope's client's host proof, once its host has answered it,
|
|
483
|
+
* and marks the client proven: it has finished being added (isAdded).
|
|
484
|
+
* The mark goes on first, so a run stopped between the two leaves a
|
|
485
|
+
* proven client with a stale proof, which answers nothing anyone holds.
|
|
486
|
+
* Studio only.
|
|
487
|
+
*/
|
|
488
|
+
async endProof(scope, actor) {
|
|
489
|
+
await allow(scope, actor, 'setup', NO_CLIENT);
|
|
490
|
+
const c = await clientAt(scope.client);
|
|
491
|
+
if (!c)
|
|
492
|
+
throw new ReviewError('not_found', NO_CLIENT);
|
|
493
|
+
const proven = c.provenAt ? c : { ...c, provenAt: now() };
|
|
494
|
+
if (proven !== c)
|
|
495
|
+
await kv.hset(keyOf('studio', 'clients'), c.slug, JSON.stringify(proven));
|
|
496
|
+
await kv.del(keyOf({ client: scope.client }, 'proof'));
|
|
497
|
+
return proven;
|
|
498
|
+
},
|
|
499
|
+
/** The answer to a host proof's ask in the scope's client, asked by the gate with no actor: null for any other ask. */
|
|
500
|
+
async proofAnswer(scope, ask) {
|
|
501
|
+
if (!isClientSlug(scope.client) || typeof ask !== 'string' || !LINK_TOKEN.test(ask))
|
|
502
|
+
return null;
|
|
503
|
+
return kv.hget(keyOf({ client: scope.client }, 'proof'), await linkKey(ask));
|
|
504
|
+
},
|
|
505
|
+
/** The scope's client record. Studio only. */
|
|
506
|
+
async clientRecord(scope, actor) {
|
|
507
|
+
allowRecord(scope, actor);
|
|
508
|
+
const c = await clientAt(scope.client);
|
|
509
|
+
if (!c)
|
|
510
|
+
throw new ReviewError('not_found', NO_CLIENT);
|
|
511
|
+
return c;
|
|
512
|
+
},
|
|
513
|
+
/** Sets how long the scope's client's new people keep access by default; null leaves it to the studio's. Studio only. */
|
|
514
|
+
async setClientAccess(scope, actor, access) {
|
|
515
|
+
await allow(scope, actor, 'setup', NO_CLIENT);
|
|
516
|
+
const length = access === null ? null : valid(checkAccess(access));
|
|
517
|
+
const c = await clientAt(scope.client);
|
|
518
|
+
if (!c)
|
|
519
|
+
throw new ReviewError('not_found', NO_CLIENT);
|
|
520
|
+
const next = { ...c, access: length };
|
|
521
|
+
await kv.hset(keyOf('studio', 'clients'), c.slug, JSON.stringify(next));
|
|
522
|
+
return next;
|
|
523
|
+
},
|
|
524
|
+
/**
|
|
525
|
+
* Archives the scope's client, the first of two steps: its record is
|
|
526
|
+
* marked archived, so its host stops serving at once (clientOfHost), then
|
|
527
|
+
* its host entry, then everything under its prefix (people, links,
|
|
528
|
+
* projects, each project's records, and any key no record names). The record stays, naming the host, until
|
|
529
|
+
* forgetClient, so `client archive` stopped part way and run again still
|
|
530
|
+
* knows the host to take off the site. Answers the record, and whether
|
|
531
|
+
* the host is the client's own to take off: not when another client
|
|
532
|
+
* serves it or a live client's record names it. null when there is no
|
|
533
|
+
* record. Safe to run again. The client's files are the caller's to
|
|
534
|
+
* remove (the files port). Studio only.
|
|
535
|
+
*/
|
|
536
|
+
async archiveClient(scope, actor) {
|
|
537
|
+
allowRecord(scope, actor);
|
|
538
|
+
const client = recordSlug(scope);
|
|
539
|
+
const found = await clientAt(client);
|
|
540
|
+
const c = found && found.archivedAt === null ? { ...found, archivedAt: now() } : found;
|
|
541
|
+
if (c && c !== found)
|
|
542
|
+
await kv.hset(keyOf('studio', 'clients'), client, JSON.stringify(c));
|
|
543
|
+
const hosts = keyOf('studio', 'hosts');
|
|
544
|
+
if (c && (await kv.hget(hosts, c.host)) === client)
|
|
545
|
+
await kv.hdel(hosts, c.host);
|
|
546
|
+
await sweep({ client });
|
|
547
|
+
return c ? { client: c, ownHost: await hostIsFree(client, c.host) } : null;
|
|
548
|
+
},
|
|
549
|
+
/**
|
|
550
|
+
* The second step, once the caller has removed the client's files, DNS
|
|
551
|
+
* record and domain: its keys once more (anything written meanwhile),
|
|
552
|
+
* then the time on its slug and host (`held`), then the host's claim
|
|
553
|
+
* unless another client holds it, then its record, then the slug's claim.
|
|
554
|
+
* With no record, it frees the slug alone: an add that stopped before its
|
|
555
|
+
* record held no host. Refused while the client is not archived. Studio only.
|
|
556
|
+
*/
|
|
557
|
+
async forgetClient(scope, actor) {
|
|
558
|
+
allowRecord(scope, actor);
|
|
559
|
+
const client = recordSlug(scope);
|
|
560
|
+
const c = await clientAt(client);
|
|
561
|
+
if (c && c.archivedAt === null)
|
|
562
|
+
throw new ReviewError('invalid', 'Archive the client first.');
|
|
563
|
+
if (c) {
|
|
564
|
+
await sweep({ client });
|
|
565
|
+
const at = now();
|
|
566
|
+
await kv.hset(keyOf('studio', 'held'), `client:${client}`, at);
|
|
567
|
+
await kv.hset(keyOf('studio', 'held'), `host:${c.host}`, at);
|
|
568
|
+
if (await hostIsFree(client, c.host))
|
|
569
|
+
await release(`host:${c.host}`);
|
|
570
|
+
await kv.hdel(keyOf('studio', 'clients'), client);
|
|
571
|
+
}
|
|
572
|
+
await release(`client:${client}`);
|
|
573
|
+
},
|
|
574
|
+
/** A new project of the scope's client, with no build yet. Studio only. */
|
|
575
|
+
async addProject(scope, actor, input) {
|
|
576
|
+
await allow(scope, actor, 'setup', NO_CLIENT);
|
|
577
|
+
const fields = valid(checkProjectInput(input));
|
|
578
|
+
if (await projectAt({ client: scope.client, project: fields.slug }))
|
|
579
|
+
throw new ReviewError('invalid', 'That project exists.');
|
|
580
|
+
const p = { ...fields, client: scope.client, build: null, createdAt: now() };
|
|
581
|
+
// A new project starts clean too: whatever a write left under its slug, outside any record, goes first.
|
|
582
|
+
await sweep({ client: p.client, project: p.slug });
|
|
583
|
+
await saveProject(p);
|
|
584
|
+
return p;
|
|
585
|
+
},
|
|
586
|
+
/**
|
|
587
|
+
* Starts a build of the scope's project, before any of its files go up:
|
|
588
|
+
* it takes the next number in the project's own order of builds, from
|
|
589
|
+
* the store, never a clock. Only a started build can go live, and setBuild
|
|
590
|
+
* refuses one while a build started after it is live. A build started for
|
|
591
|
+
* a project that was archived and made again was started for none of
|
|
592
|
+
* the new one's. Studio only.
|
|
593
|
+
*/
|
|
594
|
+
async startBuild(scope, actor, build) {
|
|
595
|
+
await allow(scope, actor, 'setup', NO_PROJECT);
|
|
596
|
+
if (!isBuildId(build))
|
|
597
|
+
throw new ReviewError('invalid', 'Some fields are not valid.', ['build: a build id']);
|
|
598
|
+
if (!(await projectAt(scope)))
|
|
599
|
+
throw new ReviewError('not_found', NO_PROJECT);
|
|
600
|
+
const key = keyOf(scope, 'builds');
|
|
601
|
+
const n = await kv.hincrby(key, 'next', 1);
|
|
602
|
+
if (!(await kv.hsetnx(key, build, JSON.stringify({ n, live: false }))))
|
|
603
|
+
throw new ReviewError('invalid', 'That build was started already.', ['build: a new build id']);
|
|
604
|
+
},
|
|
605
|
+
/**
|
|
606
|
+
* Points the scope's project at a build. The caller has checked that
|
|
607
|
+
* every file of it is in the file store. Refused for a build never
|
|
608
|
+
* started for this project, or one started before the build the pointer
|
|
609
|
+
* names when this call reads it: a share overtaken by a newer one does
|
|
610
|
+
* not take the project back, which is what lets pruning remove the
|
|
611
|
+
* builds before the live one. The check reads and then writes, with no
|
|
612
|
+
* lock, so of two calls for one project within the same few store calls
|
|
613
|
+
* both can pass and the older build's write can land last; and a build
|
|
614
|
+
* pruned meanwhile, not yet marked live, is still pointed at, with no
|
|
615
|
+
* files. The next share's call puts either right. The build is marked
|
|
616
|
+
* live before the pointer moves. Studio only.
|
|
617
|
+
*/
|
|
618
|
+
async setBuild(scope, actor, build) {
|
|
619
|
+
await allow(scope, actor, 'setup', NO_PROJECT);
|
|
620
|
+
if (!isBuildId(build))
|
|
621
|
+
throw new ReviewError('invalid', 'Some fields are not valid.', ['build: a build id']);
|
|
622
|
+
const p = await projectAt(scope);
|
|
623
|
+
if (!p)
|
|
624
|
+
throw new ReviewError('not_found', NO_PROJECT);
|
|
625
|
+
const key = keyOf(scope, 'builds');
|
|
626
|
+
const entry = readBuildEntry(await kv.hget(key, build));
|
|
627
|
+
if (!entry)
|
|
628
|
+
throw new ReviewError('invalid', 'That build was never started for this project.', [
|
|
629
|
+
'build: one started for this project',
|
|
630
|
+
]);
|
|
631
|
+
const live = p.build && p.build !== build ? readBuildEntry(await kv.hget(key, p.build)) : null;
|
|
632
|
+
if (live && live.n > entry.n)
|
|
633
|
+
throw new ReviewError('invalid', 'A build started after this one is live.', [
|
|
634
|
+
'build: started after the live one',
|
|
635
|
+
]);
|
|
636
|
+
await kv.hset(key, build, JSON.stringify({ ...entry, live: true }));
|
|
637
|
+
const next = { ...p, build };
|
|
638
|
+
await saveProject(next);
|
|
639
|
+
return next;
|
|
640
|
+
},
|
|
641
|
+
/** The scope's project's live build and every build started for it, in the order they were started. Studio only. */
|
|
642
|
+
async buildsOf(scope, actor) {
|
|
643
|
+
await allow(scope, actor, 'setup', NO_PROJECT);
|
|
644
|
+
const p = await projectAt(scope);
|
|
645
|
+
if (!p)
|
|
646
|
+
throw new ReviewError('not_found', NO_PROJECT);
|
|
647
|
+
const builds = Object.entries(await kv.hgetall(keyOf(scope, 'builds')))
|
|
648
|
+
.map(([build, json]) => ({ build, entry: readBuildEntry(json) }))
|
|
649
|
+
.filter((b) => isBuildId(b.build) && b.entry !== null)
|
|
650
|
+
.map(({ build, entry }) => ({ build, ...entry }))
|
|
651
|
+
.sort((a, b) => a.n - b.n);
|
|
652
|
+
return { live: p.build, builds };
|
|
653
|
+
},
|
|
654
|
+
/** Forgets builds of the scope's project, once their files are gone; never the live one. Studio only. */
|
|
655
|
+
async forgetBuilds(scope, actor, builds) {
|
|
656
|
+
await allow(scope, actor, 'setup', NO_PROJECT);
|
|
657
|
+
const live = (await projectAt(scope))?.build;
|
|
658
|
+
for (const build of builds)
|
|
659
|
+
if (isBuildId(build) && build !== live)
|
|
660
|
+
await kv.hdel(keyOf(scope, 'builds'), build);
|
|
661
|
+
},
|
|
662
|
+
/** The person a signed session names, if they still may sign in with it, here. */
|
|
663
|
+
async sessionPerson(scope, id, epoch) {
|
|
664
|
+
const p = await person(scope, id);
|
|
665
|
+
return active(p) && p.epoch === epoch && inScope(p, scope) ? p : null;
|
|
666
|
+
},
|
|
667
|
+
/** The scope's Clients and every Studio person. */
|
|
668
|
+
async listPeople(scope, actor) {
|
|
669
|
+
await allow(scope, actor, 'people', NO_PERSON);
|
|
670
|
+
const all = [...(await peopleAt('studio')), ...(await peopleAt(scope))];
|
|
220
671
|
return all.sort((a, b) => a.team.localeCompare(b.team) || a.name.localeCompare(b.name));
|
|
221
672
|
},
|
|
222
|
-
/**
|
|
223
|
-
|
|
224
|
-
|
|
673
|
+
/**
|
|
674
|
+
* A new person and their first link: a Client of the scope's client, or a
|
|
675
|
+
* Studio person. Without an access length, a Client gets the default and
|
|
676
|
+
* Studio is open-ended.
|
|
677
|
+
*/
|
|
678
|
+
async addPerson(scope, actor, input) {
|
|
679
|
+
await allow(scope, actor, 'people', NO_PERSON);
|
|
225
680
|
const { access, ...fields } = valid(checkPersonInput(input));
|
|
226
|
-
|
|
681
|
+
// S13: the person's own length, else (a Client) the client's default, then the studio's, then 3m.
|
|
682
|
+
const length = access ?? (fields.kind === 'studio' ? 'open' : await readDefault(scope));
|
|
227
683
|
const p = {
|
|
228
684
|
id: newId(),
|
|
229
685
|
...fields,
|
|
686
|
+
client: fields.kind === 'studio' ? null : scope.client,
|
|
230
687
|
email: null,
|
|
231
688
|
accessEndsAt: accessEnd(length, deps.now()),
|
|
232
689
|
createdAt: now(),
|
|
@@ -237,112 +694,152 @@ export function createReview(kv, overrides = {}) {
|
|
|
237
694
|
return { person: p, token: await issueLink(p) };
|
|
238
695
|
},
|
|
239
696
|
/** Another one-time link, for another device. Their sessions and other unused links stay. */
|
|
240
|
-
async newLink(actor, id) {
|
|
241
|
-
allow(actor, 'people');
|
|
242
|
-
const p = await needActive(id);
|
|
697
|
+
async newLink(scope, actor, id) {
|
|
698
|
+
await allow(scope, actor, 'people', NO_PERSON);
|
|
699
|
+
const p = await needActive(scope, id);
|
|
243
700
|
return { person: p, token: await issueLink(p) };
|
|
244
701
|
},
|
|
245
702
|
/** Ends every session they hold and every unused link, and gives one new link. */
|
|
246
|
-
async signOutEverywhere(actor, id) {
|
|
247
|
-
allow(actor, 'people');
|
|
248
|
-
const p = await needActive(id);
|
|
703
|
+
async signOutEverywhere(scope, actor, id) {
|
|
704
|
+
await allow(scope, actor, 'people', NO_PERSON);
|
|
705
|
+
const p = await needActive(scope, id);
|
|
249
706
|
// The epoch first: a failure after it has still ended every session. Atomic, so two at once raise it twice.
|
|
250
|
-
const next = { ...p, epoch: await kv.hincrby(
|
|
251
|
-
await dropLinks(
|
|
707
|
+
const next = { ...p, epoch: await kv.hincrby(keyOf(homeOf(p), 'epochs'), id, 1) };
|
|
708
|
+
await dropLinks(p);
|
|
252
709
|
return { person: next, token: await issueLink(next) };
|
|
253
710
|
},
|
|
254
|
-
async revoke(actor, id) {
|
|
255
|
-
allow(actor, 'people');
|
|
256
|
-
if (
|
|
711
|
+
async revoke(scope, actor, id) {
|
|
712
|
+
const by = await allow(scope, actor, 'people', NO_PERSON);
|
|
713
|
+
if (by.id === id)
|
|
257
714
|
throw new ReviewError('invalid', 'You cannot revoke yourself.');
|
|
258
|
-
const p = await needPerson(id);
|
|
715
|
+
const p = await needPerson(scope, id);
|
|
259
716
|
const revokedAt = p.revokedAt ?? now();
|
|
260
717
|
// Revocation and the epoch before the links go, so a failure part-way never leaves an active
|
|
261
718
|
// person; a leftover link opens nothing for someone revoked.
|
|
262
719
|
if (!p.revokedAt)
|
|
263
|
-
await kv.hset(
|
|
264
|
-
const epoch = await kv.hincrby(
|
|
265
|
-
await dropLinks(
|
|
720
|
+
await kv.hset(keyOf(homeOf(p), 'revoked'), id, revokedAt);
|
|
721
|
+
const epoch = await kv.hincrby(keyOf(homeOf(p), 'epochs'), id, 1);
|
|
722
|
+
await dropLinks(p);
|
|
266
723
|
return { ...p, revokedAt, epoch };
|
|
267
724
|
},
|
|
268
725
|
/** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
|
|
269
|
-
async setAccess(actor, id, access) {
|
|
270
|
-
allow(actor, 'people');
|
|
726
|
+
async setAccess(scope, actor, id, access) {
|
|
727
|
+
await allow(scope, actor, 'people', NO_PERSON);
|
|
271
728
|
const length = valid(checkAccess(access));
|
|
272
|
-
const p = await needPerson(id);
|
|
729
|
+
const p = await needPerson(scope, id);
|
|
273
730
|
if (p.revokedAt)
|
|
274
731
|
throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
275
732
|
await savePerson({ ...p, accessEndsAt: accessEnd(length, deps.now()) });
|
|
276
733
|
// Read back, so the answer carries a revocation or epoch that landed meanwhile.
|
|
277
|
-
return (await
|
|
734
|
+
return (await personAt(homeOf(p), id));
|
|
278
735
|
},
|
|
279
|
-
/** How long a new Client keeps access unless Studio says otherwise. */
|
|
280
|
-
async defaultAccess(actor) {
|
|
281
|
-
allow(actor, 'people');
|
|
282
|
-
return readDefault();
|
|
736
|
+
/** How long a new Client of the scope's client keeps access unless Studio says otherwise: the client's default, else the studio's. */
|
|
737
|
+
async defaultAccess(scope, actor) {
|
|
738
|
+
await allow(scope, actor, 'people', NO_PERSON);
|
|
739
|
+
return readDefault(scope);
|
|
283
740
|
},
|
|
284
|
-
|
|
285
|
-
|
|
741
|
+
/**
|
|
742
|
+
* Sets what defaultAccess reads for the scope: the client's own default
|
|
743
|
+
* on a studio site's client host, the studio's in the local store, whose
|
|
744
|
+
* client has no record. Never the studio's for a client whose record is
|
|
745
|
+
* gone. Studio only.
|
|
746
|
+
*/
|
|
747
|
+
async setDefaultAccess(scope, actor, access) {
|
|
748
|
+
await allow(scope, actor, 'people', NO_PERSON);
|
|
749
|
+
const length = valid(checkAccess(access));
|
|
750
|
+
if (scope.client === LOCAL_CLIENT) {
|
|
751
|
+
await kv.hset(keyOf('studio', 'settings'), 'access', length);
|
|
752
|
+
return length;
|
|
753
|
+
}
|
|
754
|
+
const c = await clientAt(scope.client);
|
|
755
|
+
if (!c)
|
|
756
|
+
throw new ReviewError('not_found', NO_PERSON);
|
|
757
|
+
await kv.hset(keyOf('studio', 'clients'), c.slug, JSON.stringify({ ...c, access: length }));
|
|
758
|
+
return length;
|
|
759
|
+
},
|
|
760
|
+
/** A new Studio person, who works in every client. No link: each comes from newStudioLink, for one host. Studio only. */
|
|
761
|
+
async addStudioPerson(actor, input) {
|
|
762
|
+
allowStudio(actor);
|
|
763
|
+
return newStudioPerson(input);
|
|
764
|
+
},
|
|
765
|
+
/** Another one-time link for a Studio person: it signs in one device, on whichever client host it is opened. Studio only. */
|
|
766
|
+
async newStudioLink(actor, id) {
|
|
767
|
+
allowStudio(actor);
|
|
768
|
+
const p = await personAt('studio', id);
|
|
769
|
+
if (!p)
|
|
770
|
+
throw new ReviewError('not_found', NO_PERSON);
|
|
771
|
+
if (!active(p))
|
|
772
|
+
throw new ReviewError('invalid', 'That person is revoked or their access has ended.');
|
|
773
|
+
return { person: p, token: await issueLink(p) };
|
|
774
|
+
},
|
|
775
|
+
/** The studio's default access length, what a client without its own gives. Studio only. */
|
|
776
|
+
async studioAccess(actor) {
|
|
777
|
+
allowStudio(actor);
|
|
778
|
+
return studioDefault();
|
|
779
|
+
},
|
|
780
|
+
async setStudioAccess(actor, access) {
|
|
781
|
+
allowStudio(actor);
|
|
286
782
|
const length = valid(checkAccess(access));
|
|
287
|
-
await kv.hset(
|
|
783
|
+
await kv.hset(keyOf('studio', 'settings'), 'access', length);
|
|
288
784
|
return length;
|
|
289
785
|
},
|
|
290
|
-
/** Who a link signs in, without using it: mail and chat previews open links too. */
|
|
291
|
-
async peekLink(token) {
|
|
292
|
-
const found = await findLink(token);
|
|
293
|
-
return found ? linkPerson(found
|
|
786
|
+
/** Who a link signs in here, without using it: mail and chat previews open links too. */
|
|
787
|
+
async peekLink(scope, token) {
|
|
788
|
+
const found = await findLink(scope, token);
|
|
789
|
+
return found ? linkPerson(scope, found) : null;
|
|
294
790
|
},
|
|
295
791
|
/**
|
|
296
792
|
* Uses a link up and returns who it signs in. The delete is the claim: of
|
|
297
793
|
* two requests with one token, only the one whose delete removed the link
|
|
298
794
|
* gets a person, so a link signs in one device at most. Who it signs in is
|
|
299
|
-
* read and checked first, so the delete is the last store call.
|
|
795
|
+
* read and checked first, so the delete is the last store call. Only a
|
|
796
|
+
* link of the scope's client, or a Studio person's, is found at all.
|
|
300
797
|
*/
|
|
301
|
-
async useLink(token) {
|
|
302
|
-
const found = await findLink(token);
|
|
798
|
+
async useLink(scope, token) {
|
|
799
|
+
const found = await findLink(scope, token);
|
|
303
800
|
// Every read and check comes before the delete, so a store failure before it leaves the link usable.
|
|
304
|
-
const p = found && (await linkPerson(found
|
|
801
|
+
const p = found && (await linkPerson(scope, found));
|
|
305
802
|
if (!found || !p)
|
|
306
803
|
return null;
|
|
307
|
-
if ((await kv.hdel(
|
|
804
|
+
if ((await kv.hdel(keyOf(found.home, 'links'), found.hash)) !== 1)
|
|
308
805
|
return null;
|
|
309
806
|
// A revoke or sign out everywhere can land between the check and the claim, before its
|
|
310
807
|
// dropLinks reaches this link: read again, and sign in only someone still active on the same epoch.
|
|
311
|
-
const after = await
|
|
808
|
+
const after = await personAt(found.home, p.id);
|
|
312
809
|
return active(after) && after.epoch === p.epoch ? after : null;
|
|
313
810
|
},
|
|
314
811
|
/**
|
|
315
812
|
* A prototype's comments as the actor's view sees them. `viewAs: 'client'`
|
|
316
813
|
* is Studio's Client view; it never widens a Client's.
|
|
317
814
|
*/
|
|
318
|
-
async listComments(actor, prototype, viewAs) {
|
|
319
|
-
allow(actor, 'read');
|
|
320
|
-
const view = viewOf(
|
|
321
|
-
if (!(await reachable(view, prototype)))
|
|
815
|
+
async listComments(scope, actor, prototype, viewAs) {
|
|
816
|
+
const by = await allow(scope, actor, 'read', NO_COMMENT);
|
|
817
|
+
const view = viewOf(by, valid(checkViewAs(viewAs)));
|
|
818
|
+
if (!(await reachable(scope, view, prototype)))
|
|
322
819
|
return [];
|
|
323
|
-
return visibleTo(view, await commentsOf(prototype));
|
|
820
|
+
return visibleTo(view, await commentsOf(scope, prototype));
|
|
324
821
|
},
|
|
325
|
-
/** Every comment on every prototype the actor may see, for the CLI (which acts as Studio). */
|
|
326
|
-
async allComments(actor) {
|
|
327
|
-
allow(actor, 'read');
|
|
328
|
-
const view = viewOf(
|
|
822
|
+
/** Every comment on every prototype of the scope's project the actor may see, for the CLI (which acts as Studio). */
|
|
823
|
+
async allComments(scope, actor) {
|
|
824
|
+
const by = await allow(scope, actor, 'read', NO_COMMENT);
|
|
825
|
+
const view = viewOf(by);
|
|
329
826
|
const prototypes = [];
|
|
330
|
-
for (const p of await kv.smembers(
|
|
331
|
-
if (await reachable(view, p))
|
|
827
|
+
for (const p of await kv.smembers(keyOf(scope, 'prototypes')))
|
|
828
|
+
if (await reachable(scope, view, p))
|
|
332
829
|
prototypes.push(p);
|
|
333
|
-
return visibleTo(view, (await Promise.all(prototypes.map(commentsOf))).flat());
|
|
830
|
+
return visibleTo(view, (await Promise.all(prototypes.map((p) => commentsOf(scope, p)))).flat());
|
|
334
831
|
},
|
|
335
832
|
/**
|
|
336
833
|
* A new thread. A Client's is client-facing whatever the input says;
|
|
337
834
|
* Studio's is internal unless `audience` is 'client'. Its number is the
|
|
338
835
|
* next of its audience on the prototype, never one used before.
|
|
339
836
|
*/
|
|
340
|
-
async add(actor, input) {
|
|
341
|
-
allow(actor, 'comment');
|
|
837
|
+
async add(scope, actor, input) {
|
|
838
|
+
const by = await allow(scope, actor, 'comment', NO_COMMENT);
|
|
342
839
|
const [body, anchor, asked] = validAll(checkBody(input.body), checkAnchor(input.anchor), checkAudience(input.audience));
|
|
343
840
|
// A Client's thread on a prototype that is not client-facing: the answer an unknown id gets.
|
|
344
|
-
if (!(await reachable(viewOf(
|
|
345
|
-
throw new ReviewError('not_found',
|
|
841
|
+
if (!(await reachable(scope, viewOf(by), anchor.prototype)))
|
|
842
|
+
throw new ReviewError('not_found', NO_COMMENT);
|
|
346
843
|
const id = newId();
|
|
347
844
|
const triage = deps.triage
|
|
348
845
|
? await deps
|
|
@@ -353,7 +850,7 @@ export function createReview(kv, overrides = {}) {
|
|
|
353
850
|
const c = {
|
|
354
851
|
id,
|
|
355
852
|
thread: id,
|
|
356
|
-
author: authorOf(
|
|
853
|
+
author: authorOf(by),
|
|
357
854
|
body,
|
|
358
855
|
createdAt: now(),
|
|
359
856
|
editedAt: null,
|
|
@@ -361,22 +858,22 @@ export function createReview(kv, overrides = {}) {
|
|
|
361
858
|
status: 'open',
|
|
362
859
|
resolvedBy: null,
|
|
363
860
|
triage,
|
|
364
|
-
audience: audienceFor(
|
|
861
|
+
audience: audienceFor(by, asked),
|
|
365
862
|
number: 0,
|
|
366
863
|
};
|
|
367
|
-
c.number = await nextNumber(anchor.prototype, c.audience);
|
|
368
|
-
await kv.sadd(
|
|
369
|
-
await saveComment(anchor.prototype, c);
|
|
864
|
+
c.number = await nextNumber(scope, anchor.prototype, c.audience);
|
|
865
|
+
await kv.sadd(keyOf(scope, 'prototypes'), anchor.prototype);
|
|
866
|
+
await saveComment(scope, anchor.prototype, c);
|
|
370
867
|
return c;
|
|
371
868
|
},
|
|
372
|
-
async reply(actor, input) {
|
|
373
|
-
const root = await needRoot(actor, input.prototype, input.thread);
|
|
374
|
-
|
|
869
|
+
async reply(scope, actor, input) {
|
|
870
|
+
const { actor: by, root } = await needRoot(scope, actor, input.prototype, input.thread);
|
|
871
|
+
permit(scope, by, 'comment', root);
|
|
375
872
|
const id = newId();
|
|
376
873
|
const c = {
|
|
377
874
|
id,
|
|
378
875
|
thread: root.id,
|
|
379
|
-
author: authorOf(
|
|
876
|
+
author: authorOf(by),
|
|
380
877
|
body: valid(checkBody(input.body)),
|
|
381
878
|
createdAt: now(),
|
|
382
879
|
editedAt: null,
|
|
@@ -388,43 +885,45 @@ export function createReview(kv, overrides = {}) {
|
|
|
388
885
|
audience: root.audience,
|
|
389
886
|
number: root.number,
|
|
390
887
|
};
|
|
391
|
-
await saveComment(input.prototype, c);
|
|
888
|
+
await saveComment(scope, input.prototype, c);
|
|
392
889
|
return c;
|
|
393
890
|
},
|
|
394
|
-
async edit(actor, input) {
|
|
395
|
-
const { comment: c, root } = await needThread(actor, input.prototype, input.id);
|
|
396
|
-
|
|
891
|
+
async edit(scope, actor, input) {
|
|
892
|
+
const { actor: by, comment: c, root } = await needThread(scope, actor, input.prototype, input.id);
|
|
893
|
+
permit(scope, by, 'edit', root, c);
|
|
397
894
|
const next = { ...c, body: valid(checkBody(input.body)), editedAt: now() };
|
|
398
|
-
await saveComment(input.prototype, next);
|
|
895
|
+
await saveComment(scope, input.prototype, next);
|
|
399
896
|
return next;
|
|
400
897
|
},
|
|
401
898
|
/** Deletes a comment; deleting a thread's root deletes its replies with it. */
|
|
402
|
-
async remove(actor, input) {
|
|
403
|
-
const { comment: c, root } = await needThread(actor, input.prototype, input.id);
|
|
404
|
-
|
|
405
|
-
const gone = isRoot(c)
|
|
899
|
+
async remove(scope, actor, input) {
|
|
900
|
+
const { actor: by, comment: c, root } = await needThread(scope, actor, input.prototype, input.id);
|
|
901
|
+
permit(scope, by, 'delete', root, c);
|
|
902
|
+
const gone = isRoot(c)
|
|
903
|
+
? (await commentsOf(scope, input.prototype)).filter((x) => x.thread === c.id)
|
|
904
|
+
: [c];
|
|
406
905
|
for (const x of gone)
|
|
407
|
-
await kv.hdel(
|
|
906
|
+
await kv.hdel(keyOf(scope, 'comments', input.prototype), x.id);
|
|
408
907
|
// The answer names only what the actor may see, judged with the thread's root beside it.
|
|
409
|
-
const shown = new Set(visibleTo(viewOf(
|
|
908
|
+
const shown = new Set(visibleTo(viewOf(by), [root, ...gone]).map((x) => x.id));
|
|
410
909
|
return gone.filter((x) => shown.has(x.id)).map((x) => x.id);
|
|
411
910
|
},
|
|
412
|
-
async resolve(actor, input) {
|
|
413
|
-
const c = await needRoot(actor, input.prototype, input.id);
|
|
414
|
-
|
|
911
|
+
async resolve(scope, actor, input) {
|
|
912
|
+
const { actor: by, root: c } = await needRoot(scope, actor, input.prototype, input.id);
|
|
913
|
+
permit(scope, by, 'resolve', c);
|
|
415
914
|
const next = input.resolved
|
|
416
|
-
? { ...c, status: 'resolved', resolvedBy: authorOf(
|
|
915
|
+
? { ...c, status: 'resolved', resolvedBy: authorOf(by) }
|
|
417
916
|
: { ...c, status: 'open', resolvedBy: null };
|
|
418
|
-
await saveComment(input.prototype, next);
|
|
917
|
+
await saveComment(scope, input.prototype, next);
|
|
419
918
|
return next;
|
|
420
919
|
},
|
|
421
|
-
async retriage(actor, input) {
|
|
422
|
-
allow(actor, 'triage');
|
|
423
|
-
const c = await needRoot(actor, input.prototype, input.id);
|
|
920
|
+
async retriage(scope, actor, input) {
|
|
921
|
+
await allow(scope, actor, 'triage', NO_COMMENT);
|
|
922
|
+
const { root: c } = await needRoot(scope, actor, input.prototype, input.id);
|
|
424
923
|
const states = checkStates(input.states);
|
|
425
924
|
const { kind, state } = valid(checkTriage(input.triage, states.length ? states : null));
|
|
426
925
|
const next = { ...c, triage: { kind, state, by: 'person', confidence: null } };
|
|
427
|
-
await saveComment(input.prototype, next);
|
|
926
|
+
await saveComment(scope, input.prototype, next);
|
|
428
927
|
return next;
|
|
429
928
|
},
|
|
430
929
|
/**
|
|
@@ -432,15 +931,15 @@ export function createReview(kv, overrides = {}) {
|
|
|
432
931
|
* stored, so internal deletes the field and a prototype nobody marked is
|
|
433
932
|
* internal. Studio only.
|
|
434
933
|
*/
|
|
435
|
-
async setAudience(actor, prototype, audience) {
|
|
436
|
-
allow(actor, 'audience');
|
|
934
|
+
async setAudience(scope, actor, prototype, audience) {
|
|
935
|
+
await allow(scope, actor, 'audience', NO_PROTOTYPE);
|
|
437
936
|
const [slug, value] = validAll(isPrototypeSlug(prototype)
|
|
438
937
|
? { ok: true, value: prototype }
|
|
439
938
|
: { ok: false, problems: ['prototype: a prototype slug'] }, checkPrototypeAudience(audience));
|
|
440
939
|
if (value === 'client')
|
|
441
|
-
await kv.hset(
|
|
940
|
+
await kv.hset(keyOf(scope, 'audiences'), slug, 'client');
|
|
442
941
|
else
|
|
443
|
-
await kv.hdel(
|
|
942
|
+
await kv.hdel(keyOf(scope, 'audiences'), slug);
|
|
444
943
|
return value;
|
|
445
944
|
},
|
|
446
945
|
/**
|
|
@@ -450,14 +949,14 @@ export function createReview(kv, overrides = {}) {
|
|
|
450
949
|
* names are in the prototype's own code already. An internal prototype
|
|
451
950
|
* has none for a Client, as it has no comments.
|
|
452
951
|
*/
|
|
453
|
-
async controlsOf(actor, prototype) {
|
|
454
|
-
allow(actor, 'read');
|
|
952
|
+
async controlsOf(scope, actor, prototype) {
|
|
953
|
+
const by = await allow(scope, actor, 'read', NO_PROTOTYPE);
|
|
455
954
|
if (!isPrototypeSlug(prototype))
|
|
456
955
|
throw new ReviewError('invalid', 'Some fields are not valid.', ['prototype: a prototype slug']);
|
|
457
|
-
const view = viewOf(
|
|
458
|
-
if (!(await reachable(view, prototype)))
|
|
459
|
-
throw new ReviewError('not_found',
|
|
460
|
-
return { view, ...readMarks(await kv.hgetall(
|
|
956
|
+
const view = viewOf(by);
|
|
957
|
+
if (!(await reachable(scope, view, prototype)))
|
|
958
|
+
throw new ReviewError('not_found', NO_PROTOTYPE);
|
|
959
|
+
return { view, ...readMarks(await kv.hgetall(keyOf(scope, 'controls', prototype))) };
|
|
461
960
|
},
|
|
462
961
|
/**
|
|
463
962
|
* Marks one dial or one variant client-facing or internal. A dial's mark
|
|
@@ -465,25 +964,26 @@ export function createReview(kv, overrides = {}) {
|
|
|
465
964
|
* variant's internal is the field's absence. Studio only. The server
|
|
466
965
|
* keeps no list of a prototype's controls, so any well-formed id is taken.
|
|
467
966
|
*/
|
|
468
|
-
async setControl(actor, input) {
|
|
469
|
-
allow(actor, 'audience');
|
|
967
|
+
async setControl(scope, actor, input) {
|
|
968
|
+
await allow(scope, actor, 'audience', NO_PROTOTYPE);
|
|
470
969
|
const [slug, mark] = validAll(isPrototypeSlug(input.prototype)
|
|
471
970
|
? { ok: true, value: input.prototype }
|
|
472
971
|
: { ok: false, problems: ['prototype: a prototype slug'] }, checkControl(input));
|
|
972
|
+
const controls = keyOf(scope, 'controls', slug);
|
|
473
973
|
const field = controlField(mark.kind, mark.id);
|
|
474
974
|
if (mark.kind === 'variant' && mark.audience === 'internal')
|
|
475
|
-
await kv.hdel(
|
|
975
|
+
await kv.hdel(controls, field);
|
|
476
976
|
else
|
|
477
|
-
await kv.hset(
|
|
977
|
+
await kv.hset(controls, field, mark.audience);
|
|
478
978
|
return mark;
|
|
479
979
|
},
|
|
480
980
|
/**
|
|
481
|
-
* Who a prototype is for, asked by the gate with no
|
|
482
|
-
* value but `client`, is internal; a name that
|
|
483
|
-
* internal without asking the store.
|
|
981
|
+
* Who a prototype of the scope's project is for, asked by the gate with no
|
|
982
|
+
* actor. Absent, or any value but `client`, is internal; a name that
|
|
983
|
+
* cannot be a prototype is internal without asking the store.
|
|
484
984
|
*/
|
|
485
|
-
async audienceOf(prototype) {
|
|
486
|
-
return (await isClientFacing(prototype)) ? 'client' : 'internal';
|
|
985
|
+
async audienceOf(scope, prototype) {
|
|
986
|
+
return (await isClientFacing(scope, prototype)) ? 'client' : 'internal';
|
|
487
987
|
},
|
|
488
988
|
/**
|
|
489
989
|
* What the index is built from: the view the actor gets (Client view for
|
|
@@ -491,21 +991,31 @@ export function createReview(kv, overrides = {}) {
|
|
|
491
991
|
* server keeps no list of internal ones: Studio's view reads the build's
|
|
492
992
|
* own list, which the gate refuses to a Client.
|
|
493
993
|
*/
|
|
494
|
-
async visiblePrototypes(actor, viewAs) {
|
|
495
|
-
allow(actor, 'read');
|
|
496
|
-
const view = viewOf(
|
|
497
|
-
const all = await kv.hgetall(
|
|
994
|
+
async visiblePrototypes(scope, actor, viewAs) {
|
|
995
|
+
const by = await allow(scope, actor, 'read', NO_PROTOTYPE);
|
|
996
|
+
const view = viewOf(by, valid(checkViewAs(viewAs)));
|
|
997
|
+
const all = await kv.hgetall(keyOf(scope, 'audiences'));
|
|
498
998
|
const clientFacing = Object.entries(all)
|
|
499
999
|
.filter(([slug, value]) => value === 'client' && isPrototypeSlug(slug))
|
|
500
1000
|
.map(([slug]) => slug)
|
|
501
1001
|
.sort();
|
|
502
1002
|
return { view, clientFacing };
|
|
503
1003
|
},
|
|
504
|
-
/**
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
1004
|
+
/**
|
|
1005
|
+
* Where a comment lives, for the CLI, which has an id and no prototype;
|
|
1006
|
+
* null when the actor may not see it, and outside the actor's scope or in
|
|
1007
|
+
* a client that is not live, as for an id that is not there.
|
|
1008
|
+
*/
|
|
1009
|
+
async locate(scope, actor, id) {
|
|
1010
|
+
const by = await allow(scope, actor, 'read', NO_COMMENT).catch((error) => {
|
|
1011
|
+
if (error instanceof ReviewError && error.code === 'not_found')
|
|
1012
|
+
return null;
|
|
1013
|
+
throw error;
|
|
1014
|
+
});
|
|
1015
|
+
if (!by)
|
|
1016
|
+
return null;
|
|
1017
|
+
for (const prototype of await kv.smembers(keyOf(scope, 'prototypes'))) {
|
|
1018
|
+
const found = await seen(scope, by, prototype, id);
|
|
509
1019
|
if (found)
|
|
510
1020
|
return { prototype, comment: found.comment };
|
|
511
1021
|
}
|