@bobfrankston/mailx-store 0.1.39 → 0.1.41

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 (4) hide show
  1. package/db.d.ts +26 -4
  2. package/db.js +40 -9
  3. package/package.json +1 -1
  4. package/store.js +2 -2
package/db.d.ts CHANGED
@@ -302,7 +302,15 @@ export declare class MailxDB {
302
302
  * Existing callers without folderId are tolerated (legacy) but should
303
303
  * be migrated. */
304
304
  getMessageBodyPath(accountId: string, uid: number, folderId?: number): string;
305
- updateMessageFlags(accountId: string, uid: number, flags: string[]): void;
305
+ /** Update flags for a message. folderId is REQUIRED — same UID-collision
306
+ * bug class as the deleteMessage one we fixed 2026-05-27. The
307
+ * WHERE-account-AND-uid query previously updated EVERY folder's row
308
+ * that shared the numeric UID, causing the "stars on letters I never
309
+ * starred" symptom (Bob 2026-05-27: flagging one INBOX message lit
310
+ * up stars on a same-numeric-UID row in Drafts/Sent/sub-folders).
311
+ * folderId === null is allowed as an explicit "every folder" override
312
+ * for code paths that mean it; everyone else must pass folderId. */
313
+ updateMessageFlags(accountId: string, folderId: number | null, uid: number, flags: string[]): void;
306
314
  updateMessageFolder(accountId: string, uid: number, targetFolderId: number): void;
307
315
  /** Local-first move of a message between folders. Updates BOTH the
308
316
  * legacy `messages.folder_id` column AND the `message_folders`
@@ -399,9 +407,23 @@ export declare class MailxDB {
399
407
  * appended at a different real UID). Idempotent: if no row matches
400
408
  * (already rebound by an earlier sync), it's a no-op. */
401
409
  updateMessageUid(accountId: string, folderId: number, oldUid: number, newUid: number): void;
402
- /** Delete a message by account + UID. Reason is propagated into the
403
- * audit_log so the DB carries an authoritative trail of every removal. */
404
- deleteMessage(accountId: string, uid: number, reason?: string, source?: string): void;
410
+ /** Delete a message by account + folder + UID. folderId is REQUIRED:
411
+ * IMAP UIDs are per-folder, so the same numeric UID refers to different
412
+ * messages in INBOX vs Sent vs any other folder. The pre-2026-05-27
413
+ * implementation accepted (accountId, uid) only and ran
414
+ * `DELETE FROM messages WHERE account_id=? AND uid=?` — which on a
415
+ * QRESYNC VANISHED for INBOX uid=N would also delete the row for the
416
+ * SAME numeric uid=N living in Sent, Drafts, every label folder, etc.
417
+ * That's what wiped 70k bobma rows on 2026-05-27 when QRESYNC reported
418
+ * a handful of vanished UIDs. Never again — folderId required.
419
+ *
420
+ * folderId === null is allowed as an explicit "I want every folder's
421
+ * copy of this UID gone" (used by cross-account move where the source
422
+ * is being abandoned entirely). All other callers MUST pass a folderId.
423
+ *
424
+ * Reason is propagated into the audit_log so the DB carries an
425
+ * authoritative trail of every removal. */
426
+ deleteMessage(accountId: string, folderId: number | null, uid: number, reason?: string, source?: string): void;
405
427
  /** Recalculate folder total/unread counts from actual messages */
406
428
  recalcFolderCounts(folderId: number): void;
407
429
  /** Bulk insert within a transaction for sync performance */
package/db.js CHANGED
@@ -1815,8 +1815,21 @@ export class MailxDB {
1815
1815
  : this.db.prepare("SELECT body_path FROM messages WHERE account_id = ? AND uid = ?").get(accountId, uid);
1816
1816
  return r?.body_path || "";
1817
1817
  }
1818
- updateMessageFlags(accountId, uid, flags) {
1819
- this.db.prepare("UPDATE messages SET flags_json = ? WHERE account_id = ? AND uid = ?").run(JSON.stringify(flags), accountId, uid);
1818
+ /** Update flags for a message. folderId is REQUIRED — same UID-collision
1819
+ * bug class as the deleteMessage one we fixed 2026-05-27. The
1820
+ * WHERE-account-AND-uid query previously updated EVERY folder's row
1821
+ * that shared the numeric UID, causing the "stars on letters I never
1822
+ * starred" symptom (Bob 2026-05-27: flagging one INBOX message lit
1823
+ * up stars on a same-numeric-UID row in Drafts/Sent/sub-folders).
1824
+ * folderId === null is allowed as an explicit "every folder" override
1825
+ * for code paths that mean it; everyone else must pass folderId. */
1826
+ updateMessageFlags(accountId, folderId, uid, flags) {
1827
+ if (folderId != null) {
1828
+ this.db.prepare("UPDATE messages SET flags_json = ? WHERE account_id = ? AND folder_id = ? AND uid = ?").run(JSON.stringify(flags), accountId, folderId, uid);
1829
+ }
1830
+ else {
1831
+ this.db.prepare("UPDATE messages SET flags_json = ? WHERE account_id = ? AND uid = ?").run(JSON.stringify(flags), accountId, uid);
1832
+ }
1820
1833
  }
1821
1834
  updateMessageFolder(accountId, uid, targetFolderId) {
1822
1835
  // Idempotency: if a row already exists at (account, target_folder, uid)
@@ -2021,13 +2034,31 @@ export class MailxDB {
2021
2034
  }
2022
2035
  }
2023
2036
  }
2024
- /** Delete a message by account + UID. Reason is propagated into the
2025
- * audit_log so the DB carries an authoritative trail of every removal. */
2026
- deleteMessage(accountId, uid, reason, source) {
2027
- // Get folderId + message_id + subject before deleting so the audit
2028
- // row carries enough context to identify what was removed.
2029
- const msg = this.db.prepare("SELECT folder_id, message_id, subject FROM messages WHERE account_id = ? AND uid = ?").get(accountId, uid);
2030
- const r = this.db.prepare("DELETE FROM messages WHERE account_id = ? AND uid = ?").run(accountId, uid);
2037
+ /** Delete a message by account + folder + UID. folderId is REQUIRED:
2038
+ * IMAP UIDs are per-folder, so the same numeric UID refers to different
2039
+ * messages in INBOX vs Sent vs any other folder. The pre-2026-05-27
2040
+ * implementation accepted (accountId, uid) only and ran
2041
+ * `DELETE FROM messages WHERE account_id=? AND uid=?` which on a
2042
+ * QRESYNC VANISHED for INBOX uid=N would also delete the row for the
2043
+ * SAME numeric uid=N living in Sent, Drafts, every label folder, etc.
2044
+ * That's what wiped 70k bobma rows on 2026-05-27 when QRESYNC reported
2045
+ * a handful of vanished UIDs. Never again — folderId required.
2046
+ *
2047
+ * folderId === null is allowed as an explicit "I want every folder's
2048
+ * copy of this UID gone" (used by cross-account move where the source
2049
+ * is being abandoned entirely). All other callers MUST pass a folderId.
2050
+ *
2051
+ * Reason is propagated into the audit_log so the DB carries an
2052
+ * authoritative trail of every removal. */
2053
+ deleteMessage(accountId, folderId, uid, reason, source) {
2054
+ // Get message_id + subject before deleting so the audit row carries
2055
+ // enough context to identify what was removed.
2056
+ const msg = folderId != null
2057
+ ? this.db.prepare("SELECT folder_id, message_id, subject FROM messages WHERE account_id = ? AND folder_id = ? AND uid = ?").get(accountId, folderId, uid)
2058
+ : this.db.prepare("SELECT folder_id, message_id, subject FROM messages WHERE account_id = ? AND uid = ?").get(accountId, uid);
2059
+ const r = folderId != null
2060
+ ? this.db.prepare("DELETE FROM messages WHERE account_id = ? AND folder_id = ? AND uid = ?").run(accountId, folderId, uid)
2061
+ : this.db.prepare("DELETE FROM messages WHERE account_id = ? AND uid = ?").run(accountId, uid);
2031
2062
  if (r.changes && msg) {
2032
2063
  this.audit({
2033
2064
  kind: "delete-msg",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-store",
3
- "version": "0.1.39",
3
+ "version": "0.1.41",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/store.js CHANGED
@@ -450,7 +450,7 @@ export class Store {
450
450
  * `folder:<id>` (auto fan-out)
451
451
  */
452
452
  updateFlags(accountId, uid, folderId, flags) {
453
- this.db.updateMessageFlags(accountId, uid, flags);
453
+ this.db.updateMessageFlags(accountId, folderId, uid, flags);
454
454
  const env = this.db.getMessageByUid(accountId, uid, folderId);
455
455
  const msgUuid = env?.uuid;
456
456
  if (msgUuid) {
@@ -531,7 +531,7 @@ export class Store {
531
531
  });
532
532
  return "moved-to-trash";
533
533
  }
534
- this.db.deleteMessage(accountId, uid, "user-initiated trash (already in trash → expunge)", "Store.trashMessage");
534
+ this.db.deleteMessage(accountId, folderId, uid, "user-initiated trash (already in trash → expunge)", "Store.trashMessage");
535
535
  this.db.recalcFolderCounts(folderId);
536
536
  if (msgUuid) {
537
537
  this.bus.publish({