From 9863e78cc9e80661f03b4332754f9f1bd026b5fd Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 23 Jun 2021 19:09:03 +0200 Subject: Refactors migration and enhances tests --- app/imap.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'app') 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(); } -- cgit v1.2.3