diff --git a/imap.js b/imap.js index 92047e70..c77a5e65 100644 --- a/imap.js +++ b/imap.js @@ -1202,24 +1202,18 @@ server.onSearch = function (path, options, session, callback) { // prepare query let query = { - mailbox: mailbox._id, - $and: [] + mailbox: mailbox._id }; - let hasAll = false; - let nothing = false; let walkQuery = (parent, ne, node) => { - if (hasAll || nothing) { - return; - } node.forEach(term => { switch (term.key) { case 'all': - if (!ne) { - hasAll = true; - query = { - mailbox: mailbox._id - }; + if (ne) { + parent.push({ + // should not match anything + _id: -1 + }); } break; @@ -1230,14 +1224,17 @@ server.onSearch = function (path, options, session, callback) { case 'or': { let $or = []; - parent.push({ - $or - }); [].concat(term.value || []).forEach(entry => { walkQuery($or, false, [].concat(entry || [])); }); + if ($or.length) { + parent.push({ + $or + }); + } + break; } @@ -1252,7 +1249,10 @@ server.onSearch = function (path, options, session, callback) { }); } else { // can not search by text - nothing = true; + parent.push({ + // should not match anything + _id: -1 + }); } break; @@ -1273,11 +1273,19 @@ server.onSearch = function (path, options, session, callback) { highestModseq: 0 }); } - parent.push({ - uid: { - [!ne ? '$in' : '$nin']: term.value - } - }); + if (term.value.length !== session.selected.uidList.length) { + // not 1:* + parent.push({ + uid: { + [!ne ? '$in' : '$nin']: term.value + } + }); + } else if (ne) { + parent.push({ + // should not match anything + _id: -1 + }); + } } else { parent.push({ uid: { @@ -1457,19 +1465,14 @@ server.onSearch = function (path, options, session, callback) { }); }; - walkQuery(query.$and, false, options.query); - //} + let $and = []; + walkQuery($and, false, options.query); + if ($and.length) { + query.$and = $and; + } this.logger.info('SEARCH %s', JSON.stringify(query)); - if (nothing) { - // reject immediatelly - return callback(null, { - uidList: [], - highestModseq: 0 - }); - } - let cursor = db.database.collection('messages'). find(query). project({ diff --git a/lib/imap-notifier.js b/lib/imap-notifier.js index b664892e..0aa30af0 100644 --- a/lib/imap-notifier.js +++ b/lib/imap-notifier.js @@ -204,6 +204,7 @@ class ImapNotifier extends EventEmitter { entries.forEach(entry => { entry.modseq = entry.modseq || modseq; entry.created = entry.created || created; + entry.mailbox = entry.mailbox || mailbox._id; }); if (updated.length) {