domma-cms 1.28.4 → 1.29.0

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 (98) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/admin/js/lib/contacts-arrange.js +1 -1
  3. package/docs/DommaFeatures.md +5 -3
  4. package/package.json +4 -1
  5. package/plugins/_lib/admin/mail/body-menu.js +2 -0
  6. package/plugins/_lib/admin/mail/cms-host.js +22 -0
  7. package/plugins/_lib/admin/mail/compose-window.js +1027 -1021
  8. package/plugins/_lib/admin/mail/contacts.js +10 -8
  9. package/plugins/_lib/admin/mail/diagnostics-section.js +2 -0
  10. package/plugins/_lib/admin/mail/drag-drop.js +46 -0
  11. package/plugins/_lib/admin/mail/folder-tree.js +2 -0
  12. package/plugins/_lib/admin/mail/font-controls.js +6 -4
  13. package/plugins/_lib/admin/mail/fonts.js +1 -1
  14. package/plugins/_lib/admin/mail/host-context.js +87 -0
  15. package/plugins/_lib/admin/mail/identity.js +2 -0
  16. package/plugins/_lib/admin/mail/image-senders-section.js +2 -0
  17. package/plugins/_lib/admin/mail/keyboard.js +2 -0
  18. package/plugins/_lib/admin/mail/mail.css +1 -1
  19. package/plugins/_lib/admin/mail/mail.html +4 -0
  20. package/plugins/_lib/admin/mail/message-actions.js +2 -0
  21. package/plugins/_lib/admin/mail/oauth-panel.js +2 -0
  22. package/plugins/_lib/admin/mail/panes.js +2 -0
  23. package/plugins/_lib/admin/mail/reader-kit.js +2 -0
  24. package/plugins/_lib/admin/mail/reader-runtime.js +6534 -0
  25. package/plugins/_lib/admin/mail/reader-view.js +9 -6380
  26. package/plugins/_lib/admin/mail/resizable.js +2 -0
  27. package/plugins/_lib/admin/mail/restore-spam.js +13 -0
  28. package/plugins/_lib/admin/mail/rules.js +14 -0
  29. package/plugins/_lib/admin/mail/scheduling.js +4 -1
  30. package/plugins/_lib/admin/mail/section-kit.js +4 -1
  31. package/plugins/_lib/admin/mail/security-note.js +2 -0
  32. package/plugins/_lib/admin/mail/settings-section.js +11 -10
  33. package/plugins/_lib/admin/mail/templates.js +5 -2
  34. package/plugins/_lib/admin/mail/threads.js +2 -0
  35. package/plugins/_lib/admin/mail/vacation.js +16 -2
  36. package/plugins/_lib/admin/mail/when-closed.js +24 -2
  37. package/plugins/_lib/admin/ui/dates.js +2 -0
  38. package/plugins/_lib/admin/ui/resizable.js +7 -5
  39. package/plugins/_lib/contacts/mailContact.js +69 -0
  40. package/plugins/_lib/contacts/model.js +199 -0
  41. package/plugins/_lib/domma-mail-contacts.lock.json +10 -0
  42. package/plugins/_lib/domma-mail.lock.json +85 -0
  43. package/plugins/_lib/mail/accountFileStore.js +66 -0
  44. package/plugins/_lib/mail/accountRepository.js +617 -0
  45. package/plugins/_lib/mail/accountRoutes.js +130 -0
  46. package/plugins/_lib/mail/accounts.js +66 -940
  47. package/plugins/_lib/mail/automationLock.js +10 -0
  48. package/plugins/_lib/mail/bodyTokenProvider.js +36 -0
  49. package/plugins/_lib/mail/bodyTokens.js +5 -99
  50. package/plugins/_lib/mail/compose.js +5 -3
  51. package/plugins/_lib/mail/defaults.js +2 -0
  52. package/plugins/_lib/mail/envelope.js +3 -1
  53. package/plugins/_lib/mail/folderActions.js +83 -0
  54. package/plugins/_lib/mail/folderRuntime.js +109 -0
  55. package/plugins/_lib/mail/folders.js +4 -9
  56. package/plugins/_lib/mail/imapPool.js +11 -389
  57. package/plugins/_lib/mail/imapPoolRuntime.js +396 -0
  58. package/plugins/_lib/mail/mbox.js +2 -0
  59. package/plugins/_lib/mail/message-action.js +2 -0
  60. package/plugins/_lib/mail/messageSecurity.js +4 -111
  61. package/plugins/_lib/mail/messageSecurityProvider.js +116 -0
  62. package/plugins/_lib/mail/networkAddresses.js +77 -0
  63. package/plugins/_lib/mail/networkPolicy.js +62 -0
  64. package/plugins/_lib/mail/oauth.js +3 -451
  65. package/plugins/_lib/mail/oauthAccounts.js +178 -0
  66. package/plugins/_lib/mail/oauthRouteRuntime.js +282 -0
  67. package/plugins/_lib/mail/oauthRoutes.js +7 -289
  68. package/plugins/_lib/mail/oauthRuntime.js +456 -0
  69. package/plugins/_lib/mail/pollSchedule.js +2 -0
  70. package/plugins/_lib/mail/poller.js +4 -136
  71. package/plugins/_lib/mail/pollerRuntime.js +150 -0
  72. package/plugins/_lib/mail/priority.js +2 -0
  73. package/plugins/_lib/mail/publicMailbox.js +27 -0
  74. package/plugins/_lib/mail/readRoutes.js +14 -827
  75. package/plugins/_lib/mail/readRuntime.js +701 -0
  76. package/plugins/_lib/mail/render.js +2 -0
  77. package/plugins/_lib/mail/ruleRunner.js +27 -10
  78. package/plugins/_lib/mail/scheduler.js +11 -279
  79. package/plugins/_lib/mail/schedulerRuntime.js +259 -0
  80. package/plugins/_lib/mail/secretProvider.js +112 -0
  81. package/plugins/_lib/mail/secretbox.js +12 -225
  82. package/plugins/_lib/mail/send.js +21 -537
  83. package/plugins/_lib/mail/sieve.js +211 -0
  84. package/plugins/_lib/mail/sieveHost.js +24 -0
  85. package/plugins/_lib/mail/smtpService.js +553 -0
  86. package/plugins/_lib/mail/snoozeRuntime.js +60 -0
  87. package/plugins/_lib/mail/store.js +122 -64
  88. package/plugins/_lib/mail/subscriptions.js +11 -0
  89. package/plugins/_lib/mail/sync.js +64 -290
  90. package/plugins/_lib/mail/syncPlan.js +2 -0
  91. package/plugins/_lib/mail/syncRuntime.js +255 -0
  92. package/plugins/_lib/mail/syncSelection.js +2 -0
  93. package/plugins/_lib/mail/unreadBadge.js +2 -0
  94. package/plugins/_lib/mail/unsubscribe.js +2 -0
  95. package/plugins/_lib/mail/vacationRunner.js +3 -116
  96. package/plugins/_lib/mail/vacationRuntime.js +70 -0
  97. package/plugins/_lib/mail/write.js +44 -13
  98. package/server/routes/api/contacts.js +2 -196
@@ -1,3 +1,5 @@
1
+ // Source: @domma-mail/core 0.18.0, packages/core/src/mail/store.js.
2
+ // Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
1
3
  /**
2
4
  * The synchronised message store.
3
5
  *
@@ -42,16 +44,9 @@ const AUTOREPLIES = 'cms_mail_autoreplies';
42
44
  export const MAX_QUEUED_BYTES = 12_000_000;
43
45
 
44
46
  /**
45
- * How long a claimed row may sit before another tick may take it back.
46
- *
47
- * A claim flips a row to `sending` or `waking`, and only the worker finishing
48
- * moves it out again - so a process that is restarted mid-claim leaves the
49
- * row in a state nothing ever matches again. It is then in no list, has no
50
- * button, and the message is neither sent nor reported as failed.
51
- *
52
- * Generous, because the point is to recover from a dead process rather than
53
- * to race a slow mail server: a send that genuinely takes four minutes must
54
- * not be picked up twice.
47
+ * An abandoned send becomes failed with deliveryUnknown after five minutes.
48
+ * SMTP acceptance cannot be inferred from a lost acknowledgement. An
49
+ * abandoned wake likewise needs inspection before another MOVE.
55
50
  */
56
51
  const CLAIM_STALE_MS = 5 * 60_000;
57
52
 
@@ -179,10 +174,12 @@ export function addressFilter(accountIds, addresses) {
179
174
  * @param {{uri: string, database: string}} connection
180
175
  * @returns {object} store API
181
176
  */
182
- export function createStore(connection) {
177
+ export function createStore(connection, {createClient, serverSelectionTimeoutMS = 8000} = {}) {
183
178
  let client = null;
184
179
  let db = null;
185
180
  let connecting = null;
181
+ let closed = false;
182
+ let closing = null;
186
183
 
187
184
  /**
188
185
  * Connect once, lazily, and make sure the indexes exist.
@@ -190,23 +187,35 @@ export function createStore(connection) {
190
187
  * @returns {Promise<object>} the database handle
191
188
  */
192
189
  async function connect() {
190
+ if (closed) throw new Error('The mail store is closed.');
193
191
  if (db) return db;
194
192
  if (connecting) return connecting;
195
193
 
196
- connecting = (async () => {
197
- const {MongoClient} = await import('mongodb');
198
- client = new MongoClient(connection.uri, {serverSelectionTimeoutMS: 8000});
199
- await client.connect();
200
- db = client.db(connection.database);
201
- await ensureIndexes(db);
202
- return db;
194
+ const pending = (async () => {
195
+ let candidate;
196
+ try {
197
+ if (createClient) candidate = await createClient(connection.uri, {serverSelectionTimeoutMS});
198
+ else {
199
+ const {MongoClient} = await import('mongodb');
200
+ candidate = new MongoClient(connection.uri, {serverSelectionTimeoutMS});
201
+ }
202
+ await candidate.connect();
203
+ const database = candidate.db(connection.database);
204
+ await ensureIndexes(database);
205
+ if (closed) throw new Error('The mail store is closed.');
206
+ // Publish only a fully connected and indexed handle. A failed
207
+ // attempt cannot poison the next request's cached connection.
208
+ client = candidate;
209
+ db = database;
210
+ return database;
211
+ } catch (error) {
212
+ await candidate?.close().catch(() => {});
213
+ throw error;
214
+ }
203
215
  })();
204
-
205
- try {
206
- return await connecting;
207
- } finally {
208
- connecting = null;
209
- }
216
+ connecting = pending;
217
+ try { return await pending; }
218
+ finally { if (connecting === pending) connecting = null; }
210
219
  }
211
220
 
212
221
  /**
@@ -245,13 +254,20 @@ export function createStore(connection) {
245
254
  ]);
246
255
  }
247
256
 
248
- /**
249
- * Folder sync state, or null when this folder has never been synced.
250
- *
251
- * @param {string} accountId
252
- * @param {string} path
253
- * @returns {Promise<object|null>}
254
- */
257
+ /** Ascending UID cursor for bounded processing within one folder snapshot. */
258
+ async function listMessagesByUid(accountId, path, {from, through, after = 0, limit = 200}) {
259
+ const database = await connect();
260
+ return database.collection(MESSAGES).find({accountId, folder: path, uid: {$gte: Math.max(from, after + 1), $lte: through}}, {projection: {_id: 0}})
261
+ .sort({uid: 1}).limit(Math.min(500, limit)).toArray();
262
+ }
263
+
264
+ /** Folder snapshots for one mailbox. Used to bound queries to its current mirror. */
265
+ async function listFolders(accountId) {
266
+ const database = await connect();
267
+ return database.collection(FOLDERS).find({accountId}, {projection: {_id: 0}}).sort({path: 1}).toArray();
268
+ }
269
+
270
+ /** Folder sync state, or null when this folder has never been synced. */
255
271
  async function getFolder(accountId, path) {
256
272
  const database = await connect();
257
273
  return database.collection(FOLDERS).findOne({_id: folderId(accountId, path)});
@@ -538,11 +554,17 @@ export function createStore(connection) {
538
554
  /**
539
555
  * @returns {Promise<void>}
540
556
  */
541
- async function close() {
542
- const open = client;
543
- client = null;
544
- db = null;
545
- if (open) await open.close().catch(() => {});
557
+ function close() {
558
+ closed = true;
559
+ closing ??= (async () => {
560
+ // Let a connection in flight dispose itself before returning.
561
+ await connecting?.catch(() => {});
562
+ const open = client;
563
+ client = null;
564
+ db = null;
565
+ if (open) await open.close().catch(() => {});
566
+ })();
567
+ return closing;
546
568
  }
547
569
 
548
570
  /**
@@ -719,19 +741,16 @@ export function createStore(connection) {
719
741
 
720
742
  const stale = new Date(at.getTime() - CLAIM_STALE_MS);
721
743
 
744
+ // An expired SMTP claim may have delivered before its acknowledgement
745
+ // was lost. Surface it for inspection rather than sending it twice.
746
+ await database.collection(OUTBOX).updateMany({status: 'sending', claimedAt: {$lte: stale}}, {$set: {
747
+ status: 'failed', deliveryUnknown: true, finishedAt: new Date(),
748
+ error: 'Delivery could not be confirmed. Check Sent and the recipient before sending again.'
749
+ }});
722
750
  for (let i = 0; i < max; i += 1) {
723
751
  const result = await database.collection(OUTBOX).findOneAndUpdate(
724
752
  {
725
- $or: [
726
- {status: 'pending', at: {$lte: at}},
727
- // Reclaimed. A row left `sending` by a restart matches
728
- // nothing otherwise, and the message is then neither
729
- // sent nor reported - see CLAIM_STALE_MS. The attempt
730
- // counter still applies, so a send that kills the
731
- // process every time is given up on rather than
732
- // retried forever.
733
- {status: 'sending', claimedAt: {$lte: stale}}
734
- ]
753
+ status: 'pending', at: {$lte: at}
735
754
  },
736
755
  {$set: {status: 'sending', claimedAt: new Date()}, $inc: {attempts: 1}},
737
756
  {sort: {at: 1}, returnDocument: 'after'}
@@ -788,6 +807,15 @@ export function createStore(connection) {
788
807
  return database.collection(OUTBOX).findOne({_id: id, userId});
789
808
  }
790
809
 
810
+ /** Bring forward only a still-actionable row; cancellation/claim wins atomically. */
811
+ async function retrySend(userId, id, accountId) {
812
+ const database = await connect();
813
+ return claimed(await database.collection(OUTBOX).findOneAndUpdate(
814
+ {_id: id, userId, accountId, status: {$in: ['pending', 'failed']}, deliveryUnknown: {$ne: true}},
815
+ {$set: {at: new Date(0), status: 'pending', attempts: 0, error: null}}, {returnDocument: 'after'}
816
+ ));
817
+ }
818
+
791
819
  /**
792
820
  * How much unread mail a mailbox holds, across its mirrored folders.
793
821
  *
@@ -938,13 +966,32 @@ export function createStore(connection) {
938
966
  * @param {string} address
939
967
  * @returns {Promise<string|null>} an ISO timestamp, or null
940
968
  */
941
- async function lastAutoReply(accountId, address) {
969
+ async function getAutoReply(accountId, address, epoch = 'legacy') {
942
970
  const database = await connect();
943
- const row = await database.collection(AUTOREPLIES)
944
- .findOne({_id: autoReplyId(accountId, address)});
971
+ return database.collection(AUTOREPLIES).findOne({_id: autoReplyId(accountId, address) + (epoch === 'legacy' ? '' : ':' + epoch)});
972
+ }
973
+
974
+ async function lastAutoReply(accountId, address, epoch = 'legacy') {
975
+ const row = await getAutoReply(accountId, address, epoch);
945
976
  return row?.at ? new Date(row.at).toISOString() : null;
946
977
  }
947
978
 
979
+ /** A duplicate upsert loses the claim rather than sending twice. */
980
+ async function claimAutoReply(accountId, address, {epoch = 'legacy', now = new Date(), intervalDays = 4} = {}) {
981
+ const database = await connect(), claimId = randomId();
982
+ const id = autoReplyId(accountId, address) + (epoch === 'legacy' ? '' : ':' + epoch);
983
+ try {
984
+ return claimed(await database.collection(AUTOREPLIES).findOneAndUpdate({_id: id, $or: [
985
+ {at: {$exists: false}}, {at: {$lte: new Date(+now - intervalDays * 86400000)}, status: {$nin: ['sending', 'unknown']}}
986
+ ]}, {$set: {accountId, address: String(address).trim().toLowerCase(), epoch, at: new Date(now), status: 'sending', claimId, error: null}}, {upsert: true, returnDocument: 'after'}));
987
+ } catch (error) {if (error.code === 11000) return null; throw error;}
988
+ }
989
+
990
+ async function finishAutoReply(claim, patch) {
991
+ const database = await connect();
992
+ await database.collection(AUTOREPLIES).updateOne({_id: claim._id, claimId: claim.claimId}, {$set: {...patch, finishedAt: new Date()}});
993
+ }
994
+
948
995
  /**
949
996
  * Remember that it has been answered.
950
997
  *
@@ -1009,7 +1056,7 @@ export function createStore(connection) {
1009
1056
  const database = await connect();
1010
1057
  const id = `snz-${randomId()}`;
1011
1058
  await database.collection(SNOOZED).insertOne({
1012
- ...record, _id: id, status: 'pending', createdAt: new Date()
1059
+ ...record, _id: id, status: record.status === 'parking' ? 'parking' : 'pending', attempts: 0, createdAt: new Date()
1013
1060
  });
1014
1061
  return id;
1015
1062
  }
@@ -1033,6 +1080,21 @@ export function createStore(connection) {
1033
1080
  .toArray();
1034
1081
  }
1035
1082
 
1083
+ async function getSnooze(userId, id) {
1084
+ const database = await connect();
1085
+ return database.collection(SNOOZED).findOne({_id: id, userId});
1086
+ }
1087
+
1088
+ /** Follow confirmed renames; deleted paths require manual recovery. */
1089
+ async function rewriteSnoozeFolders(accountId, changes) {
1090
+ const database = await connect();
1091
+ for (const {from, to} of changes) if (from) for (const field of ['folder', 'returnTo']) {
1092
+ await database.collection(SNOOZED).updateMany({accountId, [field]: from, status: {$nin: ['woken', 'cancelled']}}, {$set: to
1093
+ ? {[field]: to}
1094
+ : {status: 'failed', requiresInspection: true, error: 'A snooze folder was deleted. Check the message manually.'}});
1095
+ }
1096
+ }
1097
+
1036
1098
  /**
1037
1099
  * Claim the snoozes that have come due. Same one-at-a-time claim as sends.
1038
1100
  *
@@ -1047,17 +1109,13 @@ export function createStore(connection) {
1047
1109
 
1048
1110
  const stale = new Date(at.getTime() - CLAIM_STALE_MS);
1049
1111
 
1112
+ await database.collection(SNOOZED).updateMany({status: 'waking', claimedAt: {$lte: stale}}, {$set: {
1113
+ status: 'failed', requiresInspection: true, error: 'The wake-up was interrupted. Check the snooze and return folders manually.', finishedAt: new Date()
1114
+ }});
1050
1115
  for (let i = 0; i < max; i += 1) {
1051
1116
  const result = await database.collection(SNOOZED).findOneAndUpdate(
1052
1117
  {
1053
- $or: [
1054
- {status: 'pending', at: {$lte: at}},
1055
- // Same reclaim as the outbox. Worse here if it is
1056
- // missed: `listSnoozed` only shows pending rows, so a
1057
- // row stuck at `waking` vanishes from the Snoozed list
1058
- // while the message sits in a folder nobody reads.
1059
- {status: 'waking', claimedAt: {$lte: stale}}
1060
- ]
1118
+ status: 'pending', at: {$lte: at}
1061
1119
  },
1062
1120
  {$set: {status: 'waking', claimedAt: new Date()}, $inc: {attempts: 1}},
1063
1121
  {sort: {at: 1}, returnDocument: 'after'}
@@ -1092,8 +1150,8 @@ export function createStore(connection) {
1092
1150
  // A failed row is exactly the one somebody most wants to retry by
1093
1151
  // hand, so it is eligible too - it is reset to pending and the
1094
1152
  // attempt counter cleared.
1095
- {_id: id, userId, status: {$in: ['pending', 'failed']}},
1096
- {$set: {at: new Date(0), status: 'pending', attempts: 0, wokeEarly: true}},
1153
+ {_id: id, userId, status: {$in: ['pending', 'failed']}, requiresInspection: {$ne: true}},
1154
+ {$set: {at: new Date(0), status: 'pending', attempts: 0, wokeEarly: true, error: null}},
1097
1155
  {returnDocument: 'after'}
1098
1156
  );
1099
1157
  return claimed(result);
@@ -1101,12 +1159,12 @@ export function createStore(connection) {
1101
1159
 
1102
1160
  return {
1103
1161
  connect,
1104
- getFolder, saveFolder,
1162
+ listMessagesByUid, listFolders, getFolder, saveFolder,
1105
1163
  putMessages, setFlags, setMessageAction, removeMessages, clearFolder,
1106
1164
  storedUids, listMessages, listThreads, searchMessages, countUnread, listUnread, newestUnreadUid, listByAddresses, forgetAccount,
1107
- queueSend, listQueuedSends, claimDueSends, finishSend, cancelSend, getQueuedSend,
1108
- queueSnooze, listSnoozed, claimDueSnoozes, finishSnooze, wakeSnooze,
1109
- lastAutoReply, noteAutoReply, listAutoReplies, clearAutoReplies,
1165
+ queueSend, listQueuedSends, claimDueSends, finishSend, cancelSend, getQueuedSend, retrySend,
1166
+ queueSnooze, listSnoozed, getSnooze, rewriteSnoozeFolders, claimDueSnoozes, finishSnooze, wakeSnooze,
1167
+ lastAutoReply, getAutoReply, claimAutoReply, finishAutoReply, noteAutoReply, listAutoReplies, clearAutoReplies,
1110
1168
  close
1111
1169
  };
1112
1170
  }
@@ -0,0 +1,11 @@
1
+ // Source: @domma-mail/core 0.18.0, packages/core/src/mail/subscriptions.js.
2
+ // Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
3
+ export function resolveSubscriptions(boxes) {
4
+ const list = boxes ?? [];
5
+ const serverAnswers = list.some(box => box.subscribed === true);
6
+ const out = new Map();
7
+ for (const box of list) {
8
+ out.set(box.path, serverAnswers ? box.subscribed === true : true);
9
+ }
10
+ return out;
11
+ }