From 0587c2934f2cabb7bc6b66437410fb10296c085f Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Tue, 21 Jul 2020 19:31:23 +0300 Subject: [PATCH] Added missing index and address handler --- indexes.yaml | 17 +++++++++++++++++ lib/audit-handler.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/indexes.yaml b/indexes.yaml index 0a395724..929b2fe9 100644 --- a/indexes.yaml +++ b/indexes.yaml @@ -611,3 +611,20 @@ indexes: name: audit_files_expire key: metadata.info.expires: 1 + + - collection: audit.files + type: gridfs # index applies to gridfs database + index: + name: audit_files_addresses + key: + metadata.audit: 1 + metadata.addresses.type: 1 + metadata.addresses.name: 1 + metadata.addresses.address: 1 + + - collection: audit.files + type: gridfs # index applies to gridfs database + index: + name: audit_files_subject + key: + metadata.subject: 1 diff --git a/lib/audit-handler.js b/lib/audit-handler.js index f5fe0e7a..6340a738 100644 --- a/lib/audit-handler.js +++ b/lib/audit-handler.js @@ -3,6 +3,8 @@ const ObjectID = require('mongodb').ObjectID; const GridFSBucket = require('mongodb').GridFSBucket; const log = require('npmlog'); +const libmime = require('libmime'); +const { normalizeAddress } = require('./tools'); class AuditHandler { constructor(options) { @@ -117,6 +119,34 @@ class AuditHandler { metadata.audit = metadata.audit || audit; metadata.date = metadata.date || new Date(); + const headers = metadata.header || {}; + + metadata.subject = ([].concat(headers.subject || []).pop() || '').trim(); + try { + metadata.subject = libmime.decodeWords(metadata.subject); + } catch (E) { + // ignore + } + + metadata.addresses = []; + [('from', 'to', 'cc', 'bcc')].forEach(type => { + if (headers[type] && headers[type].length) { + headers[type].forEach(addr => { + let entry = { + name: addr.name, + address: normalizeAddress(addr.address), + type + }; + try { + entry.name = libmime.decodeWords(entry.name); + } catch (E) { + // ignore + } + metadata.addresses.push(entry); + }); + } + }); + let result = await new Promise((resolve, reject) => { let stream = this.gridstore.openUploadStreamWithId(id, null, { contentType: 'message/rfc822',