feat(tracking): Add routes for open and link tracking

This commit is contained in:
Jackie Luo 2016-12-08 13:38:24 -08:00
parent 1ba56c5c05
commit 7a763b604e
2 changed files with 25 additions and 6 deletions

View file

@ -64,6 +64,7 @@ class SendmailClient {
msgData[field] = formatParticipants(draft[field])
}
}
msgData.date = draft.date;
msgData.subject = draft.subject;
msgData.html = draft.body;
msgData.messageId = `${draft.id}@nylas.com`;
@ -89,6 +90,12 @@ class SendmailClient {
return msgData;
}
_getBodyWithMessageIds(draft) {
return draft.body.replace(/n1cloud\.nylas\.com\/.+MESSAGE_ID/g, (match) => {
return match.replace('MESSAGE_ID', draft.id)
})
}
async buildMime(draft) {
const builder = mailcomposer(this._draftToMsgData(draft))
const mimeNode = await (new Promise((resolve, reject) => {
@ -110,7 +117,7 @@ class SendmailClient {
async sendCustomBody(draft, body, recipients) {
const origBody = draft.body;
draft.body = body;
draft.body = this._getBodyWithMessageIds(draft);
const envelope = {};
for (const field of Object.keys(recipients)) {
envelope[field] = recipients[field].map(r => r.email);
@ -118,8 +125,8 @@ class SendmailClient {
const raw = await this.buildMime(draft);
const responseOnSuccess = draft.toJSON();
draft.body = origBody;
await this._send({raw, envelope})
return responseOnSuccess
await this._send({raw, envelope});
return responseOnSuccess;
}
}

View file

@ -110,7 +110,15 @@ module.exports = (sequelize, Sequelize) => {
hashForHeaders(headers) {
return cryptography.createHash('sha256').update(headers, 'utf8').digest('hex');
},
fromJSON(data) {
getHeadersForId(data) {
let participants = "";
const emails = _.pluck(data.from.concat(data.to, data.cc, data.bcc), 'email');
emails.sort().forEach((email) => {
participants += email
});
return `${data.date}-${data.subject}-${participants}`;
},
fromJSON(id, data) {
// TODO: events, metadata??
return this.build({
accountId: data.account_id,
@ -126,14 +134,16 @@ module.exports = (sequelize, Sequelize) => {
isSent: false,
version: 0,
date: data.date,
id: data.id,
id: id,
uploads: data.uploads,
});
},
async associateFromJSON(data, db) {
const message = this.fromJSON(data);
const {Thread, Message} = db;
const messageId = Message.getHeadersForId(data)
const message = this.fromJSON(messageId, data);
let replyToThread;
let replyToMessage;
if (data.thread_id != null) {
@ -220,6 +230,7 @@ module.exports = (sequelize, Sequelize) => {
regenerateHeaderMessageId() {
this.headerMessageId = `<${this.id}-${this.version}@mailer.nylas.com>`
},
toJSON() {
if (this.folderId && !this.folder) {
throw new Error("Message.toJSON called on a message where folder were not eagerly loaded.")
@ -250,6 +261,7 @@ module.exports = (sequelize, Sequelize) => {
};
},
},
hooks: {
beforeUpdate: (message) => {
// Update the snippet if the body has changed