summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2021-06-23 19:09:03 +0200
committerDaniel Weipert <code@drogueronin.de>2021-06-23 19:09:03 +0200
commit9863e78cc9e80661f03b4332754f9f1bd026b5fd (patch)
tree1f181360bbe4ac82bfe8ab7897a7a308c249b2ec /app
parent128a4891b790ce23aee6a7e3743a55ecf6a2ca61 (diff)
Refactors migration and enhances tests
Diffstat (limited to 'app')
-rw-r--r--app/imap.js30
1 files changed, 16 insertions, 14 deletions
diff --git a/app/imap.js b/app/imap.js
index f2e8cc2..181d9cf 100644
--- a/app/imap.js
+++ b/app/imap.js
@@ -20,6 +20,16 @@ async function connect (options) {
return client;
}
+ImapFlow.prototype.fetchArray = async function (range, query, options = {}) {
+ const msgsGenerator = await this.fetch(range, query, options);
+ const msgs = [];
+ for await (const msg of msgsGenerator) {
+ msgs.push(msg);
+ }
+
+ return msgs;
+};
+
async function listTreeFrom (event, options) {
const client = await connect(options);
@@ -47,26 +57,18 @@ async function migrate (event, { from, to }) {
const fromMailbox = await fromClient.getMailboxLock(fromFolder.path);
const toMailbox = await toClient.getMailboxLock(fromFolder.path);
try {
- const toMsgsGenerator = await toClient.fetch('1:*', { flags: true, envelope: true, source: true });
- const toMsgs = [];
- for await (const toMsg of toMsgsGenerator) {
- toMsgs.push(toMsg);
- }
- const fromMsgsGenerator = await fromClient.fetch('1:*', { flags: true, envelope: true, source: true });
+ const toMsgs = await toClient.fetchArray('1:*', { flags: true, envelope: true, source: true });
+ const fromMsgs = await fromClient.fetchArray('1:*', { flags: true, envelope: true, source: true });
- for await (const fromMsg of fromMsgsGenerator) {
- if (toMsgs.some((toMsg) => Buffer.compare(toMsg.source, fromMsg.source) === 0)) {
- continue;
- }
+ const msgs = fromMsgs.filter((fromMsg) => !toMsgs.some((toMsg) => Buffer.compare(toMsg.source, fromMsg.source) === 0));
- await toClient.append(fromFolder.path, fromMsg.source, Array.from(fromMsg.flags), fromMsg.envelope.date);
+ await toClient.noop();
+ for (const msg of msgs) {
+ toClient.append(fromFolder.path, msg.source, Array.from(msg.flags), msg.envelope.date);
}
} finally {
fromMailbox.release();
toMailbox.release();
}
}
-
- await fromClient.logout();
- await toClient.logout();
}