mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-09-25 01:36:06 +08:00
47e0683cac
Summary: This diff adds persistent unique ids for messages and contacts. For messages, we just take a hash of the headers. For contacts, we hash the contact's email address. This diff bundles a couple of tiny fixes too, like always trying to restart an account's sync, even after an exception. Note that since there's no reliable way to have persistent ids for threads, we'll have to change our code to use message ids instead. Alas, that's a story for another (massive) diff. Test Plan: Tested manually. Reviewers: bengotow Reviewed By: bengotow Differential Revision: https://phab.nylas.com/D3468
40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
const cryptography = require('crypto');
|
|
|
|
function isContactVerified(contact) {
|
|
// some suggestions: http://stackoverflow.com/questions/6317714/apache-camel-mail-to-identify-auto-generated-messages
|
|
const regex = new RegExp(/^(noreply|no-reply|donotreply|mailer|support|webmaster|news(letter)?@)/ig)
|
|
|
|
if (!contact.email) {
|
|
return false;
|
|
}
|
|
if (regex.test(contact.email) || contact.email.length > 60) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
function extractContacts({db, message}) {
|
|
const {Contact} = db;
|
|
|
|
let allContacts = [];
|
|
['to', 'from', 'bcc', 'cc'].forEach((field) => {
|
|
allContacts = allContacts.concat(message[field])
|
|
})
|
|
|
|
const verifiedContacts = allContacts.filter(c => isContactVerified(c));
|
|
return db.sequelize.transaction((transaction) => {
|
|
return Promise.all(verifiedContacts.map((contact) =>
|
|
Contact.upsert({
|
|
name: contact.name,
|
|
email: contact.email,
|
|
accountId: message.accountId,
|
|
id: cryptography.createHash('sha256').update(contact.email, 'utf8').digest('hex'),
|
|
}, {
|
|
transaction,
|
|
})
|
|
))
|
|
}).thenReturn(message)
|
|
}
|
|
|
|
module.exports = extractContacts
|