mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-09-06 04:35:30 +08:00
[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:
parent
4deef8b90c
commit
0205ac3f61
1 changed files with 12 additions and 30 deletions
|
@ -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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue