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