Skip to content

Commit

Permalink
wip: Provide hooks to manage IMAP connection #277
Browse files Browse the repository at this point in the history
  • Loading branch information
cnouguier committed Jun 19, 2024
1 parent 863fe6c commit 2816158
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions lib/hooks/hooks.imap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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')
}
})
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
}
})
}

0 comments on commit 2816158

Please sign in to comment.