[client-sync] Simplify Contact ranking query

Summary:
We were using a couple of left outer joins to avoid checking whether we
should be joining on Labels or Folders. We can greatly simplify the query
by just checking which we should use and issuing the correct inner join.

Test Plan: Run locally, verify contact ranking still works.

Reviewers: spang, juan, evan

Reviewed By: juan, evan

Differential Revision: https://phab.nylas.com/D4310
This commit is contained in:
Mark Hahnenberg 2017-03-31 11:30:02 -07:00
parent 4deef8b90c
commit 0205ac3f61

View file

@ -99,48 +99,30 @@ module.exports = (server) => {
},
handler: async (request, reply) => {
const db = await request.getAccountDatabase()
const account = request.auth.credentials;
const {Message, Label, Folder} = db;
const result = {};
let lastID = 0;
const useLabels = account.provider === 'gmail';
while (true) {
const include = [{
model: (useLabels ? Label : Folder),
attributes: ['id', 'role'],
where: {role: 'sent'}},
];
const messages = await Message.findAll({
attributes: ['rowid', 'id', 'to', 'cc', 'bcc', 'date'],
// We mark both Label and Folder as not required because we don't know
// whether the account uses folders or labels.
include: [
{
model: Label,
attributes: ['id', 'role'],
where: {role: 'sent'},
required: false, // This triggers a left outer join.
},
{
model: Folder,
attributes: ['id', 'role'],
where: {role: 'sent'},
required: false, // This triggers a left outer join.
},
],
include,
where: {
'isDraft': false, // Don't include unsent things.
// Because we did a left outer join on the Folder and Label tables,
// we can get back records that have null for both the Folder and the
// Label (i.e. all things that aren't in the Sent folder). So we need
// to require that either the label ID or the folder ID is not null
// (i.e. that the message is in the Sent folder)
'$or': [
{'$labels.id$': {$ne: null}},
{'$folder.id$': {$ne: null}},
],
'$message.rowid$': {$gt: lastID},
},
// We don't use the `limit` option here because it causes sequelize
// to generate a subquery that doesn't have access to the joined
// labels/folders, causing a SQLite error to be thrown.
order: 'message.rowid ASC LIMIT 500',
order: [['rowid', 'ASC']],
limit: 100,
});
if (messages.length === 0) {