|
|
@@ -11,6 +11,7 @@ |
| 11 | |
|
| 12 | |
|
| 13 | |
import { keepThought } from "../../actions/thought.ts"; |
| |
import { FILE_WORDS, storeFile } from "../../core/conversation_files.ts"; |
| 14 | |
import { emptyNote } from "../empty_words.ts"; |
| 15 | |
import { expireCookie, setCookie } from "../../auth/cookies.ts"; |
| 16 | |
import type { Ctx } from "../../http/context.ts"; |
|
|
@@ -23,7 +24,7 @@ import { notFoundPage, signInHref } from "../chrome.ts"; |
| 23 | |
import { field, readForm } from "../form.ts"; |
| 24 | |
import { html } from "../html.ts"; |
| 25 | |
import { page, redirect } from "../layout.ts"; |
| |
import { COMPOSER_FIELDS, SHARE_PARAMS, refuseShare, refuseTell, sharedDraft } from "../share_target.ts"; |
| |
import { COMPOSER_FIELDS, SHARE_IMAGE_FIELD, SHARE_PARAMS, refuseImage, refuseShare, refuseTell, shareOrigin, sharedDraft } from "../share_target.ts"; |
| 27 | |
import { btn, empty } from "../ui.ts"; |
| 28 | |
import { tellForm } from "./tell_form.ts"; |
| 29 | |
|
|
|
@@ -50,34 +51,59 @@ const tooMany = (ctx: Ctx): Response => |
| 50 | |
page({ ctx, title: "Share not sent", body: html`<h1>Too many shares</h1>${empty({ icon: "lock", title: "Too many shares from this address; try again in a minute.", action: btn("Back to the forge", { href: "/", variant: "primary" }) })}` }, 429); |
| 51 | |
|
| 52 | |
|
| |
const hold = (ctx: Ctx, draft: string): string => |
| |
setCookie(STASH_COOKIE, stashShare(ctx.app, draft), { maxAge: STASH_SECONDS, secure: secure(ctx) }); |
| |
const hold = (ctx: Ctx, draft: string, extra: Omit<Held, "draft" | "at"> = {}): string => |
| |
setCookie(STASH_COOKIE, stashShare(ctx.app, draft, Date.now(), extra), { maxAge: STASH_SECONDS, secure: secure(ctx) }); |
| 55 | |
|
| 56 | |
|
| 57 | |
export async function shareReceive(ctx: Ctx): Promise<Response> { |
| 58 | |
const form = await readForm(ctx, ctx.auth.user === undefined ? undefined : "rw"); |
| |
if (form instanceof Response) return form; |
| |
if (form instanceof Response) { |
| |
|
| |
|
| |
if (form.status === 413 && ctx.auth.user === undefined) return refused(ctx, "That share is too big to take before you sign in. Sign in, then share it again; nothing was kept.", 413); |
| |
return form; |
| |
} |
| 60 | |
const params = new URLSearchParams(); |
| 61 | |
for (const key of [...Object.values(SHARE_PARAMS), COMPOSER_FIELDS.text]) params.set(key, field(form, key)); |
| 62 | |
const draft = sharedDraft(params); |
| |
const words = refuseShare(params); |
| 64 | |
const user = ctx.auth.user; |
| |
|
| |
const file = form.get(SHARE_IMAGE_FIELD); |
| |
const image = file instanceof File && file.size > 0 ? { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()) } : undefined; |
| |
const words = refuseShare(params, image !== undefined); |
| 65 | |
if (user === undefined) { |
| 66 | |
|
| 67 | |
if (words !== undefined) return refused(ctx, words, refuseTell(draft) === words ? 422 : 400, draft); |
| 68 | |
if (!mayHold(ctx.app, clientAddress(ctx.request))) { |
| 69 | |
return tooMany(ctx); |
| 70 | |
} |
| |
return redirect(signInHref(ctx, "/tell/share"), { cookie: hold(ctx, draft) }); |
| |
|
| |
|
| |
return redirect(signInHref(ctx, "/tell/share"), { |
| |
cookie: hold(ctx, draft, { params, ...(image === undefined ? {} : { note: "The image was not kept because you were signed out. Share it again." }) }), |
| |
}); |
| 72 | |
} |
| 73 | |
if (words !== undefined) return refused(ctx, words, refuseTell(draft) === words ? 422 : 400, draft); |
| |
if (image !== undefined && field(form, CONFIRM_FIELD) !== "1") { |
| |
const bad = refuseImage(image.name, image.bytes); |
| |
|
| |
if (bad !== undefined) return refused(ctx, bad, 422, draft); |
| |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
if (field(form, CONFIRM_FIELD) !== "1") { |
| 77 | |
if (!mayHold(ctx.app, clientAddress(ctx.request))) { |
| 78 | |
return tooMany(ctx); |
| 79 | |
} |
| |
return redirect("/tell/share", { cookie: hold(ctx, draft) }); |
| |
|
| |
|
| |
let kept: { id: string; name: string } | undefined; |
| |
if (image !== undefined) { |
| |
const stored = storeFile(ctx.app.db, ctx.app.config.dataDir, user.id, image.name, image.bytes); |
| |
if (!stored.ok) return refused(ctx, `The image was not kept: ${FILE_WORDS[stored.error][1]}. Your words were kept.`, 422, draft); |
| |
kept = { id: stored.value.id, name: stored.value.name }; |
| |
} |
| |
return redirect("/tell/share", { cookie: hold(ctx, draft, { params, ...(kept === undefined ? {} : { image: kept }) }) }); |
| 81 | |
} |
| 82 | |
const kept = keepThought(ctx.app, user, draft, "share"); |
| 83 | |
if (!kept.ok) return refused(ctx, "That could not be kept.", 400); |
|
|
@@ -89,7 +115,8 @@ export function shareHeld(ctx: Ctx): Response { |
| 89 | |
notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 90 | |
const rendering = performance.now(); |
| 91 | |
if (ctx.auth.user === undefined) return redirect(signInHref(ctx, "/tell/share")); |
| |
const draft = takeShare(ctx.app, cookieValue(ctx.request, STASH_COOKIE)); |
| |
const held = takeHeld(ctx.app, cookieValue(ctx.request, STASH_COOKIE)); |
| |
const draft = held?.draft; |
| 93 | |
const clear = expireCookie(STASH_COOKIE, { secure: secure(ctx) }); |
| 94 | |
const response = page({ |
| 95 | |
ctx, |
|
|
@@ -97,7 +124,9 @@ export function shareHeld(ctx: Ctx): Response { |
| 97 | |
body: html`<h1>Tell the crew</h1> |
| 98 | |
${draft === undefined |
| 99 | |
? emptyNote("nothingIsWaitingToBeSent") |
| |
: html`<p class="muted small">Nothing is sent until you tap Send.</p> |
| |
: html`<p class="muted small" data-share-origin>${shareOrigin(held?.params ?? new URLSearchParams(), held?.image?.name)} Nothing is sent until you tap Send${held?.image === undefined ? "." : html`; the picture waits in <a href="/talk">Talk</a> for your next message there.`}</p> |
| |
${held?.note === undefined ? html`` : html`<p role="alert" data-share-note>${held.note}</p>`} |
| |
${held?.image === undefined ? html`` : html`<p><img src="/api/v1/conversation/files/${held.image.id}" alt="${held.image.name}" width="160" height="160" class="share-image" data-share-image></p>`} |
|
|
|
| 101 | |
<form class="stack" method="post" action="/tell/share"> |
| 102 | |
<input type="hidden" name="${CONFIRM_FIELD}" value="1"> |
| 103 | |
<textarea class="input" name="${COMPOSER_FIELDS.text}" rows="6" required aria-label="Tell the crew">${draft}</textarea> |
|
|
@@ -135,14 +164,26 @@ export function shareReceipt(ctx: Ctx): Response { |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| |
type Held = Map<string, { draft: string; at: number }>; |
| |
interface Held { |
| |
draft: string; |
| |
at: number; |
| |
|
| |
params?: URLSearchParams; |
| |
|
| |
image?: { id: string; name: string }; |
| |
|
| |
note?: string; |
| |
} |
| |
type HeldMap = Map<string, Held>; |
| 139 | |
|
|
|
|
| |
const HELD = new WeakMap<object, Held>(); |
| |
const heldShares = (app: object): Held => { |
| |
let m = HELD.get(app); |
| |
if (m === undefined) HELD.set(app, (m = new Map())); |
| |
return m; |
| |
}; |
| |
const HELD = new WeakMap<object, HeldMap>(); |
| |
function heldShares(app: object): HeldMap { |
| |
const kept = HELD.get(app); |
| |
if (kept !== undefined) return kept; |
| |
const made: HeldMap = new Map(); |
| |
HELD.set(app, made); |
| |
return made; |
| |
} |
| 146 | |
const HELD_FOR_MS = 15 * 60 * 1000; |
| 147 | |
const HELD_KEEP = 200; |
| 148 | |
|
|
|
@@ -170,19 +211,19 @@ export function mayHold(app: object, address: string): boolean { |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| |
export function stashShare(app: object, draft: string, now = Date.now()): string { |
| |
export function stashShare(app: object, draft: string, now = Date.now(), extra: Omit<Held, "draft" | "at"> = {}): string { |
| 174 | |
const held = heldShares(app); |
| 175 | |
for (const [k, v] of held) if (now - v.at >= HELD_FOR_MS || held.size >= HELD_KEEP) held.delete(k); |
| 176 | |
const key = Array.from(crypto.getRandomValues(new Uint8Array(16)), (b) => b.toString(16).padStart(2, "0")).join(""); |
| |
held.set(key, { draft, at: now }); |
| |
held.set(key, { draft, at: now, ...extra }); |
| 178 | |
return key; |
| 179 | |
} |
| 180 | |
|
| |
|
| |
export function takeShare(app: object, key: string | undefined, now = Date.now()): string | undefined { |
| |
|
| |
export function takeHeld(app: object, key: string | undefined, now = Date.now()): Held | undefined { |
| 183 | |
if (key === undefined || !KEY.test(key)) return undefined; |
| 184 | |
const map = heldShares(app); |
| 185 | |
const held = map.get(key); |
| 186 | |
map.delete(key); |
| |
return held !== undefined && now - held.at < HELD_FOR_MS ? held.draft : undefined; |
| |
return held !== undefined && now - held.at < HELD_FOR_MS ? held : undefined; |
| 188 | |
} |
Chips only appear with ?shared=1, so a plain /talk does not attach the picture and it is swept after an hour. Make the pickup not depend on a query flag (and not run an extra query for everyone), or the picture cannot be finished.