import type { AbuseCase, Attachment, AuditEvent, ComplianceProfile, Credential, Draft, IdempotencyRecord, IngressQuotaDecision, IngressQuotaReservation, Inbox, MailDomain, MailFnEvent, MailFnStore, MailFnStorePageInput, Message, MessageFilter, Project, SearchMessagesInput, SenderReputation, SupportCase, Thread, UsageRecord, Webhook, WebhookDelivery, } from '@mailfn/core'; import type { D1Database, D1PreparedStatement } from './bindings.js'; interface JsonRow { data_json: string; } const MESSAGE_ADMIN_FIELDS = new Set([ 'id', 'projectId', 'inboxId', 'providerDeliveryId', 'envelopeFrom', 'envelopeTo', 'subject', 'receivedAt', 'parsedAt', 'threadId', 'sizeBytes', 'status', 'readAt', 'createdAt', 'updatedAt', 'sender', 'senderDomain', 'recipient', 'text', 'receivedAfter', 'receivedBefore', 'unreadOnly', 'labels', ]); const MESSAGE_ADMIN_SORTS: Readonly> = { id: 'mailfn_messages.id', projectId: 'mailfn_messages.project_id', inboxId: 'mailfn_messages.inbox_id', providerDeliveryId: 'mailfn_messages.provider_delivery_id', envelopeFrom: 'mailfn_messages.envelope_from', envelopeTo: 'mailfn_messages.envelope_to', subject: 'mailfn_messages.subject', receivedAt: 'julianday(mailfn_messages.received_at)', parsedAt: "julianday(json_extract(mailfn_messages.data_json, '$.parsedAt'))", threadId: 'mailfn_messages.thread_id', sizeBytes: 'mailfn_messages.size_bytes', status: 'mailfn_messages.status', readAt: "julianday(json_extract(mailfn_messages.data_json, '$.readAt'))", createdAt: 'julianday(mailfn_messages.created_at)', updatedAt: 'julianday(mailfn_messages.updated_at)', }; const ATTACHMENT_ADMIN_FIELDS = new Set([ 'id', 'projectId', 'inboxId', 'messageId', 'filename', 'contentType', 'sizeBytes', 'sha256', 'contentId', 'disposition', 'createdAt', ]); const ATTACHMENT_ADMIN_SORTS: Readonly> = { id: 'mailfn_attachments.id', projectId: 'mailfn_attachments.project_id', inboxId: 'mailfn_attachments.inbox_id', messageId: 'mailfn_attachments.message_id', filename: 'mailfn_attachments.filename', contentType: 'mailfn_attachments.content_type', sizeBytes: 'mailfn_attachments.size_bytes', sha256: 'mailfn_attachments.sha256', contentId: "json_extract(mailfn_attachments.data_json, '$.contentId')", disposition: "json_extract(mailfn_attachments.data_json, '$.disposition')", createdAt: 'julianday(mailfn_attachments.created_at)', }; export class D1MailFnStore implements MailFnStore { public constructor(private readonly database: D1Database) {} async getProject(id: string): Promise { return this.one('SELECT data_json FROM mailfn_projects WHERE id = ?', [id]); } async getProjectBySlug(slug: string): Promise { return this.one('SELECT data_json FROM mailfn_projects WHERE slug = ?', [slug]); } async listProjects(): Promise { return this.many('SELECT data_json FROM mailfn_projects ORDER BY created_at', []); } async saveProject(value: Project): Promise { await this.run( `INSERT INTO mailfn_projects(id, slug, display_name, status, default_retention_policy, data_region, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET slug=excluded.slug, display_name=excluded.display_name, status=excluded.status, default_retention_policy=excluded.default_retention_policy, data_region=excluded.data_region, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.id, value.slug, value.displayName, value.status, JSON.stringify(value.defaultRetentionPolicy), value.dataRegion, value.createdAt, value.updatedAt, json(value)], ); } async createProjectWithCredential(project: Project, credential: Credential, audit: AuditEvent): Promise { const results = await this.database.batch([ bind(this.database.prepare( `INSERT INTO mailfn_projects(id, slug, display_name, status, default_retention_policy, data_region, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, ), [project.id, project.slug, project.displayName, project.status, JSON.stringify(project.defaultRetentionPolicy), project.dataRegion, project.createdAt, project.updatedAt, json(project)]), credentialInsert(this.database, credential), auditInsert(this.database, audit), ]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); } async getInbox(id: string): Promise { return this.one('SELECT data_json FROM mailfn_inboxes WHERE id = ?', [id]); } async getInboxByAddress(address: string): Promise { return this.one('SELECT data_json FROM mailfn_inboxes WHERE address = ?', [address]); } async listInboxes(projectId: string): Promise { return this.many('SELECT data_json FROM mailfn_inboxes WHERE project_id = ? ORDER BY created_at', [projectId]); } async saveInbox(value: Inbox): Promise { await this.run( `INSERT INTO mailfn_inboxes(id, project_id, address, kind, status, expires_at, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET address=excluded.address, kind=excluded.kind, status=excluded.status, expires_at=excluded.expires_at, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.id, value.projectId, value.address, value.kind, value.status, value.expiresAt ?? null, value.createdAt, value.updatedAt, json(value)], ); } async saveInboxIfUnchanged(value: Inbox, expected: Inbox): Promise { const result = await bind(this.database.prepare( `UPDATE mailfn_inboxes SET address = ?, kind = ?, status = ?, expires_at = ?, updated_at = ?, data_json = ? WHERE id = ? AND project_id = ? AND data_json = ? AND status NOT IN ('deleting', 'deleted')`, ), [ value.address, value.kind, value.status, value.expiresAt ?? null, value.updatedAt, json(value), value.id, value.projectId, json(expected), ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async saveInboxWithActiveQuota(value: Inbox, expected: Inbox, maxActiveInboxes: number): Promise { const result = await bind(this.database.prepare( `UPDATE mailfn_inboxes SET address = ?, kind = ?, status = ?, expires_at = ?, updated_at = ?, data_json = ? WHERE id = ? AND project_id = ? AND data_json = ? AND status NOT IN ('deleting', 'deleted') AND (status = 'active' OR ( SELECT COUNT(*) FROM mailfn_inboxes WHERE project_id = ? AND id <> ? AND status = 'active' ) < ?)`, ), [ value.address, value.kind, value.status, value.expiresAt ?? null, value.updatedAt, json(value), value.id, value.projectId, json(expected), value.projectId, value.id, maxActiveInboxes, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async claimInboxDeletion(value: Inbox, expected: Inbox): Promise { const result = await bind(this.database.prepare( `UPDATE mailfn_inboxes SET address = ?, kind = ?, status = ?, expires_at = ?, updated_at = ?, data_json = ? WHERE id = ? AND project_id = ? AND data_json = ? AND NOT EXISTS ( SELECT 1 FROM mailfn_compliance WHERE project_id = ? AND retention_locked = 1 )`, ), [ value.address, value.kind, value.status, value.expiresAt ?? null, value.updatedAt, json(value), value.id, value.projectId, json(expected), value.projectId, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async createInboxWithCredential( inbox: Inbox, credential: Credential, idempotency: IdempotencyRecord | undefined, audit: AuditEvent, maxActiveInboxes: number, ): Promise { const statements = [ bind(this.database.prepare( `INSERT INTO mailfn_inboxes(id, project_id, address, kind, status, expires_at, created_at, updated_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE (SELECT COUNT(*) FROM mailfn_inboxes WHERE project_id = ? AND status = 'active') < ?`, ), [ inbox.id, inbox.projectId, inbox.address, inbox.kind, inbox.status, inbox.expiresAt ?? null, inbox.createdAt, inbox.updatedAt, json(inbox), inbox.projectId, maxActiveInboxes, ]), credentialInsert(this.database, credential), ]; if (idempotency) { statements.push(bind(this.database.prepare( `INSERT INTO mailfn_idempotency(project_id, key, operation, resource_id, request_hash, expires_at, created_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, ), [idempotency.projectId, idempotency.key, idempotency.operation, idempotency.resourceId, idempotency.requestHash, idempotency.expiresAt, idempotency.createdAt, json(idempotency)])); } statements.push(auditInsert(this.database, audit)); const results = await this.database.batch(statements); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); } async getCredential(id: string): Promise { return this.one('SELECT data_json FROM mailfn_credentials WHERE id = ?', [id]); } async listCredentials(projectId: string, inboxId?: string): Promise { return inboxId ? this.many('SELECT data_json FROM mailfn_credentials WHERE project_id = ? AND inbox_id = ? ORDER BY created_at', [projectId, inboxId]) : this.many('SELECT data_json FROM mailfn_credentials WHERE project_id = ? ORDER BY created_at', [projectId]); } async saveCredential(value: Credential): Promise { await this.run( `INSERT INTO mailfn_credentials(id, project_id, inbox_id, token_hash, token_prefix, permissions, status, expires_at, revoked_at, created_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET token_hash=excluded.token_hash, token_prefix=excluded.token_prefix, permissions=excluded.permissions, status=excluded.status, expires_at=excluded.expires_at, revoked_at=excluded.revoked_at, data_json=excluded.data_json`, [value.id, value.projectId, value.inboxId ?? null, value.tokenHash, value.tokenPrefix, JSON.stringify(value.permissions), value.status, value.expiresAt ?? null, value.revokedAt ?? null, value.createdAt, json(value)], ); } async saveCredentialIfInboxActive(value: Credential, now: string): Promise { if (!value.inboxId) return false; const result = await bind(this.database.prepare( `INSERT INTO mailfn_credentials(id, project_id, inbox_id, token_hash, token_prefix, permissions, status, expires_at, revoked_at, created_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM mailfn_inboxes WHERE id = ? AND project_id = ? AND status = 'active' AND (expires_at IS NULL OR julianday(expires_at) > julianday(?)) ON CONFLICT(id) DO UPDATE SET token_hash=excluded.token_hash, token_prefix=excluded.token_prefix, permissions=excluded.permissions, status=excluded.status, expires_at=excluded.expires_at, revoked_at=excluded.revoked_at, data_json=excluded.data_json`, ), [ value.id, value.projectId, value.inboxId, value.tokenHash, value.tokenPrefix, JSON.stringify(value.permissions), value.status, value.expiresAt ?? null, value.revokedAt ?? null, value.createdAt, json(value), value.inboxId, value.projectId, now, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async createCredentialWithAudit(value: Credential, audit: AuditEvent, now: string): Promise { const results = await this.database.batch([ bind(this.database.prepare( `INSERT INTO mailfn_credentials(id, project_id, inbox_id, token_hash, token_prefix, permissions, status, expires_at, revoked_at, created_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE ? IS NULL OR EXISTS ( SELECT 1 FROM mailfn_inboxes WHERE id = ? AND project_id = ? AND status = 'active' AND (expires_at IS NULL OR julianday(expires_at) > julianday(?)) )`, ), [ value.id, value.projectId, value.inboxId ?? null, value.tokenHash, value.tokenPrefix, JSON.stringify(value.permissions), value.status, value.expiresAt ?? null, value.revokedAt ?? null, value.createdAt, json(value), value.inboxId ?? null, value.inboxId ?? null, value.projectId, now, ]), bind(this.database.prepare( `INSERT INTO mailfn_audits( id, project_id, actor_type, actor_id, action, resource_type, resource_id, created_at, retention_expires_at, data_json ) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE changes() = 1 AND EXISTS (SELECT 1 FROM mailfn_credentials WHERE id = ?)`, ), [ audit.id, audit.projectId, audit.actorType, audit.actorId, audit.action, audit.resourceType, audit.resourceId, audit.createdAt, audit.retentionExpiresAt, json(audit), value.id, ]), ]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); const created = Number(results[0]?.meta?.changes ?? 0) === 1; if (created && Number(results[1]?.meta?.changes ?? 0) !== 1) throw new Error('MAILFN_D1_WRITE_FAILED'); return created; } async touchCredentialIfActive(id: string, lastUsedAt: string): Promise { const result = await this.database.prepare( `UPDATE mailfn_credentials SET data_json = json_set(data_json, '$.lastUsedAt', ?) WHERE id = ? AND status = 'active'`, ).bind(lastUsedAt, id).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async getMessage(id: string): Promise { return this.one('SELECT data_json FROM mailfn_messages WHERE id = ?', [id]); } async getMessageByDelivery(inboxId: string, providerDeliveryId: string): Promise { return this.one('SELECT data_json FROM mailfn_messages WHERE inbox_id = ? AND provider_delivery_id = ?', [inboxId, providerDeliveryId]); } async listMessages(projectId: string, inboxId: string, filter: MessageFilter = {}): Promise { const clauses = ['project_id = ?', 'inbox_id = ?']; const values: unknown[] = [projectId, inboxId]; if (filter.receivedAfter) { clauses.push('julianday(received_at) > julianday(?)'); values.push(normalizeInstant(filter.receivedAfter)); } if (filter.receivedBefore) { clauses.push('julianday(received_at) < julianday(?)'); values.push(normalizeInstant(filter.receivedBefore)); } if (filter.threadId) { clauses.push('thread_id = ?'); values.push(filter.threadId); } if (filter.status) { clauses.push('status = ?'); values.push(filter.status); } const messages = await this.many( `SELECT data_json FROM mailfn_messages WHERE ${clauses.join(' AND ')} ORDER BY julianday(received_at) DESC, id DESC`, values, ); return messages.filter((message) => matchesMessage(message, filter)); } async listMessagesPage( projectId: string, inboxId: string, filter: MessageFilter, cursorId: string | undefined, limit: number, ): Promise<{ items: Message[]; hasMore: boolean; cursorFound: boolean }> { const cursor = cursorId ? await this.getMessage(cursorId) : null; if (cursorId && (!cursor || cursor.projectId !== projectId || cursor.inboxId !== inboxId || !matchesMessage(cursor, filter))) { return { items: [], hasMore: false, cursorFound: false }; } const clauses = ['project_id = ?', 'inbox_id = ?']; const values: unknown[] = [projectId, inboxId]; if (filter.receivedAfter) { clauses.push('julianday(received_at) > julianday(?)'); values.push(normalizeInstant(filter.receivedAfter)); } if (filter.receivedBefore) { clauses.push('julianday(received_at) < julianday(?)'); values.push(normalizeInstant(filter.receivedBefore)); } if (filter.threadId) { clauses.push('thread_id = ?'); values.push(filter.threadId); } if (filter.status) { clauses.push('status = ?'); values.push(filter.status); } if (filter.recipient) { clauses.push('lower(envelope_to) = ?'); values.push(filter.recipient.toLowerCase()); } if (filter.subject) { clauses.push('instr(lower(subject), ?) > 0'); values.push(filter.subject.toLowerCase()); } if (filter.text) { clauses.push(`instr(lower(coalesce(json_extract(data_json, '$.textBody'), '') || char(10) || coalesce(json_extract(data_json, '$.htmlBody'), '')), ?) > 0`); values.push(filter.text.toLowerCase()); } if (filter.unreadOnly) clauses.push(`json_type(data_json, '$.readAt') IS NULL`); if (filter.sender) { clauses.push(`(lower(envelope_from) = ? OR EXISTS ( SELECT 1 FROM json_each(mailfn_messages.data_json, '$.from') AS sender_entry WHERE lower(json_extract(sender_entry.value, '$.address')) = ? ))`); values.push(filter.sender.toLowerCase(), filter.sender.toLowerCase()); } if (filter.senderDomain) { const suffix = `@${filter.senderDomain.toLowerCase()}`; clauses.push(`EXISTS ( SELECT 1 FROM json_each(mailfn_messages.data_json, '$.from') AS sender_entry WHERE substr(lower(json_extract(sender_entry.value, '$.address')), -length(?)) = ? )`); values.push(suffix, suffix); } for (const label of filter.labels ?? []) { clauses.push(`EXISTS ( SELECT 1 FROM json_each(mailfn_messages.data_json, '$.labels') AS label_entry WHERE label_entry.value = ? )`); values.push(label); } if (cursor) { clauses.push('(julianday(received_at) < julianday(?) OR (julianday(received_at) = julianday(?) AND id < ?))'); values.push(cursor.receivedAt, cursor.receivedAt, cursor.id); } values.push(limit + 1); const messages = await this.many( `SELECT data_json FROM mailfn_messages WHERE ${clauses.join(' AND ')} ORDER BY julianday(received_at) DESC, id DESC LIMIT ?`, values, ); return { items: messages.slice(0, limit), hasMore: messages.length > limit, cursorFound: true, }; } async listProjectMessagesPage( projectId: string, input: MailFnStorePageInput, ): Promise<{ items: Message[]; hasMore: boolean }> { return this.listProjectPage( 'mailfn_messages', projectId, input, MESSAGE_ADMIN_FIELDS, MESSAGE_ADMIN_SORTS, ); } async searchMessages( projectId: string, inboxId: string, input: Omit, ): Promise { const clauses = ['mailfn_messages_fts.project_id = ?', 'mailfn_messages_fts.inbox_id = ?', 'mailfn_messages_fts MATCH ?']; const values: unknown[] = [projectId, inboxId, ftsPhrase(input.query)]; if (input.receivedAfter) { clauses.push('julianday(message.received_at) > julianday(?)'); values.push(normalizeInstant(input.receivedAfter)); } if (input.receivedBefore) { clauses.push('julianday(message.received_at) < julianday(?)'); values.push(normalizeInstant(input.receivedBefore)); } return this.many( `SELECT message.data_json FROM mailfn_messages_fts JOIN mailfn_messages AS message ON message.id = mailfn_messages_fts.message_id WHERE ${clauses.join(' AND ')} AND message.status = 'ready' ORDER BY julianday(message.received_at) DESC, message.id DESC`, values, ); } async searchMessagesPage( projectId: string, inboxId: string, input: Omit, cursorId: string | undefined, limit: number, ): Promise<{ items: Message[]; hasMore: boolean; cursorFound: boolean }> { const clauses = ['mailfn_messages_fts.project_id = ?', 'mailfn_messages_fts.inbox_id = ?', 'mailfn_messages_fts MATCH ?']; const values: unknown[] = [projectId, inboxId, ftsPhrase(input.query)]; if (input.receivedAfter) { clauses.push('julianday(message.received_at) > julianday(?)'); values.push(normalizeInstant(input.receivedAfter)); } if (input.receivedBefore) { clauses.push('julianday(message.received_at) < julianday(?)'); values.push(normalizeInstant(input.receivedBefore)); } const cursor = cursorId ? await this.getMessage(cursorId) : null; if (cursorId) { if (!cursor || cursor.projectId !== projectId || cursor.inboxId !== inboxId) { return { items: [], hasMore: false, cursorFound: false }; } const cursorMatch = await this.rawMany<{ id: string }>( `SELECT message.id FROM mailfn_messages_fts JOIN mailfn_messages AS message ON message.id = mailfn_messages_fts.message_id WHERE ${clauses.join(' AND ')} AND message.status = 'ready' AND message.id = ? LIMIT 1`, [...values, cursorId], ); if (!cursorMatch.length) return { items: [], hasMore: false, cursorFound: false }; clauses.push('(julianday(message.received_at) < julianday(?) OR (julianday(message.received_at) = julianday(?) AND message.id < ?))'); values.push(cursor.receivedAt, cursor.receivedAt, cursor.id); } values.push(limit + 1); const messages = await this.many( `SELECT message.data_json FROM mailfn_messages_fts JOIN mailfn_messages AS message ON message.id = mailfn_messages_fts.message_id WHERE ${clauses.join(' AND ')} AND message.status = 'ready' ORDER BY julianday(message.received_at) DESC, message.id DESC LIMIT ?`, values, ); return { items: messages.slice(0, limit), hasMore: messages.length > limit, cursorFound: true, }; } async createInboundMessageIfInboxActive(value: Message): Promise { const result = await this.database.prepare( `INSERT INTO mailfn_messages(id, project_id, inbox_id, provider_delivery_id, internet_message_id, envelope_from, envelope_to, subject, received_at, parsed_at, raw_object_key, raw_retention_expires_at, attachment_retention_expires_at, raw_deleted_at, thread_id, size_bytes, status, retention_expires_at, created_at, updated_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM mailfn_inboxes WHERE id = ? AND status = 'active') ON CONFLICT DO NOTHING`, ).bind( value.id, value.projectId, value.inboxId, value.providerDeliveryId, value.internetMessageId ?? null, value.envelopeFrom, value.envelopeTo, value.subject, value.receivedAt, value.parsedAt ?? null, value.rawObjectKey, value.rawRetentionExpiresAt, value.attachmentRetentionExpiresAt, value.rawDeletedAt ?? null, value.threadId ?? null, value.sizeBytes, value.status, value.retentionExpiresAt, value.createdAt, value.updatedAt, json(value), value.inboxId, ).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async saveMessage(value: Message): Promise { const save = bind( this.database.prepare( `INSERT INTO mailfn_messages(id, project_id, inbox_id, provider_delivery_id, internet_message_id, envelope_from, envelope_to, subject, received_at, parsed_at, raw_object_key, raw_retention_expires_at, attachment_retention_expires_at, raw_deleted_at, thread_id, size_bytes, status, retention_expires_at, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET internet_message_id=excluded.internet_message_id, envelope_from=excluded.envelope_from, envelope_to=excluded.envelope_to, subject=excluded.subject, parsed_at=excluded.parsed_at, thread_id=excluded.thread_id, status=excluded.status, raw_deleted_at=excluded.raw_deleted_at, retention_expires_at=excluded.retention_expires_at, updated_at=excluded.updated_at, data_json=excluded.data_json`, ), [value.id, value.projectId, value.inboxId, value.providerDeliveryId, value.internetMessageId ?? null, value.envelopeFrom, value.envelopeTo, value.subject, value.receivedAt, value.parsedAt ?? null, value.rawObjectKey, value.rawRetentionExpiresAt, value.attachmentRetentionExpiresAt, value.rawDeletedAt ?? null, value.threadId ?? null, value.sizeBytes, value.status, value.retentionExpiresAt, value.createdAt, value.updatedAt, json(value)], ); const removeSearch = this.database.prepare('DELETE FROM mailfn_messages_fts WHERE message_id = ?').bind(value.id); const saveSearch = this.database.prepare( 'INSERT INTO mailfn_messages_fts(message_id, project_id, inbox_id, subject, text_body, html_body) VALUES (?, ?, ?, ?, ?, ?)', ).bind(value.id, value.projectId, value.inboxId, value.subject, value.textBody ?? '', value.htmlBody ?? ''); const results = await this.database.batch([save, removeSearch, saveSearch]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); } async saveMessageIfUnchanged(value: Message, expected: Message): Promise { const valueJson = json(value); const save = this.database.prepare( `UPDATE mailfn_messages SET internet_message_id = ?, envelope_from = ?, envelope_to = ?, subject = ?, parsed_at = ?, thread_id = ?, status = ?, raw_deleted_at = ?, retention_expires_at = ?, updated_at = ?, data_json = ? WHERE id = ? AND data_json = ? AND EXISTS ( SELECT 1 FROM mailfn_inboxes WHERE id = ? AND status NOT IN ('deleting', 'deleted') )`, ).bind( value.internetMessageId ?? null, value.envelopeFrom, value.envelopeTo, value.subject, value.parsedAt ?? null, value.threadId ?? null, value.status, value.rawDeletedAt ?? null, value.retentionExpiresAt, value.updatedAt, valueJson, value.id, json(expected), value.inboxId, ); const removeSearch = this.database.prepare( 'DELETE FROM mailfn_messages_fts WHERE message_id = ? AND EXISTS (SELECT 1 FROM mailfn_messages WHERE id = ? AND data_json = ?)', ).bind(value.id, value.id, valueJson); const saveSearch = this.database.prepare( `INSERT INTO mailfn_messages_fts(message_id, project_id, inbox_id, subject, text_body, html_body) SELECT ?, ?, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM mailfn_messages WHERE id = ? AND data_json = ?)`, ).bind( value.id, value.projectId, value.inboxId, value.subject, value.textBody ?? '', value.htmlBody ?? '', value.id, valueJson, ); const results = await this.database.batch([save, removeSearch, saveSearch]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(results[0]?.meta?.changes ?? 0) === 1; } async claimMessageForParsing(messageId: string, claimedAt: string, leaseExpiresAt: string, leaseId: string): Promise { const result = await this.database.prepare( `UPDATE mailfn_messages SET updated_at = ?, data_json = json_set(data_json, '$.parseLeaseId', ?, '$.parseLeaseExpiresAt', ?, '$.updatedAt', ?) WHERE id = ? AND status NOT IN ('ready', 'deleted') AND raw_deleted_at IS NULL AND json_extract(data_json, '$.rawDeletionLeaseId') IS NULL AND (json_extract(data_json, '$.parseLeaseExpiresAt') IS NULL OR json_extract(data_json, '$.parseLeaseExpiresAt') <= ?)`, ).bind(claimedAt, leaseId, leaseExpiresAt, claimedAt, messageId, claimedAt).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async claimMessageRawDeletion(messageId: string, claimId: string, claimedAt: string, leaseExpiresAt: string): Promise { const result = await this.database.prepare( `UPDATE mailfn_messages SET updated_at = ?, data_json = json_set(data_json, '$.rawDeletionLeaseId', ?, '$.rawDeletionLeaseExpiresAt', ?, '$.updatedAt', ?) WHERE id = ? AND status != 'deleted' AND raw_deleted_at IS NULL AND (json_extract(data_json, '$.parseLeaseExpiresAt') IS NULL OR json_extract(data_json, '$.parseLeaseExpiresAt') <= ?) AND (json_extract(data_json, '$.rawDeletionLeaseExpiresAt') IS NULL OR json_extract(data_json, '$.rawDeletionLeaseExpiresAt') <= ?)`, ).bind(claimedAt, claimId, leaseExpiresAt, claimedAt, messageId, claimedAt, claimedAt).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async finishMessageRawDeletion(messageId: string, claimId: string, deletedAt: string): Promise { const result = await this.database.prepare( `UPDATE mailfn_messages SET raw_deleted_at = ?, updated_at = ?, data_json = json_remove(json_set(data_json, '$.rawDeletedAt', ?, '$.updatedAt', ?), '$.rawDeletionLeaseId', '$.rawDeletionLeaseExpiresAt') WHERE id = ? AND json_extract(data_json, '$.rawDeletionLeaseId') = ?`, ).bind(deletedAt, deletedAt, deletedAt, deletedAt, messageId, claimId).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async claimMessageDeletion(value: Message, expected: Message): Promise { const valueJson = json(value); const claim = this.database.prepare( `UPDATE mailfn_messages SET status = 'deleted', updated_at = ?, data_json = ? WHERE id = ? AND data_json = ?`, ).bind(value.updatedAt, valueJson, value.id, json(expected)); const removeSearch = this.database.prepare( 'DELETE FROM mailfn_messages_fts WHERE message_id = ? AND EXISTS (SELECT 1 FROM mailfn_messages WHERE id = ? AND data_json = ?)', ).bind(value.id, value.id, valueJson); const results = await this.database.batch([claim, removeSearch]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(results[0]?.meta?.changes ?? 0) === 1; } async markMessageRead(messageId: string, readAt: string): Promise { const result = await this.database.prepare( `UPDATE mailfn_messages SET updated_at = ?, data_json = json_set(data_json, '$.readAt', ?, '$.updatedAt', ?) WHERE id = ? AND json_extract(data_json, '$.readAt') IS NULL`, ).bind(readAt, readAt, readAt, messageId).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return this.getMessage(messageId); } async setMessageLabels(messageId: string, labels: string[], updatedAt: string): Promise { const result = await this.database.prepare( `UPDATE mailfn_messages SET updated_at = ?, data_json = json_set(data_json, '$.labels', json(?), '$.updatedAt', ?) WHERE id = ?`, ).bind(updatedAt, JSON.stringify(labels), updatedAt, messageId).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return this.getMessage(messageId); } async deleteMessage(id: string): Promise { const results = await this.database.batch([ this.database.prepare('DELETE FROM mailfn_messages_fts WHERE message_id = ?').bind(id), this.database.prepare('DELETE FROM mailfn_messages WHERE id = ?').bind(id), ]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); } async getAttachment(id: string): Promise { return this.one('SELECT data_json FROM mailfn_attachments WHERE id = ?', [id]); } async listAttachments(messageId: string): Promise { return this.many('SELECT data_json FROM mailfn_attachments WHERE message_id = ? ORDER BY created_at', [messageId]); } async listProjectAttachmentsPage( projectId: string, input: MailFnStorePageInput, ): Promise<{ items: Attachment[]; hasMore: boolean }> { return this.listProjectPage( 'mailfn_attachments', projectId, input, ATTACHMENT_ADMIN_FIELDS, ATTACHMENT_ADMIN_SORTS, ); } async saveAttachment(value: Attachment): Promise { await this.run( `INSERT INTO mailfn_attachments(id, project_id, inbox_id, message_id, filename, content_type, size_bytes, object_key, sha256, created_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET filename=excluded.filename, content_type=excluded.content_type, size_bytes=excluded.size_bytes, object_key=excluded.object_key, sha256=excluded.sha256, data_json=excluded.data_json`, [value.id, value.projectId, value.inboxId, value.messageId, value.filename, value.contentType, value.sizeBytes, value.objectKey, value.sha256, value.createdAt, json(value)], ); } async saveAttachmentIfMessageParseOwned(value: Attachment, parseLeaseId: string): Promise { const result = await bind(this.database.prepare( `INSERT INTO mailfn_attachments(id, project_id, inbox_id, message_id, filename, content_type, size_bytes, object_key, sha256, created_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE EXISTS ( SELECT 1 FROM mailfn_messages WHERE id = ? AND json_extract(data_json, '$.parseLeaseId') = ? ) ON CONFLICT(id) DO UPDATE SET filename=excluded.filename, content_type=excluded.content_type, size_bytes=excluded.size_bytes, object_key=excluded.object_key, sha256=excluded.sha256, data_json=excluded.data_json WHERE EXISTS ( SELECT 1 FROM mailfn_messages WHERE id = ? AND json_extract(data_json, '$.parseLeaseId') = ? )`, ), [ value.id, value.projectId, value.inboxId, value.messageId, value.filename, value.contentType, value.sizeBytes, value.objectKey, value.sha256, value.createdAt, json(value), value.messageId, parseLeaseId, value.messageId, parseLeaseId, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async deleteAttachment(id: string): Promise { await this.run('DELETE FROM mailfn_attachments WHERE id = ?', [id]); } async deleteAttachmentIfUnchanged(id: string, objectKey: string): Promise { const result = await bind( this.database.prepare('DELETE FROM mailfn_attachments WHERE id = ? AND object_key = ?'), [id, objectKey], ).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async getThread(id: string): Promise { return this.one('SELECT data_json FROM mailfn_threads WHERE id = ?', [id]); } async listThreads(projectId: string, inboxId: string): Promise { return this.many('SELECT data_json FROM mailfn_threads WHERE project_id = ? AND inbox_id = ? ORDER BY julianday(last_message_at) DESC, id DESC', [projectId, inboxId]); } async saveThread(value: Thread): Promise { await this.run( `INSERT INTO mailfn_threads(id, project_id, inbox_id, normalized_subject, last_message_at, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET normalized_subject=excluded.normalized_subject, last_message_at=excluded.last_message_at, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.id, value.projectId, value.inboxId, value.normalizedSubject, value.lastMessageAt, value.createdAt, value.updatedAt, json(value)], ); } async saveThreadIfUnchanged(value: Thread, expected: Thread | null): Promise { const statement = expected ? bind(this.database.prepare( `UPDATE mailfn_threads SET normalized_subject = ?, last_message_at = ?, updated_at = ?, data_json = ? WHERE id = ? AND data_json = ?`, ), [value.normalizedSubject, value.lastMessageAt, value.updatedAt, json(value), value.id, json(expected)]) : bind(this.database.prepare( `INSERT OR IGNORE INTO mailfn_threads(id, project_id, inbox_id, normalized_subject, last_message_at, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, ), [value.id, value.projectId, value.inboxId, value.normalizedSubject, value.lastMessageAt, value.createdAt, value.updatedAt, json(value)]); const result = await statement.run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async restoreThreadIfUnchanged(value: Thread, previous: Thread | null): Promise { const statement = previous ? bind(this.database.prepare( `UPDATE mailfn_threads SET normalized_subject = ?, last_message_at = ?, updated_at = ?, data_json = ? WHERE id = ? AND data_json = ?`, ), [previous.normalizedSubject, previous.lastMessageAt, previous.updatedAt, json(previous), value.id, json(value)]) : bind(this.database.prepare('DELETE FROM mailfn_threads WHERE id = ? AND data_json = ?'), [value.id, json(value)]); const result = await statement.run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async deleteMessageWithThread(messageId: string, expected: Thread, next: Thread | null): Promise { const expectedJson = json(expected); const threadStatement = next ? bind(this.database.prepare( `UPDATE mailfn_threads SET normalized_subject = ?, last_message_at = ?, updated_at = ?, data_json = ? WHERE id = ? AND data_json = ?`, ), [next.normalizedSubject, next.lastMessageAt, next.updatedAt, json(next), expected.id, expectedJson]) : bind(this.database.prepare('DELETE FROM mailfn_threads WHERE id = ? AND data_json = ?'), [expected.id, expectedJson]); const results = await this.database.batch([ bind(this.database.prepare( `DELETE FROM mailfn_messages_fts WHERE message_id = ? AND EXISTS ( SELECT 1 FROM mailfn_threads WHERE id = ? AND data_json = ? )`, ), [messageId, expected.id, expectedJson]), bind(this.database.prepare( `DELETE FROM mailfn_messages WHERE id = ? AND EXISTS ( SELECT 1 FROM mailfn_threads WHERE id = ? AND data_json = ? )`, ), [messageId, expected.id, expectedJson]), threadStatement, ]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(results[1]?.meta?.changes ?? 0) > 0 && Number(results[2]?.meta?.changes ?? 0) > 0; } async getWebhook(id: string): Promise { return this.one('SELECT data_json FROM mailfn_webhooks WHERE id = ?', [id]); } async listWebhooks(projectId: string, inboxId?: string): Promise { return inboxId ? this.many('SELECT data_json FROM mailfn_webhooks WHERE project_id = ? AND inbox_id = ? ORDER BY created_at', [projectId, inboxId]) : this.many('SELECT data_json FROM mailfn_webhooks WHERE project_id = ? ORDER BY created_at', [projectId]); } async createWebhookWithQuota(value: Webhook, maxWebhooks: number): Promise { const result = await bind(this.database.prepare( `INSERT INTO mailfn_webhooks(id, project_id, inbox_id, url, event_types, secret_hash, status, created_at, updated_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE (SELECT COUNT(*) FROM mailfn_webhooks WHERE project_id = ? AND status = 'active') < ?`, ), [ value.id, value.projectId, value.inboxId ?? null, value.url, JSON.stringify(value.eventTypes), value.secretHash, value.status, value.createdAt, value.updatedAt, json(value), value.projectId, maxWebhooks, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async createWebhookWithQuotaAndAudit( value: Webhook, maxWebhooks: number, audit: AuditEvent, ): Promise { const results = await this.database.batch([ bind(this.database.prepare( `INSERT INTO mailfn_webhooks(id, project_id, inbox_id, url, event_types, secret_hash, status, created_at, updated_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE (SELECT COUNT(*) FROM mailfn_webhooks WHERE project_id = ? AND status = 'active') < ?`, ), [ value.id, value.projectId, value.inboxId ?? null, value.url, JSON.stringify(value.eventTypes), value.secretHash, value.status, value.createdAt, value.updatedAt, json(value), value.projectId, maxWebhooks, ]), bind(this.database.prepare( `INSERT INTO mailfn_audits( id, project_id, actor_type, actor_id, action, resource_type, resource_id, created_at, retention_expires_at, data_json ) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE changes() = 1 AND EXISTS (SELECT 1 FROM mailfn_webhooks WHERE id = ?)`, ), [ audit.id, audit.projectId, audit.actorType, audit.actorId, audit.action, audit.resourceType, audit.resourceId, audit.createdAt, audit.retentionExpiresAt, json(audit), value.id, ]), ]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); const created = Number(results[0]?.meta?.changes ?? 0) === 1; if (created && Number(results[1]?.meta?.changes ?? 0) !== 1) throw new Error('MAILFN_D1_WRITE_FAILED'); return created; } async saveWebhook(value: Webhook): Promise { await this.run( `INSERT INTO mailfn_webhooks(id, project_id, inbox_id, url, event_types, secret_hash, status, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET url=excluded.url, event_types=excluded.event_types, secret_hash=excluded.secret_hash, status=excluded.status, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.id, value.projectId, value.inboxId ?? null, value.url, JSON.stringify(value.eventTypes), value.secretHash, value.status, value.createdAt, value.updatedAt, json(value)], ); } async recordWebhookDeliveryResult(webhookId: string, succeeded: boolean, updatedAt: string): Promise { const success = succeeded ? 1 : 0; await this.run( `UPDATE mailfn_webhooks SET status = CASE WHEN ? = 0 AND COALESCE(CAST(json_extract(data_json, '$.consecutiveFailures') AS INTEGER), 0) + 1 >= 10 THEN 'quarantined' ELSE status END, updated_at = ?, data_json = json_set( data_json, '$.consecutiveFailures', CASE WHEN ? = 1 THEN 0 ELSE COALESCE(CAST(json_extract(data_json, '$.consecutiveFailures') AS INTEGER), 0) + 1 END, '$.status', CASE WHEN ? = 0 AND COALESCE(CAST(json_extract(data_json, '$.consecutiveFailures') AS INTEGER), 0) + 1 >= 10 THEN 'quarantined' ELSE status END, '$.updatedAt', ? ) WHERE id = ? AND status = 'active'`, [success, updatedAt, success, success, updatedAt, webhookId], ); } async saveWebhookDelivery(value: WebhookDelivery): Promise { await this.run( `INSERT INTO mailfn_webhook_deliveries(id, webhook_id, event_id, attempt, status, next_attempt_at, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET attempt=excluded.attempt, status=excluded.status, next_attempt_at=excluded.next_attempt_at, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.id, value.webhookId, value.eventId, value.attempt, value.status, value.nextAttemptAt ?? null, value.createdAt, value.updatedAt, json(value)], ); } async claimWebhookDelivery( deliveryId: string, expectedStatus: WebhookDelivery['status'], expectedUpdatedAt: string, value: WebhookDelivery, ): Promise { const result = await bind(this.database.prepare( `UPDATE mailfn_webhook_deliveries SET attempt = ?, status = ?, next_attempt_at = ?, updated_at = ?, data_json = ? WHERE id = ? AND status = ? AND updated_at = ?`, ), [ value.attempt, value.status, value.nextAttemptAt ?? null, value.updatedAt, json(value), deliveryId, expectedStatus, expectedUpdatedAt, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async getWebhookDelivery(id: string): Promise { return this.one('SELECT data_json FROM mailfn_webhook_deliveries WHERE id = ?', [id]); } async listWebhookDeliveries(webhookId: string): Promise { return this.many('SELECT data_json FROM mailfn_webhook_deliveries WHERE webhook_id = ? ORDER BY created_at', [webhookId]); } async getDraft(id: string): Promise { return this.one('SELECT data_json FROM mailfn_drafts WHERE id = ?', [id]); } async listDrafts(projectId: string, inboxId: string): Promise { return this.many('SELECT data_json FROM mailfn_drafts WHERE project_id = ? AND inbox_id = ? ORDER BY created_at', [projectId, inboxId]); } async saveDraft(value: Draft): Promise { await this.run( `INSERT INTO mailfn_drafts(id, project_id, inbox_id, thread_id, status, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET thread_id=excluded.thread_id, status=excluded.status, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.id, value.projectId, value.inboxId, value.threadId ?? null, value.status, value.createdAt, value.updatedAt, json(value)], ); } async saveDraftIfInboxWritable(value: Draft, expected?: Draft): Promise { const result = expected ? await bind(this.database.prepare( `UPDATE mailfn_drafts SET thread_id = ?, status = ?, updated_at = ?, data_json = ? WHERE id = ? AND data_json = ? AND EXISTS ( SELECT 1 FROM mailfn_inboxes WHERE id = ? AND project_id = ? AND status NOT IN ('deleting', 'deleted') )`, ), [ value.threadId ?? null, value.status, value.updatedAt, json(value), value.id, json(expected), value.inboxId, value.projectId, ]).run() : await bind(this.database.prepare( `INSERT INTO mailfn_drafts(id, project_id, inbox_id, thread_id, status, created_at, updated_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ? WHERE EXISTS ( SELECT 1 FROM mailfn_inboxes WHERE id = ? AND project_id = ? AND status NOT IN ('deleting', 'deleted') ) ON CONFLICT(id) DO UPDATE SET thread_id=excluded.thread_id, status=excluded.status, updated_at=excluded.updated_at, data_json=excluded.data_json`, ), [ value.id, value.projectId, value.inboxId, value.threadId ?? null, value.status, value.createdAt, value.updatedAt, json(value), value.inboxId, value.projectId, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async claimDraft(draftId: string, expected: Draft, value: Draft): Promise { const result = await bind( this.database.prepare( `UPDATE mailfn_drafts SET status = ?, updated_at = ?, data_json = ? WHERE id = ? AND data_json = ? AND EXISTS ( SELECT 1 FROM mailfn_inboxes WHERE id = ? AND project_id = ? AND status NOT IN ('deleting', 'deleted') )`, ), [value.status, value.updatedAt, json(value), draftId, json(expected), value.inboxId, value.projectId], ).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async completeDraftSend( draftId: string, providerMessageId: string, updatedAt: string, usage: UsageRecord, ): Promise<{ draft: Draft; committed: boolean } | null> { for (let attempt = 0; attempt < 10; attempt += 1) { const current = await this.getDraft(draftId); if (!current) { await this.appendUsage(usage); return null; } if (current.status === 'sent') { await this.appendUsage(usage); return { draft: current, committed: false }; } const draft: Draft = { ...current, status: 'sent', sendLeaseId: undefined, sendLeaseExpiresAt: undefined, providerMessageId, updatedAt, }; const results = await this.database.batch([ bind(this.database.prepare( `UPDATE mailfn_drafts SET status = ?, updated_at = ?, data_json = ? WHERE id = ? AND data_json = ?`, ), [draft.status, draft.updatedAt, json(draft), draftId, json(current)]), bind(this.database.prepare( `INSERT OR IGNORE INTO mailfn_usage (id, project_id, metric, quantity, period, created_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?)`, ), [ usage.id, usage.projectId, usage.metric, usage.quantity, usage.period, usage.createdAt, json(usage), ]), ]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); if (Number(results[0]?.meta?.changes ?? 0) === 1) return { draft, committed: true }; } throw new Error('MAILFN_DRAFT_COMPLETION_CONFLICT'); } async deleteDrafts(projectId: string, inboxId: string): Promise { await this.run('DELETE FROM mailfn_drafts WHERE project_id = ? AND inbox_id = ?', [projectId, inboxId]); } async getDomain(id: string): Promise { return this.one('SELECT data_json FROM mailfn_domains WHERE id = ?', [id]); } async getDomainByName(projectId: string, domain: string): Promise { return this.one('SELECT data_json FROM mailfn_domains WHERE project_id = ? AND domain = ?', [projectId, domain]); } async getDomainByNameAcrossProjects(domain: string): Promise { return this.one('SELECT data_json FROM mailfn_domains WHERE domain = ?', [domain]); } async listDomains(projectId: string): Promise { return this.many('SELECT data_json FROM mailfn_domains WHERE project_id = ? ORDER BY created_at', [projectId]); } async createDomain(value: MailDomain): Promise { const result = await bind(this.database.prepare( `INSERT OR IGNORE INTO mailfn_domains(id, project_id, domain, status, verification_token, verified_at, created_at, updated_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM mailfn_domains WHERE domain = ?)`, ), [value.id, value.projectId, value.domain, value.status, value.verificationToken, value.verifiedAt ?? null, value.createdAt, value.updatedAt, json(value), value.domain]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async createDomainWithQuota(value: MailDomain, maxDomains: number): Promise { const result = await bind(this.database.prepare( `INSERT OR IGNORE INTO mailfn_domains(id, project_id, domain, status, verification_token, verified_at, created_at, updated_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM mailfn_domains WHERE domain = ?) AND (SELECT COUNT(*) FROM mailfn_domains WHERE project_id = ?) < ?`, ), [ value.id, value.projectId, value.domain, value.status, value.verificationToken, value.verifiedAt ?? null, value.createdAt, value.updatedAt, json(value), value.domain, value.projectId, maxDomains, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async saveDomain(value: MailDomain): Promise { await this.run( `INSERT INTO mailfn_domains(id, project_id, domain, status, verification_token, verified_at, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET status=excluded.status, verification_token=excluded.verification_token, verified_at=excluded.verified_at, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.id, value.projectId, value.domain, value.status, value.verificationToken, value.verifiedAt ?? null, value.createdAt, value.updatedAt, json(value)], ); } async saveDomainIfUnchanged(value: MailDomain, expected: MailDomain): Promise { const result = await bind(this.database.prepare( `UPDATE mailfn_domains SET status = ?, verification_token = ?, verified_at = ?, updated_at = ?, data_json = ? WHERE id = ? AND data_json = ?`, ), [ value.status, value.verificationToken, value.verifiedAt ?? null, value.updatedAt, json(value), value.id, json(expected), ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async getEvent(id: string): Promise { return this.one('SELECT data_json FROM mailfn_events WHERE id = ?', [id]); } async appendEvent(value: MailFnEvent): Promise { await this.run( `INSERT OR IGNORE INTO mailfn_events(id, project_id, inbox_id, message_id, type, version, occurred_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, [value.id, value.projectId, value.inboxId ?? null, value.messageId ?? null, value.type, value.version, value.occurredAt, json(value)], ); } async appendEventWithDeliveries(value: MailFnEvent, deliveries: WebhookDelivery[]): Promise { const statements = [bind(this.database.prepare( `INSERT INTO mailfn_events(id, project_id, inbox_id, message_id, type, version, occurred_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, ), [value.id, value.projectId, value.inboxId ?? null, value.messageId ?? null, value.type, value.version, value.occurredAt, json(value)])]; statements.push(...deliveries.map((delivery) => bind(this.database.prepare( `INSERT INTO mailfn_webhook_deliveries(id, webhook_id, event_id, attempt, status, next_attempt_at, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, ), [ delivery.id, delivery.webhookId, delivery.eventId, delivery.attempt, delivery.status, delivery.nextAttemptAt ?? null, delivery.createdAt, delivery.updatedAt, json(delivery), ]))); const results = await this.database.batch(statements); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); } async listEvents(projectId: string, after?: string): Promise { return after ? this.many('SELECT data_json FROM mailfn_events WHERE project_id = ? AND occurred_at > ? ORDER BY occurred_at', [projectId, after]) : this.many('SELECT data_json FROM mailfn_events WHERE project_id = ? ORDER BY occurred_at', [projectId]); } async deleteTerminalWebhookDeliveriesBefore(projectId: string, before: string): Promise { const ids = await this.rawMany<{ id: string }>( `SELECT delivery.id FROM mailfn_webhook_deliveries AS delivery JOIN mailfn_webhooks AS webhook ON webhook.id = delivery.webhook_id WHERE webhook.project_id = ? AND (delivery.status IN ('delivered', 'dead_letter') OR (delivery.status = 'failed' AND webhook.status != 'active')) AND delivery.updated_at <= ?`, [projectId, before], ); if (ids.length) await this.database.batch(ids.map(({ id }) => this.database.prepare('DELETE FROM mailfn_webhook_deliveries WHERE id = ?').bind(id))); return ids.length; } async deleteEventsBefore(projectId: string, before: string): Promise { const ids = await this.rawMany<{ id: string }>( `SELECT event.id FROM mailfn_events AS event WHERE event.project_id = ? AND event.occurred_at <= ? AND NOT EXISTS ( SELECT 1 FROM mailfn_webhook_deliveries AS delivery WHERE delivery.event_id = event.id AND delivery.status IN ('pending', 'failed') )`, [projectId, before], ); if (ids.length) await this.database.batch(ids.map(({ id }) => this.database.prepare('DELETE FROM mailfn_events WHERE id = ?').bind(id))); return ids.length; } async appendAudit(value: AuditEvent): Promise { const result = await auditInsert(this.database, value, true).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); } async listAudits(projectId: string, after?: string): Promise { return after ? this.many('SELECT data_json FROM mailfn_audits WHERE project_id = ? AND created_at > ? ORDER BY created_at', [projectId, after]) : this.many('SELECT data_json FROM mailfn_audits WHERE project_id = ? ORDER BY created_at', [projectId]); } async deleteExpiredAudits(projectId: string, now: string): Promise { const ids = await this.rawMany<{ id: string }>('SELECT id FROM mailfn_audits WHERE project_id = ? AND retention_expires_at <= ?', [projectId, now]); if (ids.length) await this.database.batch(ids.map(({ id }) => this.database.prepare('DELETE FROM mailfn_audits WHERE id = ?').bind(id))); return ids.length; } async getIdempotency(projectId: string, key: string): Promise { return this.one('SELECT data_json FROM mailfn_idempotency WHERE project_id = ? AND key = ?', [projectId, key]); } async createIdempotency(value: IdempotencyRecord): Promise { const result = await bind(this.database.prepare( `INSERT OR IGNORE INTO mailfn_idempotency(project_id, key, operation, resource_id, request_hash, expires_at, created_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, ), [ value.projectId, value.key, value.operation, value.resourceId, value.requestHash, value.expiresAt, value.createdAt, json(value), ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async saveIdempotency(value: IdempotencyRecord): Promise { await this.run( `INSERT INTO mailfn_idempotency(project_id, key, operation, resource_id, request_hash, expires_at, created_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(project_id, key) DO UPDATE SET operation=excluded.operation, resource_id=excluded.resource_id, request_hash=excluded.request_hash, expires_at=excluded.expires_at, data_json=excluded.data_json`, [value.projectId, value.key, value.operation, value.resourceId, value.requestHash, value.expiresAt, value.createdAt, json(value)], ); } async deleteExpiredIdempotency(projectId: string, key: string, now: string): Promise { await this.run('DELETE FROM mailfn_idempotency WHERE project_id = ? AND key = ? AND expires_at <= ?', [projectId, key, now]); } async reserveIngressQuota(reservation: IngressQuotaReservation): Promise { await this.run('DELETE FROM mailfn_ingress_reservations WHERE bucket < ?', [reservation.bucket]); const result = await bind(this.database.prepare( `INSERT INTO mailfn_ingress_reservations(id, project_id, inbox_id, sender, bucket, created_at) SELECT ?, ?, ?, ?, ?, ? WHERE (SELECT COUNT(*) FROM mailfn_ingress_reservations WHERE project_id = ? AND bucket = ?) < ? AND (SELECT COUNT(*) FROM mailfn_ingress_reservations WHERE project_id = ? AND inbox_id = ? AND bucket = ?) < ? AND (SELECT COUNT(*) FROM mailfn_ingress_reservations WHERE project_id = ? AND sender = ? AND bucket = ?) < ?`, ), [ reservation.id, reservation.projectId, reservation.inboxId, reservation.sender, reservation.bucket, reservation.createdAt, reservation.projectId, reservation.bucket, reservation.projectLimit, reservation.projectId, reservation.inboxId, reservation.bucket, reservation.inboxLimit, reservation.projectId, reservation.sender, reservation.bucket, reservation.senderLimit, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); if (Number(result.meta?.changes ?? 0) === 1) return { allowed: true }; const [counts] = await this.rawMany<{ project_count: number; inbox_count: number; sender_count: number }>( `SELECT SUM(CASE WHEN project_id = ? THEN 1 ELSE 0 END) AS project_count, SUM(CASE WHEN project_id = ? AND inbox_id = ? THEN 1 ELSE 0 END) AS inbox_count, SUM(CASE WHEN project_id = ? AND sender = ? THEN 1 ELSE 0 END) AS sender_count FROM mailfn_ingress_reservations WHERE bucket = ?`, [reservation.projectId, reservation.projectId, reservation.inboxId, reservation.projectId, reservation.sender, reservation.bucket], ); if (Number(counts?.project_count ?? 0) >= reservation.projectLimit) return { allowed: false, dimension: 'project' }; if (Number(counts?.inbox_count ?? 0) >= reservation.inboxLimit) return { allowed: false, dimension: 'inbox' }; return { allowed: false, dimension: 'sender' }; } async releaseIngressQuota(reservationId: string): Promise { await this.run('DELETE FROM mailfn_ingress_reservations WHERE id = ?', [reservationId]); } async reserveStorage( reservation: { id: string; projectId: string; bytes: number; createdAt: string }, limit: number, ): Promise<'created' | 'existing' | 'denied'> { if ((await this.rawMany<{ id: string }>('SELECT id FROM mailfn_storage_reservations WHERE id = ?', [reservation.id])).length) { return 'existing'; } const result = await bind(this.database.prepare( `INSERT INTO mailfn_storage_reservations(id, project_id, bytes, created_at) SELECT ?, ?, ?, ? WHERE COALESCE((SELECT SUM(bytes) FROM mailfn_storage_reservations WHERE project_id = ?), 0) + ? <= ?`, ), [reservation.id, reservation.projectId, reservation.bytes, reservation.createdAt, reservation.projectId, reservation.bytes, limit]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); if (Number(result.meta?.changes ?? 0) === 1) return 'created'; return (await this.rawMany<{ id: string }>('SELECT id FROM mailfn_storage_reservations WHERE id = ?', [reservation.id])).length ? 'existing' : 'denied'; } async releaseStorage(reservationId: string): Promise { const results = await this.database.batch([ bind(this.database.prepare('DELETE FROM mailfn_storage_claims WHERE id = ?'), [reservationId]), bind(this.database.prepare('DELETE FROM mailfn_storage_reservations WHERE id = ?'), [reservationId]), ]); if (results.some((result) => !result.success)) throw new Error('MAILFN_D1_WRITE_FAILED'); } async claimStorage(reservationId: string, claimedAt: string, objectKey?: string): Promise { const result = await bind(this.database.prepare( `INSERT INTO mailfn_storage_claims(id, claimed_at, object_key) SELECT id, ?, ? FROM mailfn_storage_reservations WHERE id = ? ON CONFLICT(id) DO UPDATE SET claimed_at = excluded.claimed_at, object_key = excluded.object_key`, ), [claimedAt, objectKey ?? null, reservationId]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async releaseStorageClaim(reservationId: string): Promise { await this.run('DELETE FROM mailfn_storage_claims WHERE id = ?', [reservationId]); } async releaseOrphanedStorageReservations( projectId: string, reservationBefore: string, claimBefore: string, deleteObject?: (objectKey: string) => Promise, ): Promise { const candidates = await this.rawMany<{ id: string; objectKey: string | null }>( `SELECT reservation.id, claim.object_key AS objectKey FROM mailfn_storage_reservations AS reservation LEFT JOIN mailfn_storage_claims AS claim ON claim.id = reservation.id WHERE reservation.project_id = ? AND reservation.created_at <= ? AND NOT EXISTS ( SELECT 1 FROM mailfn_storage_claims WHERE mailfn_storage_claims.id = reservation.id AND mailfn_storage_claims.claimed_at > ? ) AND NOT EXISTS ( SELECT 1 FROM mailfn_messages WHERE mailfn_messages.id = reservation.id ) AND NOT EXISTS ( SELECT 1 FROM mailfn_attachments WHERE COALESCE( json_extract(mailfn_attachments.data_json, '$.storageReservationId'), mailfn_attachments.id ) = reservation.id )`, [projectId, reservationBefore, claimBefore], ); let released = 0; for (const candidate of candidates) { if (candidate.objectKey) { if (!deleteObject) continue; try { await deleteObject(candidate.objectKey); } catch { continue; } } await this.releaseStorage(candidate.id); released += 1; } return released; } async appendUsage(value: UsageRecord): Promise { await this.run( 'INSERT OR IGNORE INTO mailfn_usage(id, project_id, metric, quantity, period, created_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?)', [value.id, value.projectId, value.metric, value.quantity, value.period, value.createdAt, json(value)], ); } async reserveOutboundUsage(value: UsageRecord, limit: number): Promise<'created' | 'existing' | 'denied'> { if (await this.one('SELECT data_json FROM mailfn_usage WHERE id = ?', [value.id])) return 'existing'; const result = await bind(this.database.prepare( `INSERT INTO mailfn_usage(id, project_id, metric, quantity, period, created_at, data_json) SELECT ?, ?, ?, ?, ?, ?, ? WHERE COALESCE((SELECT SUM(quantity) FROM mailfn_usage WHERE project_id = ? AND period = ? AND metric = 'outbound_message'), 0) + ? <= ?`, ), [ value.id, value.projectId, value.metric, value.quantity, value.period, value.createdAt, json(value), value.projectId, value.period, value.quantity, limit, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); if (Number(result.meta?.changes ?? 0) === 1) return 'created'; return await this.one('SELECT data_json FROM mailfn_usage WHERE id = ?', [value.id]) ? 'existing' : 'denied'; } async releaseUsage(id: string): Promise { await this.run('DELETE FROM mailfn_usage WHERE id = ?', [id]); } async releaseOutboundUsageIfDraftNotSent(draftId: string, usageId: string): Promise { const result = await bind(this.database.prepare( `DELETE FROM mailfn_usage WHERE id = ? AND NOT EXISTS ( SELECT 1 FROM mailfn_drafts WHERE id = ? AND status = 'sent' )`, ), [usageId, draftId]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } async listUsage(projectId: string, period?: string): Promise { return period ? this.many('SELECT data_json FROM mailfn_usage WHERE project_id = ? AND period = ? ORDER BY created_at', [projectId, period]) : this.many('SELECT data_json FROM mailfn_usage WHERE project_id = ? ORDER BY created_at', [projectId]); } async saveAbuseCase(value: AbuseCase): Promise { await this.run( `INSERT INTO mailfn_abuse_cases(id, project_id, kind, status, resource_type, resource_id, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET status=excluded.status, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.id, value.projectId, value.kind, value.status, value.resourceType, value.resourceId, value.createdAt, value.updatedAt, json(value)], ); } async listAbuseCases(projectId: string): Promise { return this.many('SELECT data_json FROM mailfn_abuse_cases WHERE project_id = ? ORDER BY created_at', [projectId]); } async getSenderReputation(projectId: string, sender: string): Promise { return this.one('SELECT data_json FROM mailfn_sender_reputation WHERE project_id = ? AND sender = ?', [projectId, sender]); } async listSenderReputations(projectId: string): Promise { return this.many('SELECT data_json FROM mailfn_sender_reputation WHERE project_id = ? ORDER BY score, sender', [projectId]); } async saveSenderReputation(value: SenderReputation): Promise { await this.run( `INSERT INTO mailfn_sender_reputation(project_id, sender, status, score, complaint_count, bounce_count, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(project_id, sender) DO UPDATE SET status=excluded.status, score=excluded.score, complaint_count=excluded.complaint_count, bounce_count=excluded.bounce_count, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.projectId, value.sender, value.status, value.score, value.complaintCount, value.bounceCount, value.updatedAt, json(value)], ); } async applySenderReputationSignal( signal: Parameters[0], ): Promise { for (let attempt = 0; attempt < 10; attempt += 1) { const existing = await this.getSenderReputation(signal.projectId, signal.sender); const score = Math.max(0, (existing?.score ?? 100) - signal.penalty); const value: SenderReputation = { projectId: signal.projectId, sender: signal.sender, status: signal.forceBlock || score <= 20 ? 'block' : score < 80 ? 'monitor' : 'allow', score, complaintCount: (existing?.complaintCount ?? 0) + signal.complaintIncrement, bounceCount: (existing?.bounceCount ?? 0) + signal.bounceIncrement, reason: signal.reason, updatedAt: signal.updatedAt, }; const result = existing ? await bind(this.database.prepare( `UPDATE mailfn_sender_reputation SET status = ?, score = ?, complaint_count = ?, bounce_count = ?, updated_at = ?, data_json = ? WHERE project_id = ? AND sender = ? AND data_json = ?`, ), [ value.status, value.score, value.complaintCount, value.bounceCount, value.updatedAt, json(value), value.projectId, value.sender, json(existing), ]).run() : await bind(this.database.prepare( `INSERT OR IGNORE INTO mailfn_sender_reputation (project_id, sender, status, score, complaint_count, bounce_count, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, ), [ value.projectId, value.sender, value.status, value.score, value.complaintCount, value.bounceCount, value.updatedAt, json(value), ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); if (Number(result.meta?.changes ?? 0) === 1) return; } throw new Error('MAILFN_REPUTATION_CONFLICT'); } async saveSupportCase(value: SupportCase): Promise { await this.run( `INSERT INTO mailfn_support_cases(id, project_id, severity, status, created_at, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET severity=excluded.severity, status=excluded.status, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.id, value.projectId, value.severity, value.status, value.createdAt, value.updatedAt, json(value)], ); } async listSupportCases(projectId: string): Promise { return this.many('SELECT data_json FROM mailfn_support_cases WHERE project_id = ? ORDER BY created_at', [projectId]); } async getComplianceProfile(projectId: string): Promise { return this.one('SELECT data_json FROM mailfn_compliance WHERE project_id = ?', [projectId]); } async saveComplianceProfile(value: ComplianceProfile): Promise { await this.run( `INSERT INTO mailfn_compliance(project_id, data_region, retention_locked, deletion_sla_hours, updated_at, data_json) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT(project_id) DO UPDATE SET data_region=excluded.data_region, retention_locked=excluded.retention_locked, deletion_sla_hours=excluded.deletion_sla_hours, updated_at=excluded.updated_at, data_json=excluded.data_json`, [value.projectId, value.dataRegion, value.retentionLocked ? 1 : 0, value.deletionSlaHours, value.updatedAt, json(value)], ); } async saveComplianceProfileIfNoDeletion(value: ComplianceProfile): Promise { const result = await bind(this.database.prepare( `INSERT INTO mailfn_compliance(project_id, data_region, retention_locked, deletion_sla_hours, updated_at, data_json) SELECT ?, ?, ?, ?, ?, ? WHERE NOT EXISTS ( SELECT 1 FROM mailfn_inboxes WHERE project_id = ? AND status = 'deleting' ) ON CONFLICT(project_id) DO UPDATE SET data_region=excluded.data_region, retention_locked=excluded.retention_locked, deletion_sla_hours=excluded.deletion_sla_hours, updated_at=excluded.updated_at, data_json=excluded.data_json WHERE NOT EXISTS ( SELECT 1 FROM mailfn_inboxes WHERE project_id = excluded.project_id AND status = 'deleting' )`, ), [ value.projectId, value.dataRegion, value.retentionLocked ? 1 : 0, value.deletionSlaHours, value.updatedAt, json(value), value.projectId, ]).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); return Number(result.meta?.changes ?? 0) === 1; } private async one(query: string, values: unknown[]): Promise { const row = await bind(this.database.prepare(query), values).first(); return row ? parse(row.data_json) : null; } private async many(query: string, values: unknown[]): Promise { const rows = await this.rawMany(query, values); return rows.map((row) => parse(row.data_json)); } private async listProjectPage( table: 'mailfn_messages' | 'mailfn_attachments', projectId: string, input: MailFnStorePageInput, allowedFields: ReadonlySet, sortExpressions: Readonly>, ): Promise<{ items: T[]; hasMore: boolean }> { const clauses = [ `${table}.project_id = ?`, `NOT EXISTS ( SELECT 1 FROM mailfn_inboxes AS admin_inbox WHERE admin_inbox.id = ${table}.inbox_id AND admin_inbox.status = 'deleted' )`, ]; const values: unknown[] = [projectId]; const search = input.search?.trim().toLowerCase(); if (search) { clauses.push(`instr(lower(${table}.data_json), ?) > 0`); values.push(search); } for (const [field, expected] of Object.entries(input.filter ?? {})) { if (!allowedFields.has(field)) throw new Error('MAILFN_D1_INVALID_LIST_FIELD'); if (table === 'mailfn_messages') { if (field === 'sender') { clauses.push(`(lower(mailfn_messages.envelope_from) = ? OR EXISTS ( SELECT 1 FROM json_each(mailfn_messages.data_json, '$.from') AS sender_entry WHERE lower(json_extract(sender_entry.value, '$.address')) = ? ))`); values.push(String(expected).toLowerCase(), String(expected).toLowerCase()); continue; } if (field === 'senderDomain') { const suffix = `@${String(expected).toLowerCase()}`; clauses.push(`EXISTS ( SELECT 1 FROM json_each(mailfn_messages.data_json, '$.from') AS sender_entry WHERE substr(lower(json_extract(sender_entry.value, '$.address')), -length(?)) = ? )`); values.push(suffix, suffix); continue; } if (field === 'recipient') { clauses.push('lower(mailfn_messages.envelope_to) = ?'); values.push(String(expected).toLowerCase()); continue; } if (field === 'subject') { clauses.push('instr(lower(mailfn_messages.subject), ?) > 0'); values.push(String(expected).toLowerCase()); continue; } if (field === 'text') { clauses.push(`instr(lower(coalesce(json_extract(mailfn_messages.data_json, '$.textBody'), '') || char(10) || coalesce(json_extract(mailfn_messages.data_json, '$.htmlBody'), '')), ?) > 0`); values.push(String(expected).toLowerCase()); continue; } if (field === 'receivedAfter' || field === 'receivedBefore') { clauses.push(`julianday(mailfn_messages.received_at) ${field === 'receivedAfter' ? '>' : '<'} julianday(?)`); values.push(normalizeInstant(String(expected))); continue; } if (field === 'unreadOnly') { if (expected) clauses.push(`json_type(mailfn_messages.data_json, '$.readAt') IS NULL`); continue; } if (field === 'threadId' || field === 'status') { clauses.push(`mailfn_messages.${field === 'threadId' ? 'thread_id' : 'status'} = ?`); values.push(expected); continue; } if (field === 'labels') { for (const label of Array.isArray(expected) ? expected : []) { clauses.push(`EXISTS ( SELECT 1 FROM json_each(mailfn_messages.data_json, '$.labels') AS label_entry WHERE label_entry.value = ? )`); values.push(label); } continue; } } const path = `$.${field}`; if (expected !== null && typeof expected === 'object') { clauses.push(`json(json_extract(${table}.data_json, ?)) = json(?)`); values.push(path, JSON.stringify(expected)); } else { clauses.push(`json_extract(${table}.data_json, ?) IS ?`); values.push(path, typeof expected === 'boolean' ? Number(expected) : expected); } } const order = (input.sort ?? []).map((descriptor) => { const expression = sortExpressions[descriptor.field]; if (!expression) throw new Error('MAILFN_D1_INVALID_LIST_FIELD'); return `${expression} ${descriptor.direction === 'desc' ? 'DESC' : 'ASC'}`; }); if (!(input.sort ?? []).some((descriptor) => descriptor.field === 'id')) { order.push(`${table}.id ASC`); } const selected = await this.many( `SELECT ${table}.data_json FROM ${table} WHERE ${clauses.join(' AND ')} ORDER BY ${order.join(', ')} LIMIT ? OFFSET ?`, [...values, input.limit + 1, input.offset], ); return { items: selected.slice(0, input.limit), hasMore: selected.length > input.limit }; } private async rawMany(query: string, values: unknown[]): Promise { const result = await bind(this.database.prepare(query), values).all(); if (!result.success) throw new Error('MAILFN_D1_QUERY_FAILED'); return result.results ?? []; } private async run(query: string, values: unknown[]): Promise { const result = await bind(this.database.prepare(query), values).run(); if (!result.success) throw new Error('MAILFN_D1_WRITE_FAILED'); } } function bind(statement: D1PreparedStatement, values: unknown[]): D1PreparedStatement { return values.length ? statement.bind(...values) : statement; } function credentialInsert(database: D1Database, value: Credential): D1PreparedStatement { return bind(database.prepare( `INSERT INTO mailfn_credentials(id, project_id, inbox_id, token_hash, token_prefix, permissions, status, expires_at, revoked_at, created_at, data_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ), [value.id, value.projectId, value.inboxId ?? null, value.tokenHash, value.tokenPrefix, JSON.stringify(value.permissions), value.status, value.expiresAt ?? null, value.revokedAt ?? null, value.createdAt, json(value)]); } function auditInsert(database: D1Database, value: AuditEvent, ignoreDuplicate = false): D1PreparedStatement { return bind(database.prepare( `INSERT ${ignoreDuplicate ? 'OR IGNORE ' : ''}INTO mailfn_audits( id, project_id, actor_type, actor_id, action, resource_type, resource_id, created_at, retention_expires_at, data_json ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ), [ value.id, value.projectId, value.actorType, value.actorId, value.action, value.resourceType, value.resourceId, value.createdAt, value.retentionExpiresAt, json(value), ]); } function json(value: unknown): string { return JSON.stringify(value); } function parse(value: string): T { return JSON.parse(value) as T; } function normalizeInstant(value: string): string { const instant = Date.parse(value); return Number.isNaN(instant) ? value : new Date(instant).toISOString(); } function ftsPhrase(value: string): string { return `"${value.replaceAll('"', '""')}"`; } function matchesMessage(message: Message, filter: MessageFilter): boolean { if (filter.sender) { const sender = filter.sender.toLowerCase(); if (message.envelopeFrom.toLowerCase() !== sender && !message.from.some((entry) => entry.address.toLowerCase() === sender)) return false; } if (filter.senderDomain && !message.from.some((entry) => entry.address.toLowerCase().endsWith(`@${filter.senderDomain!.toLowerCase()}`))) return false; if (filter.recipient && message.envelopeTo.toLowerCase() !== filter.recipient.toLowerCase()) return false; if (filter.subject && !message.subject.toLowerCase().includes(filter.subject.toLowerCase())) return false; if (filter.text && !`${message.textBody ?? ''}\n${message.htmlBody ?? ''}`.toLowerCase().includes(filter.text.toLowerCase())) return false; const receivedAt = Date.parse(message.receivedAt); if (filter.receivedAfter && receivedAt <= Date.parse(filter.receivedAfter)) return false; if (filter.receivedBefore && receivedAt >= Date.parse(filter.receivedBefore)) return false; if (filter.unreadOnly && message.readAt) return false; if (filter.threadId && message.threadId !== filter.threadId) return false; if (filter.labels?.some((label) => !message.labels.includes(label))) return false; if (filter.status && message.status !== filter.status) return false; return true; }