@mostra/runtime 0.11.0 → 0.13.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/dist/bin.js +930 -458
- package/dist/bin.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/{instance-DCiUHvgZ.js → instance-D5gh-sMM.js} +1231 -46
- package/dist/instance-D5gh-sMM.js.map +1 -0
- package/dist/reference/cli.md +112 -2
- package/dist/reference/diagnostics.json +29 -2
- package/dist/reference/errors/MOSTRA_REMOTE_QUEUE_FULL.md +2 -2
- package/dist/reference/errors/MOSTRA_TEAM_PRESENCE_RATE_LIMITED.md +23 -0
- package/dist/reference/errors/MOSTRA_TEAM_ROSTER_POLICY.md +23 -0
- package/dist/reference/errors/MOSTRA_TEAM_ROSTER_TOO_LARGE.md +23 -0
- package/dist/reference/self-hosting.md +1 -1
- package/dist/skill/SKILL.md +5 -3
- package/dist/skill/references/answers.md +3 -1
- package/dist/skill/references/mcp.md +2 -2
- package/dist/skill/references/teams.md +78 -0
- package/package.json +6 -6
- package/src/actions/index.ts +5 -2
- package/src/gateway/actions.ts +6 -1
- package/src/gateway/browser.ts +84 -1
- package/src/gateway/capabilities.ts +5 -1
- package/src/gateway/index.ts +43 -1
- package/src/gateway/team.ts +181 -0
- package/src/instance/index.ts +2 -1
- package/src/interaction/ephemeral.ts +21 -0
- package/src/interaction/events.ts +17 -0
- package/src/interaction/index.ts +265 -20
- package/src/interaction/roster.ts +585 -0
- package/src/interaction/team.ts +426 -0
- package/src/interaction/wait.ts +150 -0
- package/src/ledger/migrations.ts +44 -0
- package/src/link/hosted.ts +14 -3
- package/src/link/manager.ts +31 -1
- package/src/server/api.ts +1 -1
- package/src/server/auth.ts +83 -16
- package/src/server/authorize.ts +27 -4
- package/src/server/config.ts +53 -0
- package/src/server/directory.ts +73 -0
- package/src/server/feed.ts +89 -3
- package/src/server/hosted-pages.ts +19 -6
- package/src/server/hosted.ts +108 -49
- package/src/server/index.ts +72 -15
- package/src/server/metering.ts +117 -12
- package/src/server/operator.ts +87 -28
- package/src/server/providers.ts +61 -9
- package/src/server/relay.ts +258 -107
- package/src/share/index.ts +50 -2
- package/src/verify/index.ts +7 -0
- package/src/verify/runner.ts +5 -1
- package/dist/instance-DCiUHvgZ.js.map +0 -1
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expected respondents and their roster (D121, D168; CO-01 to CO-03, CO-07). An artifact names the
|
|
3
|
+
* people it expects answers from; each acts as one viewer of kind `token` (D104) through a personal
|
|
4
|
+
* link the gateway mints. The roster has rounds: under `once` one, complete while every respondent
|
|
5
|
+
* has a submission; under `epochs` a new one after each completion, complete once every respondent
|
|
6
|
+
* submitted since it began. A completion is a durable `roster.completed`, appended in the same
|
|
7
|
+
* transaction as the submission (or roster change) that made it, so a wait, a host and the
|
|
8
|
+
* operator's events hear it exactly once per completion.
|
|
9
|
+
*
|
|
10
|
+
* Every function here runs inside the caller's ledger transaction.
|
|
11
|
+
*/
|
|
12
|
+
import { createHash } from "node:crypto";
|
|
13
|
+
import {
|
|
14
|
+
type Actor,
|
|
15
|
+
createDiagnostic,
|
|
16
|
+
DEFAULT_TEAM_VISIBILITY,
|
|
17
|
+
type Peer,
|
|
18
|
+
type Respondent,
|
|
19
|
+
type RespondentForPage,
|
|
20
|
+
type RespondentSpec,
|
|
21
|
+
type RespondentStatus,
|
|
22
|
+
ROSTER_MAX,
|
|
23
|
+
type Roster,
|
|
24
|
+
type RosterCounts,
|
|
25
|
+
type RosterForPage,
|
|
26
|
+
RpcError,
|
|
27
|
+
type SubmissionPolicy,
|
|
28
|
+
type TeamVisibility,
|
|
29
|
+
type ViewerKind,
|
|
30
|
+
} from "@mostra/protocol";
|
|
31
|
+
import type { LedgerDb } from "../ledger/index.ts";
|
|
32
|
+
import { appendEvent } from "./events.ts";
|
|
33
|
+
import { storedPolicy } from "./policy.ts";
|
|
34
|
+
import { viewerForSpec } from "./viewers.ts";
|
|
35
|
+
|
|
36
|
+
/** A `roster_entry` row. */
|
|
37
|
+
interface EntryRow {
|
|
38
|
+
readonly artifact_id: string;
|
|
39
|
+
readonly ref: string;
|
|
40
|
+
readonly name: string | null;
|
|
41
|
+
readonly email: string | null;
|
|
42
|
+
readonly viewer_id: string;
|
|
43
|
+
readonly token_id: string | null;
|
|
44
|
+
readonly position: number;
|
|
45
|
+
readonly added_at: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A `roster_state` row. */
|
|
49
|
+
interface StateRow {
|
|
50
|
+
readonly artifact_id: string;
|
|
51
|
+
readonly round: number;
|
|
52
|
+
readonly round_cursor: number;
|
|
53
|
+
readonly round_started_at: string;
|
|
54
|
+
readonly complete: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The roster's state: round 1 from the start of everything until the roster first changes it. */
|
|
58
|
+
function stateOf(db: LedgerDb, artifactId: string): StateRow {
|
|
59
|
+
return (
|
|
60
|
+
db.get<StateRow>("SELECT * FROM roster_state WHERE artifact_id = ?", artifactId) ?? {
|
|
61
|
+
artifact_id: artifactId,
|
|
62
|
+
round: 1,
|
|
63
|
+
round_cursor: 0,
|
|
64
|
+
round_started_at: "",
|
|
65
|
+
complete: 0,
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function entries(db: LedgerDb, artifactId: string): EntryRow[] {
|
|
71
|
+
return db.all<EntryRow>(
|
|
72
|
+
"SELECT * FROM roster_entry WHERE artifact_id = ? ORDER BY position",
|
|
73
|
+
artifactId,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function policyOf(db: LedgerDb, artifactId: string): SubmissionPolicy {
|
|
78
|
+
const row = db.get<{ policy_json: string | null }>(
|
|
79
|
+
"SELECT policy_json FROM artifact WHERE id = ?",
|
|
80
|
+
artifactId,
|
|
81
|
+
);
|
|
82
|
+
return storedPolicy(row?.policy_json ?? null);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* What a page may learn of other viewers' submissions (CO-07, D168): under `once` for the whole
|
|
87
|
+
* artifact anyone's submission closes every draft, so every page hears of it; otherwise a
|
|
88
|
+
* submission is its viewer's status, shown only as the visibility allows, the owner seeing as
|
|
89
|
+
* `names`.
|
|
90
|
+
*/
|
|
91
|
+
export interface SubmissionStanding {
|
|
92
|
+
readonly closesAll: boolean;
|
|
93
|
+
readonly visibility: TeamVisibility;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function submissionStanding(
|
|
97
|
+
db: LedgerDb,
|
|
98
|
+
artifactId: string,
|
|
99
|
+
owner: boolean,
|
|
100
|
+
): SubmissionStanding {
|
|
101
|
+
const policy = policyOf(db, artifactId);
|
|
102
|
+
return {
|
|
103
|
+
closesAll: policy.policy === "once" && policy.scope !== "per-viewer",
|
|
104
|
+
visibility: owner ? "names" : teamVisibility(db, artifactId),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* What viewers see of each other on an artifact (CO-07): its latest version's `team.visibility`,
|
|
110
|
+
* else `private` (D168). The latest version decides for every view, so a version that narrows it
|
|
111
|
+
* narrows it for pages of older versions too.
|
|
112
|
+
*/
|
|
113
|
+
export function teamVisibility(db: LedgerDb, artifactId: string): TeamVisibility {
|
|
114
|
+
const row = db.get<{ manifest_json: string }>(
|
|
115
|
+
`SELECT version.manifest_json FROM artifact JOIN version ON version.id = artifact.latest_version_id
|
|
116
|
+
WHERE artifact.id = ?`,
|
|
117
|
+
artifactId,
|
|
118
|
+
);
|
|
119
|
+
if (!row) return DEFAULT_TEAM_VISIBILITY;
|
|
120
|
+
try {
|
|
121
|
+
const manifest = JSON.parse(row.manifest_json) as { team?: { visibility?: unknown } };
|
|
122
|
+
const value = manifest.team?.visibility;
|
|
123
|
+
return value === "counts" || value === "names" || value === "private"
|
|
124
|
+
? value
|
|
125
|
+
: DEFAULT_TEAM_VISIBILITY;
|
|
126
|
+
} catch {
|
|
127
|
+
return DEFAULT_TEAM_VISIBILITY;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* A viewer's key as pages see it (D168): stable for one viewer of one artifact, different across
|
|
133
|
+
* artifacts, and no viewer ID, which pages never learn of others (D104).
|
|
134
|
+
*/
|
|
135
|
+
export function peerKey(artifactId: string, viewerId: string): string {
|
|
136
|
+
const digest = createHash("sha256")
|
|
137
|
+
.update(`mostra/viewer-key\u0000${artifactId}\u0000${viewerId}`)
|
|
138
|
+
.digest("hex");
|
|
139
|
+
return `vk_${digest.slice(0, 24)}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** One respondent's standing in the round. */
|
|
143
|
+
interface Standing {
|
|
144
|
+
readonly entry: EntryRow;
|
|
145
|
+
readonly status: RespondentStatus;
|
|
146
|
+
readonly openedAt?: string;
|
|
147
|
+
readonly activeAt?: string;
|
|
148
|
+
readonly submission?: { readonly id: string; readonly at: string; readonly position: number };
|
|
149
|
+
readonly submissions: number;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Where each respondent stands in the round (CO-03): `submitted` with a submission in it (under
|
|
154
|
+
* `once`, their one submission; under `epochs`, their latest after the round began), else
|
|
155
|
+
* `drafting` when they opened the artifact or wrote their draft in it, else `not-opened`.
|
|
156
|
+
*/
|
|
157
|
+
function standings(db: LedgerDb, artifactId: string, state: StateRow): Standing[] {
|
|
158
|
+
const list = entries(db, artifactId);
|
|
159
|
+
if (list.length === 0) return [];
|
|
160
|
+
// Four grouped reads over the roster's viewers, whatever its size: a settle runs in every
|
|
161
|
+
// submit transaction, so it costs the same for 5 respondents or 1,000.
|
|
162
|
+
const inRound = new Map(
|
|
163
|
+
db
|
|
164
|
+
.all<{ viewer_id: string; id: string; created_at: string; position: number }>(
|
|
165
|
+
"SELECT s.viewer_id, s.id, s.created_at, s.position FROM submission s JOIN roster_entry r ON r.artifact_id = s.artifact_id AND r.viewer_id = s.viewer_id WHERE s.artifact_id = ? AND s.position > ? AND s.position = (SELECT MAX(t.position) FROM submission t WHERE t.artifact_id = s.artifact_id AND t.viewer_id = s.viewer_id)",
|
|
166
|
+
artifactId,
|
|
167
|
+
state.round_cursor,
|
|
168
|
+
)
|
|
169
|
+
.map((row) => [row.viewer_id, row]),
|
|
170
|
+
);
|
|
171
|
+
const totals = new Map(
|
|
172
|
+
db
|
|
173
|
+
.all<{ viewer_id: string; n: number }>(
|
|
174
|
+
"SELECT s.viewer_id, COUNT(*) AS n FROM submission s JOIN roster_entry r ON r.artifact_id = s.artifact_id AND r.viewer_id = s.viewer_id WHERE s.artifact_id = ? GROUP BY s.viewer_id",
|
|
175
|
+
artifactId,
|
|
176
|
+
)
|
|
177
|
+
.map((row) => [row.viewer_id, row.n]),
|
|
178
|
+
);
|
|
179
|
+
const views = new Map(
|
|
180
|
+
db
|
|
181
|
+
.all<{ viewer_id: string; first: string; last: string }>(
|
|
182
|
+
"SELECT v.viewer_id, MIN(v.opened_at) AS first, MAX(v.opened_at) AS last FROM view v JOIN roster_entry r ON r.artifact_id = v.artifact_id AND r.viewer_id = v.viewer_id WHERE v.artifact_id = ? GROUP BY v.viewer_id",
|
|
183
|
+
artifactId,
|
|
184
|
+
)
|
|
185
|
+
.map((row) => [row.viewer_id, row]),
|
|
186
|
+
);
|
|
187
|
+
const wrote = new Map(
|
|
188
|
+
db
|
|
189
|
+
.all<{ viewer_id: string; last: string }>(
|
|
190
|
+
"SELECT d.viewer_id, MAX(f.updated_at) AS last FROM field f JOIN document d ON d.id = f.document_id JOIN roster_entry r ON r.artifact_id = d.artifact_id AND r.viewer_id = d.viewer_id WHERE d.artifact_id = ? AND d.scope = 'viewer' GROUP BY d.viewer_id",
|
|
191
|
+
artifactId,
|
|
192
|
+
)
|
|
193
|
+
.map((row) => [row.viewer_id, row.last]),
|
|
194
|
+
);
|
|
195
|
+
return list.map((entry) => {
|
|
196
|
+
const submission = inRound.get(entry.viewer_id);
|
|
197
|
+
const opened = views.get(entry.viewer_id);
|
|
198
|
+
const activeAt =
|
|
199
|
+
[opened?.last, wrote.get(entry.viewer_id)]
|
|
200
|
+
.filter((at): at is string => typeof at === "string")
|
|
201
|
+
.sort()
|
|
202
|
+
.at(-1) ?? undefined;
|
|
203
|
+
const status: RespondentStatus = submission
|
|
204
|
+
? "submitted"
|
|
205
|
+
: activeAt !== undefined && activeAt >= state.round_started_at
|
|
206
|
+
? "drafting"
|
|
207
|
+
: "not-opened";
|
|
208
|
+
return {
|
|
209
|
+
entry,
|
|
210
|
+
status,
|
|
211
|
+
...(opened ? { openedAt: opened.first } : {}),
|
|
212
|
+
...(activeAt !== undefined ? { activeAt } : {}),
|
|
213
|
+
...(submission
|
|
214
|
+
? {
|
|
215
|
+
submission: {
|
|
216
|
+
id: submission.id,
|
|
217
|
+
at: submission.created_at,
|
|
218
|
+
position: submission.position,
|
|
219
|
+
},
|
|
220
|
+
}
|
|
221
|
+
: {}),
|
|
222
|
+
submissions: totals.get(entry.viewer_id) ?? 0,
|
|
223
|
+
};
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function countsOf(list: readonly Standing[]): RosterCounts {
|
|
228
|
+
return {
|
|
229
|
+
total: list.length,
|
|
230
|
+
notOpened: list.filter((one) => one.status === "not-opened").length,
|
|
231
|
+
drafting: list.filter((one) => one.status === "drafting").length,
|
|
232
|
+
submitted: list.filter((one) => one.status === "submitted").length,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Whether every respondent submitted in the round; an empty roster never has. */
|
|
237
|
+
function completeOf(list: readonly Standing[]): boolean {
|
|
238
|
+
return list.length > 0 && list.every((one) => one.status === "submitted");
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** The whole roster, as agents and hosts read it (`roster/get`, CO-03, CO-07). */
|
|
242
|
+
export function readRoster(
|
|
243
|
+
db: LedgerDb,
|
|
244
|
+
artifactId: string,
|
|
245
|
+
live: (tokenId: string) => boolean = () => true,
|
|
246
|
+
): Roster {
|
|
247
|
+
const state = stateOf(db, artifactId);
|
|
248
|
+
const list = standings(db, artifactId, state);
|
|
249
|
+
return {
|
|
250
|
+
round: state.round,
|
|
251
|
+
complete: completeOf(list),
|
|
252
|
+
visibility: teamVisibility(db, artifactId),
|
|
253
|
+
counts: countsOf(list),
|
|
254
|
+
respondents: list.map((one): Respondent => {
|
|
255
|
+
const { entry } = one;
|
|
256
|
+
return {
|
|
257
|
+
ref: entry.ref,
|
|
258
|
+
...(entry.name !== null ? { name: entry.name } : {}),
|
|
259
|
+
...(entry.email !== null ? { email: entry.email } : {}),
|
|
260
|
+
viewerId: entry.viewer_id,
|
|
261
|
+
status: one.status,
|
|
262
|
+
...(one.openedAt !== undefined ? { openedAt: one.openedAt } : {}),
|
|
263
|
+
...(one.activeAt !== undefined ? { activeAt: one.activeAt } : {}),
|
|
264
|
+
...(one.submission ? { submission: { id: one.submission.id, at: one.submission.at } } : {}),
|
|
265
|
+
submissions: one.submissions,
|
|
266
|
+
...(entry.token_id !== null && live(entry.token_id) ? { tokenId: entry.token_id } : {}),
|
|
267
|
+
addedAt: entry.added_at,
|
|
268
|
+
};
|
|
269
|
+
}),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Whether the artifact has any expected respondent. */
|
|
274
|
+
export function hasRoster(db: LedgerDb, artifactId: string): boolean {
|
|
275
|
+
return (
|
|
276
|
+
db.get("SELECT 1 FROM roster_entry WHERE artifact_id = ? LIMIT 1", artifactId) !== undefined
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Settles the roster after a change inside its transaction (a submission, an import, a roster
|
|
282
|
+
* change): when the round became complete, appends `roster.completed` with each respondent's
|
|
283
|
+
* submission in the round and, under `epochs`, begins the next round; when a roster change made a
|
|
284
|
+
* complete round incomplete again (someone joined), says so. Returns whether it is complete now.
|
|
285
|
+
*/
|
|
286
|
+
export function settleRoster(db: LedgerDb, artifactId: string, now: string): boolean {
|
|
287
|
+
if (
|
|
288
|
+
!hasRoster(db, artifactId) &&
|
|
289
|
+
!db.get("SELECT 1 FROM roster_state WHERE artifact_id = ?", artifactId)
|
|
290
|
+
) {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
const state = stateOf(db, artifactId);
|
|
294
|
+
const list = standings(db, artifactId, state);
|
|
295
|
+
const complete = completeOf(list);
|
|
296
|
+
const ensure = (): void => {
|
|
297
|
+
db.run(
|
|
298
|
+
"INSERT OR IGNORE INTO roster_state (artifact_id, round, round_cursor, round_started_at, complete) VALUES (?, 1, 0, '', 0)",
|
|
299
|
+
artifactId,
|
|
300
|
+
);
|
|
301
|
+
};
|
|
302
|
+
if (complete && state.complete === 0) {
|
|
303
|
+
ensure();
|
|
304
|
+
const cursor = Number(
|
|
305
|
+
appendEvent(db, {
|
|
306
|
+
artifactId,
|
|
307
|
+
type: "roster.completed",
|
|
308
|
+
actor: { kind: "mostra" },
|
|
309
|
+
payload: {
|
|
310
|
+
round: state.round,
|
|
311
|
+
respondents: list.length,
|
|
312
|
+
submissions: list.map((one) => ({
|
|
313
|
+
ref: one.entry.ref,
|
|
314
|
+
submissionId: one.submission?.id ?? "",
|
|
315
|
+
})),
|
|
316
|
+
},
|
|
317
|
+
}),
|
|
318
|
+
);
|
|
319
|
+
if (policyOf(db, artifactId).policy === "epochs") {
|
|
320
|
+
// The next round begins with this completion: what anyone submits from now on is its.
|
|
321
|
+
db.run(
|
|
322
|
+
"UPDATE roster_state SET round = round + 1, round_cursor = ?, round_started_at = ?, complete = 0 WHERE artifact_id = ?",
|
|
323
|
+
cursor,
|
|
324
|
+
now,
|
|
325
|
+
artifactId,
|
|
326
|
+
);
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
db.run("UPDATE roster_state SET complete = 1 WHERE artifact_id = ?", artifactId);
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
if (!complete && state.complete === 1) {
|
|
333
|
+
db.run("UPDATE roster_state SET complete = 0 WHERE artifact_id = ?", artifactId);
|
|
334
|
+
}
|
|
335
|
+
return complete;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function rosterTooLarge(count: number): RpcError {
|
|
339
|
+
return new RpcError(
|
|
340
|
+
createDiagnostic("MOSTRA_TEAM_ROSTER_TOO_LARGE", {
|
|
341
|
+
message: `This change would give the roster ${count} expected respondents, more than ${ROSTER_MAX}.`,
|
|
342
|
+
}),
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Adds expected respondents (CO-01), idempotently by `ref`: one already there keeps their row and
|
|
348
|
+
* link, and a name or email given again replaces theirs. `link` gives each respondent's personal
|
|
349
|
+
* link's token (`current` is the one their row names, which the caller keeps while it works) and
|
|
350
|
+
* runs in this transaction. Appends `roster.changed` when anything changed.
|
|
351
|
+
*/
|
|
352
|
+
export function addRespondents(
|
|
353
|
+
db: LedgerDb,
|
|
354
|
+
input: {
|
|
355
|
+
readonly artifactId: string;
|
|
356
|
+
readonly respondents: readonly RespondentSpec[];
|
|
357
|
+
readonly actor: Actor;
|
|
358
|
+
readonly now: string;
|
|
359
|
+
readonly link: (viewerId: string, current: string | undefined) => string;
|
|
360
|
+
},
|
|
361
|
+
): { readonly tokens: readonly { readonly ref: string; readonly tokenId: string }[] } {
|
|
362
|
+
const seen = new Set<string>();
|
|
363
|
+
const specs = input.respondents.filter((spec) => {
|
|
364
|
+
if (seen.has(spec.ref)) return false;
|
|
365
|
+
seen.add(spec.ref);
|
|
366
|
+
return true;
|
|
367
|
+
});
|
|
368
|
+
const existing = new Map(entries(db, input.artifactId).map((entry) => [entry.ref, entry]));
|
|
369
|
+
const fresh = specs.filter((spec) => !existing.has(spec.ref)).length;
|
|
370
|
+
if (existing.size + fresh > ROSTER_MAX) throw rosterTooLarge(existing.size + fresh);
|
|
371
|
+
let position =
|
|
372
|
+
db.get<{ last: number | null }>(
|
|
373
|
+
"SELECT MAX(position) AS last FROM roster_entry WHERE artifact_id = ?",
|
|
374
|
+
input.artifactId,
|
|
375
|
+
)?.last ?? 0;
|
|
376
|
+
const added: { ref: string; name?: string }[] = [];
|
|
377
|
+
const renamed: { ref: string; name?: string }[] = [];
|
|
378
|
+
const relinked: string[] = [];
|
|
379
|
+
const tokens: { ref: string; tokenId: string }[] = [];
|
|
380
|
+
for (const spec of specs) {
|
|
381
|
+
const viewerId = viewerForSpec(db, {
|
|
382
|
+
ref: spec.ref,
|
|
383
|
+
...(spec.name !== undefined ? { name: spec.name } : {}),
|
|
384
|
+
});
|
|
385
|
+
const entry = existing.get(spec.ref);
|
|
386
|
+
const tokenId = input.link(viewerId, entry?.token_id ?? undefined);
|
|
387
|
+
tokens.push({ ref: spec.ref, tokenId });
|
|
388
|
+
if (!entry) {
|
|
389
|
+
position += 1;
|
|
390
|
+
db.run(
|
|
391
|
+
"INSERT INTO roster_entry (artifact_id, ref, name, email, viewer_id, token_id, position, added_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
392
|
+
input.artifactId,
|
|
393
|
+
spec.ref,
|
|
394
|
+
spec.name ?? null,
|
|
395
|
+
spec.email ?? null,
|
|
396
|
+
viewerId,
|
|
397
|
+
tokenId,
|
|
398
|
+
position,
|
|
399
|
+
input.now,
|
|
400
|
+
);
|
|
401
|
+
added.push({ ref: spec.ref, ...(spec.name !== undefined ? { name: spec.name } : {}) });
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
const name = spec.name ?? entry.name;
|
|
405
|
+
const email = spec.email ?? entry.email;
|
|
406
|
+
if (name !== entry.name || email !== entry.email || tokenId !== entry.token_id) {
|
|
407
|
+
db.run(
|
|
408
|
+
"UPDATE roster_entry SET name = ?, email = ?, token_id = ? WHERE artifact_id = ? AND ref = ?",
|
|
409
|
+
name,
|
|
410
|
+
email,
|
|
411
|
+
tokenId,
|
|
412
|
+
input.artifactId,
|
|
413
|
+
spec.ref,
|
|
414
|
+
);
|
|
415
|
+
if (name !== entry.name) renamed.push({ ref: spec.ref, ...(name !== null ? { name } : {}) });
|
|
416
|
+
// A new personal link replaced one that stopped working (revoked, expired, moved).
|
|
417
|
+
if (tokenId !== entry.token_id) relinked.push(spec.ref);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (added.length > 0 || renamed.length > 0 || relinked.length > 0) {
|
|
421
|
+
changed(db, input, { added, renamed, relinked, removed: [] });
|
|
422
|
+
}
|
|
423
|
+
return { tokens };
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Removes expected respondents by `ref` (D168); unknown ones are ignored. Their links, drafts and
|
|
428
|
+
* submissions stay. Appends `roster.changed` and, when everyone still expected has answered,
|
|
429
|
+
* `roster.completed`.
|
|
430
|
+
*/
|
|
431
|
+
export function removeRespondents(
|
|
432
|
+
db: LedgerDb,
|
|
433
|
+
input: {
|
|
434
|
+
readonly artifactId: string;
|
|
435
|
+
readonly refs: readonly string[];
|
|
436
|
+
readonly actor: Actor;
|
|
437
|
+
readonly now: string;
|
|
438
|
+
},
|
|
439
|
+
): { readonly removed: readonly string[] } {
|
|
440
|
+
const removed: string[] = [];
|
|
441
|
+
for (const ref of new Set(input.refs)) {
|
|
442
|
+
const result = db.run(
|
|
443
|
+
"DELETE FROM roster_entry WHERE artifact_id = ? AND ref = ?",
|
|
444
|
+
input.artifactId,
|
|
445
|
+
ref,
|
|
446
|
+
);
|
|
447
|
+
if (result.changes > 0) removed.push(ref);
|
|
448
|
+
}
|
|
449
|
+
if (removed.length > 0) changed(db, input, { added: [], renamed: [], relinked: [], removed });
|
|
450
|
+
return { removed };
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** Appends `roster.changed` after settling the roster, so the event says where it stands. */
|
|
454
|
+
function changed(
|
|
455
|
+
db: LedgerDb,
|
|
456
|
+
input: { readonly artifactId: string; readonly actor: Actor; readonly now: string },
|
|
457
|
+
change: {
|
|
458
|
+
readonly added: readonly { ref: string; name?: string }[];
|
|
459
|
+
readonly renamed: readonly { ref: string; name?: string }[];
|
|
460
|
+
readonly relinked: readonly string[];
|
|
461
|
+
readonly removed: readonly string[];
|
|
462
|
+
},
|
|
463
|
+
): void {
|
|
464
|
+
db.run(
|
|
465
|
+
"INSERT OR IGNORE INTO roster_state (artifact_id, round, round_cursor, round_started_at, complete) VALUES (?, 1, 0, '', 0)",
|
|
466
|
+
input.artifactId,
|
|
467
|
+
);
|
|
468
|
+
// The change first, then a completion it caused: a reader of events sees the roster's story
|
|
469
|
+
// in order (removing the last one who hadn't answered completes the round).
|
|
470
|
+
const before = stateOf(db, input.artifactId);
|
|
471
|
+
const list = standings(db, input.artifactId, before);
|
|
472
|
+
appendEvent(db, {
|
|
473
|
+
artifactId: input.artifactId,
|
|
474
|
+
type: "roster.changed",
|
|
475
|
+
actor: input.actor,
|
|
476
|
+
payload: {
|
|
477
|
+
round: before.round,
|
|
478
|
+
respondents: list.length,
|
|
479
|
+
complete: completeOf(list),
|
|
480
|
+
...(change.added.length > 0 ? { added: change.added } : {}),
|
|
481
|
+
...(change.renamed.length > 0 ? { renamed: change.renamed } : {}),
|
|
482
|
+
...(change.relinked.length > 0 ? { relinked: change.relinked } : {}),
|
|
483
|
+
...(change.removed.length > 0 ? { removed: change.removed } : {}),
|
|
484
|
+
},
|
|
485
|
+
});
|
|
486
|
+
settleRoster(db, input.artifactId, input.now);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** The viewer a page acts as, as the roster sees them. */
|
|
490
|
+
export interface RosterViewer {
|
|
491
|
+
readonly id: string;
|
|
492
|
+
readonly kind: ViewerKind;
|
|
493
|
+
readonly name?: string;
|
|
494
|
+
readonly verified?: boolean;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function peerOf(artifactId: string, viewer: RosterViewer, self: boolean): Peer {
|
|
498
|
+
return {
|
|
499
|
+
key: peerKey(artifactId, viewer.id),
|
|
500
|
+
self,
|
|
501
|
+
kind: viewer.kind,
|
|
502
|
+
...(viewer.name !== undefined ? { name: viewer.name } : {}),
|
|
503
|
+
...(viewer.verified !== undefined ? { verified: viewer.verified } : {}),
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* The roster as one page may see it (CO-03, CO-07): the viewer's own entry always (when they are
|
|
509
|
+
* expected), the counts under `counts` and `names`, every respondent under `names`, and everything
|
|
510
|
+
* for the artifact's owner. Never another viewer's `ref`, email or viewer ID.
|
|
511
|
+
*/
|
|
512
|
+
export function rosterForPage(
|
|
513
|
+
db: LedgerDb,
|
|
514
|
+
input: {
|
|
515
|
+
readonly artifactId: string;
|
|
516
|
+
readonly viewerId: string;
|
|
517
|
+
readonly owner: boolean;
|
|
518
|
+
readonly viewerOf: (viewerId: string) => RosterViewer;
|
|
519
|
+
},
|
|
520
|
+
): RosterForPage {
|
|
521
|
+
const [one] = rosterForPages(db, {
|
|
522
|
+
artifactId: input.artifactId,
|
|
523
|
+
pages: [{ viewerId: input.viewerId, owner: input.owner }],
|
|
524
|
+
viewerOf: input.viewerOf,
|
|
525
|
+
});
|
|
526
|
+
return one as RosterForPage;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* The roster as each of several pages may see it, from one read of the standings (Opus review 9):
|
|
531
|
+
* a change heard by many following pages costs one roster read, not one per page.
|
|
532
|
+
*/
|
|
533
|
+
export function rosterForPages(
|
|
534
|
+
db: LedgerDb,
|
|
535
|
+
input: {
|
|
536
|
+
readonly artifactId: string;
|
|
537
|
+
readonly pages: readonly { readonly viewerId: string; readonly owner: boolean }[];
|
|
538
|
+
readonly viewerOf: (viewerId: string) => RosterViewer;
|
|
539
|
+
},
|
|
540
|
+
): RosterForPage[] {
|
|
541
|
+
const state = stateOf(db, input.artifactId);
|
|
542
|
+
const list = standings(db, input.artifactId, state);
|
|
543
|
+
const visibility = teamVisibility(db, input.artifactId);
|
|
544
|
+
const viewers = new Map<string, RosterViewer>();
|
|
545
|
+
const viewerOf = (viewerId: string): RosterViewer => {
|
|
546
|
+
let viewer = viewers.get(viewerId);
|
|
547
|
+
if (!viewer) {
|
|
548
|
+
viewer = input.viewerOf(viewerId);
|
|
549
|
+
viewers.set(viewerId, viewer);
|
|
550
|
+
}
|
|
551
|
+
return viewer;
|
|
552
|
+
};
|
|
553
|
+
const counts = countsOf(list);
|
|
554
|
+
const complete = completeOf(list);
|
|
555
|
+
const byViewer = new Map(list.map((one) => [one.entry.viewer_id, one]));
|
|
556
|
+
return input.pages.map((page) => {
|
|
557
|
+
const project = (one: Standing): RespondentForPage => {
|
|
558
|
+
const viewer = viewerOf(one.entry.viewer_id);
|
|
559
|
+
return {
|
|
560
|
+
...peerOf(
|
|
561
|
+
input.artifactId,
|
|
562
|
+
// The roster's name is the one the host gave; the viewer row keeps the same.
|
|
563
|
+
{ ...viewer, ...(one.entry.name !== null ? { name: one.entry.name } : {}) },
|
|
564
|
+
one.entry.viewer_id === page.viewerId,
|
|
565
|
+
),
|
|
566
|
+
status: one.status,
|
|
567
|
+
...(one.openedAt !== undefined ? { openedAt: one.openedAt } : {}),
|
|
568
|
+
...(one.submission ? { submittedAt: one.submission.at } : {}),
|
|
569
|
+
};
|
|
570
|
+
};
|
|
571
|
+
const own = byViewer.get(page.viewerId);
|
|
572
|
+
const sees = page.owner ? "names" : visibility;
|
|
573
|
+
return {
|
|
574
|
+
visibility,
|
|
575
|
+
owner: page.owner,
|
|
576
|
+
round: state.round,
|
|
577
|
+
...(own ? { self: project(own) } : {}),
|
|
578
|
+
...(sees !== "private" ? { counts, complete } : {}),
|
|
579
|
+
...(sees === "names" ? { respondents: list.map(project) } : {}),
|
|
580
|
+
};
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/** The roster's events' types: pages never see them (they name refs and emails, D168). */
|
|
585
|
+
export const ROSTER_EVENTS: ReadonlySet<string> = new Set(["roster.changed", "roster.completed"]);
|