@bobfrankston/mailx-store 0.1.2 → 0.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 (5) hide show
  1. package/db.d.ts +250 -4
  2. package/db.js +1392 -57
  3. package/file-store.d.ts +27 -13
  4. package/file-store.js +60 -39
  5. package/package.json +10 -9
package/db.js CHANGED
@@ -3,9 +3,31 @@
3
3
  * Stores message headers, folder structure, sync state.
4
4
  * Message bodies are NOT here -- they live in the MessageStore backend.
5
5
  */
6
- import Database from "better-sqlite3";
6
+ import { DatabaseSync } from "node:sqlite";
7
+ import { randomUUID } from "node:crypto";
7
8
  import * as path from "node:path";
8
9
  import * as fs from "node:fs";
10
+ /** Addresses that have no business in autocomplete. Standard automated
11
+ * sender patterns plus name-side hints ("MAILER-DAEMON" etc.). The exact
12
+ * match list keeps surprise low; the regex catches the long tail of
13
+ * *-bounces@, no-reply variants, and listserv-style addresses. */
14
+ const JUNK_LOCAL_RE = /^(no-?reply|do-?not-?reply|noreply|mailer-daemon|postmaster|abuse|automated|bounce(s|d)?|list-?(server|admin|owner|manager)?|notification|notifications?|admin@.*automated|root|daemon|nobody|undisclosed)$/i;
15
+ const JUNK_LOCAL_SUFFIX_RE = /(-bounces|\+bounces|-noreply|-no-reply|-notifications?|-mailer)$/i;
16
+ function isJunkContact(email, name) {
17
+ const local = email.split("@")[0] || "";
18
+ if (JUNK_LOCAL_RE.test(local))
19
+ return true;
20
+ if (JUNK_LOCAL_SUFFIX_RE.test(local))
21
+ return true;
22
+ // Bare numeric / hex addresses (rotating IDs from automated systems)
23
+ // — three or fewer chars is too short to be useful regardless.
24
+ if (local.length < 2)
25
+ return true;
26
+ const lname = (name || "").trim().toLowerCase();
27
+ if (lname.includes("mailer-daemon") || lname.includes("postmaster"))
28
+ return true;
29
+ return false;
30
+ }
9
31
  const SCHEMA = `
10
32
  CREATE TABLE IF NOT EXISTS accounts (
11
33
  id TEXT PRIMARY KEY,
@@ -37,6 +59,7 @@ const SCHEMA = `
37
59
  message_id TEXT,
38
60
  in_reply_to TEXT,
39
61
  refs TEXT,
62
+ thread_id TEXT,
40
63
  date INTEGER NOT NULL,
41
64
  subject TEXT DEFAULT '',
42
65
  from_address TEXT DEFAULT '',
@@ -58,6 +81,19 @@ const SCHEMA = `
58
81
  CREATE INDEX IF NOT EXISTS idx_messages_message_id
59
82
  ON messages(message_id);
60
83
 
84
+ -- Note: idx_messages_thread_id is created by the addColumnIfMissing migration
85
+ -- in the constructor, AFTER thread_id is guaranteed to exist. Including it
86
+ -- here would crash startup on any pre-thread_id DB because exec(SCHEMA) runs
87
+ -- before the column-add migration.
88
+
89
+ CREATE TABLE IF NOT EXISTS sent_log (
90
+ message_id TEXT PRIMARY KEY,
91
+ account_id TEXT NOT NULL,
92
+ subject TEXT DEFAULT '',
93
+ recipients TEXT DEFAULT '',
94
+ sent_at INTEGER NOT NULL
95
+ );
96
+
61
97
  CREATE TABLE IF NOT EXISTS queue (
62
98
  id INTEGER PRIMARY KEY AUTOINCREMENT,
63
99
  status TEXT NOT NULL DEFAULT 'pending',
@@ -79,7 +115,7 @@ const SCHEMA = `
79
115
 
80
116
  CREATE TABLE IF NOT EXISTS contacts (
81
117
  id INTEGER PRIMARY KEY AUTOINCREMENT,
82
- source TEXT NOT NULL DEFAULT 'sent',
118
+ source TEXT NOT NULL DEFAULT 'discovered',
83
119
  google_id TEXT,
84
120
  name TEXT DEFAULT '',
85
121
  email TEXT NOT NULL,
@@ -87,7 +123,7 @@ const SCHEMA = `
87
123
  last_used INTEGER DEFAULT 0,
88
124
  use_count INTEGER DEFAULT 0,
89
125
  updated_at INTEGER NOT NULL,
90
- UNIQUE(email)
126
+ UNIQUE(source, email, name)
91
127
  );
92
128
 
93
129
  CREATE INDEX IF NOT EXISTS idx_contacts_email ON contacts(email);
@@ -112,16 +148,609 @@ const SCHEMA = `
112
148
  last_error TEXT,
113
149
  UNIQUE(account_id, action, uid, folder_id)
114
150
  );
151
+
152
+ -- Tombstones: messages the user deleted locally. Sync checks this table
153
+ -- before inserting a new row so a server-side delete that hasn't yet
154
+ -- propagated (or a stale server listing during the EXPUNGE race) can't
155
+ -- resurrect a message the user already removed. Keyed by Message-ID
156
+ -- because that's the only identifier stable across UID renumbers,
157
+ -- UIDVALIDITY bumps, and cross-folder moves.
158
+ CREATE TABLE IF NOT EXISTS tombstones (
159
+ account_id TEXT NOT NULL,
160
+ message_id TEXT NOT NULL,
161
+ deleted_at INTEGER NOT NULL,
162
+ subject TEXT DEFAULT '',
163
+ PRIMARY KEY (account_id, message_id)
164
+ );
165
+ CREATE INDEX IF NOT EXISTS idx_tombstones_deleted_at ON tombstones(deleted_at);
166
+
167
+ -- Calendar events: two-way cache of Google Calendar / local events.
168
+ -- uuid = local stable identity (survives provider_id rebinds).
169
+ -- provider_id = Google Calendar event id when known (null for local-only
170
+ -- events that haven't been pushed yet).
171
+ -- deleted = tombstone marker; drainer removes row from server then deletes
172
+ -- the row locally.
173
+ CREATE TABLE IF NOT EXISTS calendar_events (
174
+ uuid TEXT PRIMARY KEY,
175
+ account_id TEXT NOT NULL,
176
+ provider_id TEXT,
177
+ calendar_id TEXT DEFAULT 'primary',
178
+ title TEXT NOT NULL DEFAULT '',
179
+ start_ms INTEGER NOT NULL,
180
+ end_ms INTEGER NOT NULL,
181
+ all_day INTEGER DEFAULT 0,
182
+ location TEXT DEFAULT '',
183
+ notes TEXT DEFAULT '',
184
+ etag TEXT,
185
+ last_synced INTEGER DEFAULT 0,
186
+ dirty INTEGER DEFAULT 0,
187
+ deleted INTEGER DEFAULT 0,
188
+ updated_at INTEGER NOT NULL
189
+ );
190
+ CREATE INDEX IF NOT EXISTS idx_calendar_events_start ON calendar_events(account_id, start_ms);
191
+ CREATE INDEX IF NOT EXISTS idx_calendar_events_dirty ON calendar_events(dirty) WHERE dirty = 1;
192
+ -- getCalendarEventByProviderId runs once per event on every Google refresh;
193
+ -- without this index each lookup is a full table scan over calendar_events.
194
+ CREATE INDEX IF NOT EXISTS idx_calendar_events_provider ON calendar_events(account_id, provider_id);
195
+
196
+ -- Tasks: two-way cache of Google Tasks / local tasks. Same shape as
197
+ -- calendar_events minus the time range.
198
+ CREATE TABLE IF NOT EXISTS tasks (
199
+ uuid TEXT PRIMARY KEY,
200
+ account_id TEXT NOT NULL,
201
+ provider_id TEXT,
202
+ list_id TEXT DEFAULT '@default',
203
+ title TEXT NOT NULL DEFAULT '',
204
+ notes TEXT DEFAULT '',
205
+ due_ms INTEGER,
206
+ completed_ms INTEGER,
207
+ etag TEXT,
208
+ last_synced INTEGER DEFAULT 0,
209
+ dirty INTEGER DEFAULT 0,
210
+ deleted INTEGER DEFAULT 0,
211
+ updated_at INTEGER NOT NULL
212
+ );
213
+ CREATE INDEX IF NOT EXISTS idx_tasks_account ON tasks(account_id);
214
+ CREATE INDEX IF NOT EXISTS idx_tasks_dirty ON tasks(dirty) WHERE dirty = 1;
215
+ -- Mirror calendar_events: any provider_id lookup path needs a proper index,
216
+ -- even if today's reconcile does the dedup in memory — prevents a future
217
+ -- refactor from accidentally introducing an O(N) scan.
218
+ CREATE INDEX IF NOT EXISTS idx_tasks_provider ON tasks(account_id, provider_id);
219
+
220
+ -- Generic store-sync queue for domains OTHER than messages. Messages
221
+ -- use sync_actions above. This table queues push-to-server actions
222
+ -- for calendar / tasks / contacts / allowlist. Kind identifies the
223
+ -- domain; op is "create" / "update" / "delete"; payload is JSON the
224
+ -- drainer posts to the provider. Target URL isn't stored — the
225
+ -- drainer knows the provider endpoint from kind + payload.
226
+ CREATE TABLE IF NOT EXISTS store_sync (
227
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
228
+ kind TEXT NOT NULL,
229
+ op TEXT NOT NULL,
230
+ account_id TEXT NOT NULL,
231
+ target_uuid TEXT NOT NULL,
232
+ payload TEXT,
233
+ attempts INTEGER DEFAULT 0,
234
+ last_error TEXT,
235
+ created_at INTEGER NOT NULL,
236
+ UNIQUE(kind, target_uuid, op)
237
+ );
238
+ CREATE INDEX IF NOT EXISTS idx_store_sync_account ON store_sync(account_id, kind);
239
+ -- UNIQUE(kind, target_uuid, op) covers queries that start with kind; lookups
240
+ -- by target_uuid alone ("is this uuid queued for any op?") would otherwise
241
+ -- table-scan. Cheap; store_sync is tiny and write-heavy.
242
+ CREATE INDEX IF NOT EXISTS idx_store_sync_target_uuid ON store_sync(target_uuid);
243
+ -- Generic per-scope/per-key string store. Used for sync tokens (Google
244
+ -- People nextSyncToken per account, Gmail history-id, calendar sync token,
245
+ -- etc.) and any other small bits of state that need to outlive a process
246
+ -- restart but don't deserve their own table. Keyed by (scope, key).
247
+ CREATE TABLE IF NOT EXISTS kv (
248
+ scope TEXT NOT NULL,
249
+ key TEXT NOT NULL,
250
+ value TEXT,
251
+ updated_at INTEGER NOT NULL,
252
+ PRIMARY KEY(scope, key)
253
+ );
115
254
  `;
116
255
  export class MailxDB {
117
256
  db;
118
257
  constructor(dbDir) {
119
258
  fs.mkdirSync(dbDir, { recursive: true });
120
259
  const dbPath = path.join(dbDir, "mailx.db");
121
- this.db = new Database(dbPath);
122
- this.db.pragma("journal_mode = WAL");
123
- this.db.pragma("foreign_keys = ON");
260
+ this.db = new DatabaseSync(dbPath);
261
+ this.db.exec("PRAGMA journal_mode = WAL");
262
+ this.db.exec("PRAGMA foreign_keys = ON");
124
263
  this.db.exec(SCHEMA);
264
+ // Idempotent migrations for older databases that predate new columns.
265
+ // SQLite doesn't support "ADD COLUMN IF NOT EXISTS", so we just try the
266
+ // ALTER and catch the "duplicate column" error. Simpler and more robust
267
+ // than probing via PRAGMA table_info (which can behave differently
268
+ // across sqlite drivers).
269
+ this.addColumnIfMissing("messages", "thread_id", "TEXT");
270
+ try {
271
+ this.db.exec("CREATE INDEX IF NOT EXISTS idx_messages_thread_id ON messages(account_id, thread_id)");
272
+ }
273
+ catch { /* already exists */ }
274
+ // provider_id: native server-side id for API-backed providers (Gmail
275
+ // hex id, Outlook Graph id, etc.). Lets fetchOne look up the message
276
+ // directly instead of paginating listMessageIds for every body fetch
277
+ // — a UID-only path costs 2-3 rate-limited API calls per message.
278
+ this.addColumnIfMissing("messages", "provider_id", "TEXT");
279
+ // uuid: stable per-message local identity. Assigned the first time
280
+ // mailx sees the message and never changes — survives server UID
281
+ // renumbers, UIDVALIDITY bumps, and cross-folder moves (the sync
282
+ // rebinds the (folder_id, uid) tuple but keeps the UUID). All UI
283
+ // references SHOULD flow through uuid; (account_id, folder_id, uid)
284
+ // remains the server-binding metadata used only by sync.
285
+ this.addColumnIfMissing("messages", "uuid", "TEXT");
286
+ try {
287
+ this.db.exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_messages_uuid ON messages(uuid)");
288
+ }
289
+ catch { /* already exists */ }
290
+ // bcc_json: pre-existing DBs predate this column. Without the migration
291
+ // every contacts seed pass throws "no such column: m.bcc_json" and the
292
+ // local autocomplete corpus stays empty.
293
+ this.addColumnIfMissing("messages", "bcc_json", "TEXT DEFAULT '[]'");
294
+ // calendar_events: recurring_event_id carries the Google Calendar
295
+ // series id when the event is an expanded instance of a recurrence.
296
+ // Filters like "hide recurring events" check this column.
297
+ this.addColumnIfMissing("calendar_events", "recurring_event_id", "TEXT");
298
+ this.addColumnIfMissing("calendar_events", "html_link", "TEXT");
299
+ // Backfill UUIDs for any pre-existing rows that were inserted before
300
+ // this column landed. One UPDATE + an id roundtrip per row — cheap
301
+ // at our row counts, runs once per DB upgrade.
302
+ this.backfillUuids();
303
+ // One-shot contacts table reset: the contacts schema's UNIQUE constraint
304
+ // was widened from `(email)` to `(source, email, name)` so the same
305
+ // address can carry multiple distinct (name, source) entries — Bob's
306
+ // wife at bob@example.com and a separate `Bob Smith <bob@example.com>`
307
+ // for work, both legitimate. Old rows with the email-only unique key
308
+ // would block the new inserts. Per user "don't migrate, start fresh":
309
+ // drop the old table, recreate it via SCHEMA, reseed from messages on
310
+ // next sync. Gated by a kv flag so we run exactly once per machine.
311
+ const contactsResetFlag = this.getKv("schema", "contacts_v2");
312
+ if (!contactsResetFlag) {
313
+ try {
314
+ this.db.exec("DROP TABLE IF EXISTS contacts");
315
+ this.db.exec(`
316
+ CREATE TABLE contacts (
317
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
318
+ source TEXT NOT NULL DEFAULT 'discovered',
319
+ google_id TEXT,
320
+ name TEXT DEFAULT '',
321
+ email TEXT NOT NULL,
322
+ organization TEXT DEFAULT '',
323
+ last_used INTEGER DEFAULT 0,
324
+ use_count INTEGER DEFAULT 0,
325
+ updated_at INTEGER NOT NULL,
326
+ UNIQUE(source, email, name)
327
+ );
328
+ CREATE INDEX IF NOT EXISTS idx_contacts_email ON contacts(email);
329
+ CREATE INDEX IF NOT EXISTS idx_contacts_name ON contacts(name);
330
+ `);
331
+ this.setKv("schema", "contacts_v2", String(Date.now()));
332
+ console.log(" [db] contacts table reset to v2 schema (multi-name-per-email)");
333
+ }
334
+ catch (e) {
335
+ console.error(` [db] contacts v2 reset failed: ${e.message}`);
336
+ }
337
+ }
338
+ // Post-migration sanity check: verify the columns we actually read in
339
+ // SELECTs exist. If any migration silently failed (stale driver, DB
340
+ // file locked, permission error), later code would throw cryptic
341
+ // "no such column" errors buried deep in a sync run. Fail loud here
342
+ // with a clear "run mailx -rebuild" message. C32 on Linux was exactly
343
+ // this — old mailx-store that predated thread_id/uuid migrations.
344
+ this.verifySchema();
345
+ }
346
+ /** Fail loud + early if expected columns are missing. Cheap (PRAGMA only
347
+ * runs at startup). The user-facing message names the recovery command. */
348
+ verifySchema() {
349
+ const required = {
350
+ messages: ["thread_id", "provider_id", "uuid", "bcc_json"],
351
+ calendar_events: ["recurring_event_id", "html_link"],
352
+ };
353
+ for (const [table, cols] of Object.entries(required)) {
354
+ let actual;
355
+ try {
356
+ actual = this.db.prepare(`PRAGMA table_info(${table})`).all();
357
+ }
358
+ catch (e) {
359
+ throw new Error(`[mailx-store] schema check failed for "${table}": ${e.message}. Run 'mailx -rebuild' to rebuild the local store.`);
360
+ }
361
+ const names = new Set(actual.map(r => r.name));
362
+ const missing = cols.filter(c => !names.has(c));
363
+ if (missing.length > 0) {
364
+ throw new Error(`[mailx-store] table "${table}" is missing columns [${missing.join(", ")}] — schema migration did not complete. Run 'mailx -rebuild' to rebuild the local store.`);
365
+ }
366
+ }
367
+ }
368
+ /** Fetch a string from the kv table. Returns null when not set. */
369
+ getKv(scope, key) {
370
+ const r = this.db.prepare("SELECT value FROM kv WHERE scope = ? AND key = ?").get(scope, key);
371
+ return r?.value ?? null;
372
+ }
373
+ /** Upsert a kv row. Pass `null` to delete. */
374
+ setKv(scope, key, value) {
375
+ if (value === null) {
376
+ this.db.prepare("DELETE FROM kv WHERE scope = ? AND key = ?").run(scope, key);
377
+ return;
378
+ }
379
+ this.db.prepare("INSERT INTO kv (scope, key, value, updated_at) VALUES (?, ?, ?, ?) "
380
+ + "ON CONFLICT(scope, key) DO UPDATE SET value=excluded.value, updated_at=excluded.updated_at").run(scope, key, value, Date.now());
381
+ }
382
+ /** One-time: assign UUIDs to every `messages` row that's missing one.
383
+ * Runs on every startup but the WHERE clause makes it a no-op after the
384
+ * first pass. */
385
+ backfillUuids() {
386
+ try {
387
+ const rows = this.db.prepare("SELECT id FROM messages WHERE uuid IS NULL OR uuid = ''").all();
388
+ if (rows.length === 0)
389
+ return;
390
+ console.log(` [db] backfilling ${rows.length} message UUIDs`);
391
+ const upd = this.db.prepare("UPDATE messages SET uuid = ? WHERE id = ?");
392
+ this.db.exec("BEGIN");
393
+ try {
394
+ for (const r of rows)
395
+ upd.run(randomUUID().replace(/-/g, ""), r.id);
396
+ this.db.exec("COMMIT");
397
+ }
398
+ catch (e) {
399
+ this.db.exec("ROLLBACK");
400
+ throw e;
401
+ }
402
+ }
403
+ catch (e) {
404
+ console.error(` [db] backfillUuids failed: ${e.message}`);
405
+ }
406
+ }
407
+ // ── Sent-log (dedup) ──
408
+ /** Has this Message-ID already been sent? Used to prevent the outbox from
409
+ * re-sending the same raw file across crash/restart cycles. */
410
+ hasSentMessage(messageId) {
411
+ if (!messageId)
412
+ return false;
413
+ const row = this.db.prepare("SELECT 1 FROM sent_log WHERE message_id = ? LIMIT 1").get(messageId);
414
+ return !!row;
415
+ }
416
+ /** Record a successfully sent message so future attempts are skipped. */
417
+ recordSent(messageId, accountId, subject, recipients) {
418
+ if (!messageId)
419
+ return;
420
+ try {
421
+ this.db.prepare("INSERT INTO sent_log (message_id, account_id, subject, recipients, sent_at) VALUES (?, ?, ?, ?, ?) ON CONFLICT(message_id) DO NOTHING").run(messageId, accountId, subject || "", recipients.join(", "), Date.now());
422
+ }
423
+ catch (e) {
424
+ console.error(` [sent_log] failed to record ${messageId}: ${e.message}`);
425
+ }
426
+ }
427
+ /** Q49 heuristic: has the user ever sent a message to `recipientEmail`
428
+ * that had a non-empty Cc field? Used by compose to auto-expand the Cc
429
+ * input when replying to someone who customarily gets Cc'd with others.
430
+ * Query scans only Sent folders (special_use='sent') and matches the
431
+ * recipient's address inside `to_json` via LIKE. No special index — the
432
+ * Sent folder's row count is typically a few thousand at most; acceptable
433
+ * on the compose-open path. */
434
+ hasCcHistoryTo(recipientEmail) {
435
+ const email = (recipientEmail || "").trim().toLowerCase();
436
+ if (!email)
437
+ return false;
438
+ try {
439
+ const row = this.db.prepare(`
440
+ SELECT 1 FROM messages m
441
+ JOIN folders f ON m.folder_id = f.id
442
+ WHERE f.special_use = 'sent'
443
+ AND lower(m.to_json) LIKE ?
444
+ AND m.cc_json IS NOT NULL AND m.cc_json != '[]' AND m.cc_json != ''
445
+ LIMIT 1
446
+ `).get(`%"${email}"%`);
447
+ return !!row;
448
+ }
449
+ catch {
450
+ return false;
451
+ }
452
+ }
453
+ /** Same shape as hasCcHistoryTo for the Bcc field. Bcc only appears in the
454
+ * user's own Sent copy, so it's still a reliable signal that this user
455
+ * habitually Bccs when writing to this recipient. */
456
+ hasBccHistoryTo(recipientEmail) {
457
+ const email = (recipientEmail || "").trim().toLowerCase();
458
+ if (!email)
459
+ return false;
460
+ try {
461
+ const row = this.db.prepare(`
462
+ SELECT 1 FROM messages m
463
+ JOIN folders f ON m.folder_id = f.id
464
+ WHERE f.special_use = 'sent'
465
+ AND lower(m.to_json) LIKE ?
466
+ AND m.bcc_json IS NOT NULL AND m.bcc_json != '[]' AND m.bcc_json != ''
467
+ LIMIT 1
468
+ `).get(`%"${email}"%`);
469
+ return !!row;
470
+ }
471
+ catch {
472
+ return false;
473
+ }
474
+ }
475
+ // ── Tombstones (local-delete record so server echo can't resurrect) ──
476
+ /** Mark a Message-ID as locally-deleted for an account. No-op if messageId
477
+ * is empty (e.g. provider stripped the header) — without a stable id we
478
+ * can't check against future sync results anyway. */
479
+ addTombstone(accountId, messageId, subject = "") {
480
+ if (!messageId)
481
+ return;
482
+ try {
483
+ this.db.prepare("INSERT INTO tombstones (account_id, message_id, deleted_at, subject) VALUES (?, ?, ?, ?) ON CONFLICT(account_id, message_id) DO UPDATE SET deleted_at = excluded.deleted_at").run(accountId, messageId, Date.now(), subject || "");
484
+ }
485
+ catch (e) {
486
+ console.error(` [tombstones] failed to record ${messageId}: ${e.message}`);
487
+ }
488
+ }
489
+ /** Is this Message-ID tombstoned for this account? */
490
+ hasTombstone(accountId, messageId) {
491
+ if (!messageId)
492
+ return false;
493
+ const row = this.db.prepare("SELECT 1 FROM tombstones WHERE account_id = ? AND message_id = ? LIMIT 1").get(accountId, messageId);
494
+ return !!row;
495
+ }
496
+ /** Remove a tombstone — used by "undelete" (Ctrl-Z) so a subsequent sync
497
+ * re-imports the message as normal. Also lets the user recover from a
498
+ * mistaken local delete. */
499
+ removeTombstone(accountId, messageId) {
500
+ if (!messageId)
501
+ return;
502
+ try {
503
+ this.db.prepare("DELETE FROM tombstones WHERE account_id = ? AND message_id = ?").run(accountId, messageId);
504
+ }
505
+ catch (e) {
506
+ console.error(` [tombstones] failed to remove ${messageId}: ${e.message}`);
507
+ }
508
+ }
509
+ /** Age-out tombstones older than the given cutoff. Keeps the table from
510
+ * growing unboundedly. Default retention is 30 days; caller passes the
511
+ * actual cutoff in ms since epoch. */
512
+ pruneTombstones(olderThanMs) {
513
+ try {
514
+ const res = this.db.prepare("DELETE FROM tombstones WHERE deleted_at < ?").run(olderThanMs);
515
+ return Number(res.changes || 0);
516
+ }
517
+ catch (e) {
518
+ console.error(` [tombstones] prune failed: ${e.message}`);
519
+ return 0;
520
+ }
521
+ }
522
+ // ── Calendar events (two-way cache) ──
523
+ upsertCalendarEvent(ev) {
524
+ const uuid = ev.uuid || randomUUID().replace(/-/g, "");
525
+ this.db.prepare(`
526
+ INSERT INTO calendar_events
527
+ (uuid, account_id, provider_id, calendar_id, title, start_ms, end_ms,
528
+ all_day, location, notes, etag, last_synced, dirty, deleted, updated_at,
529
+ recurring_event_id, html_link)
530
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)
531
+ ON CONFLICT(uuid) DO UPDATE SET
532
+ account_id=excluded.account_id, provider_id=excluded.provider_id,
533
+ calendar_id=excluded.calendar_id, title=excluded.title,
534
+ start_ms=excluded.start_ms, end_ms=excluded.end_ms,
535
+ all_day=excluded.all_day, location=excluded.location,
536
+ notes=excluded.notes, etag=excluded.etag,
537
+ last_synced=excluded.last_synced, dirty=excluded.dirty,
538
+ updated_at=excluded.updated_at,
539
+ recurring_event_id=excluded.recurring_event_id,
540
+ html_link=excluded.html_link
541
+ `).run(uuid, ev.accountId, ev.providerId || null, ev.calendarId || "primary", ev.title, ev.startMs, ev.endMs, ev.allDay ? 1 : 0, ev.location || "", ev.notes || "", ev.etag || null, ev.dirty ? 0 : Date.now(), ev.dirty ? 1 : 0, Date.now(), ev.recurringEventId || null, ev.htmlLink || null);
542
+ return uuid;
543
+ }
544
+ getCalendarEvents(accountId, fromMs, toMs) {
545
+ const rows = this.db.prepare(`
546
+ SELECT * FROM calendar_events
547
+ WHERE account_id = ? AND deleted = 0 AND start_ms >= ? AND start_ms < ?
548
+ ORDER BY start_ms ASC
549
+ `).all(accountId, fromMs, toMs);
550
+ return rows.map(this.calendarRowToObject);
551
+ }
552
+ /** Lookup by uuid only — used by patch/delete paths that don't have an
553
+ * accountId context. Returns the row even when it's soft-deleted. */
554
+ getCalendarEventByUuid(uuid) {
555
+ const r = this.db.prepare("SELECT * FROM calendar_events WHERE uuid = ?").get(uuid);
556
+ return r ? this.calendarRowToObject(r) : null;
557
+ }
558
+ getTaskByUuid(uuid) {
559
+ const r = this.db.prepare("SELECT * FROM tasks WHERE uuid = ?").get(uuid);
560
+ return r ? this.taskRowToObject(r) : null;
561
+ }
562
+ getDirtyCalendarEvents(accountId) {
563
+ const rows = this.db.prepare(`
564
+ SELECT * FROM calendar_events WHERE account_id = ? AND (dirty = 1 OR deleted = 1)
565
+ `).all(accountId);
566
+ return rows.map(this.calendarRowToObject);
567
+ }
568
+ calendarRowToObject(r) {
569
+ return {
570
+ uuid: r.uuid, accountId: r.account_id, providerId: r.provider_id,
571
+ calendarId: r.calendar_id, title: r.title, startMs: r.start_ms,
572
+ endMs: r.end_ms, allDay: !!r.all_day, location: r.location, notes: r.notes,
573
+ etag: r.etag, lastSynced: r.last_synced, dirty: !!r.dirty, deleted: !!r.deleted,
574
+ recurringEventId: r.recurring_event_id || null,
575
+ htmlLink: r.html_link || null,
576
+ };
577
+ }
578
+ /** Find a calendar event by its Google Calendar event id (provider_id).
579
+ * Global lookup — not window-scoped — so repeat pulls dedup cleanly. */
580
+ getCalendarEventByProviderId(accountId, providerId) {
581
+ const r = this.db.prepare("SELECT * FROM calendar_events WHERE account_id = ? AND provider_id = ?").get(accountId, providerId);
582
+ return r ? this.calendarRowToObject(r) : null;
583
+ }
584
+ markCalendarEventClean(uuid, providerId, etag) {
585
+ this.db.prepare(`
586
+ UPDATE calendar_events SET dirty=0, provider_id=?, etag=?, last_synced=? WHERE uuid=?
587
+ `).run(providerId, etag, Date.now(), uuid);
588
+ }
589
+ deleteCalendarEventLocal(uuid) {
590
+ this.db.prepare("UPDATE calendar_events SET deleted=1, dirty=1, updated_at=? WHERE uuid=?").run(Date.now(), uuid);
591
+ }
592
+ purgeCalendarEvent(uuid) {
593
+ this.db.prepare("DELETE FROM calendar_events WHERE uuid=?").run(uuid);
594
+ }
595
+ // ── Tasks (two-way cache) ──
596
+ upsertTask(t) {
597
+ const uuid = t.uuid || randomUUID().replace(/-/g, "");
598
+ this.db.prepare(`
599
+ INSERT INTO tasks
600
+ (uuid, account_id, provider_id, list_id, title, notes, due_ms, completed_ms,
601
+ etag, last_synced, dirty, deleted, updated_at)
602
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?)
603
+ ON CONFLICT(uuid) DO UPDATE SET
604
+ account_id=excluded.account_id, provider_id=excluded.provider_id,
605
+ list_id=excluded.list_id, title=excluded.title, notes=excluded.notes,
606
+ due_ms=excluded.due_ms, completed_ms=excluded.completed_ms,
607
+ etag=excluded.etag, last_synced=excluded.last_synced,
608
+ dirty=excluded.dirty, updated_at=excluded.updated_at
609
+ `).run(uuid, t.accountId, t.providerId || null, t.listId || "@default", t.title, t.notes || "", t.dueMs || null, t.completedMs || null, t.etag || null, t.dirty ? 0 : Date.now(), t.dirty ? 1 : 0, Date.now());
610
+ return uuid;
611
+ }
612
+ getTasks(accountId, includeCompleted = false) {
613
+ const where = includeCompleted
614
+ ? "account_id = ? AND deleted = 0"
615
+ : "account_id = ? AND deleted = 0 AND completed_ms IS NULL";
616
+ const rows = this.db.prepare(`SELECT * FROM tasks WHERE ${where} ORDER BY COALESCE(due_ms, updated_at) ASC`).all(accountId);
617
+ return rows.map(this.taskRowToObject);
618
+ }
619
+ getDirtyTasks(accountId) {
620
+ const rows = this.db.prepare(`SELECT * FROM tasks WHERE account_id = ? AND (dirty = 1 OR deleted = 1)`).all(accountId);
621
+ return rows.map(this.taskRowToObject);
622
+ }
623
+ taskRowToObject(r) {
624
+ return {
625
+ uuid: r.uuid, accountId: r.account_id, providerId: r.provider_id,
626
+ listId: r.list_id, title: r.title, notes: r.notes, dueMs: r.due_ms,
627
+ completedMs: r.completed_ms, etag: r.etag, lastSynced: r.last_synced,
628
+ dirty: !!r.dirty, deleted: !!r.deleted,
629
+ };
630
+ }
631
+ markTaskClean(uuid, providerId, etag) {
632
+ this.db.prepare(`UPDATE tasks SET dirty=0, provider_id=?, etag=?, last_synced=? WHERE uuid=?`)
633
+ .run(providerId, etag, Date.now(), uuid);
634
+ }
635
+ deleteTaskLocal(uuid) {
636
+ this.db.prepare("UPDATE tasks SET deleted=1, dirty=1, updated_at=? WHERE uuid=?").run(Date.now(), uuid);
637
+ }
638
+ purgeTask(uuid) {
639
+ this.db.prepare("DELETE FROM tasks WHERE uuid=?").run(uuid);
640
+ }
641
+ // ── Contacts two-way: existing upsertContact / deleteContact handle the
642
+ // local side; the service layer adds store_sync push-queue entries.
643
+ // (No extra methods needed here — upsertContact/deleteContact at the
644
+ // regular contact-management section are the two-way cache's local
645
+ // writers.)
646
+ /** Local delete for the two-way cache drainer (symmetric with calendar/tasks). */
647
+ deleteContactLocal(email) { this.deleteContact(email); }
648
+ // ── Store-sync queue (calendar / tasks / contacts / allowlist) ──
649
+ enqueueStoreSync(kind, op, accountId, targetUuid, payload) {
650
+ try {
651
+ this.db.prepare(`
652
+ INSERT OR REPLACE INTO store_sync (kind, op, account_id, target_uuid, payload, created_at)
653
+ VALUES (?, ?, ?, ?, ?, ?)
654
+ `).run(kind, op, accountId, targetUuid, JSON.stringify(payload), Date.now());
655
+ }
656
+ catch (e) {
657
+ console.error(` [store_sync] enqueue ${kind}/${op}/${targetUuid} failed: ${e.message}`);
658
+ }
659
+ }
660
+ getStoreSyncQueue(kind, accountId) {
661
+ let sql = "SELECT * FROM store_sync";
662
+ const params = [];
663
+ const wh = [];
664
+ if (kind) {
665
+ wh.push("kind = ?");
666
+ params.push(kind);
667
+ }
668
+ if (accountId) {
669
+ wh.push("account_id = ?");
670
+ params.push(accountId);
671
+ }
672
+ if (wh.length)
673
+ sql += " WHERE " + wh.join(" AND ");
674
+ sql += " ORDER BY created_at ASC";
675
+ const rows = this.db.prepare(sql).all(...params);
676
+ return rows.map(r => ({
677
+ id: r.id, kind: r.kind, op: r.op, accountId: r.account_id,
678
+ targetUuid: r.target_uuid,
679
+ payload: r.payload ? JSON.parse(r.payload) : null,
680
+ attempts: r.attempts, lastError: r.last_error,
681
+ }));
682
+ }
683
+ completeStoreSync(id) {
684
+ this.db.prepare("DELETE FROM store_sync WHERE id = ?").run(id);
685
+ }
686
+ failStoreSync(id, error) {
687
+ this.db.prepare("UPDATE store_sync SET attempts = attempts + 1, last_error = ? WHERE id = ?").run(error, id);
688
+ }
689
+ /** Idempotently add a column to a table if it's missing. */
690
+ addColumnIfMissing(table, column, sqlType) {
691
+ try {
692
+ this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${sqlType}`);
693
+ console.log(` [db] added column ${table}.${column}`);
694
+ }
695
+ catch (e) {
696
+ const msg = String(e?.message || e);
697
+ // "duplicate column name" is the expected case when column already exists
698
+ if (!/duplicate column/i.test(msg)) {
699
+ console.error(` [db] migration ${table}.${column} failed: ${msg}`);
700
+ }
701
+ }
702
+ }
703
+ /** Compute a thread id for an incoming message. Strategy:
704
+ * 1. If any ancestor (in_reply_to or references) is already present in
705
+ * messages with a thread_id, reuse it — this handles the case where
706
+ * replies arrive before / after the root.
707
+ * 2. Otherwise use the oldest ref (first entry in References), or
708
+ * in_reply_to, or the message's own messageId as the thread root. */
709
+ computeThreadId(accountId, messageId, inReplyTo, references) {
710
+ const candidates = [];
711
+ if (references && references.length)
712
+ candidates.push(...references);
713
+ if (inReplyTo && !candidates.includes(inReplyTo))
714
+ candidates.push(inReplyTo);
715
+ if (messageId && !candidates.includes(messageId))
716
+ candidates.push(messageId);
717
+ // Look for an existing thread anchored on any of the ancestors
718
+ for (const mid of candidates) {
719
+ if (!mid)
720
+ continue;
721
+ const row = this.db.prepare("SELECT thread_id FROM messages WHERE account_id = ? AND message_id = ? AND thread_id IS NOT NULL LIMIT 1").get(accountId, mid);
722
+ if (row?.thread_id)
723
+ return row.thread_id;
724
+ }
725
+ // No existing thread — seed from the oldest ref, falling back to
726
+ // in_reply_to, then messageId
727
+ return (references && references[0]) || inReplyTo || messageId || `orphan-${Date.now()}-${Math.random().toString(36).slice(2)}`;
728
+ }
729
+ /** Get all messages in a thread (across folders) for a given account. */
730
+ getThreadMessages(accountId, threadId) {
731
+ if (!threadId)
732
+ return [];
733
+ const rows = this.db.prepare(`SELECT * FROM messages WHERE account_id = ? AND thread_id = ? ORDER BY date ASC`).all(accountId, threadId);
734
+ return rows.map(r => ({
735
+ id: r.id,
736
+ accountId: r.account_id,
737
+ folderId: r.folder_id,
738
+ uid: r.uid,
739
+ messageId: r.message_id || "",
740
+ inReplyTo: r.in_reply_to || "",
741
+ references: JSON.parse(r.refs || "[]"),
742
+ threadId: r.thread_id || undefined,
743
+ date: r.date,
744
+ subject: r.subject,
745
+ from: { name: r.from_name, address: r.from_address },
746
+ to: JSON.parse(r.to_json),
747
+ cc: JSON.parse(r.cc_json),
748
+ flags: JSON.parse(r.flags_json),
749
+ size: r.size,
750
+ hasAttachments: !!r.has_attachments,
751
+ preview: r.preview,
752
+ bodyPath: r.body_path || undefined,
753
+ }));
125
754
  }
126
755
  close() {
127
756
  this.db.close();
@@ -137,6 +766,9 @@ export class MailxDB {
137
766
  getAccounts() {
138
767
  return this.db.prepare("SELECT id, name, email, last_sync as lastSync FROM accounts").all();
139
768
  }
769
+ getAccountConfigs() {
770
+ return this.db.prepare("SELECT id, name, email, config_json as configJson FROM accounts").all();
771
+ }
140
772
  updateLastSync(accountId, timestamp) {
141
773
  this.db.prepare("UPDATE accounts SET last_sync = ? WHERE id = ?").run(timestamp, accountId);
142
774
  }
@@ -152,7 +784,7 @@ export class MailxDB {
152
784
  }
153
785
  getFolders(accountId) {
154
786
  const rows = this.db.prepare("SELECT * FROM folders WHERE account_id = ? ORDER BY path").all(accountId);
155
- return rows.map(r => ({
787
+ const folders = rows.map(r => ({
156
788
  id: r.id,
157
789
  accountId: r.account_id,
158
790
  path: r.path,
@@ -163,11 +795,48 @@ export class MailxDB {
163
795
  unreadCount: r.unread_count,
164
796
  children: []
165
797
  }));
798
+ // Sub-folder inheritance: a folder under Drafts/Sent/Trash/Junk/Archive
799
+ // inherits the parent's special role for UI purposes (column layout,
800
+ // open-in-compose, etc.). INBOX is intentionally excluded — its sub-
801
+ // folders are typically filtered mail and inheriting "inbox" would
802
+ // inflate All Inboxes. findFolder() still resolves to the canonical
803
+ // folder because rows are sorted by path and the parent sorts before
804
+ // its children.
805
+ const INHERITABLE = new Set(["sent", "drafts", "trash", "junk", "archive"]);
806
+ const roleByPath = new Map();
807
+ for (const f of folders) {
808
+ if (f.specialUse && INHERITABLE.has(f.specialUse)) {
809
+ roleByPath.set(f.path, f.specialUse);
810
+ }
811
+ }
812
+ for (const f of folders) {
813
+ if (f.specialUse)
814
+ continue;
815
+ const delim = f.delimiter || "/";
816
+ const parts = f.path.split(delim);
817
+ while (parts.length > 1) {
818
+ parts.pop();
819
+ const role = roleByPath.get(parts.join(delim));
820
+ if (role) {
821
+ f.specialUse = role;
822
+ break;
823
+ }
824
+ }
825
+ }
826
+ return folders;
166
827
  }
167
828
  deleteFolder(folderId) {
168
829
  this.db.prepare("DELETE FROM messages WHERE folder_id = ?").run(folderId);
169
830
  this.db.prepare("DELETE FROM folders WHERE id = ?").run(folderId);
170
831
  }
832
+ markFolderRead(folderId) {
833
+ this.db.prepare(`UPDATE messages SET flags_json = REPLACE(flags_json, '[]', '["\\\\Seen"]') WHERE folder_id = ? AND flags_json NOT LIKE '%\\\\Seen%'`).run(folderId);
834
+ this.recalcFolderCounts(folderId);
835
+ }
836
+ deleteAllMessages(accountId, folderId) {
837
+ this.db.prepare("DELETE FROM messages WHERE account_id = ? AND folder_id = ?").run(accountId, folderId);
838
+ this.recalcFolderCounts(folderId);
839
+ }
171
840
  updateFolderCounts(folderId, total, unread) {
172
841
  this.db.prepare("UPDATE folders SET total_count = ?, unread_count = ? WHERE id = ?").run(total, unread, folderId);
173
842
  }
@@ -180,23 +849,68 @@ export class MailxDB {
180
849
  }
181
850
  // ── Messages ──
182
851
  upsertMessage(msg) {
183
- const existing = this.db.prepare("SELECT id FROM messages WHERE account_id = ? AND folder_id = ? AND uid = ?").get(msg.accountId, msg.folderId, msg.uid);
852
+ const existing = this.db.prepare("SELECT id, provider_id FROM messages WHERE account_id = ? AND folder_id = ? AND uid = ?").get(msg.accountId, msg.folderId, msg.uid);
184
853
  if (existing) {
185
- this.db.prepare(`
186
- UPDATE messages SET flags_json = ?, preview = ?, body_path = ?, cached_at = ?
187
- WHERE id = ?
188
- `).run(JSON.stringify(msg.flags), msg.preview, msg.bodyPath, Date.now(), existing.id);
854
+ // Backfill provider_id on existing rows that predate this column —
855
+ // critical for body fetch to bypass listMessageIds pagination.
856
+ if (msg.providerId && !existing.provider_id) {
857
+ this.db.prepare("UPDATE messages SET provider_id = ? WHERE id = ?").run(msg.providerId, existing.id);
858
+ }
859
+ // Only overwrite body_path / preview when the caller actually has a
860
+ // body. Metadata-only syncs (Gmail API storeApiMessages, IMAP
861
+ // header-only fetches) pass bodyPath: "" and would otherwise wipe
862
+ // the path that prefetch just wrote, causing prefetch to re-download
863
+ // every message every cycle.
864
+ if (msg.bodyPath) {
865
+ this.db.prepare(`
866
+ UPDATE messages SET flags_json = ?, preview = ?, body_path = ?, cached_at = ?
867
+ WHERE id = ?
868
+ `).run(JSON.stringify(msg.flags), msg.preview, msg.bodyPath, Date.now(), existing.id);
869
+ }
870
+ else {
871
+ this.db.prepare(`
872
+ UPDATE messages SET flags_json = ?, cached_at = ?
873
+ WHERE id = ?
874
+ `).run(JSON.stringify(msg.flags), Date.now(), existing.id);
875
+ }
189
876
  return existing.id;
190
877
  }
878
+ // Move-detection: if this Message-ID already exists for this account
879
+ // in a DIFFERENT folder, treat it as a server-side move rather than a
880
+ // new arrival. Rebind that row to (folder_id, uid) — keep the UUID,
881
+ // body_path, flags, all the local state. Saves a body re-fetch and
882
+ // preserves any local references (in_reply_to, dally entries, undo
883
+ // stacks) that point at the UUID. Only kicks in when messageId is
884
+ // present (servers usually include it; if not we fall through to a
885
+ // fresh insert which mints a new UUID).
886
+ if (msg.messageId) {
887
+ const moved = this.db.prepare("SELECT id, folder_id, uid FROM messages WHERE account_id = ? AND message_id = ? LIMIT 1").get(msg.accountId, msg.messageId);
888
+ if (moved) {
889
+ console.log(` [move-detect] ${msg.accountId} ${msg.messageId}: rebinding row ${moved.id} (folder ${moved.folder_id}/uid ${moved.uid} → folder ${msg.folderId}/uid ${msg.uid})`);
890
+ // Update folder_id + uid; preserve uuid, body_path, flags
891
+ // (server flags will catch up on the next full sync).
892
+ this.db.prepare("UPDATE messages SET folder_id = ?, uid = ?, cached_at = ? WHERE id = ?").run(msg.folderId, msg.uid, Date.now(), moved.id);
893
+ return moved.id;
894
+ }
895
+ }
191
896
  const toText = msg.to.map(a => `${a.name} ${a.address}`).join(" ");
192
897
  const ccText = msg.cc.map(a => `${a.name} ${a.address}`).join(" ");
898
+ // Thread id = oldest ancestor in the reference chain, or the in-reply-to
899
+ // parent, or the message's own Message-ID as a fallback. We also check
900
+ // whether an existing row already has a thread_id for any of the refs,
901
+ // so late-arriving replies latch onto the same thread.
902
+ const threadId = this.computeThreadId(msg.accountId, msg.messageId, msg.inReplyTo, msg.references);
903
+ // Mint a per-message local identity UUID at first-sight. Stable for
904
+ // the life of the row — survives server UID renumbers, folder moves
905
+ // (sync rebinds folder_id/uid but keeps uuid), UIDVALIDITY bumps.
906
+ const uuid = randomUUID().replace(/-/g, "");
193
907
  const result = this.db.prepare(`
194
908
  INSERT INTO messages (
195
- account_id, folder_id, uid, message_id, in_reply_to, refs,
909
+ account_id, folder_id, uid, uuid, message_id, in_reply_to, refs, thread_id,
196
910
  date, subject, from_address, from_name, to_json, cc_json,
197
- flags_json, size, has_attachments, preview, body_path, cached_at
198
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
199
- `).run(msg.accountId, msg.folderId, msg.uid, msg.messageId, msg.inReplyTo, JSON.stringify(msg.references), msg.date, msg.subject, msg.from.address, msg.from.name, JSON.stringify(msg.to), JSON.stringify(msg.cc), JSON.stringify(msg.flags), msg.size, msg.hasAttachments ? 1 : 0, msg.preview, msg.bodyPath, Date.now());
911
+ flags_json, size, has_attachments, preview, body_path, cached_at, provider_id
912
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
913
+ `).run(msg.accountId, msg.folderId, msg.uid, uuid, msg.messageId, msg.inReplyTo, JSON.stringify(msg.references), threadId, msg.date, msg.subject, msg.from.address, msg.from.name, JSON.stringify(msg.to), JSON.stringify(msg.cc), JSON.stringify(msg.flags), msg.size, msg.hasAttachments ? 1 : 0, msg.preview, msg.bodyPath, Date.now(), msg.providerId || null);
200
914
  const rowId = Number(result.lastInsertRowid);
201
915
  // Index for full-text search
202
916
  try {
@@ -219,16 +933,38 @@ export class MailxDB {
219
933
  const term = `%${query.search}%`;
220
934
  params.push(term, term, term);
221
935
  }
936
+ if (query.flaggedOnly) {
937
+ // flags_json is a JSON array like ["\\Seen","\\Flagged"]. A plain
938
+ // LIKE on the serialized form is sufficient to find rows with the
939
+ // \Flagged flag without decoding every row.
940
+ where += " AND flags_json LIKE '%\\\\Flagged%'";
941
+ }
222
942
  const total = this.db.prepare(`SELECT COUNT(*) as cnt FROM messages WHERE ${where}`).get(...params).cnt;
223
- const rows = this.db.prepare(`SELECT * FROM messages WHERE ${where} ORDER BY ${sortCol} ${sortDir} LIMIT ? OFFSET ?`).all(...params, pageSize, offset);
943
+ // LEFT JOIN sync_actions so each row carries a `pending` flag
944
+ // true when the user has a queued local action (move/flag/delete)
945
+ // not yet acknowledged by the server. UI renders these in pink so
946
+ // local-only state is visible (Slice C of S1). Negative UIDs also
947
+ // count as pending: that's the convention for optimistic local
948
+ // inserts (e.g. Sent rows written the moment the user hits Send,
949
+ // before the real APPENDUID comes back from the server).
950
+ const rows = this.db.prepare(`SELECT m.*, (
951
+ EXISTS(
952
+ SELECT 1 FROM sync_actions sa
953
+ WHERE sa.account_id = m.account_id AND sa.uid = m.uid
954
+ ) OR m.uid < 0
955
+ ) AS pending
956
+ FROM messages m WHERE ${where.replace(/\b(account_id|folder_id|uid|date|subject|from_name|from_address|flags_json)\b/g, "m.$1")}
957
+ ORDER BY m.${sortCol} ${sortDir} LIMIT ? OFFSET ?`).all(...params, pageSize, offset);
224
958
  const items = rows.map(r => ({
225
959
  id: r.id,
226
960
  accountId: r.account_id,
227
961
  folderId: r.folder_id,
228
962
  uid: r.uid,
963
+ uuid: r.uuid || "",
229
964
  messageId: r.message_id || "",
230
965
  inReplyTo: r.in_reply_to || "",
231
966
  references: JSON.parse(r.refs || "[]"),
967
+ threadId: r.thread_id || undefined,
232
968
  date: r.date,
233
969
  subject: r.subject,
234
970
  from: { name: r.from_name, address: r.from_address },
@@ -237,22 +973,72 @@ export class MailxDB {
237
973
  flags: JSON.parse(r.flags_json),
238
974
  size: r.size,
239
975
  hasAttachments: !!r.has_attachments,
240
- preview: r.preview
976
+ preview: r.preview,
977
+ bodyPath: r.body_path || "",
978
+ pending: !!r.pending,
241
979
  }));
242
980
  return { items, total, page, pageSize };
243
981
  }
244
- getMessageByUid(accountId, uid) {
245
- const r = this.db.prepare("SELECT * FROM messages WHERE account_id = ? AND uid = ?").get(accountId, uid);
246
- if (!r)
247
- return null;
982
+ /** Unified inbox: all inbox folders across accounts, sorted by date, paginated in SQL */
983
+ getUnifiedInbox(page = 1, pageSize = 50) {
984
+ const offset = (page - 1) * pageSize;
985
+ // Find all inbox folder IDs
986
+ const inboxRows = this.db.prepare("SELECT id FROM folders WHERE special_use = 'inbox'").all();
987
+ if (inboxRows.length === 0)
988
+ return { items: [], total: 0, page, pageSize };
989
+ const placeholders = inboxRows.map(() => "?").join(",");
990
+ const folderIds = inboxRows.map((r) => r.id);
991
+ const total = this.db.prepare(`SELECT COUNT(*) as cnt FROM messages WHERE folder_id IN (${placeholders})`).get(...folderIds).cnt;
992
+ const rows = this.db.prepare(`SELECT m.*, EXISTS(
993
+ SELECT 1 FROM sync_actions sa
994
+ WHERE sa.account_id = m.account_id AND sa.uid = m.uid
995
+ ) AS pending,
996
+ (SELECT COUNT(DISTINCT account_id) FROM messages m2
997
+ WHERE m2.message_id = m.message_id AND m.message_id != '') AS dupeCount
998
+ FROM messages m WHERE m.folder_id IN (${placeholders})
999
+ ORDER BY m.date DESC LIMIT ? OFFSET ?`).all(...folderIds, pageSize, offset);
1000
+ const items = rows.map(r => ({
1001
+ id: r.id,
1002
+ accountId: r.account_id,
1003
+ folderId: r.folder_id,
1004
+ uid: r.uid,
1005
+ messageId: r.message_id || "",
1006
+ inReplyTo: r.in_reply_to || "",
1007
+ references: JSON.parse(r.refs || "[]"),
1008
+ threadId: r.thread_id || undefined,
1009
+ date: r.date,
1010
+ subject: r.subject,
1011
+ from: { name: r.from_name, address: r.from_address },
1012
+ to: JSON.parse(r.to_json),
1013
+ cc: JSON.parse(r.cc_json),
1014
+ flags: JSON.parse(r.flags_json),
1015
+ size: r.size,
1016
+ hasAttachments: !!r.has_attachments,
1017
+ preview: r.preview,
1018
+ bodyPath: r.body_path || "",
1019
+ pending: !!r.pending,
1020
+ // >=2 means the same message-id exists under another account in
1021
+ // the local DB (delivered to both accounts, or a mailing-list
1022
+ // Bcc). The unified-inbox UI shows a small ⇆ badge on these
1023
+ // rows so the user knows "this is a copy of the same message".
1024
+ dupeCount: r.dupeCount | 0,
1025
+ }));
1026
+ return { items, total, page, pageSize };
1027
+ }
1028
+ /** Map a `messages` row to a MessageEnvelope. Exposes `uuid` (stable local
1029
+ * identity) and `bodyPath` (authoritative on-disk location) in addition
1030
+ * to the server-binding metadata. */
1031
+ rowToEnvelope(r) {
248
1032
  return {
249
1033
  id: r.id,
250
1034
  accountId: r.account_id,
251
1035
  folderId: r.folder_id,
252
1036
  uid: r.uid,
1037
+ uuid: r.uuid || "",
253
1038
  messageId: r.message_id || "",
254
1039
  inReplyTo: r.in_reply_to || "",
255
1040
  references: JSON.parse(r.refs || "[]"),
1041
+ threadId: r.thread_id || undefined,
256
1042
  date: r.date,
257
1043
  subject: r.subject,
258
1044
  from: { name: r.from_name, address: r.from_address },
@@ -261,9 +1047,34 @@ export class MailxDB {
261
1047
  flags: JSON.parse(r.flags_json),
262
1048
  size: r.size,
263
1049
  hasAttachments: !!r.has_attachments,
264
- preview: r.preview
1050
+ preview: r.preview,
1051
+ bodyPath: r.body_path || "",
1052
+ providerId: r.provider_id || undefined,
265
1053
  };
266
1054
  }
1055
+ getMessageByUid(accountId, uid, folderId) {
1056
+ const sql = folderId != null
1057
+ ? "SELECT * FROM messages WHERE account_id = ? AND uid = ? AND folder_id = ?"
1058
+ : "SELECT * FROM messages WHERE account_id = ? AND uid = ?";
1059
+ const params = folderId != null ? [accountId, uid, folderId] : [accountId, uid];
1060
+ const r = this.db.prepare(sql).get(...params);
1061
+ if (!r)
1062
+ return null;
1063
+ return this.rowToEnvelope(r);
1064
+ }
1065
+ /** Look up a message by its stable local UUID. Returned envelope includes
1066
+ * the current (folder_id, uid) — these may have changed since the UUID
1067
+ * was minted (folder move or server UID renumber) but the UUID itself
1068
+ * is stable. Use this as the identity in any long-lived reference
1069
+ * (compose in-reply-to, dally, undo stacks). */
1070
+ getMessageByUuid(uuid) {
1071
+ if (!uuid)
1072
+ return null;
1073
+ const r = this.db.prepare("SELECT * FROM messages WHERE uuid = ?").get(uuid);
1074
+ if (!r)
1075
+ return null;
1076
+ return this.rowToEnvelope(r);
1077
+ }
267
1078
  getMessageBodyPath(accountId, uid) {
268
1079
  const r = this.db.prepare("SELECT body_path FROM messages WHERE account_id = ? AND uid = ?").get(accountId, uid);
269
1080
  return r?.body_path || "";
@@ -271,10 +1082,40 @@ export class MailxDB {
271
1082
  updateMessageFlags(accountId, uid, flags) {
272
1083
  this.db.prepare("UPDATE messages SET flags_json = ? WHERE account_id = ? AND uid = ?").run(JSON.stringify(flags), accountId, uid);
273
1084
  }
1085
+ updateMessageFolder(accountId, uid, targetFolderId) {
1086
+ // Idempotency: if a row already exists at (account, target_folder, uid)
1087
+ // — common with Gmail's hash-synthesized UIDs across labels, or any
1088
+ // case where the move was already partially applied — the UPDATE
1089
+ // would fail the (acct, folder, uid) unique constraint. Treat it as
1090
+ // a no-op: drop the source row, the message is already where the
1091
+ // user wants it. Previously surfaced as "Mark-as-spam failed: UNIQUE
1092
+ // constraint failed" — bad UX for what is logically already done.
1093
+ const existingTarget = this.db.prepare("SELECT id FROM messages WHERE account_id = ? AND folder_id = ? AND uid = ?").get(accountId, targetFolderId, uid);
1094
+ if (existingTarget) {
1095
+ this.db.prepare("DELETE FROM messages WHERE account_id = ? AND uid = ? AND folder_id != ?").run(accountId, uid, targetFolderId);
1096
+ return;
1097
+ }
1098
+ this.db.prepare("UPDATE messages SET folder_id = ? WHERE account_id = ? AND uid = ?").run(targetFolderId, accountId, uid);
1099
+ }
1100
+ updateBodyPath(accountId, uid, bodyPath) {
1101
+ this.db.prepare("UPDATE messages SET body_path = ? WHERE account_id = ? AND uid = ?").run(bodyPath, accountId, uid);
1102
+ }
1103
+ /** Get messages without cached bodies (for background prefetch) */
1104
+ getMessagesWithoutBody(accountId, limit = 50) {
1105
+ return this.db.prepare("SELECT uid, folder_id as folderId FROM messages WHERE account_id = ? AND (body_path IS NULL OR body_path = '') ORDER BY date DESC LIMIT ?").all(accountId, limit);
1106
+ }
274
1107
  getHighestUid(accountId, folderId) {
275
1108
  const r = this.db.prepare("SELECT MAX(uid) as maxUid FROM messages WHERE account_id = ? AND folder_id = ?").get(accountId, folderId);
276
1109
  return r?.maxUid || 0;
277
1110
  }
1111
+ getOldestDate(accountId, folderId) {
1112
+ const r = this.db.prepare("SELECT MIN(date) as minDate FROM messages WHERE account_id = ? AND folder_id = ?").get(accountId, folderId);
1113
+ return r?.minDate || 0;
1114
+ }
1115
+ getMessageCount(accountId, folderId) {
1116
+ const r = this.db.prepare("SELECT count(*) as cnt FROM messages WHERE account_id = ? AND folder_id = ?").get(accountId, folderId);
1117
+ return r?.cnt || 0;
1118
+ }
278
1119
  /** Get all UIDs for a folder */
279
1120
  getUidsForFolder(accountId, folderId) {
280
1121
  const rows = this.db.prepare("SELECT uid FROM messages WHERE account_id = ? AND folder_id = ?").all(accountId, folderId);
@@ -282,7 +1123,19 @@ export class MailxDB {
282
1123
  }
283
1124
  /** Delete a message by account + UID */
284
1125
  deleteMessage(accountId, uid) {
1126
+ // Get folderId before deleting so we can update counts
1127
+ const msg = this.db.prepare("SELECT folder_id FROM messages WHERE account_id = ? AND uid = ?").get(accountId, uid);
285
1128
  this.db.prepare("DELETE FROM messages WHERE account_id = ? AND uid = ?").run(accountId, uid);
1129
+ // Refresh folder counts
1130
+ if (msg)
1131
+ this.recalcFolderCounts(msg.folder_id);
1132
+ }
1133
+ /** Recalculate folder total/unread counts from actual messages */
1134
+ recalcFolderCounts(folderId) {
1135
+ const counts = this.db.prepare(`SELECT COUNT(*) as total,
1136
+ SUM(CASE WHEN flags_json NOT LIKE '%\\\\Seen%' THEN 1 ELSE 0 END) as unread
1137
+ FROM messages WHERE folder_id = ?`).get(folderId);
1138
+ this.updateFolderCounts(folderId, counts?.total || 0, counts?.unread || 0);
286
1139
  }
287
1140
  /** Bulk insert within a transaction for sync performance */
288
1141
  beginTransaction() { this.db.exec("BEGIN"); }
@@ -291,51 +1144,455 @@ export class MailxDB {
291
1144
  // ── Contacts ──
292
1145
  /** Record an address used in sent mail */
293
1146
  recordSentAddress(name, email) {
1147
+ // Don't pollute the contacts table with non-addresses.
1148
+ if (!email || !/^[^\s<>@]+@[^\s<>@]+\.[^\s<>@]+$/.test(email))
1149
+ return;
1150
+ const lower = email.toLowerCase();
1151
+ if (this.isAddressDenylisted(lower))
1152
+ return;
294
1153
  const now = Date.now();
295
- const existing = this.db.prepare("SELECT id FROM contacts WHERE email = ?").get(email);
1154
+ // discovered tier holds one row per email bump if present, else
1155
+ // insert. Doesn't touch preferred or google rows for the same email;
1156
+ // those are independent address-book entries the user/Google curates.
1157
+ const existing = this.db.prepare("SELECT id, name FROM contacts WHERE source = 'discovered' AND lower(email) = ?").get(lower);
296
1158
  if (existing) {
297
- this.db.prepare("UPDATE contacts SET name = CASE WHEN ? != '' THEN ? ELSE name END, last_used = ?, use_count = use_count + 1, updated_at = ? WHERE email = ?").run(name, name, now, now, email);
1159
+ this.db.prepare("UPDATE contacts SET name = CASE WHEN name = '' AND ? != '' THEN ? ELSE name END, last_used = ?, use_count = use_count + 1, updated_at = ? WHERE id = ?").run(name, name, now, now, existing.id);
298
1160
  }
299
1161
  else {
300
- this.db.prepare("INSERT INTO contacts (source, name, email, last_used, use_count, updated_at) VALUES ('sent', ?, ?, ?, 1, ?)").run(name, email, now, now);
1162
+ this.db.prepare("INSERT INTO contacts (source, name, email, last_used, use_count, updated_at) VALUES ('discovered', ?, ?, ?, 1, ?)").run(name || "", email, now, now);
301
1163
  }
1164
+ this.notifyContactsChanged();
1165
+ }
1166
+ /** True if `email` (lowercased) appears in the active denylist. Cached
1167
+ * in-memory; refreshed on contacts.jsonc reload via setContactsDenylist. */
1168
+ _denylist = new Set();
1169
+ isAddressDenylisted(emailLower) {
1170
+ return this._denylist.has(emailLower);
302
1171
  }
303
- /** Seed contacts from all message senders in the DB */
1172
+ setContactsDenylist(emails) {
1173
+ this._denylist = new Set(emails.map(e => (e || "").trim().toLowerCase()).filter(Boolean));
1174
+ }
1175
+ /** Callback fired when local-DB contacts mutations happen (sends adding
1176
+ * to discovered, corpus seeder finding new addresses). The service
1177
+ * registers a debounced cloud flush here so the GDrive copy stays in
1178
+ * sync. NOT fired from applyContactsConfig — that's the inbound path
1179
+ * and would create a write loop. */
1180
+ _onContactsChanged;
1181
+ setOnContactsChanged(cb) {
1182
+ this._onContactsChanged = cb;
1183
+ }
1184
+ notifyContactsChanged() {
1185
+ try {
1186
+ this._onContactsChanged?.();
1187
+ }
1188
+ catch { /* ignore */ }
1189
+ }
1190
+ /** Seed `discovered`-tier contacts from every address that appears in
1191
+ * any cached message — From / To / Cc / Bcc across all folders. One row
1192
+ * per email; first non-empty name observed wins. Sent-folder rows skip
1193
+ * the From (it's us). Junk addresses (noreply, mailer-daemon, *-bounces)
1194
+ * and denylisted addresses are dropped at seed time so they never enter
1195
+ * autocomplete.
1196
+ *
1197
+ * Discovered is a single tier; sub-distinctions like sent-vs-received
1198
+ * collapse here because the user-facing UI shows them as one "discovered"
1199
+ * source. Recency-weighted use_count differentiates within the tier. */
304
1200
  seedContactsFromMessages() {
1201
+ const VALID = /^[^\s<>@]+@[^\s<>@]+\.[^\s<>@]+$/;
305
1202
  const now = Date.now();
306
- const rows = this.db.prepare(`SELECT from_name, from_address, COUNT(*) as cnt, MAX(date) as last
307
- FROM messages
308
- WHERE from_address != ''
309
- GROUP BY from_address`).all();
1203
+ const agg = new Map();
1204
+ const bump = (name, address, date) => {
1205
+ const email = (address || "").trim().toLowerCase();
1206
+ if (!email || !VALID.test(email))
1207
+ return;
1208
+ if (isJunkContact(email, name))
1209
+ return;
1210
+ if (this.isAddressDenylisted(email))
1211
+ return;
1212
+ const e = agg.get(email);
1213
+ if (e) {
1214
+ e.cnt++;
1215
+ if (date > e.last)
1216
+ e.last = date;
1217
+ if (!e.name && name)
1218
+ e.name = name;
1219
+ }
1220
+ else {
1221
+ agg.set(email, { name: name || "", cnt: 1, last: date || 0 });
1222
+ }
1223
+ };
1224
+ // Sent folder: recipients only (skip the user's own From address).
1225
+ const sentRows = this.db.prepare(`SELECT m.to_json, m.cc_json, m.bcc_json, m.date
1226
+ FROM messages m
1227
+ JOIN folders f ON m.folder_id = f.id
1228
+ WHERE f.special_use = 'sent'`).all();
1229
+ for (const r of sentRows) {
1230
+ const date = r.date || 0;
1231
+ for (const field of [r.to_json, r.cc_json, r.bcc_json]) {
1232
+ if (!field)
1233
+ continue;
1234
+ let parsed;
1235
+ try {
1236
+ parsed = JSON.parse(field);
1237
+ }
1238
+ catch {
1239
+ continue;
1240
+ }
1241
+ if (!Array.isArray(parsed))
1242
+ continue;
1243
+ for (const a of parsed) {
1244
+ if (!a)
1245
+ continue;
1246
+ bump(a.name || "", a.address || a.email || "", date);
1247
+ }
1248
+ }
1249
+ }
1250
+ // Other folders: From + recipients.
1251
+ const recvRows = this.db.prepare(`SELECT m.from_name, m.from_address, m.to_json, m.cc_json, m.bcc_json, m.date
1252
+ FROM messages m
1253
+ LEFT JOIN folders f ON m.folder_id = f.id
1254
+ WHERE f.special_use IS NULL OR f.special_use != 'sent'`).all();
1255
+ for (const r of recvRows) {
1256
+ const date = r.date || 0;
1257
+ bump(r.from_name, r.from_address, date);
1258
+ for (const field of [r.to_json, r.cc_json, r.bcc_json]) {
1259
+ if (!field)
1260
+ continue;
1261
+ let parsed;
1262
+ try {
1263
+ parsed = JSON.parse(field);
1264
+ }
1265
+ catch {
1266
+ continue;
1267
+ }
1268
+ if (!Array.isArray(parsed))
1269
+ continue;
1270
+ for (const a of parsed) {
1271
+ if (!a)
1272
+ continue;
1273
+ bump(a.name || "", a.address || a.email || "", date);
1274
+ }
1275
+ }
1276
+ }
310
1277
  let added = 0;
311
- for (const r of rows) {
312
- const existing = this.db.prepare("SELECT id FROM contacts WHERE email = ?").get(r.from_address);
1278
+ let bumped = 0;
1279
+ const insStmt = this.db.prepare("INSERT INTO contacts (source, name, email, last_used, use_count, updated_at) VALUES ('discovered', ?, ?, ?, ?, ?)");
1280
+ const updStmt = this.db.prepare(`UPDATE contacts SET use_count = ?,
1281
+ last_used = max(last_used, ?),
1282
+ name = CASE WHEN name = '' AND ? != '' THEN ? ELSE name END,
1283
+ updated_at = ?
1284
+ WHERE id = ?`);
1285
+ for (const [email, info] of agg) {
1286
+ const existing = this.db.prepare("SELECT id FROM contacts WHERE source = 'discovered' AND lower(email) = ?").get(email);
313
1287
  if (!existing) {
314
- this.db.prepare("INSERT INTO contacts (source, name, email, last_used, use_count, updated_at) VALUES ('received', ?, ?, ?, ?, ?)").run(r.from_name || "", r.from_address, r.last, r.cnt, now);
1288
+ insStmt.run(info.name, email, info.last, info.cnt, now);
315
1289
  added++;
316
1290
  }
1291
+ else {
1292
+ updStmt.run(info.cnt, info.last, info.name, info.name, now, existing.id);
1293
+ bumped++;
1294
+ }
1295
+ }
1296
+ if (added > 0 || bumped > 0) {
1297
+ console.log(` [contacts] seed: ${added} new + ${bumped} refreshed (discovered)`);
1298
+ this.notifyContactsChanged();
317
1299
  }
318
1300
  return added;
319
1301
  }
320
- /** Search contacts by name or email prefix */
1302
+ /** Apply the contents of contacts.jsonc replaces all preferred-tier rows
1303
+ * with the entries in `preferred[]`, merges `discovered[]` into the local
1304
+ * cache, sets the in-memory denylist, and purges any discovered rows
1305
+ * whose email is now denylisted. Preferred rows are *not* auto-purged on
1306
+ * denylist hit — if the user explicitly added them they win that
1307
+ * conflict; we just log a warning.
1308
+ *
1309
+ * Discovered rows from the file are MERGED with whatever the local
1310
+ * message-corpus seeder has produced. Each device contributes its
1311
+ * observed addresses; over time GDrive accumulates the union. */
1312
+ applyContactsConfig(cfg) {
1313
+ const preferred = Array.isArray(cfg.preferred) ? cfg.preferred : [];
1314
+ const denylist = Array.isArray(cfg.denylist) ? cfg.denylist : [];
1315
+ const discovered = Array.isArray(cfg.discovered) ? cfg.discovered : [];
1316
+ this.setContactsDenylist(denylist);
1317
+ // Wipe and rewrite preferred-tier rows owned by contacts.jsonc.
1318
+ // The address-book UI's legacy `upsertContact` still writes
1319
+ // source='manual' rows; those are owned by the address-book code
1320
+ // path, not contacts.jsonc, so we leave them alone here.
1321
+ this.db.exec("DELETE FROM contacts WHERE source NOT IN ('google', 'discovered', 'manual')");
1322
+ const VALID = /^[^\s<>@]+@[^\s<>@]+\.[^\s<>@]+$/;
1323
+ const now = Date.now();
1324
+ const ins = this.db.prepare(`INSERT OR IGNORE INTO contacts (source, name, email, organization, last_used, use_count, updated_at)
1325
+ VALUES (?, ?, ?, ?, 0, 0, ?)`);
1326
+ const denySet = new Set(denylist.map(e => (e || "").trim().toLowerCase()).filter(Boolean));
1327
+ const conflicts = [];
1328
+ let inserted = 0;
1329
+ for (const entry of preferred) {
1330
+ if (!entry)
1331
+ continue;
1332
+ const email = (entry.email || "").trim();
1333
+ if (!email || !VALID.test(email))
1334
+ continue;
1335
+ if (denySet.has(email.toLowerCase())) {
1336
+ conflicts.push(email);
1337
+ continue;
1338
+ }
1339
+ const source = (entry.source || "preferred").trim() || "preferred";
1340
+ const name = (entry.name || "").trim();
1341
+ const org = (entry.organization || entry.org || "").trim();
1342
+ try {
1343
+ const r = ins.run(source, name, email, org, now);
1344
+ if (r.changes)
1345
+ inserted++;
1346
+ }
1347
+ catch { /* dup row, skip */ }
1348
+ }
1349
+ // Merge discovered[] from cloud into local cache. For each entry:
1350
+ // existing row wins on use_count (max), name fills if empty, lastUsed
1351
+ // is max. Missing rows are inserted. Denylisted entries skipped.
1352
+ const insDiscovered = this.db.prepare("INSERT INTO contacts (source, name, email, last_used, use_count, updated_at) VALUES ('discovered', ?, ?, ?, ?, ?)");
1353
+ const updDiscovered = this.db.prepare(`UPDATE contacts SET use_count = max(use_count, ?),
1354
+ last_used = max(last_used, ?),
1355
+ name = CASE WHEN name = '' AND ? != '' THEN ? ELSE name END,
1356
+ updated_at = ?
1357
+ WHERE id = ?`);
1358
+ let discoveredAdded = 0;
1359
+ for (const entry of discovered) {
1360
+ if (!entry)
1361
+ continue;
1362
+ const email = (entry.email || "").trim();
1363
+ if (!email || !VALID.test(email))
1364
+ continue;
1365
+ const lower = email.toLowerCase();
1366
+ if (denySet.has(lower))
1367
+ continue;
1368
+ if (isJunkContact(lower, entry.name || ""))
1369
+ continue;
1370
+ const name = (entry.name || "").trim();
1371
+ const useCount = Math.max(0, entry.useCount || 0);
1372
+ const lastUsed = Math.max(0, entry.lastUsed || 0);
1373
+ const existing = this.db.prepare("SELECT id FROM contacts WHERE source = 'discovered' AND lower(email) = ?").get(lower);
1374
+ if (!existing) {
1375
+ insDiscovered.run(name, email, lastUsed, useCount, now);
1376
+ discoveredAdded++;
1377
+ }
1378
+ else {
1379
+ updDiscovered.run(useCount, lastUsed, name, name, now, existing.id);
1380
+ }
1381
+ }
1382
+ // Purge discovered rows for any denylisted email.
1383
+ const purge = this.db.prepare("DELETE FROM contacts WHERE source = 'discovered' AND lower(email) = ?");
1384
+ let purged = 0;
1385
+ for (const e of denySet) {
1386
+ const r = purge.run(e);
1387
+ purged += Number(r.changes || 0);
1388
+ }
1389
+ if (conflicts.length > 0) {
1390
+ console.warn(` [contacts] config: ${conflicts.length} preferred entries also appear in denylist — denylist wins, entries skipped: ${conflicts.join(", ")}`);
1391
+ }
1392
+ console.log(` [contacts] config applied: ${inserted} preferred + ${discoveredAdded} discovered row(s), ${denySet.size} denylisted, ${purged} discovered row(s) purged`);
1393
+ return { preferred: inserted, discovered: discoveredAdded, purged, conflicts };
1394
+ }
1395
+ /** Build the contacts.jsonc shape from current DB state — for round-trip
1396
+ * to GDrive. Preferred-tier rows come from anything not in the reserved
1397
+ * system sources; discovered comes from `source='discovered'` rows;
1398
+ * denylist comes from the in-memory set (set by applyContactsConfig).
1399
+ * Caller is responsible for actually writing the cloud copy. */
1400
+ exportContactsConfig() {
1401
+ const preferredRows = this.db.prepare(`SELECT name, email, source, organization
1402
+ FROM contacts
1403
+ WHERE source NOT IN ('google', 'discovered', 'manual')
1404
+ ORDER BY source, lower(email), lower(name)`).all();
1405
+ const discoveredRows = this.db.prepare(`SELECT name, email, use_count, last_used
1406
+ FROM contacts
1407
+ WHERE source = 'discovered'
1408
+ ORDER BY use_count DESC, last_used DESC, lower(email)`).all();
1409
+ return {
1410
+ preferred: preferredRows.map(r => {
1411
+ const out = { name: r.name || "", email: r.email, source: r.source };
1412
+ if (r.organization)
1413
+ out.organization = r.organization;
1414
+ return out;
1415
+ }),
1416
+ denylist: Array.from(this._denylist),
1417
+ discovered: discoveredRows.map(r => ({
1418
+ name: r.name || "",
1419
+ email: r.email,
1420
+ useCount: r.use_count,
1421
+ lastUsed: r.last_used,
1422
+ })),
1423
+ };
1424
+ }
1425
+ /** Search contacts by name or email prefix.
1426
+ *
1427
+ * Source-tier bonus is what makes the curated address book win against
1428
+ * passive corpus harvest. Anything in `contacts.jsonc#preferred[]` (any
1429
+ * source value other than the two reserved system sources) gets the
1430
+ * highest tier — that's the user's explicit address book and overrides
1431
+ * Google. Google sits in the middle (the auto-synced address book).
1432
+ * `discovered` is the corpus-harvested floor.
1433
+ *
1434
+ * Multi-name-per-email is supported: the same email can carry distinct
1435
+ * (source, name) rows — Bob's wife and Bob Smith both at bob@example.com
1436
+ * surface as two rows, each typing-completable by their own name. */
321
1437
  searchContacts(query, limit = 10) {
1438
+ query = (query || "").trim();
1439
+ if (!query)
1440
+ return [];
1441
+ // Split into whitespace-separated tokens. Each token must appear in
1442
+ // name or email — order- and adjacency-independent. So "eleanor elkin"
1443
+ // matches "Eleanor Elkin", "Elkin, Eleanor", "Eleanor M Elkin", and
1444
+ // "elkin@eleanor.example". The first token gets the prefix bonus for
1445
+ // ranking; remaining tokens just have to be present.
1446
+ const tokens = query.split(/\s+/).filter(Boolean);
1447
+ const firstSubstr = `%${tokens[0]}%`;
1448
+ const firstPrefix = `${tokens[0]}%`;
1449
+ const tokenWhere = tokens.map(() => "(name LIKE ? OR email LIKE ?)").join(" AND ");
1450
+ const tokenParams = [];
1451
+ for (const t of tokens) {
1452
+ tokenParams.push(`%${t}%`, `%${t}%`);
1453
+ }
1454
+ let rows;
1455
+ try {
1456
+ // Source tier: anything not in the two reserved system sources
1457
+ // ('google', 'discovered') is preferred-tier — i.e. came out of
1458
+ // contacts.jsonc#preferred[]. The user's `source: "work"` /
1459
+ // `source: "family"` tags all rank +40 alongside the default
1460
+ // `preferred` label.
1461
+ rows = this.db.prepare(`SELECT name, email, source, use_count, last_used,
1462
+ (CASE
1463
+ WHEN lower(name) LIKE lower(?) THEN 3
1464
+ WHEN substr(email, 1, instr(email, '@') - 1) LIKE lower(?) THEN 2
1465
+ WHEN email LIKE ? OR name LIKE ? THEN 1
1466
+ ELSE 0
1467
+ END) +
1468
+ (CASE
1469
+ WHEN source = 'google' THEN 30
1470
+ WHEN source = 'discovered' THEN 0
1471
+ ELSE 40
1472
+ END) AS match_rank
1473
+ FROM contacts
1474
+ WHERE ${tokenWhere}
1475
+ ORDER BY match_rank DESC, use_count DESC, last_used DESC
1476
+ LIMIT ?`).all(firstPrefix, firstPrefix, firstSubstr, firstSubstr, ...tokenParams, limit * 2);
1477
+ }
1478
+ catch (e) {
1479
+ console.error(` [searchContacts] ranked query failed (${e?.message}) — falling back to simple LIKE`);
1480
+ rows = this.db.prepare(`SELECT name, email, source, use_count, last_used, 0 AS match_rank
1481
+ FROM contacts
1482
+ WHERE ${tokenWhere}
1483
+ ORDER BY use_count DESC, last_used DESC
1484
+ LIMIT ?`).all(...tokenParams, limit * 2);
1485
+ }
1486
+ // Filter out denylisted emails as a defense-in-depth — applyContactsConfig
1487
+ // already purges discovered rows on denylist, but a Google sync that
1488
+ // reintroduced a denylisted address would otherwise leak through.
1489
+ rows = rows.filter(r => !this.isAddressDenylisted((r.email || "").toLowerCase()));
1490
+ const now = Date.now();
1491
+ const HALF_LIFE_MS = 30 * 86400_000;
1492
+ const score = (r) => (r.match_rank || 0) * 10_000
1493
+ + (r.use_count || 0) * Math.pow(0.5, Math.max(0, now - (r.last_used || 0)) / HALF_LIFE_MS);
1494
+ rows.sort((a, b) => score(b) - score(a));
1495
+ rows = rows.slice(0, limit);
1496
+ return rows.map(r => ({ name: r.name, email: r.email, source: r.source, useCount: r.use_count }));
1497
+ }
1498
+ /** List all contacts (address-book view) with pagination + optional filter. */
1499
+ listContacts(query, page = 1, pageSize = 100) {
1500
+ query = (query || "").trim();
1501
+ const hasQuery = !!query;
322
1502
  const q = `%${query}%`;
323
- const rows = this.db.prepare(`SELECT name, email, source, use_count FROM contacts
324
- WHERE email LIKE ? OR name LIKE ?
1503
+ const whereClause = hasQuery ? "WHERE email LIKE ? OR name LIKE ?" : "";
1504
+ const params = hasQuery ? [q, q] : [];
1505
+ const totalRow = this.db.prepare(`SELECT COUNT(*) as c FROM contacts ${whereClause}`).get(...params);
1506
+ const offset = (page - 1) * pageSize;
1507
+ const rows = this.db.prepare(`SELECT name, email, source, google_id, use_count, last_used FROM contacts
1508
+ ${whereClause}
325
1509
  ORDER BY use_count DESC, last_used DESC
326
- LIMIT ?`).all(q, q, limit);
327
- return rows.map(r => ({ name: r.name, email: r.email, source: r.source, useCount: r.use_count }));
1510
+ LIMIT ? OFFSET ?`).all(...params, pageSize, offset);
1511
+ return {
1512
+ items: rows.map(r => ({
1513
+ name: r.name, email: r.email, source: r.source,
1514
+ googleId: r.google_id || null,
1515
+ useCount: r.use_count, lastUsed: r.last_used,
1516
+ })),
1517
+ total: totalRow?.c || 0,
1518
+ page, pageSize,
1519
+ };
1520
+ }
1521
+ /** Update or insert a contact manually (from the address book UI). */
1522
+ upsertContact(name, email) {
1523
+ if (!email || !/^[^\s<>@]+@[^\s<>@]+\.[^\s<>@]+$/.test(email)) {
1524
+ throw new Error(`Invalid email: ${email}`);
1525
+ }
1526
+ const now = Date.now();
1527
+ const existing = this.db.prepare("SELECT id FROM contacts WHERE email = ?").get(email);
1528
+ if (existing) {
1529
+ this.db.prepare("UPDATE contacts SET name = ?, updated_at = ? WHERE email = ?").run(name, now, email);
1530
+ }
1531
+ else {
1532
+ this.db.prepare("INSERT INTO contacts (source, name, email, last_used, use_count, updated_at) VALUES ('manual', ?, ?, ?, 0, ?)").run(name, email, now, now);
1533
+ }
1534
+ }
1535
+ /** Delete a contact by email (address book UI). */
1536
+ deleteContact(email) {
1537
+ this.db.prepare("DELETE FROM contacts WHERE email = ?").run(email);
1538
+ }
1539
+ /** Delete contact rows by Google People resourceName. Used by the
1540
+ * incremental People sync when a person comes back with `metadata.deleted = true`
1541
+ * — the email may have already changed/disappeared, but the resourceName
1542
+ * is stable. Removes all rows tied to that Google identity (a single
1543
+ * contact can have multiple email addresses, each is its own row). */
1544
+ deleteContactByGoogleId(googleId) {
1545
+ if (!googleId)
1546
+ return 0;
1547
+ const r = this.db.prepare("DELETE FROM contacts WHERE google_id = ?").run(googleId);
1548
+ return Number(r.changes || 0);
328
1549
  }
329
1550
  // ── Search ──
330
1551
  /** Full-text search across all messages. Supports qualifiers: from:, to:, subject: */
331
- searchMessages(query, page = 1, pageSize = 50) {
332
- // Parse qualifiers
1552
+ searchMessages(query, page = 1, pageSize = 50, accountId, folderId) {
1553
+ query = (query || "").trim();
1554
+ // Parse qualifiers (C45: extended set — date:, has:, is:, folder:).
333
1555
  let ftsQuery = "";
334
1556
  const parts = query.match(/(?:[^\s"]+|"[^"]*")+/g) || [];
1557
+ // Extra SQL where-clauses for qualifiers that don't map to FTS columns.
1558
+ const extraWhere = [];
1559
+ const extraParams = [];
1560
+ // Parse a "1d", "1w", "2024-01-15", "yesterday", "today" etc. into ms epoch.
1561
+ const parseRel = (s) => {
1562
+ const lower = s.toLowerCase().trim();
1563
+ if (lower === "today") {
1564
+ const d = new Date();
1565
+ d.setHours(0, 0, 0, 0);
1566
+ return d.getTime();
1567
+ }
1568
+ if (lower === "yesterday") {
1569
+ const d = new Date();
1570
+ d.setHours(0, 0, 0, 0);
1571
+ return d.getTime() - 86400_000;
1572
+ }
1573
+ const rel = lower.match(/^(\d+)([dwmy])$/);
1574
+ if (rel) {
1575
+ const n = parseInt(rel[1]);
1576
+ const unit = rel[2];
1577
+ const ms = unit === "d" ? n * 86400_000
1578
+ : unit === "w" ? n * 7 * 86400_000
1579
+ : unit === "m" ? n * 30 * 86400_000
1580
+ : n * 365 * 86400_000;
1581
+ return Date.now() - ms;
1582
+ }
1583
+ const ts = Date.parse(s);
1584
+ return isNaN(ts) ? null : ts;
1585
+ };
335
1586
  for (const part of parts) {
336
1587
  const fromMatch = part.match(/^from:(.+)$/i);
337
1588
  const toMatch = part.match(/^to:(.+)$/i);
338
1589
  const subjectMatch = part.match(/^subject:(.+)$/i);
1590
+ const dateMatch = part.match(/^date:([><]?=?)(.+)$/i);
1591
+ const afterMatch = part.match(/^after:(.+)$/i);
1592
+ const beforeMatch = part.match(/^before:(.+)$/i);
1593
+ const hasMatch = part.match(/^has:(.+)$/i);
1594
+ const isMatch = part.match(/^is:(.+)$/i);
1595
+ const folderMatch = part.match(/^folder:(.+)$/i);
339
1596
  if (fromMatch) {
340
1597
  const term = fromMatch[1].replace(/"/g, "");
341
1598
  ftsQuery += `(from_name:${term} OR from_address:${term}) `;
@@ -348,27 +1605,95 @@ export class MailxDB {
348
1605
  const term = subjectMatch[1].replace(/"/g, "");
349
1606
  ftsQuery += `subject:${term} `;
350
1607
  }
1608
+ else if (dateMatch || afterMatch || beforeMatch) {
1609
+ const op = dateMatch ? (dateMatch[1] || "=") : (afterMatch ? ">" : "<");
1610
+ const valStr = dateMatch ? dateMatch[2] : (afterMatch ? afterMatch[1] : beforeMatch[1]);
1611
+ const ts = parseRel(valStr.replace(/"/g, ""));
1612
+ if (ts !== null) {
1613
+ if (op === ">" || op === ">=") {
1614
+ extraWhere.push("m.date >= ?");
1615
+ extraParams.push(ts);
1616
+ }
1617
+ else if (op === "<" || op === "<=") {
1618
+ extraWhere.push("m.date <= ?");
1619
+ extraParams.push(ts);
1620
+ }
1621
+ else {
1622
+ extraWhere.push("m.date >= ? AND m.date < ?");
1623
+ extraParams.push(ts, ts + 86400_000);
1624
+ }
1625
+ }
1626
+ }
1627
+ else if (hasMatch) {
1628
+ const v = hasMatch[1].toLowerCase().replace(/"/g, "");
1629
+ if (v === "attachment" || v === "attachments") {
1630
+ extraWhere.push("m.has_attachments = 1");
1631
+ }
1632
+ }
1633
+ else if (isMatch) {
1634
+ const v = isMatch[1].toLowerCase().replace(/"/g, "");
1635
+ if (v === "flagged" || v === "starred")
1636
+ extraWhere.push("m.flags_json LIKE '%\\\\Flagged%'");
1637
+ else if (v === "unread")
1638
+ extraWhere.push("m.flags_json NOT LIKE '%\\\\Seen%'");
1639
+ else if (v === "read" || v === "seen")
1640
+ extraWhere.push("m.flags_json LIKE '%\\\\Seen%'");
1641
+ else if (v === "answered")
1642
+ extraWhere.push("m.flags_json LIKE '%\\\\Answered%'");
1643
+ else if (v === "draft")
1644
+ extraWhere.push("m.flags_json LIKE '%\\\\Draft%'");
1645
+ }
1646
+ else if (folderMatch) {
1647
+ const v = folderMatch[1].replace(/"/g, "");
1648
+ extraWhere.push("LOWER(f.name) LIKE ?");
1649
+ extraParams.push(`%${v.toLowerCase()}%`);
1650
+ }
351
1651
  else {
352
- // Unqualified — search everything
353
- ftsQuery += `${part}* `;
1652
+ // Unqualified — search everything.
1653
+ let term = part.replace(/^\/|\/$/g, "");
1654
+ if (term.includes("|")) {
1655
+ const alts = term.split("|").filter(Boolean).map(t => `${t}*`).join(" OR ");
1656
+ ftsQuery += `(${alts}) `;
1657
+ }
1658
+ else {
1659
+ ftsQuery += `${term}* `;
1660
+ }
354
1661
  }
355
1662
  }
356
1663
  ftsQuery = ftsQuery.trim();
1664
+ // If the user typed only qualifier-only terms (e.g. "is:flagged after:1w"),
1665
+ // FTS query is empty — match-all surrogate.
357
1666
  if (!ftsQuery)
358
- return { items: [], total: 0, page, pageSize };
1667
+ ftsQuery = "*";
359
1668
  const offset = (page - 1) * pageSize;
360
1669
  try {
361
- const countRow = this.db.prepare("SELECT COUNT(*) as cnt FROM messages_fts WHERE messages_fts MATCH ?").get(ftsQuery);
1670
+ let scopeWhere = "";
1671
+ const scopeParams = [];
1672
+ if (accountId && folderId) {
1673
+ scopeWhere = " AND m.account_id = ? AND m.folder_id = ?";
1674
+ scopeParams.push(accountId, folderId);
1675
+ }
1676
+ else if (accountId) {
1677
+ scopeWhere = " AND m.account_id = ?";
1678
+ scopeParams.push(accountId);
1679
+ }
1680
+ if (extraWhere.length > 0) {
1681
+ scopeWhere += " AND " + extraWhere.join(" AND ");
1682
+ scopeParams.push(...extraParams);
1683
+ }
1684
+ const countRow = this.db.prepare(`SELECT COUNT(*) as cnt FROM messages m JOIN messages_fts fts ON m.id = fts.rowid WHERE messages_fts MATCH ?${scopeWhere}`).get(ftsQuery, ...scopeParams);
362
1685
  const total = countRow?.cnt || 0;
363
- const rows = this.db.prepare(`SELECT m.* FROM messages m
1686
+ const rows = this.db.prepare(`SELECT m.*, f.name AS folder_name FROM messages m
364
1687
  JOIN messages_fts fts ON m.id = fts.rowid
365
- WHERE messages_fts MATCH ?
1688
+ LEFT JOIN folders f ON f.id = m.folder_id AND f.account_id = m.account_id
1689
+ WHERE messages_fts MATCH ?${scopeWhere}
366
1690
  ORDER BY m.date DESC
367
- LIMIT ? OFFSET ?`).all(ftsQuery, pageSize, offset);
1691
+ LIMIT ? OFFSET ?`).all(ftsQuery, ...scopeParams, pageSize, offset);
368
1692
  const items = rows.map(r => ({
369
1693
  id: r.id,
370
1694
  accountId: r.account_id,
371
1695
  folderId: r.folder_id,
1696
+ folderName: r.folder_name || "",
372
1697
  uid: r.uid,
373
1698
  messageId: r.message_id || "",
374
1699
  inReplyTo: r.in_reply_to || "",
@@ -401,18 +1726,28 @@ export class MailxDB {
401
1726
  subject, from_name, from_address, to_text, cc_text, body_text,
402
1727
  content=messages, content_rowid=id
403
1728
  )`);
1729
+ // Use a single transaction + prepared statement for speed (~50x faster than individual inserts)
1730
+ const insert = this.db.prepare("INSERT INTO messages_fts (rowid, subject, from_name, from_address, to_text, cc_text, body_text) VALUES (?, ?, ?, ?, ?, ?, ?)");
404
1731
  const rows = this.db.prepare("SELECT id, subject, from_name, from_address, to_json, cc_json, preview FROM messages").all();
405
1732
  let count = 0;
406
- for (const r of rows) {
407
- const to = JSON.parse(r.to_json || "[]");
408
- const cc = JSON.parse(r.cc_json || "[]");
409
- const toText = to.map((a) => `${a.name} ${a.address}`).join(" ");
410
- const ccText = cc.map((a) => `${a.name} ${a.address}`).join(" ");
411
- try {
412
- this.db.prepare("INSERT INTO messages_fts (rowid, subject, from_name, from_address, to_text, cc_text, body_text) VALUES (?, ?, ?, ?, ?, ?, ?)").run(r.id, r.subject, r.from_name, r.from_address, toText, ccText, r.preview);
413
- count++;
1733
+ this.db.exec("BEGIN");
1734
+ try {
1735
+ for (const r of rows) {
1736
+ const to = JSON.parse(r.to_json || "[]");
1737
+ const cc = JSON.parse(r.cc_json || "[]");
1738
+ const toText = to.map((a) => `${a.name} ${a.address}`).join(" ");
1739
+ const ccText = cc.map((a) => `${a.name} ${a.address}`).join(" ");
1740
+ try {
1741
+ insert.run(r.id, r.subject, r.from_name, r.from_address, toText, ccText, r.preview);
1742
+ count++;
1743
+ }
1744
+ catch { /* skip duplicates */ }
414
1745
  }
415
- catch { /* skip duplicates */ }
1746
+ this.db.exec("COMMIT");
1747
+ }
1748
+ catch (e) {
1749
+ this.db.exec("ROLLBACK");
1750
+ throw e;
416
1751
  }
417
1752
  return count;
418
1753
  }