This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
notifications.js
59 lines (47 loc) · 2 KB
/
notifications.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'
const P = require('../promise')
const utils = require('./utils/helpers')
// Account deletion threshold for new unverified accounts that receive
// a bounce or complaint notification. Unverified accounts younger than
// 6 hours old will be deleted if a bounce or complaint occurs.
const SIX_HOURS = 1000 * 60 * 60 * 6
module.exports = (log, error) => {
return (queue, db) => {
queue.start()
queue.on('data', async message => {
try {
utils.logErrorIfHeadersAreWeirdOrMissing(log, message, 'notification')
let addresses = [], eventType = 'bounced', isDeletionCandidate = false
if (message.bounce) {
addresses = message.bounce.bouncedRecipients
isDeletionCandidate = true
} else if (message.complaint) {
addresses = message.complaint.complainedRecipients
isDeletionCandidate = true
} else if (message.delivery) {
addresses = message.delivery.recipients
eventType = 'delivered'
}
await P.all(addresses.map(async address => {
const domain = utils.getAnonymizedEmailDomain(address)
utils.logFlowEventFromMessage(log, message, eventType)
utils.logEmailEventFromMessage(log, message, eventType, domain)
if (isDeletionCandidate) {
const emailRecord = await db.accountRecord(address)
if (! emailRecord.emailVerified && emailRecord.createdAt >= Date.now() - SIX_HOURS) {
// A bounce or complaint on a new unverified account is grounds for deletion
await db.deleteAccount(emailRecord)
log.info({ op: 'accountDeleted', ...emailRecord })
}
}
}))
} catch (err) {
log.error({ op: 'email.notification.error', err })
}
message.del()
})
}
}