From 2816158b141dbb178e2f9b369d7abc8c0c01c69a Mon Sep 17 00:00:00 2001 From: cnouguier Date: Wed, 19 Jun 2024 10:21:18 +0200 Subject: [PATCH] wip: Provide hooks to manage IMAP connection #277 --- lib/hooks/hooks.imap.js | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/hooks/hooks.imap.js b/lib/hooks/hooks.imap.js index 36ff2736..2b09c5bf 100644 --- a/lib/hooks/hooks.imap.js +++ b/lib/hooks/hooks.imap.js @@ -50,8 +50,7 @@ export function disconnectIMAP (options = {}) { export function listIMAPMailboxes (options = {}) { return callOnHookItems(options)(async (item, hook) => { const client = _.get(hook.data, options.clientPath || 'client') - if (_.isNil(client)) throw new Error('You must provide client parameters to use the \'listIMAPMailboxes\' hook') - + if (_.isNil(client)) throw new Error('You must provide \'client\' parameter to use the \'listIMAPMailboxes\' hook') debug(`[IMAP] ${hook.data.id}: listing mailboxes`) const mailboxes = await client.list(options) _.set(hook, options.dataPath || 'result.data', mailboxes) @@ -62,8 +61,10 @@ export function listIMAPMailboxes (options = {}) { export function fetchIMAPMessages (options = {}) { return callOnHookItems(options)(async (item, hook) => { const client = _.get(hook.data, options.clientPath || 'client') - if (_.isNil(client)) throw new Error('You must provide client parameters to use the \'fetchIMAPMessages\' hook') - + if (_.isNil(client)) throw new Error('You must provide client parameter to use the \'fetchIMAPMessages\' hook') + if (!options.mailbox) throw new Error('You must provide \'mailbox\' parameter to use the \'fetchIMAPMessages\' hook') + if (!options.range) throw new Error('You must provide \'range\' parameter to use the \'fetchIMAPMessages\' hook') + if (!options.query) throw new Error('You must provide \'query\' parameter to use the \'fetchIMAPMessages\' hook') debug(`[IMAP] ${hook.data.id}: fetching messages`) let lock = await client.getMailboxLock(options.mailbox) try { @@ -74,7 +75,8 @@ export function fetchIMAPMessages (options = {}) { _.set(hook, options.dataPath || 'result.data', messages) } finally { // Make sure lock is released, otherwise next `getMailboxLock()` never returns - lock.release(); + lock.release() + console.log('done') } }) } @@ -83,9 +85,10 @@ export function fetchIMAPMessages (options = {}) { export function downloadIMAPAttachments (options = {}) { return callOnHookItems(options)(async (item, hook) => { const client = _.get(hook.data, options.clientPath || 'client') - if (_.isNil(client)) throw new Error('You must provide client parameters to use the \'downloadIMAPContent\' hook') + if (_.isNil(client)) throw new Error('You must provide \'client\' parameter to use the \'downloadIMAPContent\' hook') const store = await getStoreFromHook(hook, 'downloadIMAPAttachment', options) - + if (!options.mailbox) throw new Error('You must provide \'mailbox\' parameter to use the \'fetchIMAPMessages\' hook') + if (!options.range) throw new Error('You must provide \'range\' parameter to use the \'fetchIMAPMessages\' hook') debug(`[IMAP] ${hook.data.id}: download attachments`) let lock = await client.getMailboxLock(options.mailbox) try { @@ -122,8 +125,10 @@ export function downloadIMAPAttachments (options = {}) { export function flagIMAPMessages (options = {}) { return callOnHookItems(options)(async (item, hook) => { const client = _.get(hook.data, options.clientPath || 'client') - if (_.isNil(client)) throw new Error('You must provide client parameters to use the \'deleteIMAPMessages\' hook') - + if (_.isNil(client)) throw new Error('You must provide \'client\' parameter to use the \'deleteIMAPMessages\' hook') + if (!options.mailbox) throw new Error('You must provide \'mailbox\' parameter to use the \'fetchIMAPMessages\' hook') + if (!options.range) throw new Error('You must provide \'range\' parameter to use the \'fetchIMAPMessages\' hook') + if (!options.flags) throw new Error('You must provide \'flags\' parameter to use the \'fetchIMAPMessages\' hook') debug(`[IMAP] ${hook.data.id}: flag messages`) let lock = await client.getMailboxLock(options.mailbox) try { @@ -139,8 +144,10 @@ export function flagIMAPMessages (options = {}) { export function unflagIMAPMessages (options = {}) { return callOnHookItems(options)(async (item, hook) => { const client = _.get(hook.data, options.clientPath || 'client') - if (_.isNil(client)) throw new Error('You must provide client parameters to use the \'deleteIMAPMessages\' hook') - + if (_.isNil(client)) throw new Error('You must provide \'client\' parameter to use the \'deleteIMAPMessages\' hook') + if (!options.mailbox) throw new Error('You must provide \'mailbox\' parameter to use the \'fetchIMAPMessages\' hook') + if (!options.range) throw new Error('You must provide \'range\' parameter to use the \'fetchIMAPMessages\' hook') + if (!options.flags) throw new Error('You must provide \'flags\' parameter to use the \'fetchIMAPMessages\' hook') debug(`[IMAP] ${hook.data.id}: unflag messages`) let lock = await client.getMailboxLock(options.mailbox) try { @@ -156,15 +163,16 @@ export function unflagIMAPMessages (options = {}) { export function deleteIMAPMessages (options = {}) { return callOnHookItems(options)(async (item, hook) => { const client = _.get(hook.data, options.clientPath || 'client') - if (_.isNil(client)) throw new Error('You must provide client parameters to use the \'deleteIMAPMessages\' hook') - + if (_.isNil(client)) throw new Error('You must provide \'client\' parameter to use the \'deleteIMAPMessages\' hook') + if (!options.mailbox) throw new Error('You must provide \'mailbox\' parameter to use the \'fetchIMAPMessages\' hook') + if (!options.range) throw new Error('You must provide \'range\' parameter to use the \'fetchIMAPMessages\' hook') debug(`[IMAP] ${hook.data.id}: delete messages`) let lock = await client.getMailboxLock(options.mailbox) try { await client.messageDelete(options.range, _.omit(options, ['mailbox', 'range', 'clientPath'])) } finally { // Make sure lock is released, otherwise next `getMailboxLock()` never returns - lock.release(); + lock.release() } }) } \ No newline at end of file