@pleasenobugs/server 0.2.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.
@@ -22,7 +22,7 @@ export declare function cleanName(v: unknown): string | null;
22
22
  * five minutes the worst ones.
23
23
  *
24
24
  * @example
25
- * parseOrigin('https://Shitti.me/board') // → 'https://shitti.me'
25
+ * parseOrigin('https://My.App/board') // → 'https://my.app'
26
26
  * parseOrigin('javascript:alert(1)') // → null
27
27
  */
28
28
  export declare function parseOrigin(v: unknown): string | null;
@@ -31,6 +31,10 @@ export declare function cleanOrigins(v: unknown): string[] | null;
31
31
  export declare function cleanOwners(v: unknown): string[] | null;
32
32
  export declare const MAX_LOGIN_MESSAGES = 3;
33
33
  export declare const LOGIN_MESSAGE_MAX = 200;
34
+ /** The fixed start a message ending in `*` needs, so it can only ever match the app's own sentence. */
35
+ export declare const LOGIN_PREFIX_MIN = 12;
36
+ /** How long a signed app login may be, its changing end included. */
37
+ export declare const SIGNED_LOGIN_MAX = 1000;
34
38
  /**
35
39
  * The app logins a project accepts, or null. An empty array is valid and
36
40
  * turns the feature off.
@@ -41,9 +45,17 @@ export declare const LOGIN_MESSAGE_MAX = 200;
41
45
  * a scoped, expiring proof can never be turned into a permanent one by listing
42
46
  * it here.
43
47
  *
48
+ * **A `*` at the end means "starts with"**: for an app whose login message
49
+ * carries something that changes every time (a date, a random code), the
50
+ * fixed part is listed and anything may follow. That fixed part has to be at
51
+ * least `LOGIN_PREFIX_MIN` characters, so a bare `*` or a "Sign in" can never
52
+ * accept every message a wallet ever signed elsewhere.
53
+ *
44
54
  * @example
45
- * cleanLoginMessages(['Sign in to Shitti']) // → ['Sign in to Shitti']
46
- * cleanLoginMessages(['PleaseNoBugs login…']) // → null
55
+ * cleanLoginMessages(['Sign in to My app']) // → ['Sign in to My app']
56
+ * cleanLoginMessages(['Sign in to My app*']) // → ['Sign in to My app*']
57
+ * cleanLoginMessages(['Sign in*']) // → null
58
+ * cleanLoginMessages(['PleaseNoBugs login…']) // → null
47
59
  */
48
60
  export declare function cleanLoginMessages(v: unknown): string[] | null;
49
61
  /**
@@ -63,4 +75,14 @@ export declare function newProject(args: {
63
75
  /** Rules that differ from the defaults, already checked (see {@link mergeRules}). */
64
76
  rules?: Partial<Rules>;
65
77
  }): Project;
78
+ /**
79
+ * Whether a signed app login is one of the messages a project lists: the same
80
+ * text, or for a listed message ending in `*`, the same start. Never one of
81
+ * our own login messages, whatever is listed.
82
+ *
83
+ * @example
84
+ * appLoginListed(['Sign in to My app*'], 'Sign in to My app
85
+ Nonce: 8f3a') // → true
86
+ */
87
+ export declare function appLoginListed(listed: readonly string[], message: string): boolean;
66
88
  //# sourceMappingURL=projects.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"projects.d.ts","sourceRoot":"","sources":["../src/projects.ts"],"names":[],"mappings":"AACA,OAAO,EAQL,KAAK,OAAO,EACZ,KAAK,KAAK,EACX,MAAM,oBAAoB,CAAC;AAE5B;;;;;;;GAOG;AAEH,eAAO,MAAM,gBAAgB,KAAK,CAAC;AACnC,eAAO,MAAM,WAAW,KAAK,CAAC;AAC9B,eAAO,MAAM,UAAU,KAAK,CAAC;AAS7B,8EAA8E;AAC9E,wBAAgB,YAAY,IAAI,MAAM,CAIrC;AAED,qDAAqD;AACrD,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAInD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAQrD;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,EAAE,GAAG,IAAI,CASxD;AAED,6DAA6D;AAC7D,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,EAAE,GAAG,IAAI,CASvD;AAED,eAAO,MAAM,kBAAkB,IAAI,CAAC;AACpC,eAAO,MAAM,iBAAiB,MAAM,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AAO,GAAG,MAAM,EAAE,GAAG,IAAI,CASvD;AAED,eAAO,MAAM,kBAAkB,IAAI,CAAC;AACpC,eAAO,MAAM,iBAAiB,MAAM,CAAC;AACrC,uGAAuG;AACvG,eAAO,MAAM,gBAAgB,KAAK,CAAC;AACnC,qEAAqE;AACrE,eAAO,MAAM,gBAAgB,OAAO,CAAC;AAErC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,EAAE,GAAG,IAAI,CAU9D;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,GAAG,KAAK,GAAG,IAAI,CAuCnE;AAED,+DAA+D;AAC/D,wBAAgB,UAAU,CAAC,IAAI,EAAE;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,qFAAqF;IACrF,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;CACxB,GAAG,OAAO,CAUV;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAGlF"}
package/dist/projects.js CHANGED
@@ -39,7 +39,7 @@ export function cleanName(v) {
39
39
  * five minutes the worst ones.
40
40
  *
41
41
  * @example
42
- * parseOrigin('https://Shitti.me/board') // → 'https://shitti.me'
42
+ * parseOrigin('https://My.App/board') // → 'https://my.app'
43
43
  * parseOrigin('javascript:alert(1)') // → null
44
44
  */
45
45
  export function parseOrigin(v) {
@@ -82,6 +82,10 @@ export function cleanOwners(v) {
82
82
  }
83
83
  export const MAX_LOGIN_MESSAGES = 3;
84
84
  export const LOGIN_MESSAGE_MAX = 200;
85
+ /** The fixed start a message ending in `*` needs, so it can only ever match the app's own sentence. */
86
+ export const LOGIN_PREFIX_MIN = 12;
87
+ /** How long a signed app login may be, its changing end included. */
88
+ export const SIGNED_LOGIN_MAX = 1000;
85
89
  /**
86
90
  * The app logins a project accepts, or null. An empty array is valid and
87
91
  * turns the feature off.
@@ -92,9 +96,17 @@ export const LOGIN_MESSAGE_MAX = 200;
92
96
  * a scoped, expiring proof can never be turned into a permanent one by listing
93
97
  * it here.
94
98
  *
99
+ * **A `*` at the end means "starts with"**: for an app whose login message
100
+ * carries something that changes every time (a date, a random code), the
101
+ * fixed part is listed and anything may follow. That fixed part has to be at
102
+ * least `LOGIN_PREFIX_MIN` characters, so a bare `*` or a "Sign in" can never
103
+ * accept every message a wallet ever signed elsewhere.
104
+ *
95
105
  * @example
96
- * cleanLoginMessages(['Sign in to Shitti']) // → ['Sign in to Shitti']
97
- * cleanLoginMessages(['PleaseNoBugs login…']) // → null
106
+ * cleanLoginMessages(['Sign in to My app']) // → ['Sign in to My app']
107
+ * cleanLoginMessages(['Sign in to My app*']) // → ['Sign in to My app*']
108
+ * cleanLoginMessages(['Sign in*']) // → null
109
+ * cleanLoginMessages(['PleaseNoBugs login…']) // → null
98
110
  */
99
111
  export function cleanLoginMessages(v) {
100
112
  if (!Array.isArray(v) || v.length > MAX_LOGIN_MESSAGES)
@@ -105,6 +117,8 @@ export function cleanLoginMessages(v) {
105
117
  return null;
106
118
  if (m.startsWith('PleaseNoBugs login'))
107
119
  return null;
120
+ if (m.endsWith('*') && m.slice(0, -1).trim().length < LOGIN_PREFIX_MIN)
121
+ return null;
108
122
  if (!out.includes(m))
109
123
  out.push(m);
110
124
  }
@@ -170,4 +184,18 @@ export function newProject(args) {
170
184
  createdAt: args.now ?? Date.now(),
171
185
  };
172
186
  }
187
+ /**
188
+ * Whether a signed app login is one of the messages a project lists: the same
189
+ * text, or for a listed message ending in `*`, the same start. Never one of
190
+ * our own login messages, whatever is listed.
191
+ *
192
+ * @example
193
+ * appLoginListed(['Sign in to My app*'], 'Sign in to My app
194
+ Nonce: 8f3a') // → true
195
+ */
196
+ export function appLoginListed(listed, message) {
197
+ if (message.length > SIGNED_LOGIN_MAX || message.startsWith('PleaseNoBugs login'))
198
+ return false;
199
+ return listed.some((m) => (m.endsWith('*') ? message.startsWith(m.slice(0, -1)) : m === message));
200
+ }
173
201
  //# sourceMappingURL=projects.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"projects.js","sourceRoot":"","sources":["../src/projects.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EACL,aAAa,EACb,UAAU,EACV,cAAc,EACd,UAAU,EACV,UAAU,EACV,gBAAgB,GAIjB,MAAM,oBAAoB,CAAC;AAE5B;;;;;;;GAOG;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AACnC,MAAM,CAAC,MAAM,WAAW,GAAG,EAAE,CAAC;AAC9B,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,CAAC;AAE7B;;;GAGG;AACH,MAAM,WAAW,GAAG,iCAAiC,CAAC;AACtD,MAAM,SAAS,GAAG,EAAE,CAAC;AAErB,8EAA8E;AAC9E,MAAM,UAAU,YAAY;IAC1B,IAAI,EAAE,GAAG,EAAE,CAAC;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE;QAAE,EAAE,IAAI,WAAW,CAAC,SAAS,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;IACrF,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,qDAAqD;AACrD,MAAM,UAAU,SAAS,CAAC,CAAU;IAClC,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IACvC,MAAM,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IACtB,OAAO,IA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E,MAAM,EAAE,CAAC;IAC5B,uCAAuC;IACvC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAKD;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CA0enE"}
1
+ {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,SAAS,CAAC;AA+C9B,OAAO,KAAK,EAA4B,KAAK,EAAE,MAAM,YAAY,CAAC;AAElE,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,KAAK,CAAC;IACb;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,uCAAuC;IACvC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAKD;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAqxBnE"}
package/dist/router.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import cors from 'cors';
2
2
  import express from 'express';
3
- import { DASHBOARD_PROJECT, DAY_MS, NAME_MAX, POST_ID_MAX, SHOT_MAX_BYTES, cleanContext, decodeProof, isPostKind, isPostStatus, isRating, takesShot, 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, identiconCode, 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
+ import { identiconSvg } from './identicon.js';
6
7
  import { rateLimit } from './limit.js';
7
8
  import { cleanLoginMessages, cleanName, cleanOrigins, cleanOwners, mergeRules, newProject, newProjectId, } from './projects.js';
8
9
  /** How many answers `GET /answers` hands back. More than anybody will have. */
@@ -97,7 +98,7 @@ export function createRouter(options) {
97
98
  /** What one wallet needs to know about itself on one board. */
98
99
  async function me(p, address, owner) {
99
100
  const used = await store.countByAuthorSince(p.id, address, now() - DAY_MS);
100
- const out = { owner, left: Math.max(0, p.rules.perDay - used) };
101
+ const out = { owner, left: Math.max(0, p.rules.perDay - used), face: identiconCode(address) };
101
102
  const name = (await store.names(p.id, [address])).get(address);
102
103
  if (name)
103
104
  out.name = name;
@@ -128,12 +129,27 @@ export function createRouter(options) {
128
129
  // per post: a board is mostly a handful of people saying several things.
129
130
  const named = [...new Set(shown.filter((row) => !row.anon).map((row) => row.author))];
130
131
  const names = await store.names(p.id, named);
132
+ const updates = (await store.updates(p.id)).length;
131
133
  res.json({
132
134
  project: publicProject(p),
133
135
  posts: shown.map((row) => toVisible(row, address, names.get(row.author), owner)),
134
136
  ...(r.kind === 'wallet' ? { me: await me(p, r.address, r.owner) } : {}),
137
+ ...(updates ? { updates } : {}),
135
138
  });
136
139
  }));
140
+ router.get('/v1/projects/:pid/updates', h(async (req, res) => {
141
+ const p = await project(req, res);
142
+ if (!p)
143
+ return;
144
+ const r = await reader(req, p);
145
+ if (r.kind === 'refused')
146
+ return fail(res, r.status, r.error);
147
+ // Public like the board, so the posts an update names are only those
148
+ // anybody can see there: never a hidden one, nor one still waiting.
149
+ const visible = new Set((await store.posts(p.id, null)).filter((row) => !row.hidden && !row.pending).map((row) => row.id));
150
+ const updates = await store.updates(p.id);
151
+ res.json({ updates: updates.map((u) => ({ ...u, posts: u.posts.filter((id) => visible.has(id)) })) });
152
+ }));
137
153
  router.get('/v1/projects/:pid/me', h(async (req, res) => {
138
154
  const p = await project(req, res);
139
155
  if (!p)
@@ -180,6 +196,21 @@ export function createRouter(options) {
180
196
  });
181
197
  res.send(Buffer.from(shot.data));
182
198
  }));
199
+ // A wallet's identicon, by the code a post carries in `face`. Belongs to no
200
+ // project and says nothing about anybody: the same twelve digits draw the
201
+ // same picture for everyone, forever, so the browser may keep it for good.
202
+ router.get('/v1/identicons/:code.svg', h(async (req, res) => {
203
+ const svg = await identiconSvg(String(req.params.code));
204
+ if (!svg)
205
+ return fail(res, 404, 'not_found');
206
+ res.set({
207
+ 'content-type': 'image/svg+xml',
208
+ 'cache-control': 'public, max-age=31536000, immutable',
209
+ 'x-content-type-options': 'nosniff',
210
+ 'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'",
211
+ });
212
+ res.send(svg);
213
+ }));
183
214
  // ---- Writing ----
184
215
  router.put('/v1/projects/:pid/me', h(async (req, res) => {
185
216
  const p = await project(req, res);
@@ -301,6 +332,157 @@ export function createRouter(options) {
301
332
  console.info(`[post] ${p.id} ${tag(w.address)} deleted ${post.id}`);
302
333
  res.json({ ok: true });
303
334
  }));
335
+ // ---- The conversation under a post ----
336
+ //
337
+ // Two sides, decided by who signed: an owner speaks for the app, the
338
+ // post's author for themselves, and nobody else speaks at all. The app may
339
+ // write whenever it likes; the author only right after the app, and only
340
+ // on a bug or an idea that is not closed (see `authorMayAnswer` in core).
341
+ // Each change is written only if the conversation is still what this
342
+ // request read, so two answers sent at once never both land.
343
+ /** The post in the URL if this wallet may see it, or a 404 already sent. */
344
+ async function readablePost(res, p, w, id) {
345
+ const post = await store.getPost(p.id, id);
346
+ if (!post || (!w.owner && (post.hidden || (post.pending && post.author !== w.address)))) {
347
+ fail(res, 404, 'no_post');
348
+ return null;
349
+ }
350
+ return post;
351
+ }
352
+ /** Which side of the conversation this wallet is on, if any. An owner's own post is still the app's. */
353
+ const side = (post, w) => w.owner ? 'app' : post.author === w.address ? 'author' : null;
354
+ /** A message's text from a request body, or null when it is not one. */
355
+ const messageText = (v, max) => {
356
+ const text = typeof v === 'string' ? v.trim() : '';
357
+ return text && text.length <= max ? text : null;
358
+ };
359
+ router.post('/v1/projects/:pid/posts/:id/messages', rateLimit({
360
+ windowMs: 15 * 60_000,
361
+ max: 30,
362
+ name: 'message',
363
+ by: (req) => decodeProof(req.header('authorization'))?.address,
364
+ }), h(async (req, res) => {
365
+ const p = await project(req, res);
366
+ if (!p)
367
+ return;
368
+ const w = await wallet(req, res, p);
369
+ if (!w)
370
+ return;
371
+ const post = await readablePost(res, p, w, String(req.params.id));
372
+ if (!post)
373
+ return;
374
+ const body = (req.body ?? {});
375
+ const text = messageText(body.text, p.rules.replyMax);
376
+ if (typeof body.id !== 'string' || !body.id || body.id.length > POST_ID_MAX || text === null) {
377
+ return fail(res, 400, 'bad_request');
378
+ }
379
+ const from = side(post, w);
380
+ if (!from)
381
+ return fail(res, 403, 'not_yours');
382
+ const prev = post.thread ?? [];
383
+ if (prev.some((m) => m.id === body.id))
384
+ return fail(res, 409, 'duplicate');
385
+ if (from === 'author') {
386
+ if (!takesAnswers(post.kind) || isClosed(post.status))
387
+ return fail(res, 409, 'closed');
388
+ if (lastWord(prev)?.from !== 'app')
389
+ return fail(res, 409, 'not_your_turn');
390
+ }
391
+ else if (post.kind === 'review' && prev.some((m) => m.from === 'app' && !m.hidden)) {
392
+ // A review gets one answer from the app, which can be changed.
393
+ return fail(res, 409, 'not_your_turn');
394
+ }
395
+ const message = { id: body.id, from, text, at: now() };
396
+ if (!(await store.setThread(p.id, post.id, [...prev, message], prev)))
397
+ return fail(res, 409, 'not_your_turn');
398
+ // The app writing is an answer its author hears about; the author
399
+ // writing is not news to the author.
400
+ if (from === 'app')
401
+ await store.updatePost(p.id, post.id, { answeredAt: message.at });
402
+ console.info(`[message] ${p.id} ${from === 'app' ? 'owner' : tag(w.address)} wrote under ${post.id}`);
403
+ res.json({ ok: true, message });
404
+ }));
405
+ router.patch('/v1/projects/:pid/posts/:id/messages/:mid', h(async (req, res) => {
406
+ const p = await project(req, res);
407
+ if (!p)
408
+ return;
409
+ const w = await wallet(req, res, p);
410
+ if (!w)
411
+ return;
412
+ const post = await readablePost(res, p, w, String(req.params.id));
413
+ if (!post)
414
+ return;
415
+ const prev = post.thread ?? [];
416
+ const message = prev.find((m) => m.id === String(req.params.mid));
417
+ if (!message)
418
+ return fail(res, 404, 'not_found');
419
+ const { text: rawText, hidden } = (req.body ?? {});
420
+ if (rawText === undefined && hidden === undefined)
421
+ return fail(res, 400, 'bad_request');
422
+ let next = { ...message };
423
+ const at = now();
424
+ if (hidden !== undefined) {
425
+ // Owners take an author's words off the board, like a post. The
426
+ // app's own words they change or take back instead.
427
+ if (!w.owner)
428
+ return fail(res, 403, 'not_owner');
429
+ if (typeof hidden !== 'boolean' || message.from !== 'author')
430
+ return fail(res, 400, 'bad_request');
431
+ if (hidden)
432
+ next.hidden = true;
433
+ else
434
+ delete next.hidden;
435
+ }
436
+ if (rawText !== undefined) {
437
+ const text = messageText(rawText, p.rules.replyMax);
438
+ if (text === null)
439
+ return fail(res, 400, 'bad_request');
440
+ if (side(post, w) !== message.from)
441
+ return fail(res, 403, 'not_yours');
442
+ if (message.from === 'author' && isClosed(post.status))
443
+ return fail(res, 409, 'closed');
444
+ // Only while nobody answered it: changing what somebody already
445
+ // replied to would change what they replied to.
446
+ if (lastWord(prev)?.id !== message.id)
447
+ return fail(res, 409, 'not_your_turn');
448
+ if (text !== message.text)
449
+ next = { ...next, text, edited: at };
450
+ }
451
+ const thread = prev.map((m) => (m.id === message.id ? next : m));
452
+ if (!(await store.setThread(p.id, post.id, thread, prev)))
453
+ return fail(res, 409, 'not_your_turn');
454
+ if (message.from === 'app' && next.text !== message.text) {
455
+ await store.updatePost(p.id, post.id, { answeredAt: at });
456
+ }
457
+ res.json({ ok: true, message: next });
458
+ }));
459
+ router.delete('/v1/projects/:pid/posts/:id/messages/:mid', h(async (req, res) => {
460
+ const p = await project(req, res);
461
+ if (!p)
462
+ return;
463
+ const w = await wallet(req, res, p);
464
+ if (!w)
465
+ return;
466
+ const post = await readablePost(res, p, w, String(req.params.id));
467
+ if (!post)
468
+ return;
469
+ const prev = post.thread ?? [];
470
+ const message = prev.find((m) => m.id === String(req.params.mid));
471
+ if (!message)
472
+ return fail(res, 404, 'not_found');
473
+ // Your own last word, taken back before anybody answered it. An owner
474
+ // hides an author's message instead, so it can come back.
475
+ if (side(post, w) !== message.from)
476
+ return fail(res, 403, 'not_yours');
477
+ if (message.from === 'author' && isClosed(post.status))
478
+ return fail(res, 409, 'closed');
479
+ if (lastWord(prev)?.id !== message.id)
480
+ return fail(res, 409, 'not_your_turn');
481
+ const thread = prev.filter((m) => m.id !== message.id);
482
+ if (!(await store.setThread(p.id, post.id, thread, prev)))
483
+ return fail(res, 409, 'not_your_turn');
484
+ res.json({ ok: true });
485
+ }));
304
486
  // ---- Owners ----
305
487
  router.post('/v1/projects/:pid/posts/:id/manage', h(async (req, res) => {
306
488
  const p = await project(req, res);
@@ -314,7 +496,7 @@ export function createRouter(options) {
314
496
  const post = await store.getPost(p.id, String(req.params.id));
315
497
  if (!post)
316
498
  return fail(res, 404, 'no_post');
317
- const { status, reply, hidden, shotOff, approved } = (req.body ?? {});
499
+ const { status, reply, hidden, shotOff, approved, fix } = (req.body ?? {});
318
500
  const patch = {};
319
501
  // Whether this call was an *answer*, which is what the author is told
320
502
  // about. Hiding is not: telling somebody their post was taken down is
@@ -330,16 +512,56 @@ export function createRouter(options) {
330
512
  answered = true;
331
513
  patch.status = status;
332
514
  }
515
+ // `reply` is how widgets from before conversations answer: one text,
516
+ // the app's latest word. Saving the same words again changes nothing,
517
+ // which matters because such a widget sends them with every save.
518
+ // Different words change the app's last message while nobody has
519
+ // answered it, or add one after the author's answer; empty takes the
520
+ // last one back. The messages routes below do the same, one at a time.
521
+ let thread = null;
333
522
  if (reply !== undefined) {
334
523
  if (typeof reply !== 'string' || reply.trim().length > p.rules.replyMax) {
335
524
  return fail(res, 400, 'bad_request');
336
525
  }
337
526
  const said = reply.trim();
338
- if (said !== (post.reply?.text ?? ''))
527
+ const prev = post.thread ?? [];
528
+ const last = lastWord(prev);
529
+ if (said !== (latestReply(prev)?.text ?? '')) {
530
+ const at = now();
531
+ let next = prev;
532
+ if (last?.from === 'app') {
533
+ next = said
534
+ ? prev.map((m) => (m.id === last.id ? { ...m, text: said, edited: at } : m))
535
+ : prev.filter((m) => m.id !== last.id);
536
+ }
537
+ else if (said) {
538
+ next = [...prev, { id: `reply-${at}`, from: 'app', text: said, at }];
539
+ }
540
+ if (next !== prev) {
541
+ answered = true;
542
+ thread = { next, prev };
543
+ }
544
+ }
545
+ }
546
+ // Where the fix is: a version and a GitHub or GitLab link, on a bug or an
547
+ // idea that is done once this call is through, and on nothing else.
548
+ // Telling somebody "fixed in 1.4.2" is an answer like any other.
549
+ const done = (patch.status ?? post.status) === 'done';
550
+ if (fix !== undefined) {
551
+ const cleaned = fix === null ? {} : cleanFix(fix);
552
+ if (cleaned === null)
553
+ return fail(res, 400, 'bad_request');
554
+ const next = Object.keys(cleaned).length ? cleaned : null;
555
+ if (next && (post.kind === 'review' || !done))
556
+ return fail(res, 400, 'bad_request');
557
+ if (!sameFix(next, post.fix))
339
558
  answered = true;
340
- // An empty reply is how one is taken back, rather than a route of its own.
341
- patch.reply = said ? { text: said, at: now() } : null;
559
+ patch.fix = next;
342
560
  }
561
+ // Moved away from done: the fix goes with it, rather than lingering
562
+ // under a post that says it is planned again.
563
+ if (!done && post.fix)
564
+ patch.fix = null;
343
565
  if (hidden !== undefined) {
344
566
  if (typeof hidden !== 'boolean')
345
567
  return fail(res, 400, 'bad_request');
@@ -359,6 +581,10 @@ export function createRouter(options) {
359
581
  return fail(res, 400, 'bad_request');
360
582
  if (answered)
361
583
  patch.answeredAt = now();
584
+ // First, since it is the one part that can find the conversation moved on.
585
+ if (thread && !(await store.setThread(p.id, post.id, thread.next, thread.prev))) {
586
+ return fail(res, 409, 'not_your_turn');
587
+ }
362
588
  await store.updatePost(p.id, post.id, patch);
363
589
  if (typeof shotOff === 'boolean')
364
590
  await store.setShotOff(p.id, post.id, shotOff);
@@ -369,6 +595,86 @@ export function createRouter(options) {
369
595
  console.info(`[post] ${p.id} owner managed ${post.id}`);
370
596
  res.json({ ok: true });
371
597
  }));
598
+ router.post('/v1/projects/:pid/updates', h(async (req, res) => {
599
+ const p = await project(req, res);
600
+ if (!p)
601
+ return;
602
+ const w = await wallet(req, res, p);
603
+ if (!w)
604
+ return;
605
+ if (!w.owner)
606
+ return fail(res, 403, 'not_owner');
607
+ const body = (req.body ?? {});
608
+ const title = typeof body.title === 'string' ? body.title.trim() : '';
609
+ const text = body.text === undefined ? '' : typeof body.text === 'string' ? body.text.trim() : null;
610
+ const version = body.version === undefined ? '' : cleanVersion(body.version);
611
+ const link = cleanCodeLink(body.link);
612
+ const asked = body.posts === undefined ? [] : body.posts;
613
+ if (typeof body.id !== 'string' ||
614
+ !body.id ||
615
+ body.id.length > POST_ID_MAX ||
616
+ !title ||
617
+ title.length > UPDATE_TITLE_MAX ||
618
+ text === null ||
619
+ text.length > UPDATE_TEXT_MAX ||
620
+ version === null ||
621
+ // Every update has somewhere to go: that is what tapping one does.
622
+ !link ||
623
+ !Array.isArray(asked) ||
624
+ asked.length > UPDATE_POSTS_MAX ||
625
+ !asked.every((id) => typeof id === 'string')) {
626
+ return fail(res, 400, 'bad_request');
627
+ }
628
+ // A retry of one already written: refused before any post is touched.
629
+ if ((await store.updates(p.id)).some((u) => u.id === body.id))
630
+ return fail(res, 409, 'duplicate');
631
+ // The bugs and ideas it fixed, of those on this board. Each goes to
632
+ // done with this update's version and link, which is the point of
633
+ // ticking it: one write, and every author hears their thing shipped.
634
+ const at = now();
635
+ const fix = { ...(version ? { version } : {}), link };
636
+ const fixed = [];
637
+ for (const id of new Set(asked)) {
638
+ const post = await store.getPost(p.id, id);
639
+ if (!post || post.kind === 'review')
640
+ continue;
641
+ fixed.push(post.id);
642
+ if (post.status === 'done' && sameFix(fix, post.fix))
643
+ continue;
644
+ await store.updatePost(p.id, post.id, { status: 'done', fix, answeredAt: at });
645
+ // Done, so nobody needs the picture of somebody's screen any more.
646
+ await store.deleteShot(p.id, post.id);
647
+ }
648
+ const update = {
649
+ id: body.id,
650
+ title,
651
+ ...(text ? { text } : {}),
652
+ ...(version ? { version } : {}),
653
+ link,
654
+ at,
655
+ posts: fixed,
656
+ };
657
+ if (!(await store.createUpdate(p.id, update)))
658
+ return fail(res, 409, 'duplicate');
659
+ console.info(`[update] ${p.id} owner wrote ${update.id}, fixing ${fixed.length}`);
660
+ res.json({ ok: true, update });
661
+ }));
662
+ router.delete('/v1/projects/:pid/updates/:id', h(async (req, res) => {
663
+ const p = await project(req, res);
664
+ if (!p)
665
+ return;
666
+ const w = await wallet(req, res, p);
667
+ if (!w)
668
+ return;
669
+ if (!w.owner)
670
+ return fail(res, 403, 'not_owner');
671
+ // The posts it fixed stay fixed: taking a page off the list does not
672
+ // un-ship anything.
673
+ if (!(await store.deleteUpdate(p.id, String(req.params.id))))
674
+ return fail(res, 404, 'no_update');
675
+ console.info(`[update] ${p.id} owner deleted ${String(req.params.id)}`);
676
+ res.json({ ok: true });
677
+ }));
372
678
  // ---- Projects (dashboard) ----
373
679
  router.post('/v1/projects', rateLimit({ windowMs: 60 * 60_000, max: 5, name: 'create-project' }), h(async (req, res) => {
374
680
  const owner = await dashboard(req, res);
@@ -496,6 +802,10 @@ function readShot(v) {
496
802
  return { type, data: bytes, board: agreed };
497
803
  return null;
498
804
  }
805
+ /** Whether two fixes say the same, absent and empty being the same nothing. */
806
+ function sameFix(a, b) {
807
+ return (a?.version ?? '') === (b?.version ?? '') && (a?.link ?? '') === (b?.link ?? '');
808
+ }
499
809
  /** A wallet, short enough for a log line and not enough to identify anyone at a glance. */
500
810
  function tag(address) {
501
811
  const a = normalizeAddress(address);