critiqs-react 0.3.0 → 0.4.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/AGENT_SETUP.md +198 -66
- package/README.md +214 -31
- package/bin/critiqs.mjs +3 -1
- package/dist/index.js +16877 -4770
- package/dist/server/index.d.ts +1 -87
- package/dist/server/index.js +1 -4889
- package/package.json +6 -5
- package/bin/cli.d.mts +0 -21
- package/bin/cli.mjs +0 -293
- package/bin/env.d.mts +0 -41
- package/bin/env.mjs +0 -384
- package/bin/init.d.mts +0 -126
- package/bin/init.mjs +0 -965
- package/bin/mcp.d.mts +0 -20
- package/bin/mcp.mjs +0 -314
- package/bin/setup.d.mts +0 -183
- package/bin/setup.mjs +0 -1054
- package/dist/backend/convex/_generated/api.d.ts +0 -95
- package/dist/backend/convex/_generated/api.js +0 -23
- package/dist/backend/convex/_generated/dataModel.d.ts +0 -60
- package/dist/backend/convex/_generated/server.d.ts +0 -162
- package/dist/backend/convex/_generated/server.js +0 -101
- package/dist/backend/convex/ai.ts +0 -93
- package/dist/backend/convex/aiSettings.ts +0 -43
- package/dist/backend/convex/auth.config.ts +0 -18
- package/dist/backend/convex/captureHttp.ts +0 -96
- package/dist/backend/convex/captures.ts +0 -167
- package/dist/backend/convex/crons.ts +0 -14
- package/dist/backend/convex/deviceSignIn.ts +0 -151
- package/dist/backend/convex/grouping.ts +0 -41
- package/dist/backend/convex/http.ts +0 -724
- package/dist/backend/convex/lib/ai.ts +0 -6
- package/dist/backend/convex/lib/links.ts +0 -12
- package/dist/backend/convex/lib/pages.ts +0 -207
- package/dist/backend/convex/lib/r2.ts +0 -75
- package/dist/backend/convex/lib/store.ts +0 -509
- package/dist/backend/convex/lib/tokens.ts +0 -101
- package/dist/backend/convex/lib/validators.ts +0 -73
- package/dist/backend/convex/lib/vendor/aws4fetch.js +0 -289
- package/dist/backend/convex/lib/vendor/critiqs_ai.js +0 -49081
- package/dist/backend/convex/lib/vendor/critiqs_core.js +0 -385
- package/dist/backend/convex/lib/vendor/critiqs_server.js +0 -3108
- package/dist/backend/convex/notificationEmails.ts +0 -61
- package/dist/backend/convex/notifications.ts +0 -182
- package/dist/backend/convex/plans.ts +0 -67
- package/dist/backend/convex/projects.ts +0 -46
- package/dist/backend/convex/schema.ts +0 -343
- package/dist/backend/convex/search.ts +0 -26
- package/dist/backend/convex/signIn.ts +0 -368
- package/dist/backend/convex/tags.ts +0 -68
- package/dist/backend/convex/threads.ts +0 -146
- package/dist/backend/convex/users.ts +0 -13
- package/dist/server/chunks/driver-n9zpr0k3.js +0 -21
- package/dist/server/chunks/index-9bg7tmnq.js +0 -522
- package/dist/server/chunks/index-askcfn7t.js +0 -148
- package/dist/server/chunks/providers-rv184xvj.js +0 -48444
- package/dist/server/chunks/token-kzddms7r.js +0 -63
- package/dist/server/chunks/token-util-k8bee1wc.js +0 -4
|
@@ -1,368 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
countUse,
|
|
3
|
-
displayName,
|
|
4
|
-
EMAIL_SEND_LIMIT,
|
|
5
|
-
EMAIL_SEND_WINDOW_MS,
|
|
6
|
-
GLOBAL_SEND_LIMIT,
|
|
7
|
-
GLOBAL_SEND_WINDOW_MS,
|
|
8
|
-
MAX_CODE_SENDS,
|
|
9
|
-
MAX_GUESSES,
|
|
10
|
-
sameDigest,
|
|
11
|
-
} from "./lib/vendor/critiqs_server.js";
|
|
12
|
-
import { v } from "convex/values";
|
|
13
|
-
import { internal } from "./_generated/api";
|
|
14
|
-
import type { Doc, Id } from "./_generated/dataModel";
|
|
15
|
-
import { internalMutation, type MutationCtx } from "./_generated/server";
|
|
16
|
-
import { type ReviewerProfile, SESSION_DAYS, SESSION_MAX_DAYS } from "./lib/tokens";
|
|
17
|
-
|
|
18
|
-
// State behind Critiqs's sign-in popup (see http.ts). Each attempt is bound to
|
|
19
|
-
// the browser that started it by a cookie, so a forwarded sign-in link or a
|
|
20
|
-
// leaked attempt key cannot finish it elsewhere. The reviewer proves they
|
|
21
|
-
// control an email address with a code sent to it. The popup reports back only
|
|
22
|
-
// to the site that opened it, and only after the reviewer allowed that site.
|
|
23
|
-
|
|
24
|
-
/** How long a sign-in window stays usable, long enough for a few codes. */
|
|
25
|
-
export const ATTEMPT_MINUTES = 30;
|
|
26
|
-
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
27
|
-
const CLEANUP_BATCH = 500;
|
|
28
|
-
export const START_WINDOW_MS = 60_000;
|
|
29
|
-
export const START_ORIGIN_LIMIT = 20;
|
|
30
|
-
export const START_GLOBAL_LIMIT = 200;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
export const startAttempt = internalMutation({
|
|
34
|
-
args: {
|
|
35
|
-
key: v.string(),
|
|
36
|
-
origin: v.string(),
|
|
37
|
-
clientState: v.string(),
|
|
38
|
-
bindingHash: v.string(),
|
|
39
|
-
},
|
|
40
|
-
handler: async (ctx, args) => {
|
|
41
|
-
const now = Date.now();
|
|
42
|
-
const buckets = await Promise.all(["global", `origin:${args.origin}`].map(async (key) =>
|
|
43
|
-
({ key, row: await ctx.db.query("signInLimits").withIndex("by_key", q => q.eq("key", key)).unique() })));
|
|
44
|
-
for (const { key, row } of buckets) {
|
|
45
|
-
const limit = key === "global" ? START_GLOBAL_LIMIT : START_ORIGIN_LIMIT;
|
|
46
|
-
if (row !== null && row.expiresAt > now && row.count >= limit) {
|
|
47
|
-
return { allowed: false, retryAfter: Math.ceil((row.expiresAt - now) / 1000) };
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
for (const { key, row } of buckets) {
|
|
51
|
-
const value = { key, count: row !== null && row.expiresAt > now ? row.count + 1 : 1,
|
|
52
|
-
expiresAt: row !== null && row.expiresAt > now ? row.expiresAt : now + START_WINDOW_MS };
|
|
53
|
-
if (row === null) await ctx.db.insert("signInLimits", value);
|
|
54
|
-
else await ctx.db.patch(row._id, value);
|
|
55
|
-
}
|
|
56
|
-
await ctx.db.insert("signInAttempts", { ...args, createdAt: now, codesSent: 0, codeGeneration: 0, guessesLeft: MAX_GUESSES });
|
|
57
|
-
return { allowed: true, retryAfter: 0 };
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Reserves the next code for an attempt and an address. Each attempt sends
|
|
63
|
-
* a few codes at most; each address, and the deployment as a whole, a limited
|
|
64
|
-
* number per window. A new code replaces any earlier one, but the attempt's
|
|
65
|
-
* remaining guesses carry over. The caller generates the code and stores its
|
|
66
|
-
* digest with `storeCode`.
|
|
67
|
-
*/
|
|
68
|
-
export const prepareCode = internalMutation({
|
|
69
|
-
args: { key: v.string(), bindingHash: v.string(), email: v.string() },
|
|
70
|
-
handler: async (ctx, { key, bindingHash, email }) => {
|
|
71
|
-
const attempt = await liveAttempt(ctx, key, bindingHash);
|
|
72
|
-
if (attempt === null || attempt.profile !== undefined || attempt.verifiedEmail !== undefined) {
|
|
73
|
-
return { status: "expired" as const };
|
|
74
|
-
}
|
|
75
|
-
if ((attempt.guessesLeft ?? 0) <= 0) return { status: "locked" as const };
|
|
76
|
-
if ((attempt.codesSent ?? 0) >= MAX_CODE_SENDS) return { status: "too-many-codes" as const };
|
|
77
|
-
const now = Date.now();
|
|
78
|
-
const limits = [
|
|
79
|
-
{ key: `send:${email}`, limit: EMAIL_SEND_LIMIT, windowMs: EMAIL_SEND_WINDOW_MS },
|
|
80
|
-
{ key: "send:global", limit: GLOBAL_SEND_LIMIT, windowMs: GLOBAL_SEND_WINDOW_MS },
|
|
81
|
-
];
|
|
82
|
-
const rows = await Promise.all(
|
|
83
|
-
limits.map(async (limit) => ({
|
|
84
|
-
...limit,
|
|
85
|
-
row: await ctx.db.query("signInLimits").withIndex("by_key", (q) => q.eq("key", limit.key)).unique(),
|
|
86
|
-
})),
|
|
87
|
-
);
|
|
88
|
-
const counted = [];
|
|
89
|
-
for (const { key: limitKey, limit, windowMs, row } of rows) {
|
|
90
|
-
const use = countUse(row === null ? null : { count: row.count, expiresAt: row.expiresAt }, limit, windowMs, now);
|
|
91
|
-
if (!use.allowed) return { status: "rate-limited" as const, retryAfter: Math.ceil(use.retryAfterMs / 1000) };
|
|
92
|
-
counted.push({ key: limitKey, row, next: use.next });
|
|
93
|
-
}
|
|
94
|
-
for (const { key: limitKey, row, next } of counted) {
|
|
95
|
-
if (row === null) await ctx.db.insert("signInLimits", { key: limitKey, ...next });
|
|
96
|
-
else await ctx.db.patch(row._id, next);
|
|
97
|
-
}
|
|
98
|
-
const generation = (attempt.codeGeneration ?? 0) + 1;
|
|
99
|
-
await ctx.db.patch(attempt._id, {
|
|
100
|
-
email,
|
|
101
|
-
codeGeneration: generation,
|
|
102
|
-
codesSent: (attempt.codesSent ?? 0) + 1,
|
|
103
|
-
codeDigest: undefined,
|
|
104
|
-
codeExpiresAt: undefined,
|
|
105
|
-
});
|
|
106
|
-
return { status: "ready" as const, generation, origin: attempt.origin };
|
|
107
|
-
},
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
/** Stores the digest of the code just generated, if no newer code replaced it meanwhile. */
|
|
111
|
-
export const storeCode = internalMutation({
|
|
112
|
-
args: { key: v.string(), bindingHash: v.string(), generation: v.number(), digest: v.string(), expiresAt: v.number() },
|
|
113
|
-
handler: async (ctx, { key, bindingHash, generation, digest, expiresAt }) => {
|
|
114
|
-
const attempt = await liveAttempt(ctx, key, bindingHash);
|
|
115
|
-
if (attempt === null || attempt.codeGeneration !== generation || attempt.verifiedEmail !== undefined) return false;
|
|
116
|
-
await ctx.db.patch(attempt._id, { codeDigest: digest, codeExpiresAt: expiresAt });
|
|
117
|
-
return true;
|
|
118
|
-
},
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
/** The address and send a typed code must match, for computing its digest. */
|
|
122
|
-
export const codeContext = internalMutation({
|
|
123
|
-
args: { key: v.string(), bindingHash: v.string() },
|
|
124
|
-
handler: async (ctx, { key, bindingHash }) => {
|
|
125
|
-
const attempt = await liveAttempt(ctx, key, bindingHash);
|
|
126
|
-
if (attempt === null || attempt.email === undefined || attempt.profile !== undefined) return null;
|
|
127
|
-
return { email: attempt.email, generation: attempt.codeGeneration ?? 0, origin: attempt.origin };
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Checks a typed code, once per guess. A wrong code uses up one of the
|
|
133
|
-
* attempt's guesses; when they run out, the attempt is locked. A right code
|
|
134
|
-
* verifies the address: a known reviewer goes on to allow the site, and a new
|
|
135
|
-
* one chooses a display name first.
|
|
136
|
-
*/
|
|
137
|
-
export const verifyCode = internalMutation({
|
|
138
|
-
args: { key: v.string(), bindingHash: v.string(), generation: v.number(), digest: v.string(), consentHash: v.string() },
|
|
139
|
-
handler: async (ctx, { key, bindingHash, generation, digest, consentHash }) => {
|
|
140
|
-
const attempt = await liveAttempt(ctx, key, bindingHash);
|
|
141
|
-
if (attempt === null || attempt.profile !== undefined || attempt.verifiedEmail !== undefined || attempt.email === undefined) {
|
|
142
|
-
return { status: "expired" as const };
|
|
143
|
-
}
|
|
144
|
-
const guessesLeft = attempt.guessesLeft ?? 0;
|
|
145
|
-
if (guessesLeft <= 0) return { status: "locked" as const };
|
|
146
|
-
const now = Date.now();
|
|
147
|
-
if (
|
|
148
|
-
attempt.codeDigest === undefined ||
|
|
149
|
-
attempt.codeGeneration !== generation ||
|
|
150
|
-
attempt.codeExpiresAt === undefined ||
|
|
151
|
-
attempt.codeExpiresAt <= now
|
|
152
|
-
) {
|
|
153
|
-
return { status: "code-expired" as const };
|
|
154
|
-
}
|
|
155
|
-
if (!sameDigest(attempt.codeDigest, digest)) {
|
|
156
|
-
await ctx.db.patch(attempt._id, { guessesLeft: guessesLeft - 1 });
|
|
157
|
-
return guessesLeft - 1 <= 0 ? { status: "locked" as const } : { status: "wrong" as const, guessesLeft: guessesLeft - 1 };
|
|
158
|
-
}
|
|
159
|
-
const email = attempt.email;
|
|
160
|
-
await ctx.db.patch(attempt._id, { codeDigest: undefined, codeExpiresAt: undefined, verifiedEmail: email });
|
|
161
|
-
const reviewer = await ctx.db.query("reviewers").withIndex("by_email", (q) => q.eq("email", email)).unique();
|
|
162
|
-
if (reviewer === null) return { status: "needs-name" as const, origin: attempt.origin };
|
|
163
|
-
return await verified(ctx, { ...attempt, verifiedEmail: email }, reviewer, consentHash);
|
|
164
|
-
},
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
/** Registers a reviewer whose address was just verified, with the display name they chose. */
|
|
168
|
-
export const registerReviewer = internalMutation({
|
|
169
|
-
args: { key: v.string(), bindingHash: v.string(), name: v.string(), consentHash: v.string() },
|
|
170
|
-
handler: async (ctx, { key, bindingHash, name, consentHash }) => {
|
|
171
|
-
const attempt = await liveAttempt(ctx, key, bindingHash);
|
|
172
|
-
if (attempt === null || attempt.profile !== undefined || attempt.verifiedEmail === undefined) {
|
|
173
|
-
return { status: "expired" as const };
|
|
174
|
-
}
|
|
175
|
-
const chosen = displayName(name);
|
|
176
|
-
if (chosen === null) return { status: "invalid-name" as const };
|
|
177
|
-
const email = attempt.verifiedEmail;
|
|
178
|
-
// Another window may have registered the same address meanwhile; its name stands.
|
|
179
|
-
let reviewer = await ctx.db.query("reviewers").withIndex("by_email", (q) => q.eq("email", email)).unique();
|
|
180
|
-
if (reviewer === null) {
|
|
181
|
-
const id = await ctx.db.insert("reviewers", { email, name: chosen, nameVersion: 1, createdAt: Date.now() });
|
|
182
|
-
reviewer = (await ctx.db.get(id))!;
|
|
183
|
-
}
|
|
184
|
-
return await verified(ctx, attempt, reviewer, consentHash);
|
|
185
|
-
},
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
/** Records a verified reviewer on an attempt, and whether they already allowed its site. */
|
|
189
|
-
async function verified(ctx: MutationCtx, attempt: Doc<"signInAttempts">, reviewer: Doc<"reviewers">, consentHash: string) {
|
|
190
|
-
const profile: ReviewerProfile = {
|
|
191
|
-
subject: reviewer._id,
|
|
192
|
-
name: reviewer.name,
|
|
193
|
-
email: reviewer.email,
|
|
194
|
-
pictureUrl: null,
|
|
195
|
-
profileVersion: reviewer.nameVersion,
|
|
196
|
-
};
|
|
197
|
-
// A device approval is always asked, never remembered like a site.
|
|
198
|
-
const device = attempt.device !== undefined;
|
|
199
|
-
const allowed = !device && (await grantFor(ctx, profile.subject, attempt.origin)) !== null;
|
|
200
|
-
await ctx.db.patch(attempt._id, { profile, consentHash });
|
|
201
|
-
return { status: "verified" as const, origin: attempt.origin, allowed, device, profile };
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Changes a reviewer's display name, for the reviewer whose session this is.
|
|
206
|
-
* Every deployment sees the new name from the next token.
|
|
207
|
-
*/
|
|
208
|
-
export const renameReviewer = internalMutation({
|
|
209
|
-
args: { refreshHash: v.string(), origin: v.string(), name: v.string() },
|
|
210
|
-
handler: async (ctx, { refreshHash, origin, name }) => {
|
|
211
|
-
const session = await sessionFor(ctx, refreshHash);
|
|
212
|
-
if (session === null || session.origin !== origin || !sessionLive(session, Date.now())) return "signed-out" as const;
|
|
213
|
-
const chosen = displayName(name);
|
|
214
|
-
if (chosen === null) return "invalid" as const;
|
|
215
|
-
const reviewer = await reviewerOf(ctx, session.profile.subject);
|
|
216
|
-
if (reviewer === null) return "signed-out" as const;
|
|
217
|
-
await ctx.db.patch(reviewer._id, { name: chosen, nameVersion: reviewer.nameVersion + 1 });
|
|
218
|
-
return "renamed" as const;
|
|
219
|
-
},
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Finishes an attempt once. Allowing records the site grant and starts a
|
|
224
|
-
* session; declining ends the attempt without one. A consent token is needed
|
|
225
|
-
* unless the reviewer allowed the site before.
|
|
226
|
-
*/
|
|
227
|
-
export const finishAttempt = internalMutation({
|
|
228
|
-
args: {
|
|
229
|
-
key: v.string(),
|
|
230
|
-
bindingHash: v.string(),
|
|
231
|
-
consentHash: v.union(v.string(), v.null()),
|
|
232
|
-
allow: v.boolean(),
|
|
233
|
-
refreshHash: v.string(),
|
|
234
|
-
},
|
|
235
|
-
handler: async (ctx, { key, bindingHash, consentHash, allow, refreshHash }) => {
|
|
236
|
-
const attempt = await liveAttempt(ctx, key, bindingHash);
|
|
237
|
-
// A device approval never becomes a site session; it finishes in deviceSignIn.ts.
|
|
238
|
-
if (attempt?.profile === undefined || attempt.device !== undefined) return null;
|
|
239
|
-
const { origin, clientState, profile } = attempt;
|
|
240
|
-
const existing = await grantFor(ctx, profile.subject, origin);
|
|
241
|
-
if (consentHash === null ? existing === null : consentHash !== attempt.consentHash) return null;
|
|
242
|
-
await ctx.db.delete(attempt._id);
|
|
243
|
-
if (!allow) return { origin, clientState, profile: null };
|
|
244
|
-
|
|
245
|
-
const now = Date.now();
|
|
246
|
-
if (existing === null) await ctx.db.insert("siteGrants", { subject: profile.subject, origin, grantedAt: now });
|
|
247
|
-
await ctx.db.insert("sessions", {
|
|
248
|
-
refreshHash,
|
|
249
|
-
origin,
|
|
250
|
-
profile,
|
|
251
|
-
createdAt: now,
|
|
252
|
-
expiresAt: now + SESSION_DAYS * DAY_MS,
|
|
253
|
-
endsAt: now + SESSION_MAX_DAYS * DAY_MS,
|
|
254
|
-
});
|
|
255
|
-
return { origin, clientState, profile };
|
|
256
|
-
},
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
/** Keeps a session alive for the site that owns it. Sessions slide forward on use. */
|
|
260
|
-
export const refreshSession = internalMutation({
|
|
261
|
-
args: { refreshHash: v.string(), nextRefreshHash: v.string(), origin: v.string() },
|
|
262
|
-
handler: async (ctx, { refreshHash, nextRefreshHash, origin }) => {
|
|
263
|
-
const session = await sessionFor(ctx, refreshHash);
|
|
264
|
-
const now = Date.now();
|
|
265
|
-
if (session === null || session.origin !== origin) return null;
|
|
266
|
-
// Sessions slide forward with use, but never past their absolute end, so a
|
|
267
|
-
// leaked refresh token can't keep a session alive forever.
|
|
268
|
-
const endsAt = sessionEnd(session);
|
|
269
|
-
if (!sessionLive(session, now)) {
|
|
270
|
-
await ctx.db.delete(session._id);
|
|
271
|
-
return null;
|
|
272
|
-
}
|
|
273
|
-
// A session whose reviewer no longer exists ends rather than renewing a stale identity.
|
|
274
|
-
const reviewer = await reviewerOf(ctx, session.profile.subject);
|
|
275
|
-
if (reviewer === null) {
|
|
276
|
-
await ctx.db.delete(session._id);
|
|
277
|
-
return null;
|
|
278
|
-
}
|
|
279
|
-
await ctx.db.patch(session._id, {
|
|
280
|
-
refreshHash: nextRefreshHash,
|
|
281
|
-
expiresAt: Math.min(now + SESSION_DAYS * DAY_MS, endsAt),
|
|
282
|
-
});
|
|
283
|
-
// The current display name, so a rename reaches every deployment with the next token.
|
|
284
|
-
return { profile: { ...session.profile, name: reviewer.name, profileVersion: reviewer.nameVersion }, endsAt };
|
|
285
|
-
},
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
export const endSession = internalMutation({
|
|
289
|
-
args: { refreshHash: v.string(), origin: v.string() },
|
|
290
|
-
handler: async (ctx, { refreshHash, origin }) => {
|
|
291
|
-
const session = await sessionFor(ctx, refreshHash);
|
|
292
|
-
if (session !== null && session.origin === origin) await ctx.db.delete(session._id);
|
|
293
|
-
},
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
/** Removes abandoned attempts and expired sessions (scheduled in crons.ts). */
|
|
297
|
-
export const cleanUp = internalMutation({
|
|
298
|
-
args: {},
|
|
299
|
-
handler: async (ctx) => {
|
|
300
|
-
const now = Date.now();
|
|
301
|
-
const attempts = await ctx.db
|
|
302
|
-
.query("signInAttempts")
|
|
303
|
-
.withIndex("by_created", (q) => q.lt("createdAt", now - ATTEMPT_MINUTES * 60 * 1000))
|
|
304
|
-
.take(CLEANUP_BATCH);
|
|
305
|
-
for (const attempt of attempts) await ctx.db.delete(attempt._id);
|
|
306
|
-
const sessions = await ctx.db
|
|
307
|
-
.query("sessions")
|
|
308
|
-
.withIndex("by_expiry", (q) => q.lt("expiresAt", now))
|
|
309
|
-
.take(CLEANUP_BATCH);
|
|
310
|
-
for (const session of sessions) await ctx.db.delete(session._id);
|
|
311
|
-
const limits = await ctx.db.query("signInLimits").withIndex("by_expiry", q => q.lt("expiresAt", now)).take(CLEANUP_BATCH);
|
|
312
|
-
for (const limit of limits) await ctx.db.delete(limit._id);
|
|
313
|
-
const devices = await ctx.db.query("deviceGrants").withIndex("by_expiry", (q) => q.lt("expiresAt", now)).take(CLEANUP_BATCH);
|
|
314
|
-
for (const device of devices) await ctx.db.delete(device._id);
|
|
315
|
-
// Keep going in further transactions until the backlog is gone.
|
|
316
|
-
if ([attempts, sessions, limits, devices].some((batch) => batch.length === CLEANUP_BATCH)) {
|
|
317
|
-
await ctx.scheduler.runAfter(0, internal.signIn.cleanUp, {});
|
|
318
|
-
}
|
|
319
|
-
},
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
export async function liveAttempt(ctx: MutationCtx, key: string, bindingHash: string) {
|
|
323
|
-
const attempt = await ctx.db
|
|
324
|
-
.query("signInAttempts")
|
|
325
|
-
.withIndex("by_key", (q) => q.eq("key", key))
|
|
326
|
-
.unique();
|
|
327
|
-
if (attempt === null) return null;
|
|
328
|
-
if (attempt.createdAt < Date.now() - ATTEMPT_MINUTES * 60 * 1000) {
|
|
329
|
-
await ctx.db.delete(attempt._id);
|
|
330
|
-
return null;
|
|
331
|
-
}
|
|
332
|
-
return attempt.bindingHash === bindingHash ? attempt : null;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
async function grantFor(ctx: MutationCtx, subject: string, origin: string) {
|
|
336
|
-
return await ctx.db
|
|
337
|
-
.query("siteGrants")
|
|
338
|
-
.withIndex("by_subject_origin", (q) => q.eq("subject", subject).eq("origin", origin))
|
|
339
|
-
.unique();
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
async function reviewerOf(ctx: MutationCtx, subject: string) {
|
|
343
|
-
const id = ctx.db.normalizeId("reviewers", subject);
|
|
344
|
-
return id === null ? null : await ctx.db.get(id as Id<"reviewers">);
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/** When a session ends however often it is used; older sessions without a recorded end count from sign-in. */
|
|
348
|
-
function sessionEnd(session: Doc<"sessions">): number {
|
|
349
|
-
return session.endsAt ?? session.createdAt + SESSION_MAX_DAYS * DAY_MS;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/** Whether a session may still be used: within both its sliding and its absolute end. */
|
|
353
|
-
function sessionLive(session: Doc<"sessions">, now: number): boolean {
|
|
354
|
-
return session.expiresAt > now && sessionEnd(session) > now;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
async function sessionFor(ctx: MutationCtx, refreshHash: string) {
|
|
358
|
-
return await ctx.db
|
|
359
|
-
.query("sessions")
|
|
360
|
-
.withIndex("by_refresh_hash", (q) => q.eq("refreshHash", refreshHash))
|
|
361
|
-
.unique();
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/** The site an attempt signs in to, for the browser that started it. */
|
|
365
|
-
export const attemptOrigin = internalMutation({
|
|
366
|
-
args: { key: v.string(), bindingHash: v.string() },
|
|
367
|
-
handler: async (ctx, { key, bindingHash }) => (await liveAttempt(ctx, key, bindingHash))?.origin ?? null,
|
|
368
|
-
});
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { operations, registerTagsForBackfill } from "./lib/vendor/critiqs_server.js";
|
|
2
|
-
import { v } from "convex/values";
|
|
3
|
-
import { internal } from "./_generated/api";
|
|
4
|
-
import type { Id } from "./_generated/dataModel";
|
|
5
|
-
import { internalMutation, mutation, query } from "./_generated/server";
|
|
6
|
-
import { convexStore, identityOf, run } from "./lib/store";
|
|
7
|
-
|
|
8
|
-
// Tags and shared tag groups. Anyone with project access reads them; only the
|
|
9
|
-
// project's owner organizes groups.
|
|
10
|
-
|
|
11
|
-
/** The project's tags for suggestions and filters, and its tag groups. */
|
|
12
|
-
export const list = query({
|
|
13
|
-
args: { projectId: v.id("projects") },
|
|
14
|
-
handler: async (ctx, args) => run(async () => operations.listTags(convexStore(ctx), await identityOf(ctx), args)),
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
export const createGroup = mutation({
|
|
18
|
-
args: { projectId: v.id("projects"), name: v.string() },
|
|
19
|
-
handler: async (ctx, args) =>
|
|
20
|
-
run(async () => (await operations.createGroup(convexStore(ctx), await identityOf(ctx), args)) as Id<"tagGroups">),
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
export const renameGroup = mutation({
|
|
24
|
-
args: { projectId: v.id("projects"), groupId: v.string(), name: v.string() },
|
|
25
|
-
handler: async (ctx, args) => run(async () => operations.renameGroup(convexStore(ctx), await identityOf(ctx), args)),
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
export const deleteGroup = mutation({
|
|
29
|
-
args: { projectId: v.id("projects"), groupId: v.string() },
|
|
30
|
-
handler: async (ctx, args) => run(async () => operations.deleteGroup(convexStore(ctx), await identityOf(ctx), args)),
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
export const mergeGroups = mutation({
|
|
34
|
-
args: { projectId: v.id("projects"), groupId: v.string(), intoGroupId: v.string() },
|
|
35
|
-
handler: async (ctx, args) => run(async () => operations.mergeGroups(convexStore(ctx), await identityOf(ctx), args)),
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
/** Sets a tag's groups, or hands it back to automatic grouping. */
|
|
39
|
-
export const setTagGroups = mutation({
|
|
40
|
-
args: { projectId: v.id("projects"), tag: v.string(), groupIds: v.array(v.string()), automatic: v.optional(v.boolean()) },
|
|
41
|
-
handler: async (ctx, args) => run(async () => operations.setTagGroups(convexStore(ctx), await identityOf(ctx), args)),
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Derives tags for feedback from before tags were derived: reads hashtags from
|
|
46
|
-
* each older message (giving it its project, for search), then records every
|
|
47
|
-
* thread's tags, explicit and written, in the registry and thread-tag
|
|
48
|
-
* records. A page per run; it schedules the next. Safe to run again. Run once
|
|
49
|
-
* after deploying with `npx convex run tags:backfill`.
|
|
50
|
-
*/
|
|
51
|
-
export const backfill = internalMutation({
|
|
52
|
-
args: { phase: v.optional(v.union(v.literal("messages"), v.literal("threads"))), cursor: v.optional(v.union(v.string(), v.null())) },
|
|
53
|
-
handler: async (ctx, { phase = "messages", cursor = null }) => {
|
|
54
|
-
const store = convexStore(ctx);
|
|
55
|
-
const table = phase === "messages" ? "messages" : "threads";
|
|
56
|
-
const result = await ctx.db.query(table).paginate({ numItems: 100, cursor });
|
|
57
|
-
for (const row of result.page) {
|
|
58
|
-
if (phase === "messages") {
|
|
59
|
-
const message = row as Awaited<ReturnType<typeof store.messages.get>> & {};
|
|
60
|
-
if (message.hashtags === undefined) await registerTagsForBackfill.message(store, message);
|
|
61
|
-
} else {
|
|
62
|
-
await registerTagsForBackfill.thread(store, row._id);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
if (!result.isDone) await ctx.scheduler.runAfter(0, internal.tags.backfill, { phase, cursor: result.continueCursor });
|
|
66
|
-
else if (phase === "messages") await ctx.scheduler.runAfter(0, internal.tags.backfill, { phase: "threads", cursor: null });
|
|
67
|
-
},
|
|
68
|
-
});
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { operations } from "./lib/vendor/critiqs_server.js";
|
|
2
|
-
import { paginationOptsValidator } from "convex/server";
|
|
3
|
-
import { v } from "convex/values";
|
|
4
|
-
import type { Id } from "./_generated/dataModel";
|
|
5
|
-
import { mutation, query } from "./_generated/server";
|
|
6
|
-
import { convexStore, identityOf, run } from "./lib/store";
|
|
7
|
-
import { anchorValidator, captureValidator, captureFileValidator, locationValidator, revisionValidator } from "./lib/validators";
|
|
8
|
-
import { checkAttachment, markAttached } from "./captures";
|
|
9
|
-
|
|
10
|
-
/** A mention: who, and the name the text shows after `@`. */
|
|
11
|
-
const mentionValidator = v.object({ userId: v.id("users"), label: v.string() });
|
|
12
|
-
|
|
13
|
-
// Thread functions: Convex validates argument shapes; @critiqs/server applies
|
|
14
|
-
// Critiqs's rules in the same transaction.
|
|
15
|
-
|
|
16
|
-
/** Starts a thread with its first message. The author has read their own thread. */
|
|
17
|
-
export const create = mutation({
|
|
18
|
-
args: {
|
|
19
|
-
projectId: v.id("projects"),
|
|
20
|
-
anchor: anchorValidator,
|
|
21
|
-
body: v.string(),
|
|
22
|
-
tags: v.array(v.string()),
|
|
23
|
-
capture: captureValidator,
|
|
24
|
-
mentions: v.optional(v.array(mentionValidator)),
|
|
25
|
-
},
|
|
26
|
-
handler: async (ctx, { capture, ...args }) =>
|
|
27
|
-
run(async () => {
|
|
28
|
-
const identity = await identityOf(ctx);
|
|
29
|
-
if (capture.status !== "failed") await checkAttachment(ctx, capture.storageId, args.projectId, identity);
|
|
30
|
-
const id = await operations.createThread(convexStore(ctx), identity, {
|
|
31
|
-
...args,
|
|
32
|
-
capture:
|
|
33
|
-
capture.status === "failed"
|
|
34
|
-
? capture
|
|
35
|
-
: capture.status === "captured"
|
|
36
|
-
? { status: "captured", fileId: capture.storageId }
|
|
37
|
-
: { status: "supplied", fileId: capture.storageId, failureReason: capture.failureReason },
|
|
38
|
-
});
|
|
39
|
-
if (capture.status !== "failed") await markAttached(ctx, capture.storageId);
|
|
40
|
-
return id as Id<"threads">;
|
|
41
|
-
}),
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
/** Threads the reviewer may see, with their personal unread state. Filtering and grouping happen in @critiqs/core. */
|
|
45
|
-
export const list = query({
|
|
46
|
-
args: { projectId: v.id("projects"), paginationOpts: paginationOptsValidator },
|
|
47
|
-
handler: async (ctx, { paginationOpts, ...args }) =>
|
|
48
|
-
run(async () => operations.listThreads(convexStore(ctx), await identityOf(ctx), args, paginationOpts)),
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
/** A bounded discussion page. Metadata/read state accompany every page. */
|
|
52
|
-
export const get = query({
|
|
53
|
-
args: { threadId: v.id("threads"), paginationOpts: paginationOptsValidator },
|
|
54
|
-
handler: async (ctx, { paginationOpts, ...args }) =>
|
|
55
|
-
run(async () => operations.getThread(convexStore(ctx), await identityOf(ctx), args, paginationOpts)),
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
/** Reactions paginate independently; a busy discussion cannot overflow a query. */
|
|
59
|
-
export const listReactions = query({
|
|
60
|
-
args: { threadId: v.id("threads"), paginationOpts: paginationOptsValidator },
|
|
61
|
-
handler: async (ctx, { paginationOpts, ...args }) =>
|
|
62
|
-
run(async () => operations.listReactions(convexStore(ctx), await identityOf(ctx), args, paginationOpts)),
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
/** Anyone with project access can resolve or reopen a thread (F3). */
|
|
66
|
-
export const setStatus = mutation({
|
|
67
|
-
args: { threadId: v.id("threads"), status: v.union(v.literal("open"), v.literal("resolved")) },
|
|
68
|
-
handler: async (ctx, args) => run(async () => operations.setStatus(convexStore(ctx), await identityOf(ctx), args)),
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
/** Soft delete: hides the thread and records who deleted it, keeping its history. */
|
|
72
|
-
export const remove = mutation({
|
|
73
|
-
args: { threadId: v.id("threads") },
|
|
74
|
-
handler: async (ctx, args) => run(async () => operations.removeThread(convexStore(ctx), await identityOf(ctx), args)),
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
export const setTags = mutation({
|
|
78
|
-
args: { threadId: v.id("threads"), tags: v.array(v.string()) },
|
|
79
|
-
handler: async (ctx, args) => run(async () => operations.setTags(convexStore(ctx), await identityOf(ctx), args)),
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Points a thread at a replacement target. The anchor keeps the original
|
|
84
|
-
* target, capture, and context as evidence (F8).
|
|
85
|
-
*/
|
|
86
|
-
export const reattach = mutation({
|
|
87
|
-
args: { threadId: v.id("threads"), location: locationValidator },
|
|
88
|
-
handler: async (ctx, args) => run(async () => operations.reattach(convexStore(ctx), await identityOf(ctx), args)),
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
/** One selected thread's export page; callers combine pages into full discussions. */
|
|
92
|
-
export const exportThreads = query({
|
|
93
|
-
args: {
|
|
94
|
-
projectId: v.id("projects"),
|
|
95
|
-
threadIds: v.array(v.id("threads")),
|
|
96
|
-
threadIndex: v.number(),
|
|
97
|
-
paginationOpts: paginationOptsValidator,
|
|
98
|
-
},
|
|
99
|
-
handler: async (ctx, { paginationOpts, ...args }) =>
|
|
100
|
-
run(async () => operations.exportThreads(convexStore(ctx), await identityOf(ctx), args, paginationOpts)),
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
/** Attaches a reviewer-supplied screenshot when automatic capture failed (F6). */
|
|
104
|
-
export const attachScreenshot = mutation({
|
|
105
|
-
args: { threadId: v.id("threads"), storageId: captureFileValidator },
|
|
106
|
-
handler: async (ctx, { threadId, storageId }) =>
|
|
107
|
-
run(async () => {
|
|
108
|
-
const identity = await identityOf(ctx);
|
|
109
|
-
await operations.requireUser(convexStore(ctx), identity);
|
|
110
|
-
const { thread } = await operations.requireThread(convexStore(ctx), threadId);
|
|
111
|
-
await checkAttachment(ctx, storageId, thread.projectId, identity);
|
|
112
|
-
await operations.attachScreenshot(convexStore(ctx), identity, { threadId, fileId: storageId });
|
|
113
|
-
await markAttached(ctx, storageId);
|
|
114
|
-
}),
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
export const reply = mutation({
|
|
118
|
-
args: { threadId: v.id("threads"), body: v.string(), revision: revisionValidator, mentions: v.optional(v.array(mentionValidator)) },
|
|
119
|
-
handler: async (ctx, args) =>
|
|
120
|
-
run(async () => (await operations.reply(convexStore(ctx), await identityOf(ctx), args)) as Id<"messages">),
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
/** Authors can edit only their own messages (F2). Edits do not make the thread unread. */
|
|
124
|
-
export const editMessage = mutation({
|
|
125
|
-
args: { messageId: v.id("messages"), body: v.string(), mentions: v.optional(v.array(mentionValidator)) },
|
|
126
|
-
handler: async (ctx, args) => run(async () => operations.editMessage(convexStore(ctx), await identityOf(ctx), args)),
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
export const toggleReaction = mutation({
|
|
130
|
-
args: { messageId: v.id("messages"), emoji: v.string() },
|
|
131
|
-
handler: async (ctx, args) => run(async () => operations.toggleReaction(convexStore(ctx), await identityOf(ctx), args)),
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Marks the thread read through the newest message the reviewer has seen.
|
|
136
|
-
* Clients pass that message's time so replies arriving unseen stay unread (F5).
|
|
137
|
-
*/
|
|
138
|
-
export const markRead = mutation({
|
|
139
|
-
args: { threadId: v.id("threads"), seenThrough: v.number() },
|
|
140
|
-
handler: async (ctx, args) => run(async () => operations.markRead(convexStore(ctx), await identityOf(ctx), args)),
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
export const markUnread = mutation({
|
|
144
|
-
args: { threadId: v.id("threads") },
|
|
145
|
-
handler: async (ctx, args) => run(async () => operations.markUnread(convexStore(ctx), await identityOf(ctx), args)),
|
|
146
|
-
});
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { operations } from "./lib/vendor/critiqs_server.js";
|
|
2
|
-
import { mutation } from "./_generated/server";
|
|
3
|
-
import { convexStore, identityOf, run } from "./lib/store";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Updates this deployment's record of the signed-in reviewer from their token,
|
|
7
|
-
* so a changed display name shows on their existing comments straight away.
|
|
8
|
-
* Returns the name now stored.
|
|
9
|
-
*/
|
|
10
|
-
export const sync = mutation({
|
|
11
|
-
args: {},
|
|
12
|
-
handler: async (ctx): Promise<string> => run(async () => operations.syncReviewer(convexStore(ctx), await identityOf(ctx))),
|
|
13
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// ../server/src/sqlite/driver.ts
|
|
2
|
-
function bunDriver(db) {
|
|
3
|
-
return {
|
|
4
|
-
exec: (sql) => void db.exec(sql),
|
|
5
|
-
run: (sql, params = []) => void db.query(sql).run(...params),
|
|
6
|
-
get: (sql, params = []) => db.query(sql).get(...params) ?? undefined,
|
|
7
|
-
all: (sql, params = []) => db.query(sql).all(...params)
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
function preparedDriver(db) {
|
|
11
|
-
return {
|
|
12
|
-
exec: (sql) => void db.exec(sql),
|
|
13
|
-
run: (sql, params = []) => void db.prepare(sql).run(...params),
|
|
14
|
-
get: (sql, params = []) => db.prepare(sql).get(...params) ?? undefined,
|
|
15
|
-
all: (sql, params = []) => db.prepare(sql).all(...params)
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
export {
|
|
19
|
-
bunDriver,
|
|
20
|
-
preparedDriver
|
|
21
|
-
};
|