// Optional "View full email" links.
//
// Chat delivery is lossy by design: bodies are truncated to fit a message,
// and an image-only email has nothing readable to show at all. A webview
// link trades that for keeping the body somewhere for a week, which is a
// real privacy cost -- so it is opt-in per owner, and nothing is stored for
// anyone who has not turned it on.
//
// What makes the cost bearable is that the Worker cannot read what it
// stores. Each message is sealed with a fresh AES-GCM key that exists only
// in the link's URL fragment, which browsers never put in a request. The
// row in D1 is the ciphertext and its IV; the key is in the chat message,
// which already contained the whole email anyway. Reading a stolen database
// gives you nothing, and neither does reading the Worker's own logs.
// Long enough to be useful (a verification link still works days later),
// short enough that a leaked chat history isn't a permanent archive.
export const WEBVIEW_RETENTION_SECONDS = 7 * 24 * 60 * 60;
// The id is the only thing authenticating a fetch of the ciphertext, so it
// gets the same treatment as an address: crypto-random, not guessable. 16
// bytes is 128 bits of id on top of the 256-bit key an attacker would still
// need afterward.
const ID_BYTES = 16;
const IV_BYTES = 12;
// Bodies past this are not stored at all, and the chat message says so
// rather than linking to a page that would fail. Keeps one enormous email
// from eating a user's D1 row budget for a week.
export const MAX_WEBVIEW_BYTES = 512 * 1024;
// What a webview page shows. Attachments are deliberately absent: they are
// already delivered to chat, and storing them would multiply both the size
// cap and the retention cost for no gain.
export interface WebviewPayload {
from: string;
to: string;
subject: string;
text: string;
html?: string;