UNPKG

imapflow

Version:

IMAP Client for Node

56 lines (48 loc) 1.9 kB
'use strict'; const { getStatusCode, getErrorText } = require('../tools.js'); const { searchCompiler } = require('../search-compiler.js'); // Updates flags for a message module.exports = async (connection, query, options) => { if (connection.state !== connection.states.SELECTED) { // nothing to do here return false; } options = options || {}; let attributes; if (!query || query === true || (typeof query === 'object' && (!Object.keys(query).length || (Object.keys(query).length === 1 && query.all)))) { // search for all messages attributes = [{ type: 'ATOM', value: 'ALL' }]; } else if (query && typeof query === 'object') { // normal query attributes = searchCompiler(connection, query); } else { return false; } let results = new Set(); let response; try { response = await connection.exec(options.uid ? 'UID SEARCH' : 'SEARCH', attributes, { untagged: { SEARCH: async untagged => { if (untagged && untagged.attributes && untagged.attributes.length) { untagged.attributes.forEach(attribute => { if (attribute && attribute.value && typeof attribute.value === 'string' && !isNaN(attribute.value)) { results.add(Number(attribute.value)); } }); } } } }); response.next(); return Array.from(results).sort((a, b) => a - b); } catch (err) { let errorCode = getStatusCode(err.response); if (errorCode) { err.serverResponseCode = errorCode; } err.response = await getErrorText(err.response); connection.log.warn({ err, cid: connection.id }); return false; } };