mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-09-09 22:24:27 +08:00
[fix] Fix transaction creation for contacts
Summary: Fixes T7283. We weren't creating deltas for contacts because we were inserting contacts using `UPSERT`, which requires us to add another sequelize hook. Unfortunately, support for this hook is only available on sequelize 4.x. I didn't want to upgrade our sequelize version right now and cause of bunch of mysterious failures, so I've simply changed the `UPSERT` to be either an `INSERT`or `UPDATE`. We didn't really need it anyway. Test Plan: Checked that N1 was receiving contact deltas. Reviewers: bengotow Reviewed By: bengotow Maniphest Tasks: T7283 Differential Revision: https://phab.nylas.com/D3481
This commit is contained in:
parent
269d61171a
commit
04b1763965
4 changed files with 25 additions and 16 deletions
4
.arcconfig
Normal file
4
.arcconfig
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"project_id" : "K2",
|
||||||
|
"conduit_uri" : "https://phab.nylas.com/"
|
||||||
|
}
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,6 +1,3 @@
|
||||||
.arcconfig
|
|
||||||
.arclint
|
|
||||||
arclib
|
|
||||||
*.swp
|
*.swp
|
||||||
*~
|
*~
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
|
@ -46,5 +46,9 @@ module.exports = (db, sequelize, {only, onCreatedTransaction} = {}) => {
|
||||||
|
|
||||||
sequelize.addHook("afterCreate", transactionLogger("create"))
|
sequelize.addHook("afterCreate", transactionLogger("create"))
|
||||||
sequelize.addHook("afterUpdate", transactionLogger("modify"))
|
sequelize.addHook("afterUpdate", transactionLogger("modify"))
|
||||||
|
|
||||||
|
// NOTE: Hooking UPSERT requires Sequelize 4.x. We're
|
||||||
|
// on version 3 right now, but leaving this here for when we upgrade.
|
||||||
|
sequelize.addHook("afterUpsert", transactionLogger("modify"))
|
||||||
sequelize.addHook("afterDelete", transactionLogger("delete"))
|
sequelize.addHook("afterDelete", transactionLogger("delete"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,26 +13,30 @@ function isContactVerified(contact) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractContacts({db, message}) {
|
async function extractContacts({db, message}) {
|
||||||
const {Contact} = db;
|
|
||||||
|
|
||||||
let allContacts = [];
|
let allContacts = [];
|
||||||
['to', 'from', 'bcc', 'cc'].forEach((field) => {
|
['to', 'from', 'bcc', 'cc'].forEach((field) => {
|
||||||
allContacts = allContacts.concat(message[field])
|
allContacts = allContacts.concat(message[field])
|
||||||
})
|
})
|
||||||
|
|
||||||
const verifiedContacts = allContacts.filter(c => isContactVerified(c));
|
const verifiedContacts = allContacts.filter(c => isContactVerified(c));
|
||||||
return db.sequelize.transaction((transaction) => {
|
return db.sequelize.transaction(async (transaction) => {
|
||||||
return Promise.all(verifiedContacts.map((contact) =>
|
for (const c of verifiedContacts) {
|
||||||
Contact.upsert({
|
const id = cryptography.createHash('sha256').update(c.email, 'utf8').digest('hex');
|
||||||
name: contact.name,
|
let contact = await db.Contact.findById(id);
|
||||||
email: contact.email,
|
const cdata = {
|
||||||
|
name: c.name,
|
||||||
|
email: c.email,
|
||||||
accountId: message.accountId,
|
accountId: message.accountId,
|
||||||
id: cryptography.createHash('sha256').update(contact.email, 'utf8').digest('hex'),
|
id: id,
|
||||||
}, {
|
};
|
||||||
transaction,
|
|
||||||
})
|
if (!contact) {
|
||||||
))
|
contact = await db.Contact.create(cdata)
|
||||||
|
} else {
|
||||||
|
await contact.update(cdata);
|
||||||
|
}
|
||||||
|
}
|
||||||
}).thenReturn(message)
|
}).thenReturn(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue