@pleasenobugs/server 0.1.1 → 0.3.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 +6 -1
- package/dist/board.d.ts +2 -2
- package/dist/board.d.ts.map +1 -1
- package/dist/board.js +29 -3
- package/dist/board.js.map +1 -1
- package/dist/bootstrap.d.ts +3 -1
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +27 -4
- package/dist/bootstrap.js.map +1 -1
- package/dist/importer.d.ts +3 -0
- package/dist/importer.d.ts.map +1 -1
- package/dist/importer.js +11 -5
- package/dist/importer.js.map +1 -1
- package/dist/limit.d.ts.map +1 -1
- package/dist/limit.js +6 -2
- package/dist/limit.js.map +1 -1
- package/dist/main.js +23 -7
- package/dist/main.js.map +1 -1
- package/dist/memory-store.d.ts +21 -3
- package/dist/memory-store.d.ts.map +1 -1
- package/dist/memory-store.js +84 -8
- package/dist/memory-store.js.map +1 -1
- package/dist/projects.d.ts +2 -0
- package/dist/projects.d.ts.map +1 -1
- package/dist/projects.js +12 -2
- package/dist/projects.js.map +1 -1
- package/dist/router.d.ts +6 -0
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +420 -13
- package/dist/router.js.map +1 -1
- package/dist/sqlite-store.d.ts +18 -3
- package/dist/sqlite-store.d.ts.map +1 -1
- package/dist/sqlite-store.js +193 -15
- package/dist/sqlite-store.js.map +1 -1
- package/dist/store.d.ts +66 -11
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +4 -1
- package/dist/store.js.map +1 -1
- package/package.json +2 -2
package/dist/router.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import cors from 'cors';
|
|
2
2
|
import express from 'express';
|
|
3
|
-
import { DASHBOARD_PROJECT, DAY_MS, NAME_MAX, POST_ID_MAX, cleanContext, decodeProof, isPostKind, isPostStatus, normalizeAddress, normalizeOrigin, } from '@pleasenobugs/core';
|
|
3
|
+
import { DASHBOARD_PROJECT, DAY_MS, NAME_MAX, POST_ID_MAX, SHOT_MAX_BYTES, UPDATE_POSTS_MAX, UPDATE_TEXT_MAX, UPDATE_TITLE_MAX, cleanContext, cleanFix, cleanCodeLink, cleanVersion, decodeProof, isClosed, isPostKind, isPostStatus, isRating, lastWord, latestReply, takesAnswers, takesShot, normalizeAddress, normalizeOrigin, } from '@pleasenobugs/core';
|
|
4
4
|
import { checkProof } from './auth.js';
|
|
5
5
|
import { publicProject, toVisible } from './board.js';
|
|
6
6
|
import { rateLimit } from './limit.js';
|
|
@@ -29,6 +29,10 @@ export function createRouter(options) {
|
|
|
29
29
|
// cannot borrow somebody else's. The origin rules that *do* matter (which
|
|
30
30
|
// sites may post on which board) are enforced on the proof itself.
|
|
31
31
|
router.use(cors({ origin: true, allowedHeaders: ['content-type', 'authorization'], maxAge: 600 }));
|
|
32
|
+
// A new post may carry a screenshot, base64 inside the JSON, so that one
|
|
33
|
+
// route reads more. Everything else stays small; the parser below skips a
|
|
34
|
+
// body that is already read.
|
|
35
|
+
router.post('/v1/projects/:pid/posts', express.json({ limit: SHOT_B64_MAX + 32_768 }));
|
|
32
36
|
router.use(express.json({ limit: '32kb' }));
|
|
33
37
|
/** Express 4 does not catch a rejected promise; this hands it to the error handler. */
|
|
34
38
|
const h = (fn) => (req, res, next) => {
|
|
@@ -116,18 +120,35 @@ export function createRouter(options) {
|
|
|
116
120
|
const owner = r.kind === 'wallet' && r.owner;
|
|
117
121
|
const rows = await store.posts(p.id, address);
|
|
118
122
|
// Hidden posts reach owners, marked, so hiding can be undone. Everybody
|
|
119
|
-
// else gets a board without them, their authors included.
|
|
120
|
-
|
|
123
|
+
// else gets a board without them, their authors included. A post waiting
|
|
124
|
+
// for approval reaches owners and its own author, who would otherwise
|
|
125
|
+
// think it had been lost.
|
|
126
|
+
const shown = owner ? rows : rows.filter((row) => !row.hidden && (!row.pending || row.author === address));
|
|
121
127
|
// One lookup for every author who let their name show, rather than one
|
|
122
128
|
// per post: a board is mostly a handful of people saying several things.
|
|
123
129
|
const named = [...new Set(shown.filter((row) => !row.anon).map((row) => row.author))];
|
|
124
130
|
const names = await store.names(p.id, named);
|
|
131
|
+
const updates = (await store.updates(p.id)).length;
|
|
125
132
|
res.json({
|
|
126
133
|
project: publicProject(p),
|
|
127
134
|
posts: shown.map((row) => toVisible(row, address, names.get(row.author), owner)),
|
|
128
135
|
...(r.kind === 'wallet' ? { me: await me(p, r.address, r.owner) } : {}),
|
|
136
|
+
...(updates ? { updates } : {}),
|
|
129
137
|
});
|
|
130
138
|
}));
|
|
139
|
+
router.get('/v1/projects/:pid/updates', h(async (req, res) => {
|
|
140
|
+
const p = await project(req, res);
|
|
141
|
+
if (!p)
|
|
142
|
+
return;
|
|
143
|
+
const r = await reader(req, p);
|
|
144
|
+
if (r.kind === 'refused')
|
|
145
|
+
return fail(res, r.status, r.error);
|
|
146
|
+
// Public like the board, so the posts an update names are only those
|
|
147
|
+
// anybody can see there: never a hidden one, nor one still waiting.
|
|
148
|
+
const visible = new Set((await store.posts(p.id, null)).filter((row) => !row.hidden && !row.pending).map((row) => row.id));
|
|
149
|
+
const updates = await store.updates(p.id);
|
|
150
|
+
res.json({ updates: updates.map((u) => ({ ...u, posts: u.posts.filter((id) => visible.has(id)) })) });
|
|
151
|
+
}));
|
|
131
152
|
router.get('/v1/projects/:pid/me', h(async (req, res) => {
|
|
132
153
|
const p = await project(req, res);
|
|
133
154
|
if (!p)
|
|
@@ -144,6 +165,36 @@ export function createRouter(options) {
|
|
|
144
165
|
if (w)
|
|
145
166
|
res.json({ answers: await store.answersFor(p.id, w.address, ANSWERS_MAX) });
|
|
146
167
|
}));
|
|
168
|
+
router.get('/v1/projects/:pid/posts/:id/shot', h(async (req, res) => {
|
|
169
|
+
const p = await project(req, res);
|
|
170
|
+
if (!p)
|
|
171
|
+
return;
|
|
172
|
+
const r = await reader(req, p);
|
|
173
|
+
if (r.kind === 'refused')
|
|
174
|
+
return fail(res, r.status, r.error);
|
|
175
|
+
const id = String(req.params.id);
|
|
176
|
+
const [post, shot] = await Promise.all([store.getPost(p.id, id), store.getShot(p.id, id)]);
|
|
177
|
+
// On the board it is anybody's, like the post it belongs to, and needs
|
|
178
|
+
// no proof, so the widget can show it as a plain image. Anything else,
|
|
179
|
+
// a screenshot its author kept for the owners, one an owner took off,
|
|
180
|
+
// or one under a hidden post, is the owners' alone. Everybody else hears
|
|
181
|
+
// there is nothing here, rather than that there is something they may
|
|
182
|
+
// not see.
|
|
183
|
+
const owner = r.kind === 'wallet' && r.owner;
|
|
184
|
+
if (!post || !shot || (!owner && (shot.for !== 'board' || post.hidden || post.pending))) {
|
|
185
|
+
return fail(res, 404, 'no_post');
|
|
186
|
+
}
|
|
187
|
+
res.set({
|
|
188
|
+
'content-type': shot.type,
|
|
189
|
+
// Never kept, by a browser or anything in between, so taking one off
|
|
190
|
+
// the board takes it off for the next person who looks.
|
|
191
|
+
'cache-control': 'no-store',
|
|
192
|
+
'x-content-type-options': 'nosniff',
|
|
193
|
+
'x-robots-tag': 'noindex',
|
|
194
|
+
'content-security-policy': "default-src 'none'",
|
|
195
|
+
});
|
|
196
|
+
res.send(Buffer.from(shot.data));
|
|
197
|
+
}));
|
|
147
198
|
// ---- Writing ----
|
|
148
199
|
router.put('/v1/projects/:pid/me', h(async (req, res) => {
|
|
149
200
|
const p = await project(req, res);
|
|
@@ -159,6 +210,9 @@ export function createRouter(options) {
|
|
|
159
210
|
await store.setName(p.id, w.address, name);
|
|
160
211
|
res.json({ ok: true, ...(name ? { name } : {}) });
|
|
161
212
|
}));
|
|
213
|
+
if (options.postsPerIpDay) {
|
|
214
|
+
router.post('/v1/projects/:pid/posts', rateLimit({ windowMs: DAY_MS, max: options.postsPerIpDay, name: 'post-day' }));
|
|
215
|
+
}
|
|
162
216
|
router.post('/v1/projects/:pid/posts', rateLimit({
|
|
163
217
|
windowMs: 15 * 60_000,
|
|
164
218
|
max: 10,
|
|
@@ -176,6 +230,11 @@ export function createRouter(options) {
|
|
|
176
230
|
const body = (req.body ?? {});
|
|
177
231
|
const text = typeof body.text === 'string' ? body.text.trim() : '';
|
|
178
232
|
const context = body.context === undefined ? {} : cleanContext(body.context);
|
|
233
|
+
const shotRule = p.rules.screenshots ?? 'board';
|
|
234
|
+
const sent = body.shot === undefined ? undefined : readShot(body.shot);
|
|
235
|
+
// What the board allows beats what the widget asked: none at all, or
|
|
236
|
+
// the owners' alone whatever the author ticked.
|
|
237
|
+
const shot = sent && shotRule === 'owners' ? { ...sent, board: false } : sent;
|
|
179
238
|
if (typeof body.id !== 'string' ||
|
|
180
239
|
!body.id ||
|
|
181
240
|
body.id.length > POST_ID_MAX ||
|
|
@@ -183,10 +242,21 @@ export function createRouter(options) {
|
|
|
183
242
|
!p.rules.kinds.includes(body.kind) ||
|
|
184
243
|
!text ||
|
|
185
244
|
text.length > p.rules.textMax ||
|
|
186
|
-
context === null
|
|
245
|
+
context === null ||
|
|
246
|
+
// Stars on a review and on nothing else.
|
|
247
|
+
(body.kind === 'review' ? !isRating(body.rating) : body.rating !== undefined) ||
|
|
248
|
+
shot === null ||
|
|
249
|
+
(shot && (!takesShot(body.kind) || shotRule === 'off'))) {
|
|
187
250
|
return fail(res, 400, 'bad_request');
|
|
188
251
|
}
|
|
252
|
+
// One review per wallet, so the average counts people, not keystrokes.
|
|
253
|
+
// Changing your mind is throwing yours away and writing a new one.
|
|
254
|
+
if (body.kind === 'review' && (await store.hasPostOfKind(p.id, w.address, 'review'))) {
|
|
255
|
+
return fail(res, 409, 'reviewed');
|
|
256
|
+
}
|
|
189
257
|
const at = now();
|
|
258
|
+
// An owner's own post needs nobody's approval.
|
|
259
|
+
const pending = !!p.rules.approve && !w.owner;
|
|
190
260
|
const used = await store.countByAuthorSince(p.id, w.address, at - DAY_MS);
|
|
191
261
|
if (used >= p.rules.perDay) {
|
|
192
262
|
console.warn(`[post] ${p.id} too_many ${tag(w.address)} today=${used}`);
|
|
@@ -205,11 +275,14 @@ export function createRouter(options) {
|
|
|
205
275
|
at,
|
|
206
276
|
status: 'open',
|
|
207
277
|
...(Object.keys(context).length ? { context } : {}),
|
|
208
|
-
|
|
278
|
+
...(body.kind === 'review' ? { rating: body.rating } : {}),
|
|
279
|
+
...(pending ? { pending: true } : {}),
|
|
280
|
+
}, shot);
|
|
209
281
|
if (!created)
|
|
210
282
|
return fail(res, 409, 'duplicate');
|
|
211
|
-
|
|
212
|
-
|
|
283
|
+
const with_ = shot ? ` with a ${shot.data.length} byte screenshot` : '';
|
|
284
|
+
console.info(`[post] ${p.id} ${tag(w.address)} posted ${body.kind} ${body.id}${with_}${pending ? ', waiting' : ''}`);
|
|
285
|
+
res.json({ ok: true, left: Math.max(0, p.rules.perDay - used - 1), ...(pending ? { pending: true } : {}) });
|
|
213
286
|
}));
|
|
214
287
|
router.post('/v1/projects/:pid/posts/:id/vote', h(async (req, res) => {
|
|
215
288
|
const p = await project(req, res);
|
|
@@ -219,8 +292,9 @@ export function createRouter(options) {
|
|
|
219
292
|
if (!w)
|
|
220
293
|
return;
|
|
221
294
|
const post = await store.getPost(p.id, String(req.params.id));
|
|
222
|
-
if (!post || (post.hidden &&
|
|
295
|
+
if (!post || (!w.owner && (post.hidden || (post.pending && post.author !== w.address)))) {
|
|
223
296
|
return fail(res, 404, 'no_post');
|
|
297
|
+
}
|
|
224
298
|
// Stated rather than toggled, so a retry lands where the tap meant it to.
|
|
225
299
|
const up = (req.body ?? {}).up !== false;
|
|
226
300
|
const votes = await store.vote(p.id, post.id, w.address, up);
|
|
@@ -242,6 +316,157 @@ export function createRouter(options) {
|
|
|
242
316
|
console.info(`[post] ${p.id} ${tag(w.address)} deleted ${post.id}`);
|
|
243
317
|
res.json({ ok: true });
|
|
244
318
|
}));
|
|
319
|
+
// ---- The conversation under a post ----
|
|
320
|
+
//
|
|
321
|
+
// Two sides, decided by who signed: an owner speaks for the app, the
|
|
322
|
+
// post's author for themselves, and nobody else speaks at all. The app may
|
|
323
|
+
// write whenever it likes; the author only right after the app, and only
|
|
324
|
+
// on a bug or an idea that is not closed (see `authorMayAnswer` in core).
|
|
325
|
+
// Each change is written only if the conversation is still what this
|
|
326
|
+
// request read, so two answers sent at once never both land.
|
|
327
|
+
/** The post in the URL if this wallet may see it, or a 404 already sent. */
|
|
328
|
+
async function readablePost(res, p, w, id) {
|
|
329
|
+
const post = await store.getPost(p.id, id);
|
|
330
|
+
if (!post || (!w.owner && (post.hidden || (post.pending && post.author !== w.address)))) {
|
|
331
|
+
fail(res, 404, 'no_post');
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
return post;
|
|
335
|
+
}
|
|
336
|
+
/** Which side of the conversation this wallet is on, if any. An owner's own post is still the app's. */
|
|
337
|
+
const side = (post, w) => w.owner ? 'app' : post.author === w.address ? 'author' : null;
|
|
338
|
+
/** A message's text from a request body, or null when it is not one. */
|
|
339
|
+
const messageText = (v, max) => {
|
|
340
|
+
const text = typeof v === 'string' ? v.trim() : '';
|
|
341
|
+
return text && text.length <= max ? text : null;
|
|
342
|
+
};
|
|
343
|
+
router.post('/v1/projects/:pid/posts/:id/messages', rateLimit({
|
|
344
|
+
windowMs: 15 * 60_000,
|
|
345
|
+
max: 30,
|
|
346
|
+
name: 'message',
|
|
347
|
+
by: (req) => decodeProof(req.header('authorization'))?.address,
|
|
348
|
+
}), h(async (req, res) => {
|
|
349
|
+
const p = await project(req, res);
|
|
350
|
+
if (!p)
|
|
351
|
+
return;
|
|
352
|
+
const w = await wallet(req, res, p);
|
|
353
|
+
if (!w)
|
|
354
|
+
return;
|
|
355
|
+
const post = await readablePost(res, p, w, String(req.params.id));
|
|
356
|
+
if (!post)
|
|
357
|
+
return;
|
|
358
|
+
const body = (req.body ?? {});
|
|
359
|
+
const text = messageText(body.text, p.rules.replyMax);
|
|
360
|
+
if (typeof body.id !== 'string' || !body.id || body.id.length > POST_ID_MAX || text === null) {
|
|
361
|
+
return fail(res, 400, 'bad_request');
|
|
362
|
+
}
|
|
363
|
+
const from = side(post, w);
|
|
364
|
+
if (!from)
|
|
365
|
+
return fail(res, 403, 'not_yours');
|
|
366
|
+
const prev = post.thread ?? [];
|
|
367
|
+
if (prev.some((m) => m.id === body.id))
|
|
368
|
+
return fail(res, 409, 'duplicate');
|
|
369
|
+
if (from === 'author') {
|
|
370
|
+
if (!takesAnswers(post.kind) || isClosed(post.status))
|
|
371
|
+
return fail(res, 409, 'closed');
|
|
372
|
+
if (lastWord(prev)?.from !== 'app')
|
|
373
|
+
return fail(res, 409, 'not_your_turn');
|
|
374
|
+
}
|
|
375
|
+
else if (post.kind === 'review' && prev.some((m) => m.from === 'app' && !m.hidden)) {
|
|
376
|
+
// A review gets one answer from the app, which can be changed.
|
|
377
|
+
return fail(res, 409, 'not_your_turn');
|
|
378
|
+
}
|
|
379
|
+
const message = { id: body.id, from, text, at: now() };
|
|
380
|
+
if (!(await store.setThread(p.id, post.id, [...prev, message], prev)))
|
|
381
|
+
return fail(res, 409, 'not_your_turn');
|
|
382
|
+
// The app writing is an answer its author hears about; the author
|
|
383
|
+
// writing is not news to the author.
|
|
384
|
+
if (from === 'app')
|
|
385
|
+
await store.updatePost(p.id, post.id, { answeredAt: message.at });
|
|
386
|
+
console.info(`[message] ${p.id} ${from === 'app' ? 'owner' : tag(w.address)} wrote under ${post.id}`);
|
|
387
|
+
res.json({ ok: true, message });
|
|
388
|
+
}));
|
|
389
|
+
router.patch('/v1/projects/:pid/posts/:id/messages/:mid', h(async (req, res) => {
|
|
390
|
+
const p = await project(req, res);
|
|
391
|
+
if (!p)
|
|
392
|
+
return;
|
|
393
|
+
const w = await wallet(req, res, p);
|
|
394
|
+
if (!w)
|
|
395
|
+
return;
|
|
396
|
+
const post = await readablePost(res, p, w, String(req.params.id));
|
|
397
|
+
if (!post)
|
|
398
|
+
return;
|
|
399
|
+
const prev = post.thread ?? [];
|
|
400
|
+
const message = prev.find((m) => m.id === String(req.params.mid));
|
|
401
|
+
if (!message)
|
|
402
|
+
return fail(res, 404, 'not_found');
|
|
403
|
+
const { text: rawText, hidden } = (req.body ?? {});
|
|
404
|
+
if (rawText === undefined && hidden === undefined)
|
|
405
|
+
return fail(res, 400, 'bad_request');
|
|
406
|
+
let next = { ...message };
|
|
407
|
+
const at = now();
|
|
408
|
+
if (hidden !== undefined) {
|
|
409
|
+
// Owners take an author's words off the board, like a post. The
|
|
410
|
+
// app's own words they change or take back instead.
|
|
411
|
+
if (!w.owner)
|
|
412
|
+
return fail(res, 403, 'not_owner');
|
|
413
|
+
if (typeof hidden !== 'boolean' || message.from !== 'author')
|
|
414
|
+
return fail(res, 400, 'bad_request');
|
|
415
|
+
if (hidden)
|
|
416
|
+
next.hidden = true;
|
|
417
|
+
else
|
|
418
|
+
delete next.hidden;
|
|
419
|
+
}
|
|
420
|
+
if (rawText !== undefined) {
|
|
421
|
+
const text = messageText(rawText, p.rules.replyMax);
|
|
422
|
+
if (text === null)
|
|
423
|
+
return fail(res, 400, 'bad_request');
|
|
424
|
+
if (side(post, w) !== message.from)
|
|
425
|
+
return fail(res, 403, 'not_yours');
|
|
426
|
+
if (message.from === 'author' && isClosed(post.status))
|
|
427
|
+
return fail(res, 409, 'closed');
|
|
428
|
+
// Only while nobody answered it: changing what somebody already
|
|
429
|
+
// replied to would change what they replied to.
|
|
430
|
+
if (lastWord(prev)?.id !== message.id)
|
|
431
|
+
return fail(res, 409, 'not_your_turn');
|
|
432
|
+
if (text !== message.text)
|
|
433
|
+
next = { ...next, text, edited: at };
|
|
434
|
+
}
|
|
435
|
+
const thread = prev.map((m) => (m.id === message.id ? next : m));
|
|
436
|
+
if (!(await store.setThread(p.id, post.id, thread, prev)))
|
|
437
|
+
return fail(res, 409, 'not_your_turn');
|
|
438
|
+
if (message.from === 'app' && next.text !== message.text) {
|
|
439
|
+
await store.updatePost(p.id, post.id, { answeredAt: at });
|
|
440
|
+
}
|
|
441
|
+
res.json({ ok: true, message: next });
|
|
442
|
+
}));
|
|
443
|
+
router.delete('/v1/projects/:pid/posts/:id/messages/:mid', h(async (req, res) => {
|
|
444
|
+
const p = await project(req, res);
|
|
445
|
+
if (!p)
|
|
446
|
+
return;
|
|
447
|
+
const w = await wallet(req, res, p);
|
|
448
|
+
if (!w)
|
|
449
|
+
return;
|
|
450
|
+
const post = await readablePost(res, p, w, String(req.params.id));
|
|
451
|
+
if (!post)
|
|
452
|
+
return;
|
|
453
|
+
const prev = post.thread ?? [];
|
|
454
|
+
const message = prev.find((m) => m.id === String(req.params.mid));
|
|
455
|
+
if (!message)
|
|
456
|
+
return fail(res, 404, 'not_found');
|
|
457
|
+
// Your own last word, taken back before anybody answered it. An owner
|
|
458
|
+
// hides an author's message instead, so it can come back.
|
|
459
|
+
if (side(post, w) !== message.from)
|
|
460
|
+
return fail(res, 403, 'not_yours');
|
|
461
|
+
if (message.from === 'author' && isClosed(post.status))
|
|
462
|
+
return fail(res, 409, 'closed');
|
|
463
|
+
if (lastWord(prev)?.id !== message.id)
|
|
464
|
+
return fail(res, 409, 'not_your_turn');
|
|
465
|
+
const thread = prev.filter((m) => m.id !== message.id);
|
|
466
|
+
if (!(await store.setThread(p.id, post.id, thread, prev)))
|
|
467
|
+
return fail(res, 409, 'not_your_turn');
|
|
468
|
+
res.json({ ok: true });
|
|
469
|
+
}));
|
|
245
470
|
// ---- Owners ----
|
|
246
471
|
router.post('/v1/projects/:pid/posts/:id/manage', h(async (req, res) => {
|
|
247
472
|
const p = await project(req, res);
|
|
@@ -255,7 +480,7 @@ export function createRouter(options) {
|
|
|
255
480
|
const post = await store.getPost(p.id, String(req.params.id));
|
|
256
481
|
if (!post)
|
|
257
482
|
return fail(res, 404, 'no_post');
|
|
258
|
-
const { status, reply, hidden } = (req.body ?? {});
|
|
483
|
+
const { status, reply, hidden, shotOff, approved, fix } = (req.body ?? {});
|
|
259
484
|
const patch = {};
|
|
260
485
|
// Whether this call was an *answer*, which is what the author is told
|
|
261
486
|
// about. Hiding is not: telling somebody their post was taken down is
|
|
@@ -263,33 +488,177 @@ export function createRouter(options) {
|
|
|
263
488
|
// the same words twice.
|
|
264
489
|
let answered = false;
|
|
265
490
|
if (status !== undefined) {
|
|
266
|
-
|
|
491
|
+
// A review is not a request, so it never stands anywhere but open.
|
|
492
|
+
if (!isPostStatus(status) || (post.kind === 'review' && status !== 'open')) {
|
|
267
493
|
return fail(res, 400, 'bad_request');
|
|
494
|
+
}
|
|
268
495
|
if (status !== post.status)
|
|
269
496
|
answered = true;
|
|
270
497
|
patch.status = status;
|
|
271
498
|
}
|
|
499
|
+
// `reply` is how widgets from before conversations answer: one text,
|
|
500
|
+
// the app's latest word. Saving the same words again changes nothing,
|
|
501
|
+
// which matters because such a widget sends them with every save.
|
|
502
|
+
// Different words change the app's last message while nobody has
|
|
503
|
+
// answered it, or add one after the author's answer; empty takes the
|
|
504
|
+
// last one back. The messages routes below do the same, one at a time.
|
|
505
|
+
let thread = null;
|
|
272
506
|
if (reply !== undefined) {
|
|
273
507
|
if (typeof reply !== 'string' || reply.trim().length > p.rules.replyMax) {
|
|
274
508
|
return fail(res, 400, 'bad_request');
|
|
275
509
|
}
|
|
276
510
|
const said = reply.trim();
|
|
277
|
-
|
|
511
|
+
const prev = post.thread ?? [];
|
|
512
|
+
const last = lastWord(prev);
|
|
513
|
+
if (said !== (latestReply(prev)?.text ?? '')) {
|
|
514
|
+
const at = now();
|
|
515
|
+
let next = prev;
|
|
516
|
+
if (last?.from === 'app') {
|
|
517
|
+
next = said
|
|
518
|
+
? prev.map((m) => (m.id === last.id ? { ...m, text: said, edited: at } : m))
|
|
519
|
+
: prev.filter((m) => m.id !== last.id);
|
|
520
|
+
}
|
|
521
|
+
else if (said) {
|
|
522
|
+
next = [...prev, { id: `reply-${at}`, from: 'app', text: said, at }];
|
|
523
|
+
}
|
|
524
|
+
if (next !== prev) {
|
|
525
|
+
answered = true;
|
|
526
|
+
thread = { next, prev };
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
// Where the fix is: a version and a GitHub or GitLab link, on a bug or an
|
|
531
|
+
// idea that is done once this call is through, and on nothing else.
|
|
532
|
+
// Telling somebody "fixed in 1.4.2" is an answer like any other.
|
|
533
|
+
const done = (patch.status ?? post.status) === 'done';
|
|
534
|
+
if (fix !== undefined) {
|
|
535
|
+
const cleaned = fix === null ? {} : cleanFix(fix);
|
|
536
|
+
if (cleaned === null)
|
|
537
|
+
return fail(res, 400, 'bad_request');
|
|
538
|
+
const next = Object.keys(cleaned).length ? cleaned : null;
|
|
539
|
+
if (next && (post.kind === 'review' || !done))
|
|
540
|
+
return fail(res, 400, 'bad_request');
|
|
541
|
+
if (!sameFix(next, post.fix))
|
|
278
542
|
answered = true;
|
|
279
|
-
|
|
280
|
-
patch.reply = said ? { text: said, at: now() } : null;
|
|
543
|
+
patch.fix = next;
|
|
281
544
|
}
|
|
545
|
+
// Moved away from done: the fix goes with it, rather than lingering
|
|
546
|
+
// under a post that says it is planned again.
|
|
547
|
+
if (!done && post.fix)
|
|
548
|
+
patch.fix = null;
|
|
282
549
|
if (hidden !== undefined) {
|
|
283
550
|
if (typeof hidden !== 'boolean')
|
|
284
551
|
return fail(res, 400, 'bad_request');
|
|
285
552
|
patch.hidden = hidden;
|
|
286
553
|
}
|
|
554
|
+
// Letting a waiting post onto the board. Not an answer either: the
|
|
555
|
+
// author sees it arrive there, which says it.
|
|
556
|
+
if (approved !== undefined) {
|
|
557
|
+
if (typeof approved !== 'boolean')
|
|
558
|
+
return fail(res, 400, 'bad_request');
|
|
559
|
+
patch.pending = !approved;
|
|
560
|
+
}
|
|
561
|
+
// Taking a screenshot off the board leaves the post on it. Putting one
|
|
562
|
+
// back only works for a screenshot its author agreed to show; the store
|
|
563
|
+
// leaves any other with the owners.
|
|
564
|
+
if (shotOff !== undefined && typeof shotOff !== 'boolean')
|
|
565
|
+
return fail(res, 400, 'bad_request');
|
|
287
566
|
if (answered)
|
|
288
567
|
patch.answeredAt = now();
|
|
568
|
+
// First, since it is the one part that can find the conversation moved on.
|
|
569
|
+
if (thread && !(await store.setThread(p.id, post.id, thread.next, thread.prev))) {
|
|
570
|
+
return fail(res, 409, 'not_your_turn');
|
|
571
|
+
}
|
|
289
572
|
await store.updatePost(p.id, post.id, patch);
|
|
573
|
+
if (typeof shotOff === 'boolean')
|
|
574
|
+
await store.setShotOff(p.id, post.id, shotOff);
|
|
575
|
+
// A screenshot is there to get something fixed. Once it is, or will not
|
|
576
|
+
// be, nobody needs a picture of somebody's screen any more.
|
|
577
|
+
if (patch.status === 'done' || patch.status === 'declined')
|
|
578
|
+
await store.deleteShot(p.id, post.id);
|
|
290
579
|
console.info(`[post] ${p.id} owner managed ${post.id}`);
|
|
291
580
|
res.json({ ok: true });
|
|
292
581
|
}));
|
|
582
|
+
router.post('/v1/projects/:pid/updates', h(async (req, res) => {
|
|
583
|
+
const p = await project(req, res);
|
|
584
|
+
if (!p)
|
|
585
|
+
return;
|
|
586
|
+
const w = await wallet(req, res, p);
|
|
587
|
+
if (!w)
|
|
588
|
+
return;
|
|
589
|
+
if (!w.owner)
|
|
590
|
+
return fail(res, 403, 'not_owner');
|
|
591
|
+
const body = (req.body ?? {});
|
|
592
|
+
const title = typeof body.title === 'string' ? body.title.trim() : '';
|
|
593
|
+
const text = body.text === undefined ? '' : typeof body.text === 'string' ? body.text.trim() : null;
|
|
594
|
+
const version = body.version === undefined ? '' : cleanVersion(body.version);
|
|
595
|
+
const link = cleanCodeLink(body.link);
|
|
596
|
+
const asked = body.posts === undefined ? [] : body.posts;
|
|
597
|
+
if (typeof body.id !== 'string' ||
|
|
598
|
+
!body.id ||
|
|
599
|
+
body.id.length > POST_ID_MAX ||
|
|
600
|
+
!title ||
|
|
601
|
+
title.length > UPDATE_TITLE_MAX ||
|
|
602
|
+
text === null ||
|
|
603
|
+
text.length > UPDATE_TEXT_MAX ||
|
|
604
|
+
version === null ||
|
|
605
|
+
// Every update has somewhere to go: that is what tapping one does.
|
|
606
|
+
!link ||
|
|
607
|
+
!Array.isArray(asked) ||
|
|
608
|
+
asked.length > UPDATE_POSTS_MAX ||
|
|
609
|
+
!asked.every((id) => typeof id === 'string')) {
|
|
610
|
+
return fail(res, 400, 'bad_request');
|
|
611
|
+
}
|
|
612
|
+
// A retry of one already written: refused before any post is touched.
|
|
613
|
+
if ((await store.updates(p.id)).some((u) => u.id === body.id))
|
|
614
|
+
return fail(res, 409, 'duplicate');
|
|
615
|
+
// The bugs and ideas it fixed, of those on this board. Each goes to
|
|
616
|
+
// done with this update's version and link, which is the point of
|
|
617
|
+
// ticking it: one write, and every author hears their thing shipped.
|
|
618
|
+
const at = now();
|
|
619
|
+
const fix = { ...(version ? { version } : {}), link };
|
|
620
|
+
const fixed = [];
|
|
621
|
+
for (const id of new Set(asked)) {
|
|
622
|
+
const post = await store.getPost(p.id, id);
|
|
623
|
+
if (!post || post.kind === 'review')
|
|
624
|
+
continue;
|
|
625
|
+
fixed.push(post.id);
|
|
626
|
+
if (post.status === 'done' && sameFix(fix, post.fix))
|
|
627
|
+
continue;
|
|
628
|
+
await store.updatePost(p.id, post.id, { status: 'done', fix, answeredAt: at });
|
|
629
|
+
// Done, so nobody needs the picture of somebody's screen any more.
|
|
630
|
+
await store.deleteShot(p.id, post.id);
|
|
631
|
+
}
|
|
632
|
+
const update = {
|
|
633
|
+
id: body.id,
|
|
634
|
+
title,
|
|
635
|
+
...(text ? { text } : {}),
|
|
636
|
+
...(version ? { version } : {}),
|
|
637
|
+
link,
|
|
638
|
+
at,
|
|
639
|
+
posts: fixed,
|
|
640
|
+
};
|
|
641
|
+
if (!(await store.createUpdate(p.id, update)))
|
|
642
|
+
return fail(res, 409, 'duplicate');
|
|
643
|
+
console.info(`[update] ${p.id} owner wrote ${update.id}, fixing ${fixed.length}`);
|
|
644
|
+
res.json({ ok: true, update });
|
|
645
|
+
}));
|
|
646
|
+
router.delete('/v1/projects/:pid/updates/:id', h(async (req, res) => {
|
|
647
|
+
const p = await project(req, res);
|
|
648
|
+
if (!p)
|
|
649
|
+
return;
|
|
650
|
+
const w = await wallet(req, res, p);
|
|
651
|
+
if (!w)
|
|
652
|
+
return;
|
|
653
|
+
if (!w.owner)
|
|
654
|
+
return fail(res, 403, 'not_owner');
|
|
655
|
+
// The posts it fixed stay fixed: taking a page off the list does not
|
|
656
|
+
// un-ship anything.
|
|
657
|
+
if (!(await store.deleteUpdate(p.id, String(req.params.id))))
|
|
658
|
+
return fail(res, 404, 'no_update');
|
|
659
|
+
console.info(`[update] ${p.id} owner deleted ${String(req.params.id)}`);
|
|
660
|
+
res.json({ ok: true });
|
|
661
|
+
}));
|
|
293
662
|
// ---- Projects (dashboard) ----
|
|
294
663
|
router.post('/v1/projects', rateLimit({ windowMs: 60 * 60_000, max: 5, name: 'create-project' }), h(async (req, res) => {
|
|
295
664
|
const owner = await dashboard(req, res);
|
|
@@ -383,6 +752,44 @@ export function createRouter(options) {
|
|
|
383
752
|
}));
|
|
384
753
|
return router;
|
|
385
754
|
}
|
|
755
|
+
/** The longest base64 a screenshot of `SHOT_MAX_BYTES` can be. */
|
|
756
|
+
const SHOT_B64_MAX = Math.ceil(SHOT_MAX_BYTES / 3) * 4;
|
|
757
|
+
/**
|
|
758
|
+
* A screenshot from a request body, `{ type, data, board? }` with `data` in base64, or
|
|
759
|
+
* null when it is not one. The bytes have to start the way the type says, so
|
|
760
|
+
* nothing but an image is ever stored or served back.
|
|
761
|
+
*/
|
|
762
|
+
function readShot(v) {
|
|
763
|
+
if (!v || typeof v !== 'object')
|
|
764
|
+
return null;
|
|
765
|
+
const { type, data, board } = v;
|
|
766
|
+
// Only an explicit yes puts it on the board. A widget that never asked its
|
|
767
|
+
// user, an older one or a script, sends nothing and gets owners only.
|
|
768
|
+
if (board !== undefined && typeof board !== 'boolean')
|
|
769
|
+
return null;
|
|
770
|
+
const agreed = board === true;
|
|
771
|
+
if (typeof data !== 'string' || !data || data.length > SHOT_B64_MAX)
|
|
772
|
+
return null;
|
|
773
|
+
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(data))
|
|
774
|
+
return null;
|
|
775
|
+
const bytes = new Uint8Array(Buffer.from(data, 'base64'));
|
|
776
|
+
if (!bytes.length || bytes.length > SHOT_MAX_BYTES)
|
|
777
|
+
return null;
|
|
778
|
+
const starts = (...sig) => sig.every((b, i) => bytes[i] === b);
|
|
779
|
+
const ascii = (at, s) => [...s].every((c, i) => bytes[at + i] === c.charCodeAt(0));
|
|
780
|
+
if (type === 'image/jpeg' && starts(0xff, 0xd8, 0xff))
|
|
781
|
+
return { type, data: bytes, board: agreed };
|
|
782
|
+
if (type === 'image/png' && starts(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)) {
|
|
783
|
+
return { type, data: bytes, board: agreed };
|
|
784
|
+
}
|
|
785
|
+
if (type === 'image/webp' && ascii(0, 'RIFF') && ascii(8, 'WEBP'))
|
|
786
|
+
return { type, data: bytes, board: agreed };
|
|
787
|
+
return null;
|
|
788
|
+
}
|
|
789
|
+
/** Whether two fixes say the same, absent and empty being the same nothing. */
|
|
790
|
+
function sameFix(a, b) {
|
|
791
|
+
return (a?.version ?? '') === (b?.version ?? '') && (a?.link ?? '') === (b?.link ?? '');
|
|
792
|
+
}
|
|
386
793
|
/** A wallet, short enough for a log line and not enough to identify anyone at a glance. */
|
|
387
794
|
function tag(address) {
|
|
388
795
|
const a = normalizeAddress(address);
|