@bobfrankston/rmfmail 1.1.4 → 1.1.5

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.
Files changed (57) hide show
  1. package/bin/build-bundles.mjs +14 -4
  2. package/bin/mailx.js +8 -3
  3. package/bin/mailx.js.map +1 -1
  4. package/bin/mailx.ts +8 -3
  5. package/client/android-bootstrap.bundle.js +2151 -2
  6. package/client/android-bootstrap.bundle.js.map +4 -4
  7. package/package.json +1 -1
  8. package/packages/mailx-api/index.d.ts +2 -2
  9. package/packages/mailx-api/index.d.ts.map +1 -1
  10. package/packages/mailx-api/index.js +2 -2
  11. package/packages/mailx-api/index.js.map +1 -1
  12. package/packages/mailx-api/index.ts +3 -3
  13. package/packages/mailx-core/index.d.ts.map +1 -1
  14. package/packages/mailx-core/index.js +3 -2
  15. package/packages/mailx-core/index.js.map +1 -1
  16. package/packages/mailx-core/index.ts +3 -2
  17. package/packages/mailx-imap/index.d.ts +13 -4
  18. package/packages/mailx-imap/index.d.ts.map +1 -1
  19. package/packages/mailx-imap/index.js +16 -8
  20. package/packages/mailx-imap/index.js.map +1 -1
  21. package/packages/mailx-imap/index.ts +15 -7
  22. package/packages/mailx-imap/package-lock.json +2 -2
  23. package/packages/mailx-imap/package.json +1 -1
  24. package/packages/mailx-server/index.d.ts.map +1 -1
  25. package/packages/mailx-server/index.js +4 -3
  26. package/packages/mailx-server/index.js.map +1 -1
  27. package/packages/mailx-server/index.ts +4 -3
  28. package/packages/mailx-service/db-worker.js +3 -4
  29. package/packages/mailx-service/db-worker.js.map +1 -1
  30. package/packages/mailx-service/db-worker.ts +5 -6
  31. package/packages/mailx-service/index.d.ts +20 -3
  32. package/packages/mailx-service/index.d.ts.map +1 -1
  33. package/packages/mailx-service/index.js +19 -17
  34. package/packages/mailx-service/index.js.map +1 -1
  35. package/packages/mailx-service/index.ts +18 -17
  36. package/packages/mailx-service/local-store.d.ts +7 -144
  37. package/packages/mailx-service/local-store.d.ts.map +1 -1
  38. package/packages/mailx-service/local-store.js +6 -511
  39. package/packages/mailx-service/local-store.js.map +1 -1
  40. package/packages/mailx-service/local-store.ts +7 -551
  41. package/packages/mailx-store/charset.d.ts +15 -0
  42. package/packages/mailx-store/charset.d.ts.map +1 -0
  43. package/packages/mailx-store/charset.js +61 -0
  44. package/packages/mailx-store/charset.js.map +1 -0
  45. package/packages/mailx-store/charset.ts +45 -0
  46. package/packages/mailx-store/index.d.ts +2 -0
  47. package/packages/mailx-store/index.d.ts.map +1 -1
  48. package/packages/mailx-store/index.js +2 -0
  49. package/packages/mailx-store/index.js.map +1 -1
  50. package/packages/mailx-store/index.ts +4 -0
  51. package/packages/mailx-store/package.json +1 -1
  52. package/packages/mailx-store/store.d.ts +169 -0
  53. package/packages/mailx-store/store.d.ts.map +1 -0
  54. package/packages/mailx-store/store.js +528 -0
  55. package/packages/mailx-store/store.js.map +1 -0
  56. package/packages/mailx-store/store.ts +567 -0
  57. /package/packages/mailx-imap/{node_modules.npmglobalize-stash-39436 → node_modules.npmglobalize-stash-11408}/.package-lock.json +0 -0
@@ -0,0 +1,567 @@
1
+ /**
2
+ * Store — the nexus. Owns the local database, the .eml file store, the
3
+ * operations API, and the event bus. Single source of truth for everything
4
+ * about a mailx account that lives on this device.
5
+ *
6
+ * UI ──┐ ┌── Sync clients (IMAP, Gmail API, …)
7
+ * │ reads / writes / subscribes │ read pending actions, commit
8
+ * ↓ ↓ server-discovered changes
9
+ * [ Store ]
10
+ *
11
+ * Contract: every method here mutates local-only state. No IMAP, no Gmail,
12
+ * no SMTP, no DNS, no Drive API. Mutations emit on the bus; subscribers
13
+ * (UI in another process, in-process reconciler, etc.) react. Sync clients
14
+ * (mailx-imap, mailx-sync) hold a Store reference and never touch the
15
+ * underlying MailxDB / FileMessageStore directly.
16
+ *
17
+ * This was previously named `Store` and lived in mailx-service. It
18
+ * moved to mailx-store because mailx-imap (and other sync clients) must
19
+ * be able to consume it without depending on mailx-service — that's the
20
+ * "arrow points the right way" property of the architecture.
21
+ */
22
+
23
+ import * as fs from "node:fs";
24
+ import { parseSerial } from "./parse-serial.js";
25
+ import { storeBus } from "./bus.js";
26
+ import { MailxDB } from "./db.js";
27
+ import { FileMessageStore } from "./file-store.js";
28
+ import type { StoreBus } from "./bus.js";
29
+ import type {
30
+ MessageEnvelope, MessageQuery, PagedResult,
31
+ } from "@bobfrankston/mailx-types";
32
+ import { sanitizeHtml } from "@bobfrankston/mailx-types";
33
+ import { loadSettings, loadAllowlist } from "@bobfrankston/mailx-settings";
34
+ import { sniffAndFixCharset } from "./charset.js";
35
+
36
+ /** Parse `List-Unsubscribe` (RFC 2369) and `List-Unsubscribe-Post` (RFC 8058).
37
+ * mailparser only exposes ONE of mail/url even when both are present, so we
38
+ * also scan the raw header text for the full set of angle-bracketed URIs. */
39
+ function parseListUnsubscribe(headers: any): { listUnsubscribeMail: string; listUnsubscribeHttp: string; listUnsubscribeOneClick: boolean } {
40
+ let mail = "";
41
+ let http = "";
42
+ let oneClick = false;
43
+
44
+ const raw = headers.get("list-unsubscribe");
45
+ const rawStr = typeof raw === "string" ? raw : (raw && typeof (raw as any).text === "string" ? (raw as any).text : "");
46
+ if (rawStr) {
47
+ const matches = rawStr.match(/<([^>]+)>/g) || [];
48
+ for (const m of matches) {
49
+ const url = m.slice(1, -1).trim();
50
+ if (!mail && /^mailto:/i.test(url)) mail = url;
51
+ else if (!http && /^https?:/i.test(url)) http = url;
52
+ }
53
+ }
54
+ if (!mail && !http) {
55
+ const listHeaders = headers.get("list");
56
+ if (listHeaders?.unsubscribe) {
57
+ const unsub = listHeaders.unsubscribe;
58
+ if (unsub.url) http = Array.isArray(unsub.url) ? unsub.url[0] : unsub.url;
59
+ if (unsub.mail) mail = `mailto:${Array.isArray(unsub.mail) ? unsub.mail[0] : unsub.mail}`;
60
+ }
61
+ }
62
+
63
+ const post = headers.get("list-unsubscribe-post");
64
+ const postStr = typeof post === "string" ? post : (post && typeof (post as any).text === "string" ? (post as any).text : "");
65
+ if (postStr && /one-?click/i.test(postStr)) oneClick = true;
66
+
67
+ return { listUnsubscribeMail: mail, listUnsubscribeHttp: http, listUnsubscribeOneClick: oneClick };
68
+ }
69
+
70
+ /** What the UI gets back from a body read. Mirrors the historical
71
+ * `getMessage` shape so call-site migration is mechanical. `cached: false`
72
+ * means the body isn't on disk yet — the UI shows a "downloading…"
73
+ * placeholder and listens for `bodyAvailable` to re-render. The reconciler
74
+ * is responsible for actually fetching and emitting the event. */
75
+ export interface StoreMessage extends MessageEnvelope {
76
+ bodyHtml: string;
77
+ bodyText: string;
78
+ hasRemoteContent: boolean;
79
+ remoteAllowed: boolean;
80
+ attachments: Array<{ id: number; filename: string; mimeType: string; size: number; contentId: string }>;
81
+ cached: boolean; // false = body not on disk; UI shows downloading
82
+ deliveredTo: string;
83
+ returnPath: string;
84
+ listUnsubscribe: string;
85
+ listUnsubscribeMail: string;
86
+ listUnsubscribeHttp: string;
87
+ listUnsubscribeOneClick: boolean;
88
+ emlPath: string;
89
+ isFlagged: boolean;
90
+ }
91
+
92
+ export class Store {
93
+ // Parsed-body LRU. `simpleParser` is the dominant cost of getMessage on
94
+ // a body that's already on disk — 100-500 ms for a typical Gmail message
95
+ // (heavier multipart/alternative + embedded-image swizzling than plainer
96
+ // IMAP mail). Stash the parsed result keyed by .eml path + mtime so
97
+ // repeat views of the same message return in microseconds.
98
+ //
99
+ // Invalidation is implicit: when the underlying .eml is overwritten
100
+ // (rare — bodies are immutable once cached, but reconcile can re-fetch
101
+ // on UIDVALIDITY change), the new mtime won't match the cached key.
102
+ // Capacity 200 entries × ~50 kB parsed envelope = ~10 MB ceiling.
103
+ private static readonly PARSED_LRU_CAPACITY = 200;
104
+ private parsedLru = new Map<string, StoreMessage>();
105
+ private parsedLruGet(key: string): StoreMessage | undefined {
106
+ const v = this.parsedLru.get(key);
107
+ if (!v) return undefined;
108
+ // Bump recency.
109
+ this.parsedLru.delete(key);
110
+ this.parsedLru.set(key, v);
111
+ return v;
112
+ }
113
+ private parsedLruPut(key: string, msg: StoreMessage): void {
114
+ this.parsedLru.delete(key);
115
+ this.parsedLru.set(key, msg);
116
+ while (this.parsedLru.size > Store.PARSED_LRU_CAPACITY) {
117
+ const oldest = this.parsedLru.keys().next().value;
118
+ if (oldest === undefined) break;
119
+ this.parsedLru.delete(oldest);
120
+ }
121
+ }
122
+
123
+ // Allowlist + settings caches. Both files live on the GDrive-mounted
124
+ // shared dir; their sync `readFileSync` calls in `loadAllowlist()` and
125
+ // `loadSettings()` can stall for seconds per call. getMessage runs on
126
+ // every preview click, so paying that twice each click made .eml
127
+ // display "go to GDrive" even though the body itself is local. Cached
128
+ // here; invalidated externally via invalidateConfigCaches() when the
129
+ // parent service receives a `configChanged` event.
130
+ private _allowlistCache: any | null = null;
131
+ private _settingsCache: any | null = null;
132
+ private getCachedAllowlist(): any {
133
+ if (!this._allowlistCache) this._allowlistCache = loadAllowlist();
134
+ return this._allowlistCache;
135
+ }
136
+ private getCachedSettings(): any {
137
+ if (!this._settingsCache) this._settingsCache = loadSettings();
138
+ return this._settingsCache;
139
+ }
140
+ invalidateConfigCaches(): void {
141
+ this._allowlistCache = null;
142
+ this._settingsCache = null;
143
+ }
144
+
145
+ constructor(
146
+ /** SQLite metadata index. Exposed as a public field — sync clients
147
+ * (mailx-imap, mailx-sync) read/write through it during the
148
+ * refactor. Future state: every external mutation routes through
149
+ * a Store method that emits a bus event; raw `store.db.X()` calls
150
+ * shrink to zero. Today, this is read mostly. */
151
+ public readonly db: MailxDB,
152
+ /** .eml file backend. Same migration story as `db` — sync clients
153
+ * read/write directly today, will route through Store methods. */
154
+ public readonly bodyStore: FileMessageStore,
155
+ /** Event bus for Store mutations. Defaults to the process-singleton
156
+ * so cross-package subscribers (bin/mailx.ts forwarder → WebView,
157
+ * in-process reconciler triggers) see all writes without explicit
158
+ * wiring. Tests can pass a fresh StoreBus for isolation. */
159
+ public readonly bus: StoreBus = storeBus,
160
+ ) {}
161
+
162
+ // ── Account list (read-only here; mutations go through MailxService
163
+ // until the cloud-write path is part of the queue too) ──
164
+
165
+ /** DB-shape account list (id/name/email/lastSync). The richer
166
+ * AccountConfig (with imap/smtp/etc.) lives in accounts.jsonc and is
167
+ * loaded by mailx-settings, not the DB — that path stays in
168
+ * MailxService until step 3 of the local-first plan. */
169
+ getAccounts(): { id: string; name: string; email: string; lastSync: number }[] {
170
+ return this.db.getAccounts();
171
+ }
172
+
173
+ // ── Folders ──
174
+
175
+ getFolders(accountId: string): any[] {
176
+ return this.db.getFolders(accountId);
177
+ }
178
+
179
+ /** Look up a folder by RFC 6154 specialUse tag (`trash`, `drafts`, `sent`,
180
+ * `junk`, etc.) for the given account. Falls back to a case-insensitive
181
+ * path match for legacy rows where specialUse never got tagged.
182
+ * Returns null when the account has no such folder configured. */
183
+ findSpecialFolder(accountId: string, specialUse: string): { id: number; path: string } | null {
184
+ const folders = this.db.getFolders(accountId);
185
+ const f = folders.find(x =>
186
+ x.specialUse === specialUse ||
187
+ x.path.toLowerCase() === specialUse.toLowerCase()
188
+ );
189
+ return f ? { id: f.id, path: f.path } : null;
190
+ }
191
+
192
+ // ── Message envelopes ──
193
+
194
+ /** Single envelope by (account, uid, folder). Null when the row isn't
195
+ * in the DB — caller decides whether to show "deleted" or queue a
196
+ * server lookup via the reconciler. */
197
+ getMessageEnvelope(accountId: string, uid: number, folderId?: number): MessageEnvelope | null {
198
+ const env = this.db.getMessageByUid(accountId, uid, folderId);
199
+ return env || null;
200
+ }
201
+
202
+ /** Paginated message list for a (account, folder, ...) query. */
203
+ getMessages(query: MessageQuery): PagedResult<MessageEnvelope> {
204
+ return this.db.getMessages(query);
205
+ }
206
+
207
+ /** All-Inboxes view: union of every account's INBOX, paginated. */
208
+ getUnifiedInbox(page = 1, pageSize = 50): PagedResult<MessageEnvelope> {
209
+ return this.db.getUnifiedInbox(page, pageSize);
210
+ }
211
+
212
+ /** Local FTS5 search. Server-scope search is the reconciler's job. */
213
+ searchMessages(query: string, page = 1, pageSize = 50, accountId?: string, folderId?: number, includeTrashSpam = false): PagedResult<MessageEnvelope> {
214
+ return this.db.searchMessages(query, page, pageSize, accountId, folderId, includeTrashSpam);
215
+ }
216
+
217
+ // ── Message body (read-from-disk only) ──
218
+
219
+ /** Read a fully-parsed message (envelope + body + attachments) entirely
220
+ * from local state. Returns null when the envelope isn't known.
221
+ * Returns `{ ...envelope, cached: false }` when the envelope is known
222
+ * but the body file isn't on disk — UI shows a placeholder and the
223
+ * reconciler queues the fetch.
224
+ *
225
+ * `allowRemote=true` skips HTML sanitization. Used when the user has
226
+ * explicitly allowed remote content for this sender / domain. */
227
+ async getMessage(accountId: string, uid: number, allowRemote: boolean, folderId?: number): Promise<StoreMessage | null> {
228
+ const envelope: any = this.db.getMessageByUid(accountId, uid, folderId);
229
+ if (!envelope) return null;
230
+
231
+ const allowList = this.getCachedAllowlist() as any;
232
+ const senderAddr = (envelope.from?.address || "").toLowerCase();
233
+ const senderDomain = senderAddr.split("@")[1] || "";
234
+ const toAddrs = (envelope.to || []).map((a: any) => (a.address || "").toLowerCase());
235
+
236
+ // Allowlist auto-allow: trusted sender / domain / recipient skips
237
+ // sanitization. Same rule as the legacy service implementation.
238
+ if (!allowRemote) {
239
+ const senders = (allowList.senders || []).map((s: string) => (s || "").toLowerCase());
240
+ const domains = (allowList.domains || []).map((d: string) => (d || "").toLowerCase());
241
+ const recipients = (allowList.recipients || []).map((r: string) => (r || "").toLowerCase());
242
+ if (senders.includes(senderAddr) ||
243
+ domains.includes(senderDomain) ||
244
+ toAddrs.some((a: string) => recipients.includes(a))) {
245
+ allowRemote = true;
246
+ }
247
+ }
248
+
249
+ const isFlagged = !!(
250
+ (allowList.flaggedSenders || []).some((s: string) => (s || "").toLowerCase() === senderAddr) ||
251
+ (allowList.flaggedDomains || []).some((d: string) => (d || "").toLowerCase() === senderDomain)
252
+ );
253
+
254
+ // Resolve body path: prefer the row's own bodyPath, fall back to
255
+ // the historical lookup. Either may be empty (body never fetched).
256
+ let storedPath = envelope.bodyPath || "";
257
+ if (!storedPath) storedPath = this.db.getMessageBodyPath(accountId, uid) || "";
258
+
259
+ const empty: StoreMessage = {
260
+ ...envelope,
261
+ bodyHtml: "", bodyText: "",
262
+ hasRemoteContent: false, remoteAllowed: allowRemote,
263
+ attachments: [],
264
+ cached: false,
265
+ deliveredTo: "", returnPath: "",
266
+ listUnsubscribe: "", listUnsubscribeMail: "", listUnsubscribeHttp: "", listUnsubscribeOneClick: false,
267
+ emlPath: "",
268
+ isFlagged,
269
+ };
270
+
271
+ if (!storedPath) return empty;
272
+ if (!await this.bodyStore.hasByPath(storedPath)) return empty;
273
+
274
+ // Parse-cache lookup. Key = path + mtime so a re-fetched body
275
+ // (mtime changes) invalidates automatically. mtime stat is cheap
276
+ // (sub-ms) compared to the parse it avoids (~100-500 ms).
277
+ let mtimeMs = 0;
278
+ try { mtimeMs = (await fs.promises.stat(storedPath)).mtimeMs; }
279
+ catch { /* will fall through and fail at readByPath */ }
280
+ const cacheKey = `${storedPath}|${mtimeMs}`;
281
+ const cached = this.parsedLruGet(cacheKey);
282
+ if (cached) {
283
+ // Allowlist state can change between views even with the same
284
+ // body cached; recompute the volatile fields and overlay.
285
+ return { ...cached, remoteAllowed: allowRemote, isFlagged };
286
+ }
287
+
288
+ let raw: Buffer;
289
+ try {
290
+ raw = await this.bodyStore.readByPath(storedPath);
291
+ } catch {
292
+ // File path is in DB but the file is missing — the prefetch
293
+ // dot is lying. Caller (UI) should mark the row as broken and
294
+ // the reconciler should re-fetch. Don't crash; return as if
295
+ // not cached.
296
+ return empty;
297
+ }
298
+
299
+ // Parse + sanitize. Same logic as today's MailxService.getMessage,
300
+ // but executed only when the body is local — never on a fresh
301
+ // server fetch.
302
+ const adjusted = sniffAndFixCharset(raw);
303
+ // simpleParser blocks the event loop while it parses — visible cost
304
+ // on >100 KB messages. Time it so the log makes the cost concrete:
305
+ // when this number is high, that's why other IPC calls (the next
306
+ // message click, the folder-count update tick) stall.
307
+ const _parseT0 = Date.now();
308
+ const parsed = await parseSerial(adjusted);
309
+ const _parseMs = Date.now() - _parseT0;
310
+ if (_parseMs > 50) {
311
+ console.log(` [parse] simpleParser ${_parseMs}ms for ${(raw.length / 1024).toFixed(0)} KB (${storedPath})`);
312
+ }
313
+ let bodyHtml = parsed.html || "";
314
+ const bodyText = parsed.text || "";
315
+ // Backfill FTS body_text now that we've parsed the body. upsertMessage
316
+ // only had the short `preview` snippet at index time; without this
317
+ // backfill, searches miss any word that only appears deeper in the
318
+ // body. Fire-and-forget — failures are non-fatal, never block the
319
+ // user's preview render.
320
+ if (bodyText) {
321
+ try { this.db.updateFtsBodyByUid(accountId, envelope.folderId, uid, bodyText); }
322
+ catch { /* */ }
323
+ }
324
+ let hasRemoteContent = false;
325
+ // Filter out "spurious" attachments: mailing-list footers and signature
326
+ // blocks frequently arrive as a separate text/plain MIME part with
327
+ // Content-Disposition: inline and no filename. mailparser dutifully
328
+ // surfaces them as attachments. Showing them as attachment chips
329
+ // confuses the user (Bob 2026-05-13: spurious image-shaped chip on
330
+ // a list message that had no real attachment). Rule: if the part is
331
+ // text/* AND has no filename AND is dispositionally inline (or no
332
+ // disposition at all), it's body content, not a real attachment.
333
+ // Keep the original index even for filtered rows so getAttachment's
334
+ // index lookup still works for the surviving ones.
335
+ const attachments = (parsed.attachments || [])
336
+ .map((a, i) => {
337
+ const mime = (a.contentType || "application/octet-stream").toLowerCase();
338
+ const disposition = (a.contentDisposition || "").toLowerCase();
339
+ const isSpuriousTextPart = mime.startsWith("text/")
340
+ && !a.filename
341
+ && (disposition === "inline" || disposition === "");
342
+ return { a, i, isSpuriousTextPart };
343
+ })
344
+ .filter(x => !x.isSpuriousTextPart)
345
+ .map(x => ({
346
+ id: x.i,
347
+ filename: x.a.filename || `attachment-${x.i}`,
348
+ mimeType: x.a.contentType || "application/octet-stream",
349
+ size: x.a.size || 0,
350
+ contentId: x.a.contentId || "",
351
+ }));
352
+
353
+ if (bodyHtml && !allowRemote) {
354
+ const result = sanitizeHtml(bodyHtml);
355
+ bodyHtml = result.html;
356
+ hasRemoteContent = result.hasRemoteContent;
357
+ }
358
+
359
+ // Header extraction — Delivered-To, Return-Path, List-Unsubscribe.
360
+ // mlproc preprocesses Delivered-To server-side, so the inner
361
+ // address is already clean by the time mailx sees it. We take the
362
+ // last entry of the chain (the final-delivery hop). The
363
+ // `relayDomains` filtering layer was retired 2026-05-13: nothing
364
+ // configures it in practice and the conditional made the code
365
+ // harder to reason about than the one-liner that replaces it.
366
+ let deliveredTo = "";
367
+ const rawDelivered = parsed.headers.get("delivered-to");
368
+ if (rawDelivered) {
369
+ const deliveredList = Array.isArray(rawDelivered) ? rawDelivered : [rawDelivered];
370
+ const d = deliveredList[deliveredList.length - 1];
371
+ deliveredTo = typeof d === "string" ? d : (d as any)?.text || (d as any)?.address || String(d);
372
+ }
373
+
374
+ const hdr = (key: string): string => {
375
+ let v = parsed.headers.get(key);
376
+ if (!v) return "";
377
+ if (Array.isArray(v)) v = v[0];
378
+ if (typeof v === "string") return v;
379
+ if (typeof v === "object" && v !== null) {
380
+ if ("text" in v) return (v as any).text || "";
381
+ if ("value" in v) return String((v as any).value);
382
+ if ("address" in v) return (v as any).address || "";
383
+ }
384
+ return String(v);
385
+ };
386
+ const returnPath = hdr("return-path").replace(/[<>]/g, "");
387
+ const { listUnsubscribeMail, listUnsubscribeHttp, listUnsubscribeOneClick } =
388
+ parseListUnsubscribe(parsed.headers);
389
+ const listUnsubscribe = listUnsubscribeHttp || listUnsubscribeMail;
390
+
391
+ const result: StoreMessage = {
392
+ ...envelope,
393
+ bodyHtml, bodyText,
394
+ hasRemoteContent, remoteAllowed: allowRemote,
395
+ attachments,
396
+ cached: true,
397
+ deliveredTo, returnPath,
398
+ listUnsubscribe, listUnsubscribeMail, listUnsubscribeHttp, listUnsubscribeOneClick,
399
+ // body_path in the DB is stored relative to the body-store
400
+ // basePath; resolve to absolute so the UI's "Source" button
401
+ // can hand the path to an OS file open without re-deriving
402
+ // basePath every time.
403
+ emlPath: this.bodyStore.absolutePath(storedPath),
404
+ isFlagged,
405
+ };
406
+ // Memoize the parsed result for subsequent views of the same UID.
407
+ // `remoteAllowed` and `isFlagged` get re-overlaid on read so a flag
408
+ // toggle or allowlist edit doesn't require a re-parse to take
409
+ // effect (see the parsedLruGet path above).
410
+ //
411
+ // BUT don't poison the cache with an empty parse — a malformed .eml
412
+ // or a fetch that left a stub file gives bodyHtml === "" AND
413
+ // bodyText === ""; caching that means future views serve emptiness
414
+ // until the daemon restarts. Let the next view try again; the
415
+ // parse cost on a 9 kB .eml is ~20 ms, so re-parsing an oddity
416
+ // is cheap.
417
+ const hasContent = (bodyHtml && bodyHtml.length > 0) || (bodyText && bodyText.length > 0);
418
+ if (mtimeMs > 0 && hasContent) this.parsedLruPut(cacheKey, result);
419
+ return result;
420
+ }
421
+
422
+ // ── Calendar / tasks / contacts (read paths) ──
423
+
424
+ getCalendarEvents(accountId: string, fromMs: number, toMs: number): any[] {
425
+ return this.db.getCalendarEvents(accountId, fromMs, toMs);
426
+ }
427
+
428
+ getTasks(accountId: string, includeCompleted = false): any[] {
429
+ return this.db.getTasks(accountId, includeCompleted);
430
+ }
431
+
432
+ searchContacts(query: string, limit = 10): any[] {
433
+ return this.db.searchContacts(query, limit);
434
+ }
435
+
436
+ listContacts(query: string, page = 1, pageSize = 100): any {
437
+ return this.db.listContacts(query, page, pageSize);
438
+ }
439
+
440
+ // ── Write paths (local-only; mirror to server is queued separately) ──
441
+
442
+ /** Update a message's flag set. Local DB write completes synchronously;
443
+ * the server-mirror enqueue is the caller's responsibility (typically
444
+ * via SyncQueue.enqueueFlag) so callers that don't want a server push
445
+ * — pure-local UI state like "pin in pane" — can skip it.
446
+ *
447
+ * Publishes:
448
+ * `message:<uuid>` { kind: "flagsChanged" }
449
+ * `folder:<id>` (auto fan-out)
450
+ */
451
+ updateFlags(accountId: string, uid: number, folderId: number, flags: string[]): void {
452
+ this.db.updateMessageFlags(accountId, uid, flags);
453
+ const env: any = this.db.getMessageByUid(accountId, uid, folderId);
454
+ const msgUuid: string | undefined = env?.uuid;
455
+ if (msgUuid) {
456
+ this.bus.publish({
457
+ topic: `message:${msgUuid}`,
458
+ kind: "flagsChanged",
459
+ accountId, folderId, uid, msgUuid, flags,
460
+ });
461
+ }
462
+ }
463
+
464
+ /** Move a message between folders in the same account. Adds a tombstone
465
+ * on the Message-ID so the next sync doesn't re-import the pre-move row
466
+ * in the source folder before the server-side MOVE completes; tombstone
467
+ * is cleared on terminal IMAP failure (see processSyncActions).
468
+ *
469
+ * Returns true if a local row existed and was moved, false otherwise.
470
+ *
471
+ * Publishes:
472
+ * `message:<uuid>` { kind: "messageMoved", folderId: source, targetFolderId }
473
+ * `folder:<source>` and `folder:<target>` (auto fan-out + explicit count)
474
+ */
475
+ moveMessage(accountId: string, uid: number, fromFolderId: number, targetFolderId: number): boolean {
476
+ const env: any = this.db.getMessageByUid(accountId, uid, fromFolderId);
477
+ if (!env) return false;
478
+ if (env.messageId) this.db.addTombstone(accountId, env.messageId, env.subject || "");
479
+ const moved = this.db.moveMessageLocal(accountId, uid, fromFolderId, targetFolderId);
480
+ if (!moved) return false;
481
+ this.db.recalcFolderCounts(fromFolderId);
482
+ this.db.recalcFolderCounts(targetFolderId);
483
+ const msgUuid: string | undefined = env?.uuid;
484
+ if (msgUuid) {
485
+ this.bus.publish({
486
+ topic: `message:${msgUuid}`,
487
+ kind: "messageMoved",
488
+ accountId, folderId: fromFolderId, targetFolderId, uid, msgUuid,
489
+ });
490
+ }
491
+ // Folder count change isn't tied to a specific message uuid — publish
492
+ // to both folder topics directly. (The fan-out only covers the source
493
+ // via the message event's folderId; target needs its own publish.)
494
+ this.bus.publish({
495
+ topic: `folder:${targetFolderId}`,
496
+ kind: "folderCountsChanged",
497
+ accountId, folderId: targetFolderId,
498
+ });
499
+ return true;
500
+ }
501
+
502
+ /** Trash a message. If a trash folder is configured and the message is
503
+ * not already in it, this is a move-to-trash. If the message is already
504
+ * in trash (or no trash exists), it's a hard delete + body unlink.
505
+ *
506
+ * Returns "moved-to-trash" or "expunged" so the caller knows whether
507
+ * to enqueue an IMAP MOVE or a DELETE+EXPUNGE on the queue.
508
+ */
509
+ trashMessage(accountId: string, uid: number, folderId: number, trashFolderId: number | null): "moved-to-trash" | "expunged" {
510
+ const env: any = this.db.getMessageByUid(accountId, uid, folderId);
511
+ if (env?.messageId) this.db.addTombstone(accountId, env.messageId, env.subject || "");
512
+ const msgUuid: string | undefined = env?.uuid;
513
+ if (trashFolderId != null && trashFolderId !== folderId) {
514
+ this.db.moveMessageLocal(accountId, uid, folderId, trashFolderId);
515
+ this.db.recalcFolderCounts(folderId);
516
+ this.db.recalcFolderCounts(trashFolderId);
517
+ if (msgUuid) {
518
+ this.bus.publish({
519
+ topic: `message:${msgUuid}`,
520
+ kind: "messageMoved",
521
+ accountId, folderId, targetFolderId: trashFolderId, uid, msgUuid,
522
+ });
523
+ }
524
+ this.bus.publish({
525
+ topic: `folder:${trashFolderId}`,
526
+ kind: "folderCountsChanged",
527
+ accountId, folderId: trashFolderId,
528
+ });
529
+ return "moved-to-trash";
530
+ }
531
+ this.db.deleteMessage(accountId, uid, "user-initiated trash (already in trash → expunge)", "Store.trashMessage");
532
+ this.db.recalcFolderCounts(folderId);
533
+ if (msgUuid) {
534
+ this.bus.publish({
535
+ topic: `message:${msgUuid}`,
536
+ kind: "messageRemoved",
537
+ accountId, folderId, uid, msgUuid,
538
+ });
539
+ }
540
+ return "expunged";
541
+ }
542
+
543
+ /** Restore a message from trash back to its original folder. Local-only;
544
+ * caller handles the queue (cancel-pending-MOVE vs queue-counter-MOVE).
545
+ * Returns true if a local row was moved. */
546
+ undeleteMessage(accountId: string, uid: number, trashFolderId: number, originalFolderId: number): boolean {
547
+ const moved = this.db.moveMessageLocal(accountId, uid, trashFolderId, originalFolderId);
548
+ if (!moved) return false;
549
+ this.db.recalcFolderCounts(trashFolderId);
550
+ this.db.recalcFolderCounts(originalFolderId);
551
+ const env: any = this.db.getMessageByUid(accountId, uid, originalFolderId);
552
+ const msgUuid: string | undefined = env?.uuid;
553
+ if (msgUuid) {
554
+ this.bus.publish({
555
+ topic: `message:${msgUuid}`,
556
+ kind: "messageMoved",
557
+ accountId, folderId: trashFolderId, targetFolderId: originalFolderId, uid, msgUuid,
558
+ });
559
+ }
560
+ this.bus.publish({
561
+ topic: `folder:${originalFolderId}`,
562
+ kind: "folderCountsChanged",
563
+ accountId, folderId: originalFolderId,
564
+ });
565
+ return true;
566
+ }
567
+ }