@bobfrankston/rmfmail 1.0.467

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 (330) hide show
  1. package/README.md +429 -0
  2. package/bin/build-icon-ico.js +283 -0
  3. package/bin/lean-accounts.js +172 -0
  4. package/bin/mailx.js +1580 -0
  5. package/bin/mailx.js.map +1 -0
  6. package/bin/mailx.ts +1549 -0
  7. package/bin/postinstall.js +39 -0
  8. package/bin/postinstall.js.map +1 -0
  9. package/bin/postinstall.ts +41 -0
  10. package/bin/tsconfig.json +10 -0
  11. package/client/.gitattributes +10 -0
  12. package/client/.msger-window.json +1 -0
  13. package/client/android.html +226 -0
  14. package/client/app.js +3400 -0
  15. package/client/app.js.map +1 -0
  16. package/client/app.ts +3190 -0
  17. package/client/components/address-book.js +199 -0
  18. package/client/components/address-book.js.map +1 -0
  19. package/client/components/address-book.ts +204 -0
  20. package/client/components/alarms.js +299 -0
  21. package/client/components/alarms.js.map +1 -0
  22. package/client/components/alarms.ts +276 -0
  23. package/client/components/calendar-sidebar.js +508 -0
  24. package/client/components/calendar-sidebar.js.map +1 -0
  25. package/client/components/calendar-sidebar.ts +474 -0
  26. package/client/components/calendar.js +217 -0
  27. package/client/components/calendar.js.map +1 -0
  28. package/client/components/calendar.ts +211 -0
  29. package/client/components/context-menu.js +80 -0
  30. package/client/components/context-menu.js.map +1 -0
  31. package/client/components/context-menu.ts +95 -0
  32. package/client/components/folder-picker.js +119 -0
  33. package/client/components/folder-picker.js.map +1 -0
  34. package/client/components/folder-picker.ts +127 -0
  35. package/client/components/folder-tree.js +1093 -0
  36. package/client/components/folder-tree.js.map +1 -0
  37. package/client/components/folder-tree.ts +1069 -0
  38. package/client/components/message-list.js +1140 -0
  39. package/client/components/message-list.js.map +1 -0
  40. package/client/components/message-list.ts +1129 -0
  41. package/client/components/message-viewer.js +1291 -0
  42. package/client/components/message-viewer.js.map +1 -0
  43. package/client/components/message-viewer.ts +1257 -0
  44. package/client/components/outbox-view.js +104 -0
  45. package/client/components/outbox-view.js.map +1 -0
  46. package/client/components/outbox-view.ts +102 -0
  47. package/client/components/tasks.js +261 -0
  48. package/client/components/tasks.js.map +1 -0
  49. package/client/components/tasks.ts +234 -0
  50. package/client/compose/compose.css +560 -0
  51. package/client/compose/compose.html +52 -0
  52. package/client/compose/compose.js +1289 -0
  53. package/client/compose/compose.js.map +1 -0
  54. package/client/compose/compose.ts +1231 -0
  55. package/client/compose/editor.js +616 -0
  56. package/client/compose/editor.js.map +1 -0
  57. package/client/compose/editor.ts +599 -0
  58. package/client/compose/ghost-text.js +122 -0
  59. package/client/compose/ghost-text.js.map +1 -0
  60. package/client/compose/ghost-text.ts +140 -0
  61. package/client/favicon.svg +11 -0
  62. package/client/icon.ico +0 -0
  63. package/client/icon.png +0 -0
  64. package/client/index.html +230 -0
  65. package/client/lib/android-bootstrap.js +9 -0
  66. package/client/lib/android-bootstrap.js.map +1 -0
  67. package/client/lib/android-bootstrap.ts +9 -0
  68. package/client/lib/api-client.js +378 -0
  69. package/client/lib/api-client.js.map +1 -0
  70. package/client/lib/api-client.ts +439 -0
  71. package/client/lib/local-service.js +603 -0
  72. package/client/lib/local-service.js.map +1 -0
  73. package/client/lib/local-service.ts +646 -0
  74. package/client/lib/local-store.js +214 -0
  75. package/client/lib/local-store.js.map +1 -0
  76. package/client/lib/local-store.ts +283 -0
  77. package/client/lib/mailxapi.js +304 -0
  78. package/client/lib/message-state.js +132 -0
  79. package/client/lib/message-state.js.map +1 -0
  80. package/client/lib/message-state.ts +160 -0
  81. package/client/package.json +16 -0
  82. package/client/styles/components.css +2049 -0
  83. package/client/styles/layout.css +332 -0
  84. package/client/styles/variables.css +85 -0
  85. package/client/tsconfig.json +19 -0
  86. package/package.json +124 -0
  87. package/packages/mailx-api/.gitattributes +10 -0
  88. package/packages/mailx-api/index.d.ts +10 -0
  89. package/packages/mailx-api/index.d.ts.map +1 -0
  90. package/packages/mailx-api/index.js +322 -0
  91. package/packages/mailx-api/index.js.map +1 -0
  92. package/packages/mailx-api/index.ts +283 -0
  93. package/packages/mailx-api/package.json +27 -0
  94. package/packages/mailx-api/tsconfig.json +9 -0
  95. package/packages/mailx-api/tsconfig.tsbuildinfo +1 -0
  96. package/packages/mailx-compose/.gitattributes +10 -0
  97. package/packages/mailx-compose/index.d.ts +21 -0
  98. package/packages/mailx-compose/index.d.ts.map +1 -0
  99. package/packages/mailx-compose/index.js +61 -0
  100. package/packages/mailx-compose/index.js.map +1 -0
  101. package/packages/mailx-compose/index.ts +85 -0
  102. package/packages/mailx-compose/package.json +18 -0
  103. package/packages/mailx-compose/tsconfig.json +9 -0
  104. package/packages/mailx-core/index.d.ts +139 -0
  105. package/packages/mailx-core/index.d.ts.map +1 -0
  106. package/packages/mailx-core/index.js +403 -0
  107. package/packages/mailx-core/index.js.map +1 -0
  108. package/packages/mailx-core/index.ts +424 -0
  109. package/packages/mailx-core/ipc.d.ts +13 -0
  110. package/packages/mailx-core/ipc.d.ts.map +1 -0
  111. package/packages/mailx-core/ipc.js +56 -0
  112. package/packages/mailx-core/ipc.js.map +1 -0
  113. package/packages/mailx-core/ipc.ts +62 -0
  114. package/packages/mailx-core/package.json +18 -0
  115. package/packages/mailx-core/tsconfig.json +9 -0
  116. package/packages/mailx-host/.gitattributes +10 -0
  117. package/packages/mailx-host/index.d.ts +21 -0
  118. package/packages/mailx-host/index.d.ts.map +1 -0
  119. package/packages/mailx-host/index.js +29 -0
  120. package/packages/mailx-host/index.js.map +1 -0
  121. package/packages/mailx-host/index.ts +38 -0
  122. package/packages/mailx-host/package.json +23 -0
  123. package/packages/mailx-host/tsconfig.json +9 -0
  124. package/packages/mailx-host/types-shim.d.ts +14 -0
  125. package/packages/mailx-imap/.gitattributes +10 -0
  126. package/packages/mailx-imap/index.d.ts +442 -0
  127. package/packages/mailx-imap/index.d.ts.map +1 -0
  128. package/packages/mailx-imap/index.js +3684 -0
  129. package/packages/mailx-imap/index.js.map +1 -0
  130. package/packages/mailx-imap/index.ts +3652 -0
  131. package/packages/mailx-imap/package-lock.json +131 -0
  132. package/packages/mailx-imap/package.json +28 -0
  133. package/packages/mailx-imap/providers/gmail-api.d.ts +8 -0
  134. package/packages/mailx-imap/providers/gmail-api.d.ts.map +1 -0
  135. package/packages/mailx-imap/providers/gmail-api.js +8 -0
  136. package/packages/mailx-imap/providers/gmail-api.js.map +1 -0
  137. package/packages/mailx-imap/providers/gmail-api.ts +8 -0
  138. package/packages/mailx-imap/providers/outlook-api.ts +7 -0
  139. package/packages/mailx-imap/providers/types.d.ts +9 -0
  140. package/packages/mailx-imap/providers/types.d.ts.map +1 -0
  141. package/packages/mailx-imap/providers/types.js +9 -0
  142. package/packages/mailx-imap/providers/types.js.map +1 -0
  143. package/packages/mailx-imap/providers/types.ts +9 -0
  144. package/packages/mailx-imap/tsconfig.json +9 -0
  145. package/packages/mailx-imap/tsconfig.tsbuildinfo +1 -0
  146. package/packages/mailx-send/.gitattributes +10 -0
  147. package/packages/mailx-send/README.md +217 -0
  148. package/packages/mailx-send/cli-queue.d.ts +11 -0
  149. package/packages/mailx-send/cli-queue.d.ts.map +1 -0
  150. package/packages/mailx-send/cli-queue.js +59 -0
  151. package/packages/mailx-send/cli-queue.js.map +1 -0
  152. package/packages/mailx-send/cli-queue.ts +62 -0
  153. package/packages/mailx-send/cli-send.d.ts +17 -0
  154. package/packages/mailx-send/cli-send.d.ts.map +1 -0
  155. package/packages/mailx-send/cli-send.js +75 -0
  156. package/packages/mailx-send/cli-send.js.map +1 -0
  157. package/packages/mailx-send/cli-send.ts +83 -0
  158. package/packages/mailx-send/cli.d.ts +22 -0
  159. package/packages/mailx-send/cli.d.ts.map +1 -0
  160. package/packages/mailx-send/cli.js +115 -0
  161. package/packages/mailx-send/cli.js.map +1 -0
  162. package/packages/mailx-send/cli.ts +126 -0
  163. package/packages/mailx-send/index.d.ts +77 -0
  164. package/packages/mailx-send/index.d.ts.map +1 -0
  165. package/packages/mailx-send/index.js +264 -0
  166. package/packages/mailx-send/index.js.map +1 -0
  167. package/packages/mailx-send/index.ts +333 -0
  168. package/packages/mailx-send/mailsend/README.md +133 -0
  169. package/packages/mailx-send/mailsend/cli.d.ts +12 -0
  170. package/packages/mailx-send/mailsend/cli.d.ts.map +1 -0
  171. package/packages/mailx-send/mailsend/cli.js +70 -0
  172. package/packages/mailx-send/mailsend/cli.js.map +1 -0
  173. package/packages/mailx-send/mailsend/cli.ts +81 -0
  174. package/packages/mailx-send/mailsend/index.d.ts +77 -0
  175. package/packages/mailx-send/mailsend/index.d.ts.map +1 -0
  176. package/packages/mailx-send/mailsend/index.js +264 -0
  177. package/packages/mailx-send/mailsend/index.js.map +1 -0
  178. package/packages/mailx-send/mailsend/index.ts +333 -0
  179. package/packages/mailx-send/mailsend/package-lock.json +65 -0
  180. package/packages/mailx-send/mailsend/package.json +26 -0
  181. package/packages/mailx-send/mailsend/tsconfig.json +21 -0
  182. package/packages/mailx-send/package-lock.json +65 -0
  183. package/packages/mailx-send/package.json +37 -0
  184. package/packages/mailx-send/tsconfig.json +21 -0
  185. package/packages/mailx-server/.gitattributes +10 -0
  186. package/packages/mailx-server/index.d.ts +8 -0
  187. package/packages/mailx-server/index.d.ts.map +1 -0
  188. package/packages/mailx-server/index.js +397 -0
  189. package/packages/mailx-server/index.js.map +1 -0
  190. package/packages/mailx-server/index.ts +429 -0
  191. package/packages/mailx-server/package.json +28 -0
  192. package/packages/mailx-server/tsconfig.json +9 -0
  193. package/packages/mailx-server/tsconfig.tsbuildinfo +1 -0
  194. package/packages/mailx-service/google-sync.d.ts +96 -0
  195. package/packages/mailx-service/google-sync.d.ts.map +1 -0
  196. package/packages/mailx-service/google-sync.js +153 -0
  197. package/packages/mailx-service/google-sync.js.map +1 -0
  198. package/packages/mailx-service/google-sync.ts +238 -0
  199. package/packages/mailx-service/index.d.ts +366 -0
  200. package/packages/mailx-service/index.d.ts.map +1 -0
  201. package/packages/mailx-service/index.js +2589 -0
  202. package/packages/mailx-service/index.js.map +1 -0
  203. package/packages/mailx-service/index.ts +2512 -0
  204. package/packages/mailx-service/jsonrpc.d.ts +25 -0
  205. package/packages/mailx-service/jsonrpc.d.ts.map +1 -0
  206. package/packages/mailx-service/jsonrpc.js +238 -0
  207. package/packages/mailx-service/jsonrpc.js.map +1 -0
  208. package/packages/mailx-service/jsonrpc.ts +268 -0
  209. package/packages/mailx-service/package.json +22 -0
  210. package/packages/mailx-service/tsconfig.json +9 -0
  211. package/packages/mailx-settings/.gitattributes +10 -0
  212. package/packages/mailx-settings/cloud.d.ts +47 -0
  213. package/packages/mailx-settings/cloud.d.ts.map +1 -0
  214. package/packages/mailx-settings/cloud.js +408 -0
  215. package/packages/mailx-settings/cloud.js.map +1 -0
  216. package/packages/mailx-settings/cloud.ts +393 -0
  217. package/packages/mailx-settings/index.d.ts +140 -0
  218. package/packages/mailx-settings/index.d.ts.map +1 -0
  219. package/packages/mailx-settings/index.js +872 -0
  220. package/packages/mailx-settings/index.js.map +1 -0
  221. package/packages/mailx-settings/index.ts +908 -0
  222. package/packages/mailx-settings/package.json +20 -0
  223. package/packages/mailx-settings/tsconfig.json +9 -0
  224. package/packages/mailx-settings/tsconfig.tsbuildinfo +1 -0
  225. package/packages/mailx-store/.gitattributes +10 -0
  226. package/packages/mailx-store/db.d.ts +346 -0
  227. package/packages/mailx-store/db.d.ts.map +1 -0
  228. package/packages/mailx-store/db.js +1796 -0
  229. package/packages/mailx-store/db.js.map +1 -0
  230. package/packages/mailx-store/db.ts +2007 -0
  231. package/packages/mailx-store/file-store.d.ts +36 -0
  232. package/packages/mailx-store/file-store.d.ts.map +1 -0
  233. package/packages/mailx-store/file-store.js +80 -0
  234. package/packages/mailx-store/file-store.js.map +1 -0
  235. package/packages/mailx-store/file-store.ts +82 -0
  236. package/packages/mailx-store/index.d.ts +7 -0
  237. package/packages/mailx-store/index.d.ts.map +1 -0
  238. package/packages/mailx-store/index.js +7 -0
  239. package/packages/mailx-store/index.js.map +1 -0
  240. package/packages/mailx-store/index.ts +7 -0
  241. package/packages/mailx-store/package.json +19 -0
  242. package/packages/mailx-store/tsconfig.json +9 -0
  243. package/packages/mailx-store/tsconfig.tsbuildinfo +1 -0
  244. package/packages/mailx-store-web/android-bootstrap.d.ts +16 -0
  245. package/packages/mailx-store-web/android-bootstrap.d.ts.map +1 -0
  246. package/packages/mailx-store-web/android-bootstrap.js +1247 -0
  247. package/packages/mailx-store-web/android-bootstrap.js.map +1 -0
  248. package/packages/mailx-store-web/android-bootstrap.ts +1262 -0
  249. package/packages/mailx-store-web/db.d.ts +132 -0
  250. package/packages/mailx-store-web/db.d.ts.map +1 -0
  251. package/packages/mailx-store-web/db.js +642 -0
  252. package/packages/mailx-store-web/db.js.map +1 -0
  253. package/packages/mailx-store-web/db.ts +756 -0
  254. package/packages/mailx-store-web/gmail-api-web.d.ts +11 -0
  255. package/packages/mailx-store-web/gmail-api-web.d.ts.map +1 -0
  256. package/packages/mailx-store-web/gmail-api-web.js +11 -0
  257. package/packages/mailx-store-web/gmail-api-web.js.map +1 -0
  258. package/packages/mailx-store-web/gmail-api-web.ts +11 -0
  259. package/packages/mailx-store-web/imap-web-provider.d.ts +33 -0
  260. package/packages/mailx-store-web/imap-web-provider.d.ts.map +1 -0
  261. package/packages/mailx-store-web/imap-web-provider.js +140 -0
  262. package/packages/mailx-store-web/imap-web-provider.js.map +1 -0
  263. package/packages/mailx-store-web/imap-web-provider.ts +156 -0
  264. package/packages/mailx-store-web/index.d.ts +10 -0
  265. package/packages/mailx-store-web/index.d.ts.map +1 -0
  266. package/packages/mailx-store-web/index.js +10 -0
  267. package/packages/mailx-store-web/index.js.map +1 -0
  268. package/packages/mailx-store-web/index.ts +10 -0
  269. package/packages/mailx-store-web/main-thread-host.d.ts +15 -0
  270. package/packages/mailx-store-web/main-thread-host.d.ts.map +1 -0
  271. package/packages/mailx-store-web/main-thread-host.js +292 -0
  272. package/packages/mailx-store-web/main-thread-host.js.map +1 -0
  273. package/packages/mailx-store-web/main-thread-host.ts +322 -0
  274. package/packages/mailx-store-web/package.json +23 -0
  275. package/packages/mailx-store-web/provider-types.d.ts +7 -0
  276. package/packages/mailx-store-web/provider-types.d.ts.map +1 -0
  277. package/packages/mailx-store-web/provider-types.js +7 -0
  278. package/packages/mailx-store-web/provider-types.js.map +1 -0
  279. package/packages/mailx-store-web/provider-types.ts +7 -0
  280. package/packages/mailx-store-web/sql-wasm-esm.js +10 -0
  281. package/packages/mailx-store-web/sql.js.d.ts +29 -0
  282. package/packages/mailx-store-web/sync-manager.d.ts +68 -0
  283. package/packages/mailx-store-web/sync-manager.d.ts.map +1 -0
  284. package/packages/mailx-store-web/sync-manager.js +506 -0
  285. package/packages/mailx-store-web/sync-manager.js.map +1 -0
  286. package/packages/mailx-store-web/sync-manager.ts +508 -0
  287. package/packages/mailx-store-web/tsconfig.json +10 -0
  288. package/packages/mailx-store-web/web-jsonrpc.d.ts +20 -0
  289. package/packages/mailx-store-web/web-jsonrpc.d.ts.map +1 -0
  290. package/packages/mailx-store-web/web-jsonrpc.js +102 -0
  291. package/packages/mailx-store-web/web-jsonrpc.js.map +1 -0
  292. package/packages/mailx-store-web/web-jsonrpc.ts +116 -0
  293. package/packages/mailx-store-web/web-message-store.d.ts +16 -0
  294. package/packages/mailx-store-web/web-message-store.d.ts.map +1 -0
  295. package/packages/mailx-store-web/web-message-store.js +89 -0
  296. package/packages/mailx-store-web/web-message-store.js.map +1 -0
  297. package/packages/mailx-store-web/web-message-store.ts +97 -0
  298. package/packages/mailx-store-web/web-service.d.ts +111 -0
  299. package/packages/mailx-store-web/web-service.d.ts.map +1 -0
  300. package/packages/mailx-store-web/web-service.js +542 -0
  301. package/packages/mailx-store-web/web-service.js.map +1 -0
  302. package/packages/mailx-store-web/web-service.ts +616 -0
  303. package/packages/mailx-store-web/web-settings.d.ts +83 -0
  304. package/packages/mailx-store-web/web-settings.d.ts.map +1 -0
  305. package/packages/mailx-store-web/web-settings.js +491 -0
  306. package/packages/mailx-store-web/web-settings.js.map +1 -0
  307. package/packages/mailx-store-web/web-settings.ts +522 -0
  308. package/packages/mailx-store-web/worker-bundle.js +6838 -0
  309. package/packages/mailx-store-web/worker-entry.d.ts +8 -0
  310. package/packages/mailx-store-web/worker-entry.d.ts.map +1 -0
  311. package/packages/mailx-store-web/worker-entry.js +187 -0
  312. package/packages/mailx-store-web/worker-entry.js.map +1 -0
  313. package/packages/mailx-store-web/worker-entry.ts +215 -0
  314. package/packages/mailx-store-web/worker-tcp-transport.d.ts +28 -0
  315. package/packages/mailx-store-web/worker-tcp-transport.d.ts.map +1 -0
  316. package/packages/mailx-store-web/worker-tcp-transport.js +98 -0
  317. package/packages/mailx-store-web/worker-tcp-transport.js.map +1 -0
  318. package/packages/mailx-store-web/worker-tcp-transport.ts +101 -0
  319. package/packages/mailx-types/.gitattributes +10 -0
  320. package/packages/mailx-types/index.d.ts +319 -0
  321. package/packages/mailx-types/index.d.ts.map +1 -0
  322. package/packages/mailx-types/index.js +243 -0
  323. package/packages/mailx-types/index.js.map +1 -0
  324. package/packages/mailx-types/index.ts +498 -0
  325. package/packages/mailx-types/package.json +16 -0
  326. package/packages/mailx-types/tsconfig.json +9 -0
  327. package/packages/mailx-types/tsconfig.tsbuildinfo +1 -0
  328. package/todo.json +13 -0
  329. package/tsconfig.base.json +31 -0
  330. package/tsconfig.json +9 -0
@@ -0,0 +1,1796 @@
1
+ /**
2
+ * SQLite metadata index for mailx.
3
+ * Stores message headers, folder structure, sync state.
4
+ * Message bodies are NOT here -- they live in the MessageStore backend.
5
+ */
6
+ import { DatabaseSync } from "node:sqlite";
7
+ import { randomUUID } from "node:crypto";
8
+ import * as path from "node:path";
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
+ }
31
+ const SCHEMA = `
32
+ CREATE TABLE IF NOT EXISTS accounts (
33
+ id TEXT PRIMARY KEY,
34
+ name TEXT NOT NULL,
35
+ email TEXT NOT NULL,
36
+ config_json TEXT NOT NULL,
37
+ last_sync INTEGER DEFAULT 0
38
+ );
39
+
40
+ CREATE TABLE IF NOT EXISTS folders (
41
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
42
+ account_id TEXT NOT NULL REFERENCES accounts(id),
43
+ path TEXT NOT NULL,
44
+ name TEXT NOT NULL,
45
+ special_use TEXT,
46
+ delimiter TEXT DEFAULT '/',
47
+ total_count INTEGER DEFAULT 0,
48
+ unread_count INTEGER DEFAULT 0,
49
+ uidvalidity INTEGER DEFAULT 0,
50
+ highest_modseq TEXT DEFAULT '0',
51
+ UNIQUE(account_id, path)
52
+ );
53
+
54
+ CREATE TABLE IF NOT EXISTS messages (
55
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
56
+ account_id TEXT NOT NULL,
57
+ folder_id INTEGER NOT NULL REFERENCES folders(id),
58
+ uid INTEGER NOT NULL,
59
+ message_id TEXT,
60
+ in_reply_to TEXT,
61
+ refs TEXT,
62
+ thread_id TEXT,
63
+ date INTEGER NOT NULL,
64
+ subject TEXT DEFAULT '',
65
+ from_address TEXT DEFAULT '',
66
+ from_name TEXT DEFAULT '',
67
+ to_json TEXT DEFAULT '[]',
68
+ cc_json TEXT DEFAULT '[]',
69
+ flags_json TEXT DEFAULT '[]',
70
+ size INTEGER DEFAULT 0,
71
+ has_attachments INTEGER DEFAULT 0,
72
+ preview TEXT DEFAULT '',
73
+ body_path TEXT,
74
+ cached_at INTEGER NOT NULL,
75
+ UNIQUE(account_id, folder_id, uid)
76
+ );
77
+
78
+ CREATE INDEX IF NOT EXISTS idx_messages_folder_date
79
+ ON messages(account_id, folder_id, date DESC);
80
+
81
+ CREATE INDEX IF NOT EXISTS idx_messages_message_id
82
+ ON messages(message_id);
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
+
97
+ CREATE TABLE IF NOT EXISTS queue (
98
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
99
+ status TEXT NOT NULL DEFAULT 'pending',
100
+ created_at INTEGER NOT NULL,
101
+ send_after INTEGER NOT NULL,
102
+ attempts INTEGER DEFAULT 0,
103
+ last_attempt INTEGER DEFAULT 0,
104
+ error TEXT,
105
+ from_account TEXT NOT NULL,
106
+ to_json TEXT NOT NULL,
107
+ cc_json TEXT DEFAULT '[]',
108
+ bcc_json TEXT DEFAULT '[]',
109
+ subject TEXT DEFAULT '',
110
+ body_html TEXT DEFAULT '',
111
+ body_text TEXT DEFAULT '',
112
+ in_reply_to TEXT,
113
+ refs TEXT
114
+ );
115
+
116
+ CREATE TABLE IF NOT EXISTS contacts (
117
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
118
+ source TEXT NOT NULL DEFAULT 'discovered',
119
+ google_id TEXT,
120
+ name TEXT DEFAULT '',
121
+ email TEXT NOT NULL,
122
+ organization TEXT DEFAULT '',
123
+ last_used INTEGER DEFAULT 0,
124
+ use_count INTEGER DEFAULT 0,
125
+ updated_at INTEGER NOT NULL,
126
+ UNIQUE(source, email, name)
127
+ );
128
+
129
+ CREATE INDEX IF NOT EXISTS idx_contacts_email ON contacts(email);
130
+ CREATE INDEX IF NOT EXISTS idx_contacts_name ON contacts(name);
131
+
132
+ CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING fts5(
133
+ subject, from_name, from_address, to_text, cc_text, body_text,
134
+ content=messages, content_rowid=id
135
+ );
136
+
137
+ CREATE TABLE IF NOT EXISTS sync_actions (
138
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
139
+ account_id TEXT NOT NULL,
140
+ action TEXT NOT NULL,
141
+ uid INTEGER,
142
+ folder_id INTEGER,
143
+ target_folder_id INTEGER,
144
+ flags_json TEXT,
145
+ raw_message TEXT,
146
+ created_at INTEGER NOT NULL,
147
+ attempts INTEGER DEFAULT 0,
148
+ last_error TEXT,
149
+ UNIQUE(account_id, action, uid, folder_id)
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
+ );
254
+ `;
255
+ export class MailxDB {
256
+ db;
257
+ constructor(dbDir) {
258
+ fs.mkdirSync(dbDir, { recursive: true });
259
+ const dbPath = path.join(dbDir, "mailx.db");
260
+ this.db = new DatabaseSync(dbPath);
261
+ this.db.exec("PRAGMA journal_mode = WAL");
262
+ this.db.exec("PRAGMA foreign_keys = ON");
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
+ }));
754
+ }
755
+ close() {
756
+ this.db.close();
757
+ }
758
+ // ── Accounts ──
759
+ upsertAccount(id, name, email, configJson) {
760
+ this.db.prepare(`
761
+ INSERT INTO accounts (id, name, email, config_json)
762
+ VALUES (?, ?, ?, ?)
763
+ ON CONFLICT(id) DO UPDATE SET name=?, email=?, config_json=?
764
+ `).run(id, name, email, configJson, name, email, configJson);
765
+ }
766
+ getAccounts() {
767
+ return this.db.prepare("SELECT id, name, email, last_sync as lastSync FROM accounts").all();
768
+ }
769
+ getAccountConfigs() {
770
+ return this.db.prepare("SELECT id, name, email, config_json as configJson FROM accounts").all();
771
+ }
772
+ updateLastSync(accountId, timestamp) {
773
+ this.db.prepare("UPDATE accounts SET last_sync = ? WHERE id = ?").run(timestamp, accountId);
774
+ }
775
+ // ── Folders ──
776
+ upsertFolder(accountId, folderPath, name, specialUse, delimiter) {
777
+ const existing = this.db.prepare("SELECT id FROM folders WHERE account_id = ? AND path = ?").get(accountId, folderPath);
778
+ if (existing) {
779
+ this.db.prepare("UPDATE folders SET name = ?, special_use = ?, delimiter = ? WHERE id = ?").run(name, specialUse, delimiter, existing.id);
780
+ return existing.id;
781
+ }
782
+ const result = this.db.prepare("INSERT INTO folders (account_id, path, name, special_use, delimiter) VALUES (?, ?, ?, ?, ?)").run(accountId, folderPath, name, specialUse, delimiter);
783
+ return Number(result.lastInsertRowid);
784
+ }
785
+ getFolders(accountId) {
786
+ const rows = this.db.prepare("SELECT * FROM folders WHERE account_id = ? ORDER BY path").all(accountId);
787
+ const folders = rows.map(r => ({
788
+ id: r.id,
789
+ accountId: r.account_id,
790
+ path: r.path,
791
+ name: r.name,
792
+ specialUse: r.special_use,
793
+ delimiter: r.delimiter,
794
+ totalCount: r.total_count,
795
+ unreadCount: r.unread_count,
796
+ children: []
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;
827
+ }
828
+ deleteFolder(folderId) {
829
+ this.db.prepare("DELETE FROM messages WHERE folder_id = ?").run(folderId);
830
+ this.db.prepare("DELETE FROM folders WHERE id = ?").run(folderId);
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
+ }
840
+ updateFolderCounts(folderId, total, unread) {
841
+ this.db.prepare("UPDATE folders SET total_count = ?, unread_count = ? WHERE id = ?").run(total, unread, folderId);
842
+ }
843
+ updateFolderSync(folderId, uidvalidity, highestModseq) {
844
+ this.db.prepare("UPDATE folders SET uidvalidity = ?, highest_modseq = ? WHERE id = ?").run(uidvalidity, highestModseq, folderId);
845
+ }
846
+ getFolderSync(folderId) {
847
+ const row = this.db.prepare("SELECT uidvalidity, highest_modseq as highestModseq FROM folders WHERE id = ?").get(folderId);
848
+ return row || { uidvalidity: 0, highestModseq: "0" };
849
+ }
850
+ // ── Messages ──
851
+ upsertMessage(msg) {
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);
853
+ if (existing) {
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
+ }
876
+ return existing.id;
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
+ }
896
+ const toText = msg.to.map(a => `${a.name} ${a.address}`).join(" ");
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, "");
907
+ const result = this.db.prepare(`
908
+ INSERT INTO messages (
909
+ account_id, folder_id, uid, uuid, message_id, in_reply_to, refs, thread_id,
910
+ date, subject, from_address, from_name, to_json, cc_json,
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);
914
+ const rowId = Number(result.lastInsertRowid);
915
+ // Index for full-text search
916
+ try {
917
+ this.db.prepare("INSERT INTO messages_fts (rowid, subject, from_name, from_address, to_text, cc_text, body_text) VALUES (?, ?, ?, ?, ?, ?, ?)").run(rowId, msg.subject, msg.from.name, msg.from.address, toText, ccText, msg.preview);
918
+ }
919
+ catch { /* FTS insert may fail on rebuild, non-fatal */ }
920
+ return rowId;
921
+ }
922
+ getMessages(query) {
923
+ const page = query.page || 1;
924
+ const pageSize = query.pageSize || 50;
925
+ const offset = (page - 1) * pageSize;
926
+ const sort = query.sort || "date";
927
+ const sortDir = query.sortDir || "desc";
928
+ const sortCol = sort === "from" ? "from_name" : sort === "subject" ? "subject" : "date";
929
+ let where = "account_id = ? AND folder_id = ?";
930
+ const params = [query.accountId, query.folderId];
931
+ if (query.search) {
932
+ where += " AND (subject LIKE ? OR from_name LIKE ? OR from_address LIKE ?)";
933
+ const term = `%${query.search}%`;
934
+ params.push(term, term, term);
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
+ }
942
+ const total = this.db.prepare(`SELECT COUNT(*) as cnt FROM messages WHERE ${where}`).get(...params).cnt;
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);
958
+ const items = rows.map(r => ({
959
+ id: r.id,
960
+ accountId: r.account_id,
961
+ folderId: r.folder_id,
962
+ uid: r.uid,
963
+ uuid: r.uuid || "",
964
+ messageId: r.message_id || "",
965
+ inReplyTo: r.in_reply_to || "",
966
+ references: JSON.parse(r.refs || "[]"),
967
+ threadId: r.thread_id || undefined,
968
+ date: r.date,
969
+ subject: r.subject,
970
+ from: { name: r.from_name, address: r.from_address },
971
+ to: JSON.parse(r.to_json),
972
+ cc: JSON.parse(r.cc_json),
973
+ flags: JSON.parse(r.flags_json),
974
+ size: r.size,
975
+ hasAttachments: !!r.has_attachments,
976
+ preview: r.preview,
977
+ bodyPath: r.body_path || "",
978
+ pending: !!r.pending,
979
+ }));
980
+ return { items, total, page, pageSize };
981
+ }
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) {
1032
+ return {
1033
+ id: r.id,
1034
+ accountId: r.account_id,
1035
+ folderId: r.folder_id,
1036
+ uid: r.uid,
1037
+ uuid: r.uuid || "",
1038
+ messageId: r.message_id || "",
1039
+ inReplyTo: r.in_reply_to || "",
1040
+ references: JSON.parse(r.refs || "[]"),
1041
+ threadId: r.thread_id || undefined,
1042
+ date: r.date,
1043
+ subject: r.subject,
1044
+ from: { name: r.from_name, address: r.from_address },
1045
+ to: JSON.parse(r.to_json),
1046
+ cc: JSON.parse(r.cc_json),
1047
+ flags: JSON.parse(r.flags_json),
1048
+ size: r.size,
1049
+ hasAttachments: !!r.has_attachments,
1050
+ preview: r.preview,
1051
+ bodyPath: r.body_path || "",
1052
+ providerId: r.provider_id || undefined,
1053
+ };
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
+ }
1078
+ getMessageBodyPath(accountId, uid) {
1079
+ const r = this.db.prepare("SELECT body_path FROM messages WHERE account_id = ? AND uid = ?").get(accountId, uid);
1080
+ return r?.body_path || "";
1081
+ }
1082
+ updateMessageFlags(accountId, uid, flags) {
1083
+ this.db.prepare("UPDATE messages SET flags_json = ? WHERE account_id = ? AND uid = ?").run(JSON.stringify(flags), accountId, uid);
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
+ }
1107
+ getHighestUid(accountId, folderId) {
1108
+ const r = this.db.prepare("SELECT MAX(uid) as maxUid FROM messages WHERE account_id = ? AND folder_id = ?").get(accountId, folderId);
1109
+ return r?.maxUid || 0;
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
+ }
1119
+ /** Get all UIDs for a folder */
1120
+ getUidsForFolder(accountId, folderId) {
1121
+ const rows = this.db.prepare("SELECT uid FROM messages WHERE account_id = ? AND folder_id = ?").all(accountId, folderId);
1122
+ return rows.map(r => r.uid);
1123
+ }
1124
+ /** Delete a message by account + UID */
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);
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);
1139
+ }
1140
+ /** Bulk insert within a transaction for sync performance */
1141
+ beginTransaction() { this.db.exec("BEGIN"); }
1142
+ commitTransaction() { this.db.exec("COMMIT"); }
1143
+ rollbackTransaction() { this.db.exec("ROLLBACK"); }
1144
+ // ── Contacts ──
1145
+ /** Record an address used in sent mail */
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;
1153
+ const now = Date.now();
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);
1158
+ if (existing) {
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);
1160
+ }
1161
+ else {
1162
+ this.db.prepare("INSERT INTO contacts (source, name, email, last_used, use_count, updated_at) VALUES ('discovered', ?, ?, ?, 1, ?)").run(name || "", email, now, now);
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);
1171
+ }
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. */
1200
+ seedContactsFromMessages() {
1201
+ const VALID = /^[^\s<>@]+@[^\s<>@]+\.[^\s<>@]+$/;
1202
+ const now = Date.now();
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
+ }
1277
+ let added = 0;
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);
1287
+ if (!existing) {
1288
+ insStmt.run(info.name, email, info.last, info.cnt, now);
1289
+ added++;
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();
1299
+ }
1300
+ return added;
1301
+ }
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. */
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;
1502
+ const q = `%${query}%`;
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}
1509
+ ORDER BY use_count DESC, last_used DESC
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);
1549
+ }
1550
+ // ── Search ──
1551
+ /** Full-text search across all messages. Supports qualifiers: from:, to:, subject: */
1552
+ searchMessages(query, page = 1, pageSize = 50, accountId, folderId) {
1553
+ query = (query || "").trim();
1554
+ // Parse qualifiers (C45: extended set — date:, has:, is:, folder:).
1555
+ let ftsQuery = "";
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
+ };
1586
+ for (const part of parts) {
1587
+ const fromMatch = part.match(/^from:(.+)$/i);
1588
+ const toMatch = part.match(/^to:(.+)$/i);
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);
1596
+ if (fromMatch) {
1597
+ const term = fromMatch[1].replace(/"/g, "");
1598
+ ftsQuery += `(from_name:${term} OR from_address:${term}) `;
1599
+ }
1600
+ else if (toMatch) {
1601
+ const term = toMatch[1].replace(/"/g, "");
1602
+ ftsQuery += `(to_text:${term} OR cc_text:${term}) `;
1603
+ }
1604
+ else if (subjectMatch) {
1605
+ const term = subjectMatch[1].replace(/"/g, "");
1606
+ ftsQuery += `subject:${term} `;
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
+ }
1651
+ else {
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
+ }
1661
+ }
1662
+ }
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.
1666
+ if (!ftsQuery)
1667
+ ftsQuery = "*";
1668
+ const offset = (page - 1) * pageSize;
1669
+ try {
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);
1685
+ const total = countRow?.cnt || 0;
1686
+ const rows = this.db.prepare(`SELECT m.*, f.name AS folder_name FROM messages m
1687
+ JOIN messages_fts fts ON m.id = fts.rowid
1688
+ LEFT JOIN folders f ON f.id = m.folder_id AND f.account_id = m.account_id
1689
+ WHERE messages_fts MATCH ?${scopeWhere}
1690
+ ORDER BY m.date DESC
1691
+ LIMIT ? OFFSET ?`).all(ftsQuery, ...scopeParams, pageSize, offset);
1692
+ const items = rows.map(r => ({
1693
+ id: r.id,
1694
+ accountId: r.account_id,
1695
+ folderId: r.folder_id,
1696
+ folderName: r.folder_name || "",
1697
+ uid: r.uid,
1698
+ messageId: r.message_id || "",
1699
+ inReplyTo: r.in_reply_to || "",
1700
+ references: JSON.parse(r.refs || "[]"),
1701
+ date: r.date,
1702
+ subject: r.subject,
1703
+ from: { name: r.from_name, address: r.from_address },
1704
+ to: JSON.parse(r.to_json),
1705
+ cc: JSON.parse(r.cc_json),
1706
+ flags: JSON.parse(r.flags_json),
1707
+ size: r.size,
1708
+ hasAttachments: !!r.has_attachments,
1709
+ preview: r.preview
1710
+ }));
1711
+ return { items, total, page, pageSize };
1712
+ }
1713
+ catch (e) {
1714
+ console.error(`Search error: ${e.message}`);
1715
+ return { items: [], total: 0, page, pageSize };
1716
+ }
1717
+ }
1718
+ /** Rebuild FTS index from existing messages */
1719
+ rebuildSearchIndex() {
1720
+ // Drop and recreate in case schema changed
1721
+ try {
1722
+ this.db.exec("DROP TABLE IF EXISTS messages_fts");
1723
+ }
1724
+ catch { /* ignore */ }
1725
+ this.db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING fts5(
1726
+ subject, from_name, from_address, to_text, cc_text, body_text,
1727
+ content=messages, content_rowid=id
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 (?, ?, ?, ?, ?, ?, ?)");
1731
+ const rows = this.db.prepare("SELECT id, subject, from_name, from_address, to_json, cc_json, preview FROM messages").all();
1732
+ let count = 0;
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 */ }
1745
+ }
1746
+ this.db.exec("COMMIT");
1747
+ }
1748
+ catch (e) {
1749
+ this.db.exec("ROLLBACK");
1750
+ throw e;
1751
+ }
1752
+ return count;
1753
+ }
1754
+ // ── Sync Actions ──
1755
+ /** Queue a local action for later sync to IMAP */
1756
+ queueSyncAction(accountId, action, uid, folderId, extra) {
1757
+ try {
1758
+ this.db.prepare(`INSERT OR REPLACE INTO sync_actions (account_id, action, uid, folder_id, target_folder_id, flags_json, raw_message, created_at)
1759
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(accountId, action, uid, folderId, extra?.targetFolderId || null, extra?.flags ? JSON.stringify(extra.flags) : null, extra?.rawMessage || null, Date.now());
1760
+ }
1761
+ catch { /* UNIQUE constraint — already queued */ }
1762
+ }
1763
+ /** Get all pending sync actions for an account */
1764
+ getPendingSyncActions(accountId) {
1765
+ const rows = this.db.prepare("SELECT * FROM sync_actions WHERE account_id = ? ORDER BY created_at").all(accountId);
1766
+ return rows.map(r => ({
1767
+ id: r.id,
1768
+ action: r.action,
1769
+ uid: r.uid,
1770
+ folderId: r.folder_id,
1771
+ targetFolderId: r.target_folder_id,
1772
+ flags: r.flags_json ? JSON.parse(r.flags_json) : [],
1773
+ rawMessage: r.raw_message,
1774
+ attempts: r.attempts,
1775
+ }));
1776
+ }
1777
+ /** Remove a completed sync action */
1778
+ completeSyncAction(id) {
1779
+ this.db.prepare("DELETE FROM sync_actions WHERE id = ?").run(id);
1780
+ }
1781
+ /** Mark a sync action as failed */
1782
+ failSyncAction(id, error) {
1783
+ this.db.prepare("UPDATE sync_actions SET attempts = attempts + 1, last_error = ? WHERE id = ?").run(error, id);
1784
+ }
1785
+ /** Count pending sync actions for an account */
1786
+ getPendingSyncCount(accountId) {
1787
+ const r = this.db.prepare("SELECT COUNT(*) as cnt FROM sync_actions WHERE account_id = ?").get(accountId);
1788
+ return r?.cnt || 0;
1789
+ }
1790
+ /** Count total pending sync actions across all accounts */
1791
+ getTotalPendingSyncCount() {
1792
+ const r = this.db.prepare("SELECT COUNT(*) as cnt FROM sync_actions").get();
1793
+ return r?.cnt || 0;
1794
+ }
1795
+ }
1796
+ //# sourceMappingURL=db.js.map