2016-06-29 09:01:43 +08:00
|
|
|
// const _ = require('underscore');
|
|
|
|
|
|
|
|
class ThreadingProcessor {
|
|
|
|
pickMatchingThread(message, threads) {
|
|
|
|
return threads.pop();
|
|
|
|
|
|
|
|
// This logic is tricky... Used to say that threads with >2 participants in common
|
|
|
|
// should be treated as the same, plus special cases for when it's a 1<>1
|
|
|
|
// conversation. Put it back soonish.
|
|
|
|
|
|
|
|
// const messageEmails = _.uniq([].concat(message.to, message.cc, message.from).map(p => p.email));
|
2016-07-09 08:13:30 +08:00
|
|
|
// this.logger.info({
|
|
|
|
// num_candidate_threads: threads.length,
|
|
|
|
// message_subject: message.subject,
|
|
|
|
// }, `Found candidate threads for message`)
|
2016-06-29 09:01:43 +08:00
|
|
|
//
|
|
|
|
// for (const thread of threads) {
|
|
|
|
// const threadEmails = _.uniq([].concat(thread.participants).map(p => p.email));
|
2016-07-09 08:13:30 +08:00
|
|
|
// this.logger.info(`Intersection: ${_.intersection(threadEmails, messageEmails).join(',')}`)
|
2016-06-29 09:01:43 +08:00
|
|
|
//
|
|
|
|
// if (_.intersection(threadEmails, messageEmails) >= threadEmails.length * 0.9) {
|
|
|
|
// return thread;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return null;
|
2016-06-23 08:34:21 +08:00
|
|
|
}
|
2016-06-24 06:44:03 +08:00
|
|
|
|
2016-06-29 09:01:43 +08:00
|
|
|
cleanSubject(subject = "") {
|
2016-06-29 09:11:55 +08:00
|
|
|
const regex = new RegExp(/^((re|fw|fwd|aw|wg|undeliverable|undelivered):\s*)+/ig);
|
|
|
|
return subject.replace(regex, () => "");
|
2016-06-24 06:44:03 +08:00
|
|
|
}
|
|
|
|
|
2016-07-01 00:29:21 +08:00
|
|
|
emptyThread(Thread, options = {}) {
|
|
|
|
const t = Thread.build(options)
|
|
|
|
t.folders = [];
|
|
|
|
t.labels = [];
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2016-06-29 09:01:43 +08:00
|
|
|
findOrCreateByMatching(db, message) {
|
2016-07-01 00:29:21 +08:00
|
|
|
const {Thread, Label, Folder} = db
|
2016-06-29 09:01:43 +08:00
|
|
|
|
|
|
|
// in the future, we should look at In-reply-to. Problem is it's a single-
|
|
|
|
// directional linked list, and we don't scan the mailbox from oldest=>newest,
|
|
|
|
// but from newest->oldest, so when we ingest a message it's very unlikely
|
|
|
|
// we have the "In-reply-to" message yet.
|
|
|
|
|
|
|
|
return Thread.findAll({
|
|
|
|
where: {
|
|
|
|
subject: this.cleanSubject(message.subject),
|
|
|
|
},
|
|
|
|
order: [
|
|
|
|
['id', 'DESC'],
|
|
|
|
],
|
2016-07-01 00:29:21 +08:00
|
|
|
limit: 10,
|
|
|
|
include: [{model: Label}, {model: Folder}],
|
2016-06-29 09:01:43 +08:00
|
|
|
}).then((threads) =>
|
2016-07-01 00:29:21 +08:00
|
|
|
this.pickMatchingThread(message, threads) || this.emptyThread(Thread)
|
2016-06-29 09:01:43 +08:00
|
|
|
)
|
|
|
|
}
|
2016-06-24 06:44:03 +08:00
|
|
|
|
2016-07-01 00:29:21 +08:00
|
|
|
findOrCreateByThreadId({Thread, Label, Folder}, threadId) {
|
|
|
|
return Thread.find({
|
|
|
|
where: {threadId},
|
|
|
|
include: [{model: Label}, {model: Folder}],
|
|
|
|
}).then((thread) => {
|
|
|
|
return thread || this.emptyThread(Thread, {threadId})
|
2016-06-24 06:44:03 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-09 08:13:30 +08:00
|
|
|
processMessage({db, message, logger}) {
|
2016-07-01 00:29:21 +08:00
|
|
|
if (!(message.labels instanceof Array)) {
|
|
|
|
throw new Error("Threading processMessage expects labels to be an inflated array.");
|
|
|
|
}
|
|
|
|
if (message.folder === undefined) {
|
|
|
|
throw new Error("Threading processMessage expects folder value to be present.");
|
|
|
|
}
|
|
|
|
|
2016-07-09 08:13:30 +08:00
|
|
|
this.logger = logger
|
|
|
|
|
2016-07-01 00:29:21 +08:00
|
|
|
const {Folder, Label} = db;
|
2016-06-29 09:01:43 +08:00
|
|
|
let findOrCreateThread = null;
|
|
|
|
if (message.headers['x-gm-thrid']) {
|
|
|
|
findOrCreateThread = this.findOrCreateByThreadId(db, message.headers['x-gm-thrid'])
|
|
|
|
} else {
|
|
|
|
findOrCreateThread = this.findOrCreateByMatching(db, message)
|
2016-06-24 06:44:03 +08:00
|
|
|
}
|
|
|
|
|
2016-06-29 09:01:43 +08:00
|
|
|
return Promise.props({
|
|
|
|
thread: findOrCreateThread,
|
2016-07-01 00:29:21 +08:00
|
|
|
sentFolder: Folder.find({where: {role: 'sent'}}),
|
|
|
|
sentLabel: Label.find({where: {role: 'sent'}}),
|
2016-06-28 05:52:05 +08:00
|
|
|
})
|
2016-07-01 00:29:21 +08:00
|
|
|
.then(({thread, sentFolder, sentLabel}) => {
|
2016-06-29 09:01:43 +08:00
|
|
|
thread.addMessage(message);
|
2016-06-24 06:44:03 +08:00
|
|
|
|
2016-07-01 00:29:21 +08:00
|
|
|
if (!(thread.labels instanceof Array)) {
|
|
|
|
throw new Error("Threading processMessage expects thread.labels to be an inflated array.");
|
|
|
|
}
|
|
|
|
if (!(thread.folders instanceof Array)) {
|
|
|
|
throw new Error("Threading processMessage expects thread.folders to be an inflated array.");
|
|
|
|
}
|
|
|
|
|
2016-06-30 01:36:32 +08:00
|
|
|
// update the basic properties of the thread
|
|
|
|
thread.accountId = message.accountId;
|
2016-06-28 03:30:28 +08:00
|
|
|
|
2016-06-29 09:01:43 +08:00
|
|
|
// update the participants on the thread
|
|
|
|
const threadParticipants = [].concat(thread.participants);
|
|
|
|
const threadEmails = thread.participants.map(p => p.email);
|
2016-06-29 06:01:41 +08:00
|
|
|
|
2016-06-29 09:01:43 +08:00
|
|
|
for (const p of [].concat(message.to, message.cc, message.from)) {
|
|
|
|
if (!threadEmails.includes(p.email)) {
|
|
|
|
threadParticipants.push(p);
|
|
|
|
threadEmails.push(p.email);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thread.participants = threadParticipants;
|
|
|
|
|
|
|
|
// update starred and unread
|
|
|
|
if (thread.starredCount == null) { thread.starredCount = 0; }
|
|
|
|
thread.starredCount += message.starred ? 1 : 0;
|
|
|
|
if (thread.unreadCount == null) { thread.unreadCount = 0; }
|
|
|
|
thread.unreadCount += message.unread ? 1 : 0;
|
|
|
|
|
|
|
|
// update dates
|
|
|
|
if (!thread.lastMessageDate || (message.date > thread.lastMessageDate)) {
|
|
|
|
thread.lastMessageDate = message.date;
|
2016-06-30 01:36:32 +08:00
|
|
|
thread.snippet = message.snippet;
|
|
|
|
thread.subject = this.cleanSubject(message.subject);
|
2016-06-29 09:01:43 +08:00
|
|
|
}
|
|
|
|
if (!thread.firstMessageDate || (message.date < thread.firstMessageDate)) {
|
|
|
|
thread.firstMessageDate = message.date;
|
|
|
|
}
|
2016-07-01 00:29:21 +08:00
|
|
|
|
|
|
|
let isSent = false;
|
|
|
|
if (sentFolder) {
|
|
|
|
isSent = message.folderId === sentFolder.id
|
|
|
|
} else if (sentLabel) {
|
|
|
|
isSent = !!message.labels.find(l => l.id === sentLabel.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSent && (message.date > thread.lastMessageSentDate)) {
|
2016-06-29 09:01:43 +08:00
|
|
|
thread.lastMessageSentDate = message.date;
|
|
|
|
}
|
2016-07-01 00:29:21 +08:00
|
|
|
if (!isSent && (message.date > thread.lastMessageReceivedDate)) {
|
2016-06-29 09:01:43 +08:00
|
|
|
thread.lastMessageReceivedDate = message.date;
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:29:21 +08:00
|
|
|
// update folders and labels
|
|
|
|
if (!thread.folders.find(f => f.id === message.folderId)) {
|
|
|
|
thread.addFolder(message.folder)
|
|
|
|
}
|
|
|
|
for (const label of message.labels) {
|
|
|
|
if (!thread.labels.find(l => l.id === label)) {
|
|
|
|
thread.addLabel(label)
|
2016-06-29 09:01:43 +08:00
|
|
|
}
|
2016-07-01 00:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return thread.save().then((saved) => {
|
|
|
|
message.threadId = saved.id;
|
|
|
|
return message;
|
2016-06-29 09:01:43 +08:00
|
|
|
});
|
|
|
|
});
|
2016-06-28 01:15:05 +08:00
|
|
|
}
|
2016-06-25 07:14:04 +08:00
|
|
|
}
|
|
|
|
|
2016-06-29 09:01:43 +08:00
|
|
|
const processor = new ThreadingProcessor();
|
2016-06-23 08:34:21 +08:00
|
|
|
|
2016-06-21 05:57:54 +08:00
|
|
|
module.exports = {
|
2016-06-29 09:01:43 +08:00
|
|
|
processMessage: processor.processMessage.bind(processor),
|
2016-06-24 01:26:41 +08:00
|
|
|
order: 1,
|
2016-06-29 09:01:43 +08:00
|
|
|
};
|