@bobfrankston/mailx-imap 0.1.35 → 0.1.37

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 (2) hide show
  1. package/index.js +61 -8
  2. package/package.json +7 -7
package/index.js CHANGED
@@ -2181,15 +2181,43 @@ export class ImapManager extends EventEmitter {
2181
2181
  // it can't share the ops queue. Counts against the per-host
2182
2182
  // semaphore (one slot for the IDLE socket).
2183
2183
  const watchClient = await this.createClient(accountId, "idle");
2184
+ // RFC 5465 NOTIFY: when the server supports it (Dovecot does;
2185
+ // gmail-IMAP and Outlook IMAP typically don't), engage
2186
+ // cross-mailbox push so changes in Sent/Drafts/subfolders
2187
+ // surface in real time instead of waiting for the next
2188
+ // periodic sync. SELECTED group keeps INBOX events flowing
2189
+ // through the existing IDLE/EXISTS path; PERSONAL group
2190
+ // pushes STATUS for every other mailbox the user owns.
2191
+ const caps = typeof watchClient.getCapabilities === "function"
2192
+ ? watchClient.getCapabilities()
2193
+ : new Set();
2194
+ const useNotify = caps.has("NOTIFY");
2195
+ const notifySpec = useNotify
2196
+ ? "(SELECTED (MessageNew MessageExpunge FlagChange)) (PERSONAL (MessageNew MessageExpunge FlagChange MailboxName))"
2197
+ : undefined;
2198
+ const onMailboxStatus = useNotify
2199
+ ? (mailboxPath, _data) => {
2200
+ // Find the local folder row by IMAP path and sync it.
2201
+ // STATUS pushes are lightweight count hints — the real
2202
+ // change set is fetched by syncFolder.
2203
+ const folder = this.db.getFolders(accountId).find(f => f.path === mailboxPath);
2204
+ if (!folder) {
2205
+ console.log(` [notify] ${accountId}: STATUS for unknown mailbox "${mailboxPath}" — skipping`);
2206
+ return;
2207
+ }
2208
+ console.log(` [notify] ${accountId}: ${mailboxPath} changed → syncFolder`);
2209
+ this.syncFolder(accountId, folder.id).catch(e => console.error(` [notify] sync ${mailboxPath} failed: ${e.message}`));
2210
+ }
2211
+ : undefined;
2184
2212
  const stop = await watchClient.watchMailbox("INBOX", (newCount) => {
2185
2213
  console.log(` [idle] ${accountId}: ${newCount} new message(s)`);
2186
2214
  this.syncInboxNewOnly(accountId).catch(e => console.error(` [idle] sync error: ${e.message}`));
2187
- });
2215
+ }, { notifySpec, onMailboxStatus });
2188
2216
  this.watchers.set(accountId, async () => {
2189
2217
  await stop();
2190
2218
  await watchClient.logout();
2191
2219
  });
2192
- console.log(` [idle] Watching INBOX for ${accountId}`);
2220
+ console.log(` [idle] Watching INBOX for ${accountId}${useNotify ? " (+NOTIFY personal mailboxes)" : ""}`);
2193
2221
  }
2194
2222
  catch (e) {
2195
2223
  console.error(` [idle] Failed to watch ${accountId}: ${e.message}`);
@@ -3124,6 +3152,34 @@ export class ImapManager extends EventEmitter {
3124
3152
  return;
3125
3153
  if (!draftUid && !draftId)
3126
3154
  return;
3155
+ // Local-first: drop the draft from the local DB FIRST so the user's
3156
+ // Drafts view reflects the Send click instantly. The IMAP delete is
3157
+ // queued for the background reconciler. Without this, the draft
3158
+ // stayed visible in Drafts until the IMAP round-trip completed (or
3159
+ // forever if it failed) — Bob 2026-05-12: "when I send a letter you
3160
+ // seem to still leave it in draft so I don't know if it has been
3161
+ // sent or not. Once you hand it off it must leave draft."
3162
+ let localDeletedUid = 0;
3163
+ if (draftUid) {
3164
+ const existing = this.db.getMessageByUid(accountId, draftUid, drafts.id);
3165
+ if (existing) {
3166
+ this.unlinkBodyFile(accountId, draftUid, drafts.id).catch(() => { });
3167
+ this.db.deleteMessage(accountId, draftUid, "user sent the message (draft cleanup)", "mailx-imap deleteDraft (local)");
3168
+ localDeletedUid = draftUid;
3169
+ }
3170
+ }
3171
+ if (draftId && !localDeletedUid) {
3172
+ // Look up any local draft rows by X-Mailx-Draft-ID. The DB
3173
+ // doesn't index that header directly, but locally-saved drafts
3174
+ // share a stable header set; rely on the UID path 99% of the
3175
+ // time. This branch covers the "draft was saved on another
3176
+ // device, came in via sync, no UID known yet" edge case — for
3177
+ // now the IMAP path below handles it.
3178
+ }
3179
+ if (localDeletedUid > 0) {
3180
+ this.db.recalcFolderCounts(drafts.id);
3181
+ this.emit("folderCountsChanged", accountId, {});
3182
+ }
3127
3183
  let succeeded = false;
3128
3184
  try {
3129
3185
  await this.withConnection(accountId, async (client) => {
@@ -3162,12 +3218,9 @@ export class ImapManager extends EventEmitter {
3162
3218
  console.error(` [drafts] withConnection failed for ${accountId}: ${e?.message || e}`);
3163
3219
  }
3164
3220
  // Reliable cleanup: if the inline attempt didn't actually delete the
3165
- // draft, queue a sync_action so the regular processSyncActions loop
3166
- // retries it later. Without this, transient IMAP failures left stale
3167
- // drafts in the folder ("draft droppings" that pile up after sends).
3168
- // Only the UID path can be queued — searchByHeader retries on every
3169
- // future call anyway, and queuing a header-lookup isn't a real
3170
- // sync_action shape.
3221
+ // draft on the server, queue a sync_action so the regular
3222
+ // processSyncActions loop retries it later. The local row is already
3223
+ // gone (above), so the user doesn't see stale UI either way.
3171
3224
  if (!succeeded && draftUid) {
3172
3225
  console.log(` [drafts] Queueing sync_action for retry: delete UID ${draftUid} in ${drafts.path}`);
3173
3226
  this.db.queueSyncAction(accountId, "delete", draftUid, drafts.id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-imap",
3
- "version": "0.1.35",
3
+ "version": "0.1.37",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -9,10 +9,10 @@
9
9
  },
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
- "@bobfrankston/mailx-types": "^0.1.10",
12
+ "@bobfrankston/mailx-types": "^0.1.11",
13
13
  "@bobfrankston/mailx-settings": "^0.1.14",
14
- "@bobfrankston/mailx-store": "^0.1.17",
15
- "@bobfrankston/iflow-direct": "^0.1.39",
14
+ "@bobfrankston/mailx-store": "^0.1.18",
15
+ "@bobfrankston/iflow-direct": "^0.1.41",
16
16
  "@bobfrankston/tcp-transport": "^0.1.6",
17
17
  "@bobfrankston/smtp-direct": "^0.1.8",
18
18
  "@bobfrankston/mailx-sync": "^0.1.16",
@@ -37,10 +37,10 @@
37
37
  },
38
38
  ".transformedSnapshot": {
39
39
  "dependencies": {
40
- "@bobfrankston/mailx-types": "^0.1.10",
40
+ "@bobfrankston/mailx-types": "^0.1.11",
41
41
  "@bobfrankston/mailx-settings": "^0.1.14",
42
- "@bobfrankston/mailx-store": "^0.1.17",
43
- "@bobfrankston/iflow-direct": "^0.1.39",
42
+ "@bobfrankston/mailx-store": "^0.1.18",
43
+ "@bobfrankston/iflow-direct": "^0.1.41",
44
44
  "@bobfrankston/tcp-transport": "^0.1.6",
45
45
  "@bobfrankston/smtp-direct": "^0.1.8",
46
46
  "@bobfrankston/mailx-sync": "^0.1.16",