Skip to content

Commit

Permalink
for mozilla#251: remove subscribers on bounce or complaint
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Aug 14, 2018
1 parent 872d5f8 commit c2b9953
Show file tree
Hide file tree
Showing 9 changed files with 1,905 additions and 1,727 deletions.
68 changes: 68 additions & 0 deletions controllers/ses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"use strict";

const DB = require("../db/DB");


const notification = async function(req, res) {
try {
const notification = JSON.parse(req.body);
// TODO: verifyNotification(notification) or use http basic auth
await handleNotification(notification);

res.status(200).json(
{status: "OK"}
);
} catch (e) {
res.status(500).json(
{info: "Internal error."}
);
}
};


const handleNotification = async function(notification) {
console.log("Received SES message, ID: ", notification.MessageId);
const message = JSON.parse(notification.Message);
switch (message.eventType) {
case "Bounce":
await handleBounceMessage(message);
break;
case "Complaint":
await handleComplaintMessage(message);
break;
default:
console.log("Unhandled eventType: ", message.eventType);
}
};


const handleBounceMessage = async function(message) {
const bounce = message.bounce;
if (
bounce.bounceType === "Permanent" &&
["General", "NoEmail"].includes(bounce.bounceSubType)
) {
return await removeSubscribersFromDB(bounce.bouncedRecipients);
}
};


const handleComplaintMessage = async function(message) {
const complaint = message.complaint;
return await removeSubscribersFromDB(complaint.complainedRecipients);
};


const removeSubscribersFromDB = async function(recipients) {
for (const recipient of recipients) {
await DB.removeSubscriber(recipient.emailAddress);
}
};

module.exports = {
notification,
handleNotification,
handleBounceMessage,
handleComplaintMessage,
removeSubscribersFromDB,
};
15 changes: 9 additions & 6 deletions db/DB.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";

const AppConstants = require("../app-constants");

// eslint-disable-next-line node/no-extraneous-require
const uuidv4 = require("uuid/v4");
const Knex = require("knex");
const knexConfig = require("./knexfile");

const AppConstants = require("../app-constants");
const HIBP = require("../hibp");
const getSha1 = require("../sha1-utils");

const knexConfig = require("./knexfile");
const knex = Knex(knexConfig[AppConstants.NODE_ENV]);


Expand Down Expand Up @@ -88,11 +88,14 @@ const DB = {
const sha1 = getSha1(email);

return await this._getSha1EntryAndDo(sha1, async aEntry => {
const removedSubscriber = await knex("subscribers")
.update({ email: null })
await knex("subscribers")
.where("id", "=", aEntry.id)
.returning("*");
return removedSubscriber[0];
.del();
console.log("Removed subscriber ID: ", aEntry.id);
return aEntry;
}, async () => {
console.warn("removeSubscriber called with email not found in database.");
return;
});
},

Expand Down
Loading

0 comments on commit c2b9953

Please sign in to comment.