@milesroxas/design-hub 0.9.0 → 0.11.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 +13 -11
- package/core/controls.test.ts +176 -0
- package/core/controls.ts +195 -0
- 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 +144 -2
- package/core/review/policy.test.ts +79 -22
- package/core/review/policy.ts +36 -8
- package/core/review/service.test.ts +1525 -322
- package/core/review/service.ts +810 -183
- package/core/review/store.test.ts +91 -0
- package/core/review/store.ts +143 -26
- package/dist/core/controls.d.ts +55 -0
- package/dist/core/controls.js +174 -0
- 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 +87 -2
- package/dist/core/review/model.js +97 -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 +222 -43
- package/dist/core/review/service.js +727 -182
- package/dist/core/review/store.d.ts +84 -24
- package/dist/core/review/store.js +66 -26
- 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/guard.d.ts +11 -0
- package/dist/scripts/guard.js +44 -0
- 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/rules.d.ts +1 -0
- package/dist/scripts/lib/rules.js +1 -0
- 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 +125 -44
- 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/Artboard.tsx +21 -8
- package/kit/Space.tsx +4 -3
- package/kit/Variants.tsx +193 -65
- package/kit/ViewTools.tsx +77 -11
- package/kit/audience.ts +85 -0
- package/kit/dials.css +59 -0
- package/kit/dials.tsx +272 -35
- package/kit/harness.css +34 -0
- package/kit/prefs.ts +23 -1
- package/package.json +3 -2
- package/shell/ClientViewNote.tsx +31 -3
- package/shell/Guide.tsx +11 -5
- package/shell/Index.tsx +32 -10
- package/shell/Notice.tsx +1 -1
- package/shell/controls.ts +26 -0
- package/shell/hub.ts +5 -1
- package/shell/main.tsx +33 -6
- package/shell/review/Review.tsx +2 -2
- package/shell/review/ThreadCard.tsx +5 -5
- package/shell/review/client.ts +28 -11
- package/shell/review/view.test.ts +19 -0
- package/shell/review/view.ts +25 -0
- package/shell/shell.css +8 -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-brand/SKILL.md +11 -1
- package/skills/miles-deploy/SKILL.md +121 -64
- package/skills/miles-design-hub/SKILL.md +11 -9
- package/skills/miles-prototype/SKILL.md +34 -1
- 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,11 +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
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
13
|
+
import { controlField, readMarks } from '../controls.js';
|
|
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';
|
|
12
19
|
export class ReviewError extends Error {
|
|
13
20
|
code;
|
|
14
21
|
problems;
|
|
@@ -27,6 +34,31 @@ const defaults = {
|
|
|
27
34
|
/** What a link's token looks like: 32 random bytes, base64url. Anything else is never looked up. */
|
|
28
35
|
const LINK_TOKEN = /^[\w-]{20,100}$/;
|
|
29
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 });
|
|
30
62
|
export function createReview(kv, overrides = {}) {
|
|
31
63
|
const deps = { ...defaults, ...overrides };
|
|
32
64
|
const now = () => deps.now().toISOString();
|
|
@@ -42,10 +74,56 @@ export function createReview(kv, overrides = {}) {
|
|
|
42
74
|
return null;
|
|
43
75
|
}
|
|
44
76
|
};
|
|
45
|
-
|
|
77
|
+
/** Signed in, then in scope. Out of scope answers `missing`, what the op answers an unknown id. */
|
|
78
|
+
function admit(scope, actor, missing) {
|
|
46
79
|
if (!actor)
|
|
47
80
|
throw new ReviewError('unauthorized', 'Sign in with your link to do that.');
|
|
48
|
-
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))
|
|
49
127
|
throw new ReviewError('forbidden', 'You cannot do that.');
|
|
50
128
|
}
|
|
51
129
|
function valid(checked) {
|
|
@@ -60,28 +138,46 @@ export function createReview(kv, overrides = {}) {
|
|
|
60
138
|
throw new ReviewError('invalid', 'Some fields are not valid.', problems);
|
|
61
139
|
return checks.map((c) => c.value);
|
|
62
140
|
}
|
|
63
|
-
// A person is three records. The profile (
|
|
141
|
+
// A person is three records. The profile (`people`) is read-modify-write, so
|
|
64
142
|
// a stale write of it can land after anything. Revocation and the epoch live
|
|
65
143
|
// outside it: revocation is written once and never cleared, the epoch only
|
|
66
144
|
// rises (HINCRBY), so no profile write, however stale, can revive a session.
|
|
67
|
-
|
|
145
|
+
// A person's client is where the three live, never a field anyone writes.
|
|
146
|
+
const compose = (home, json, revoked, epoch) => {
|
|
68
147
|
const profile = safeParse(json);
|
|
69
148
|
if (!isProfile(profile))
|
|
70
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;
|
|
71
153
|
// Any revocation record means revoked, an empty one included: it reads as the start of time.
|
|
72
154
|
const revokedAt = revoked == null ? null : revoked || new Date(0).toISOString();
|
|
73
|
-
|
|
155
|
+
const client = home === 'studio' ? null : home.client;
|
|
156
|
+
return { ...profile, client, revokedAt, epoch: Number(epoch ?? 0) };
|
|
74
157
|
};
|
|
75
|
-
const
|
|
158
|
+
const personAt = async (home, id) => {
|
|
76
159
|
const [json, revoked, epoch] = await Promise.all([
|
|
77
|
-
kv.hget(
|
|
78
|
-
kv.hget(
|
|
79
|
-
kv.hget(
|
|
160
|
+
kv.hget(keyOf(home, 'people'), id),
|
|
161
|
+
kv.hget(keyOf(home, 'revoked'), id),
|
|
162
|
+
kv.hget(keyOf(home, 'epochs'), id),
|
|
163
|
+
]);
|
|
164
|
+
return compose(home, json, revoked, epoch);
|
|
165
|
+
};
|
|
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')),
|
|
80
174
|
]);
|
|
81
|
-
return
|
|
175
|
+
return Object.entries(profiles)
|
|
176
|
+
.map(([id, json]) => compose(home, json, revoked[id], epochs[id]))
|
|
177
|
+
.filter((p) => p !== null);
|
|
82
178
|
};
|
|
83
|
-
/** Writes the profile alone: revocation and
|
|
84
|
-
const savePerson = ({ revokedAt: _r, epoch: _e, ...profile }) => kv.hset(
|
|
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));
|
|
85
181
|
/** Not revoked, and their access has not ended. */
|
|
86
182
|
const active = (p) => !!p && hasAccess(p, deps.now());
|
|
87
183
|
async function issueLink(p) {
|
|
@@ -91,47 +187,116 @@ export function createReview(kv, overrides = {}) {
|
|
|
91
187
|
person: p.id,
|
|
92
188
|
expiresAt: new Date(deps.now().getTime() + LINK_DAYS * DAY_MS).toISOString(),
|
|
93
189
|
};
|
|
94
|
-
|
|
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));
|
|
95
192
|
return token;
|
|
96
193
|
}
|
|
97
|
-
async function dropLinks(
|
|
98
|
-
const
|
|
99
|
-
for (const [hash, json] of Object.entries(
|
|
100
|
-
if (JSON.parse(json).person ===
|
|
101
|
-
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);
|
|
102
199
|
}
|
|
103
|
-
/** The link a token names, or null for a malformed or unknown token. */
|
|
104
|
-
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) {
|
|
105
202
|
if (!LINK_TOKEN.test(token))
|
|
106
203
|
return null;
|
|
107
|
-
const
|
|
108
|
-
const
|
|
109
|
-
|
|
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;
|
|
110
211
|
}
|
|
111
|
-
/** The person a current link signs in, while they may sign in. */
|
|
112
|
-
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 }) {
|
|
113
214
|
if (link.expiresAt <= now())
|
|
114
215
|
return null;
|
|
115
|
-
const p = await
|
|
116
|
-
return active(p) ? p : null;
|
|
216
|
+
const p = await personAt(home, link.person);
|
|
217
|
+
return active(p) && inScope(p, scope) ? p : null;
|
|
117
218
|
}
|
|
118
|
-
async function
|
|
119
|
-
const stored = await kv.hget(
|
|
219
|
+
async function studioDefault() {
|
|
220
|
+
const stored = await kv.hget(keyOf('studio', 'settings'), 'access');
|
|
120
221
|
return checkAccess(stored).ok ? stored : DEFAULT_ACCESS;
|
|
121
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
|
+
}
|
|
122
287
|
/** Someone Studio can still give a link to. */
|
|
123
|
-
async function needActive(id) {
|
|
124
|
-
const p = await needPerson(id);
|
|
288
|
+
async function needActive(scope, id) {
|
|
289
|
+
const p = await needPerson(scope, id);
|
|
125
290
|
if (p.revokedAt)
|
|
126
291
|
throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
127
292
|
if (!active(p))
|
|
128
293
|
throw new ReviewError('invalid', 'Their access has ended; change their access first.');
|
|
129
294
|
return p;
|
|
130
295
|
}
|
|
131
|
-
async function needPerson(id) {
|
|
132
|
-
const p = await person(id);
|
|
296
|
+
async function needPerson(scope, id) {
|
|
297
|
+
const p = await person(scope, id);
|
|
133
298
|
if (!p)
|
|
134
|
-
throw new ReviewError('not_found',
|
|
299
|
+
throw new ReviewError('not_found', NO_PERSON);
|
|
135
300
|
return p;
|
|
136
301
|
}
|
|
137
302
|
// Every comment read goes through readComment, so a record with no audience reads as internal.
|
|
@@ -142,90 +307,383 @@ export function createReview(kv, overrides = {}) {
|
|
|
142
307
|
* numbers always had. Internal threads count in their own, which starts past any internal
|
|
143
308
|
* thread numbered before the two counted apart, so no internal number is given twice.
|
|
144
309
|
*/
|
|
145
|
-
const nextNumber = async (prototype, audience) => {
|
|
310
|
+
const nextNumber = async (scope, prototype, audience) => {
|
|
311
|
+
const counters = keyOf(scope, 'counters');
|
|
146
312
|
if (audience === 'client')
|
|
147
|
-
return kv.hincrby(
|
|
313
|
+
return kv.hincrby(counters, prototype, 1);
|
|
148
314
|
const field = `${prototype}:internal`;
|
|
149
|
-
const n = await kv.hincrby(
|
|
315
|
+
const n = await kv.hincrby(counters, field, 1);
|
|
150
316
|
if (n !== 1)
|
|
151
317
|
return n;
|
|
152
|
-
const before = Math.max(0, ...(await commentsOf(prototype)).filter((c) => c.audience === 'internal').map((c) => c.number));
|
|
153
|
-
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;
|
|
154
320
|
};
|
|
155
|
-
const commentsOf = async (prototype) => Object.values(await kv.hgetall(
|
|
321
|
+
const commentsOf = async (scope, prototype) => Object.values(await kv.hgetall(keyOf(scope, 'comments', prototype)))
|
|
156
322
|
.map(readComment)
|
|
157
323
|
.sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id));
|
|
158
|
-
const commentAt = async (prototype, id) => {
|
|
159
|
-
const json = await kv.hget(
|
|
324
|
+
const commentAt = async (scope, prototype, id) => {
|
|
325
|
+
const json = await kv.hget(keyOf(scope, 'comments', prototype), id);
|
|
160
326
|
return json === null ? null : readComment(json);
|
|
161
327
|
};
|
|
162
|
-
const saveComment = (prototype, c) => kv.hset(
|
|
163
|
-
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';
|
|
164
330
|
/**
|
|
165
331
|
* Whether a view reaches a prototype's comments at all. A Client's view
|
|
166
332
|
* reaches only a client-facing prototype: on any other, every op answers
|
|
167
333
|
* as if it had no comments, so no answer confirms an internal slug.
|
|
168
334
|
*/
|
|
169
|
-
const reachable = async (view, prototype) => view === 'studio' || (await isClientFacing(prototype));
|
|
335
|
+
const reachable = async (scope, view, prototype) => view === 'studio' || (await isClientFacing(scope, prototype));
|
|
170
336
|
/**
|
|
171
337
|
* A comment and its thread's root, if `actor` may see both; else null. A
|
|
172
338
|
* reply whose root is gone is not there either.
|
|
173
339
|
*/
|
|
174
|
-
async function seen(actor, prototype, id) {
|
|
175
|
-
if (!(await reachable(viewOf(actor), prototype)))
|
|
340
|
+
async function seen(scope, actor, prototype, id) {
|
|
341
|
+
if (!(await reachable(scope, viewOf(actor), prototype)))
|
|
176
342
|
return null;
|
|
177
|
-
const comment = await commentAt(prototype, id);
|
|
343
|
+
const comment = await commentAt(scope, prototype, id);
|
|
178
344
|
if (!comment)
|
|
179
345
|
return null;
|
|
180
|
-
const root = isRoot(comment) ? comment : await commentAt(prototype, comment.thread);
|
|
181
|
-
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;
|
|
182
348
|
}
|
|
183
349
|
/**
|
|
184
350
|
* The comment an op names, and its root. One that `actor` may not see gets
|
|
185
351
|
* the same answer as one that does not exist, so an id never tells a Client
|
|
186
352
|
* an internal thread is there.
|
|
187
353
|
*/
|
|
188
|
-
async function needThread(actor, prototype, id) {
|
|
189
|
-
allow(actor, 'read');
|
|
190
|
-
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);
|
|
191
357
|
if (!found)
|
|
192
|
-
throw new ReviewError('not_found',
|
|
193
|
-
return found;
|
|
358
|
+
throw new ReviewError('not_found', NO_COMMENT);
|
|
359
|
+
return { actor: by, ...found };
|
|
194
360
|
}
|
|
195
|
-
async function needRoot(actor, prototype, id) {
|
|
196
|
-
const
|
|
197
|
-
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))
|
|
198
364
|
throw new ReviewError('invalid', 'That is a reply; act on its thread.');
|
|
199
|
-
return comment;
|
|
365
|
+
return { actor: found.actor, root: found.comment };
|
|
200
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));
|
|
201
379
|
return {
|
|
202
380
|
person,
|
|
203
|
-
/**
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
async
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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))];
|
|
219
671
|
return all.sort((a, b) => a.team.localeCompare(b.team) || a.name.localeCompare(b.name));
|
|
220
672
|
},
|
|
221
|
-
/**
|
|
222
|
-
|
|
223
|
-
|
|
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);
|
|
224
680
|
const { access, ...fields } = valid(checkPersonInput(input));
|
|
225
|
-
|
|
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));
|
|
226
683
|
const p = {
|
|
227
684
|
id: newId(),
|
|
228
685
|
...fields,
|
|
686
|
+
client: fields.kind === 'studio' ? null : scope.client,
|
|
229
687
|
email: null,
|
|
230
688
|
accessEndsAt: accessEnd(length, deps.now()),
|
|
231
689
|
createdAt: now(),
|
|
@@ -236,112 +694,152 @@ export function createReview(kv, overrides = {}) {
|
|
|
236
694
|
return { person: p, token: await issueLink(p) };
|
|
237
695
|
},
|
|
238
696
|
/** Another one-time link, for another device. Their sessions and other unused links stay. */
|
|
239
|
-
async newLink(actor, id) {
|
|
240
|
-
allow(actor, 'people');
|
|
241
|
-
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);
|
|
242
700
|
return { person: p, token: await issueLink(p) };
|
|
243
701
|
},
|
|
244
702
|
/** Ends every session they hold and every unused link, and gives one new link. */
|
|
245
|
-
async signOutEverywhere(actor, id) {
|
|
246
|
-
allow(actor, 'people');
|
|
247
|
-
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);
|
|
248
706
|
// The epoch first: a failure after it has still ended every session. Atomic, so two at once raise it twice.
|
|
249
|
-
const next = { ...p, epoch: await kv.hincrby(
|
|
250
|
-
await dropLinks(
|
|
707
|
+
const next = { ...p, epoch: await kv.hincrby(keyOf(homeOf(p), 'epochs'), id, 1) };
|
|
708
|
+
await dropLinks(p);
|
|
251
709
|
return { person: next, token: await issueLink(next) };
|
|
252
710
|
},
|
|
253
|
-
async revoke(actor, id) {
|
|
254
|
-
allow(actor, 'people');
|
|
255
|
-
if (
|
|
711
|
+
async revoke(scope, actor, id) {
|
|
712
|
+
const by = await allow(scope, actor, 'people', NO_PERSON);
|
|
713
|
+
if (by.id === id)
|
|
256
714
|
throw new ReviewError('invalid', 'You cannot revoke yourself.');
|
|
257
|
-
const p = await needPerson(id);
|
|
715
|
+
const p = await needPerson(scope, id);
|
|
258
716
|
const revokedAt = p.revokedAt ?? now();
|
|
259
717
|
// Revocation and the epoch before the links go, so a failure part-way never leaves an active
|
|
260
718
|
// person; a leftover link opens nothing for someone revoked.
|
|
261
719
|
if (!p.revokedAt)
|
|
262
|
-
await kv.hset(
|
|
263
|
-
const epoch = await kv.hincrby(
|
|
264
|
-
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);
|
|
265
723
|
return { ...p, revokedAt, epoch };
|
|
266
724
|
},
|
|
267
725
|
/** Sets one person's access length, counted from now. Their sessions follow at the gate's next check, within five minutes. */
|
|
268
|
-
async setAccess(actor, id, access) {
|
|
269
|
-
allow(actor, 'people');
|
|
726
|
+
async setAccess(scope, actor, id, access) {
|
|
727
|
+
await allow(scope, actor, 'people', NO_PERSON);
|
|
270
728
|
const length = valid(checkAccess(access));
|
|
271
|
-
const p = await needPerson(id);
|
|
729
|
+
const p = await needPerson(scope, id);
|
|
272
730
|
if (p.revokedAt)
|
|
273
731
|
throw new ReviewError('invalid', 'That person is revoked; add them again.');
|
|
274
732
|
await savePerson({ ...p, accessEndsAt: accessEnd(length, deps.now()) });
|
|
275
733
|
// Read back, so the answer carries a revocation or epoch that landed meanwhile.
|
|
276
|
-
return (await
|
|
734
|
+
return (await personAt(homeOf(p), id));
|
|
735
|
+
},
|
|
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);
|
|
740
|
+
},
|
|
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;
|
|
277
759
|
},
|
|
278
|
-
/**
|
|
279
|
-
async
|
|
280
|
-
|
|
281
|
-
return
|
|
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);
|
|
282
764
|
},
|
|
283
|
-
|
|
284
|
-
|
|
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);
|
|
285
782
|
const length = valid(checkAccess(access));
|
|
286
|
-
await kv.hset(
|
|
783
|
+
await kv.hset(keyOf('studio', 'settings'), 'access', length);
|
|
287
784
|
return length;
|
|
288
785
|
},
|
|
289
|
-
/** Who a link signs in, without using it: mail and chat previews open links too. */
|
|
290
|
-
async peekLink(token) {
|
|
291
|
-
const found = await findLink(token);
|
|
292
|
-
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;
|
|
293
790
|
},
|
|
294
791
|
/**
|
|
295
792
|
* Uses a link up and returns who it signs in. The delete is the claim: of
|
|
296
793
|
* two requests with one token, only the one whose delete removed the link
|
|
297
794
|
* gets a person, so a link signs in one device at most. Who it signs in is
|
|
298
|
-
* 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.
|
|
299
797
|
*/
|
|
300
|
-
async useLink(token) {
|
|
301
|
-
const found = await findLink(token);
|
|
798
|
+
async useLink(scope, token) {
|
|
799
|
+
const found = await findLink(scope, token);
|
|
302
800
|
// Every read and check comes before the delete, so a store failure before it leaves the link usable.
|
|
303
|
-
const p = found && (await linkPerson(found
|
|
801
|
+
const p = found && (await linkPerson(scope, found));
|
|
304
802
|
if (!found || !p)
|
|
305
803
|
return null;
|
|
306
|
-
if ((await kv.hdel(
|
|
804
|
+
if ((await kv.hdel(keyOf(found.home, 'links'), found.hash)) !== 1)
|
|
307
805
|
return null;
|
|
308
806
|
// A revoke or sign out everywhere can land between the check and the claim, before its
|
|
309
807
|
// dropLinks reaches this link: read again, and sign in only someone still active on the same epoch.
|
|
310
|
-
const after = await
|
|
808
|
+
const after = await personAt(found.home, p.id);
|
|
311
809
|
return active(after) && after.epoch === p.epoch ? after : null;
|
|
312
810
|
},
|
|
313
811
|
/**
|
|
314
812
|
* A prototype's comments as the actor's view sees them. `viewAs: 'client'`
|
|
315
813
|
* is Studio's Client view; it never widens a Client's.
|
|
316
814
|
*/
|
|
317
|
-
async listComments(actor, prototype, viewAs) {
|
|
318
|
-
allow(actor, 'read');
|
|
319
|
-
const view = viewOf(
|
|
320
|
-
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)))
|
|
321
819
|
return [];
|
|
322
|
-
return visibleTo(view, await commentsOf(prototype));
|
|
820
|
+
return visibleTo(view, await commentsOf(scope, prototype));
|
|
323
821
|
},
|
|
324
|
-
/** Every comment on every prototype the actor may see, for the CLI (which acts as Studio). */
|
|
325
|
-
async allComments(actor) {
|
|
326
|
-
allow(actor, 'read');
|
|
327
|
-
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);
|
|
328
826
|
const prototypes = [];
|
|
329
|
-
for (const p of await kv.smembers(
|
|
330
|
-
if (await reachable(view, p))
|
|
827
|
+
for (const p of await kv.smembers(keyOf(scope, 'prototypes')))
|
|
828
|
+
if (await reachable(scope, view, p))
|
|
331
829
|
prototypes.push(p);
|
|
332
|
-
return visibleTo(view, (await Promise.all(prototypes.map(commentsOf))).flat());
|
|
830
|
+
return visibleTo(view, (await Promise.all(prototypes.map((p) => commentsOf(scope, p)))).flat());
|
|
333
831
|
},
|
|
334
832
|
/**
|
|
335
833
|
* A new thread. A Client's is client-facing whatever the input says;
|
|
336
834
|
* Studio's is internal unless `audience` is 'client'. Its number is the
|
|
337
835
|
* next of its audience on the prototype, never one used before.
|
|
338
836
|
*/
|
|
339
|
-
async add(actor, input) {
|
|
340
|
-
allow(actor, 'comment');
|
|
837
|
+
async add(scope, actor, input) {
|
|
838
|
+
const by = await allow(scope, actor, 'comment', NO_COMMENT);
|
|
341
839
|
const [body, anchor, asked] = validAll(checkBody(input.body), checkAnchor(input.anchor), checkAudience(input.audience));
|
|
342
840
|
// A Client's thread on a prototype that is not client-facing: the answer an unknown id gets.
|
|
343
|
-
if (!(await reachable(viewOf(
|
|
344
|
-
throw new ReviewError('not_found',
|
|
841
|
+
if (!(await reachable(scope, viewOf(by), anchor.prototype)))
|
|
842
|
+
throw new ReviewError('not_found', NO_COMMENT);
|
|
345
843
|
const id = newId();
|
|
346
844
|
const triage = deps.triage
|
|
347
845
|
? await deps
|
|
@@ -352,7 +850,7 @@ export function createReview(kv, overrides = {}) {
|
|
|
352
850
|
const c = {
|
|
353
851
|
id,
|
|
354
852
|
thread: id,
|
|
355
|
-
author: authorOf(
|
|
853
|
+
author: authorOf(by),
|
|
356
854
|
body,
|
|
357
855
|
createdAt: now(),
|
|
358
856
|
editedAt: null,
|
|
@@ -360,22 +858,22 @@ export function createReview(kv, overrides = {}) {
|
|
|
360
858
|
status: 'open',
|
|
361
859
|
resolvedBy: null,
|
|
362
860
|
triage,
|
|
363
|
-
audience: audienceFor(
|
|
861
|
+
audience: audienceFor(by, asked),
|
|
364
862
|
number: 0,
|
|
365
863
|
};
|
|
366
|
-
c.number = await nextNumber(anchor.prototype, c.audience);
|
|
367
|
-
await kv.sadd(
|
|
368
|
-
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);
|
|
369
867
|
return c;
|
|
370
868
|
},
|
|
371
|
-
async reply(actor, input) {
|
|
372
|
-
const root = await needRoot(actor, input.prototype, input.thread);
|
|
373
|
-
|
|
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);
|
|
374
872
|
const id = newId();
|
|
375
873
|
const c = {
|
|
376
874
|
id,
|
|
377
875
|
thread: root.id,
|
|
378
|
-
author: authorOf(
|
|
876
|
+
author: authorOf(by),
|
|
379
877
|
body: valid(checkBody(input.body)),
|
|
380
878
|
createdAt: now(),
|
|
381
879
|
editedAt: null,
|
|
@@ -387,43 +885,45 @@ export function createReview(kv, overrides = {}) {
|
|
|
387
885
|
audience: root.audience,
|
|
388
886
|
number: root.number,
|
|
389
887
|
};
|
|
390
|
-
await saveComment(input.prototype, c);
|
|
888
|
+
await saveComment(scope, input.prototype, c);
|
|
391
889
|
return c;
|
|
392
890
|
},
|
|
393
|
-
async edit(actor, input) {
|
|
394
|
-
const { comment: c, root } = await needThread(actor, input.prototype, input.id);
|
|
395
|
-
|
|
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);
|
|
396
894
|
const next = { ...c, body: valid(checkBody(input.body)), editedAt: now() };
|
|
397
|
-
await saveComment(input.prototype, next);
|
|
895
|
+
await saveComment(scope, input.prototype, next);
|
|
398
896
|
return next;
|
|
399
897
|
},
|
|
400
898
|
/** Deletes a comment; deleting a thread's root deletes its replies with it. */
|
|
401
|
-
async remove(actor, input) {
|
|
402
|
-
const { comment: c, root } = await needThread(actor, input.prototype, input.id);
|
|
403
|
-
|
|
404
|
-
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];
|
|
405
905
|
for (const x of gone)
|
|
406
|
-
await kv.hdel(
|
|
906
|
+
await kv.hdel(keyOf(scope, 'comments', input.prototype), x.id);
|
|
407
907
|
// The answer names only what the actor may see, judged with the thread's root beside it.
|
|
408
|
-
const shown = new Set(visibleTo(viewOf(
|
|
908
|
+
const shown = new Set(visibleTo(viewOf(by), [root, ...gone]).map((x) => x.id));
|
|
409
909
|
return gone.filter((x) => shown.has(x.id)).map((x) => x.id);
|
|
410
910
|
},
|
|
411
|
-
async resolve(actor, input) {
|
|
412
|
-
const c = await needRoot(actor, input.prototype, input.id);
|
|
413
|
-
|
|
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);
|
|
414
914
|
const next = input.resolved
|
|
415
|
-
? { ...c, status: 'resolved', resolvedBy: authorOf(
|
|
915
|
+
? { ...c, status: 'resolved', resolvedBy: authorOf(by) }
|
|
416
916
|
: { ...c, status: 'open', resolvedBy: null };
|
|
417
|
-
await saveComment(input.prototype, next);
|
|
917
|
+
await saveComment(scope, input.prototype, next);
|
|
418
918
|
return next;
|
|
419
919
|
},
|
|
420
|
-
async retriage(actor, input) {
|
|
421
|
-
allow(actor, 'triage');
|
|
422
|
-
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);
|
|
423
923
|
const states = checkStates(input.states);
|
|
424
924
|
const { kind, state } = valid(checkTriage(input.triage, states.length ? states : null));
|
|
425
925
|
const next = { ...c, triage: { kind, state, by: 'person', confidence: null } };
|
|
426
|
-
await saveComment(input.prototype, next);
|
|
926
|
+
await saveComment(scope, input.prototype, next);
|
|
427
927
|
return next;
|
|
428
928
|
},
|
|
429
929
|
/**
|
|
@@ -431,24 +931,59 @@ export function createReview(kv, overrides = {}) {
|
|
|
431
931
|
* stored, so internal deletes the field and a prototype nobody marked is
|
|
432
932
|
* internal. Studio only.
|
|
433
933
|
*/
|
|
434
|
-
async setAudience(actor, prototype, audience) {
|
|
435
|
-
allow(actor, 'audience');
|
|
934
|
+
async setAudience(scope, actor, prototype, audience) {
|
|
935
|
+
await allow(scope, actor, 'audience', NO_PROTOTYPE);
|
|
436
936
|
const [slug, value] = validAll(isPrototypeSlug(prototype)
|
|
437
937
|
? { ok: true, value: prototype }
|
|
438
938
|
: { ok: false, problems: ['prototype: a prototype slug'] }, checkPrototypeAudience(audience));
|
|
439
939
|
if (value === 'client')
|
|
440
|
-
await kv.hset(
|
|
940
|
+
await kv.hset(keyOf(scope, 'audiences'), slug, 'client');
|
|
441
941
|
else
|
|
442
|
-
await kv.hdel(
|
|
942
|
+
await kv.hdel(keyOf(scope, 'audiences'), slug);
|
|
443
943
|
return value;
|
|
444
944
|
},
|
|
445
945
|
/**
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
* internal
|
|
946
|
+
* What Studio marked on a prototype's dials and variants, with the view
|
|
947
|
+
* the actor gets. A Client reads the marks too: their page needs a dial
|
|
948
|
+
* Studio switched back to internal as much as one it shared, and the
|
|
949
|
+
* names are in the prototype's own code already. An internal prototype
|
|
950
|
+
* has none for a Client, as it has no comments.
|
|
449
951
|
*/
|
|
450
|
-
async
|
|
451
|
-
|
|
952
|
+
async controlsOf(scope, actor, prototype) {
|
|
953
|
+
const by = await allow(scope, actor, 'read', NO_PROTOTYPE);
|
|
954
|
+
if (!isPrototypeSlug(prototype))
|
|
955
|
+
throw new ReviewError('invalid', 'Some fields are not valid.', ['prototype: a prototype slug']);
|
|
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))) };
|
|
960
|
+
},
|
|
961
|
+
/**
|
|
962
|
+
* Marks one dial or one variant client-facing or internal. A dial's mark
|
|
963
|
+
* is stored either way, because it overrules what the code says; a
|
|
964
|
+
* variant's internal is the field's absence. Studio only. The server
|
|
965
|
+
* keeps no list of a prototype's controls, so any well-formed id is taken.
|
|
966
|
+
*/
|
|
967
|
+
async setControl(scope, actor, input) {
|
|
968
|
+
await allow(scope, actor, 'audience', NO_PROTOTYPE);
|
|
969
|
+
const [slug, mark] = validAll(isPrototypeSlug(input.prototype)
|
|
970
|
+
? { ok: true, value: input.prototype }
|
|
971
|
+
: { ok: false, problems: ['prototype: a prototype slug'] }, checkControl(input));
|
|
972
|
+
const controls = keyOf(scope, 'controls', slug);
|
|
973
|
+
const field = controlField(mark.kind, mark.id);
|
|
974
|
+
if (mark.kind === 'variant' && mark.audience === 'internal')
|
|
975
|
+
await kv.hdel(controls, field);
|
|
976
|
+
else
|
|
977
|
+
await kv.hset(controls, field, mark.audience);
|
|
978
|
+
return mark;
|
|
979
|
+
},
|
|
980
|
+
/**
|
|
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.
|
|
984
|
+
*/
|
|
985
|
+
async audienceOf(scope, prototype) {
|
|
986
|
+
return (await isClientFacing(scope, prototype)) ? 'client' : 'internal';
|
|
452
987
|
},
|
|
453
988
|
/**
|
|
454
989
|
* What the index is built from: the view the actor gets (Client view for
|
|
@@ -456,21 +991,31 @@ export function createReview(kv, overrides = {}) {
|
|
|
456
991
|
* server keeps no list of internal ones: Studio's view reads the build's
|
|
457
992
|
* own list, which the gate refuses to a Client.
|
|
458
993
|
*/
|
|
459
|
-
async visiblePrototypes(actor, viewAs) {
|
|
460
|
-
allow(actor, 'read');
|
|
461
|
-
const view = viewOf(
|
|
462
|
-
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'));
|
|
463
998
|
const clientFacing = Object.entries(all)
|
|
464
999
|
.filter(([slug, value]) => value === 'client' && isPrototypeSlug(slug))
|
|
465
1000
|
.map(([slug]) => slug)
|
|
466
1001
|
.sort();
|
|
467
1002
|
return { view, clientFacing };
|
|
468
1003
|
},
|
|
469
|
-
/**
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
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);
|
|
474
1019
|
if (found)
|
|
475
1020
|
return { prototype, comment: found.comment };
|
|
476
1021
|
}
|