summaryrefslogtreecommitdiff
path: root/app/imap.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/imap.js')
-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();
}