mirror of
https://github.com/bulwarkmail/webmail.git
synced 2026-11-04 16:14:07 +00:00
The IndexedDB cache, its crypto, the responder and the page-side sync go. lib/offline keeps its structure, policy, wipes, sign-out gates and notices; answers, the outbox and sync now live in lib/store, in a worker behind lib/store/client/api.ts. What users get besides that: - every folder is kept, not just the Inbox and opened ones - delete for good and empty trash work offline - a change the server refuses, or that meets a message changed on another device, is shown instead of dropped - a send whose response got lost is looked up by a Message-ID fixed when it was queued, then finished, submitted or sent again - with the store on, a start paints from the local copy, then connects An account the app drops by itself no longer wipes its data: only a sign-out or an explicit removal does. The outbox of the old database moves over once, in lib/offline/migrate-legacy.ts, and the old database goes only after the store committed. `isNetworkFailure` now counts timeouts and proxy 5xx. Sends and outbox requests are never answered from the device. Tests moved with the code: cache.test.ts and responder.test.ts are replaced by lib/store/__tests__/, and review-fixes.test.ts, outbox.test.ts and client-fallback.test.ts now run the real engine on node:sqlite. Changed assertions: an uncertain send is looked up rather than left alone forever, destroy and empty trash are queued rather than refused, and #9 (opened folders) became "a folder the server deleted goes", since all folders are kept.
92 lines
4.0 KiB
TypeScript
92 lines
4.0 KiB
TypeScript
/**
|
|
* What waits on this device for the network (#523): the page's view of the
|
|
* outbox in the local store (lib/store/outbox). Changes made offline wait there
|
|
* merged per record; messages wait as finished sends. Everything is kept
|
|
* encrypted in the store, and taking an entry for sending is one SQLite
|
|
* transaction, so two tabs can't both take it (claimForSending).
|
|
*/
|
|
|
|
import { requestOutboxSync } from '@/lib/pwa-notify/outbox-sync';
|
|
import { getStore } from '@/lib/store/client/api';
|
|
import type { ConflictSummary, OutboxError, OutboxStatus, OutboxSummary, PolicyFingerprint, SendItem, StoredConflict, StoredOutboxEntry } from '@/lib/store/outbox/items';
|
|
import { useOfflineStore } from './state';
|
|
|
|
export { LOCAL_BLOB_PREFIX, heldBlobIds, isLocalBlobId } from '@/lib/store/outbox/items';
|
|
export type { EmailSendItem, OutboxError, OutboxStatus, OutboxSummary, PolicyFingerprint, RawSendItem, SendItem } from '@/lib/store/outbox/items';
|
|
|
|
export interface OutboxEntry extends StoredOutboxEntry {
|
|
account: string;
|
|
}
|
|
|
|
export interface ConflictEntry {
|
|
id: number;
|
|
account: string;
|
|
recordId: string;
|
|
createdAt: number;
|
|
summary: ConflictSummary;
|
|
}
|
|
|
|
export async function queueSend(account: string, summary: OutboxSummary, item: SendItem, fingerprint?: PolicyFingerprint, messageId?: string): Promise<number> {
|
|
const id = await getStore().area<number>(account, 'outbox.queueSend', { summary, item, fingerprint, messageId });
|
|
requestOutboxSync();
|
|
return id;
|
|
}
|
|
|
|
interface StoredQueue {
|
|
patches: number;
|
|
entries: StoredOutboxEntry[];
|
|
conflicts: StoredConflict[];
|
|
}
|
|
|
|
/** Everything waiting for one account (or every account with it on), oldest first. */
|
|
export async function readQueue(account?: string): Promise<{ patches: number; outbox: OutboxEntry[]; conflicts: ConflictEntry[] }> {
|
|
const accounts = account ? [account] : useOfflineStore.getState().enabled;
|
|
const read = await Promise.all(accounts.map(async (id) => ({ id, queue: await getStore().area<StoredQueue>(id, 'outbox.read') })));
|
|
return {
|
|
patches: read.reduce((sum, { queue }) => sum + queue.patches, 0),
|
|
outbox: read.flatMap(({ id, queue }) => queue.entries.map((entry) => ({ ...entry, account: id }))),
|
|
conflicts: read.flatMap(({ id, queue }) => queue.conflicts.map((conflict) => ({
|
|
id: conflict.id,
|
|
account: id,
|
|
recordId: conflict.recordId,
|
|
createdAt: conflict.createdAt,
|
|
summary: conflict.summary,
|
|
}))),
|
|
};
|
|
}
|
|
|
|
/** Takes a waiting entry for sending: false when it isn't waiting any more (another tab took it, the user deleted it). */
|
|
export function claimForSending(account: string, id: number): Promise<boolean> {
|
|
return getStore().area<boolean>(account, 'outbox.claim', { id });
|
|
}
|
|
|
|
export function updateOutboxEntry(
|
|
entry: Pick<OutboxEntry, 'account' | 'id'>,
|
|
patch: { status: OutboxStatus; error?: OutboxError; copyId?: string | null },
|
|
): Promise<void> {
|
|
return getStore().area(entry.account, 'outbox.update', { id: entry.id, ...patch });
|
|
}
|
|
|
|
/** Removes an outbox entry and the files it held. */
|
|
export function removeOutboxEntry(entry: Pick<OutboxEntry, 'account' | 'id'>): Promise<void> {
|
|
return getStore().area(entry.account, 'outbox.remove', { id: entry.id });
|
|
}
|
|
|
|
export function dismissConflict(account: string, id: number): Promise<void> {
|
|
return getStore().area(account, 'outbox.dismissConflict', { id });
|
|
}
|
|
|
|
// ---- Files attached while offline ----
|
|
|
|
/** Keeps a file under a stand-in blob id until its message is sent. */
|
|
export async function holdFile(account: string, file: Blob, name: string, jmap?: string): Promise<{ blobId: string; size: number; type: string }> {
|
|
const data = new Uint8Array(await file.arrayBuffer());
|
|
return getStore().area(account, 'outbox.holdFile', { jmap, name, type: file.type, data });
|
|
}
|
|
|
|
export async function heldFile(account: string, blobId: string): Promise<File | null> {
|
|
const held = await getStore().area<{ name: string; type: string; data: Uint8Array } | null>(account, 'outbox.heldFile', { blobId });
|
|
if (!held) return null;
|
|
return new File([held.data as BlobPart], held.name, { type: held.type });
|
|
}
|