Use more reliable and to update flag array in db

This commit is contained in:
Andris Reinman 2017-03-11 23:43:21 +02:00
parent 4194df97f7
commit db1b4a7b31
7 changed files with 33 additions and 19 deletions

View file

@ -280,7 +280,7 @@ server.onAppend = function (mailbox, flags, date, raw, session, callback) {
};
// STORE / UID STORE, updates flags for selected UIDs
server.onUpdate = function (mailbox, update, session, callback) {
server.onStore = function (mailbox, update, session, callback) {
this.logger.debug('[%s] Updating messages in "%s"', session.id, mailbox);
if (!folders.has(mailbox)) {

View file

@ -18,6 +18,7 @@ module.exports = {
let mailbox = command.attributes[0] && command.attributes[0].value || '';
if (!this.acceptUTF8Enabled) {
// decode before normalizing to uncover stuff like ending / etc.
mailbox = utf7.decode(mailbox);
}

View file

@ -24,7 +24,7 @@ module.exports = {
handler(command, callback) {
// Check if STORE method is set
if (typeof this._server.onUpdate !== 'function') {
if (typeof this._server.onStore !== 'function') {
return callback(null, {
response: 'NO',
message: 'STORE not implemented'
@ -114,7 +114,7 @@ module.exports = {
let messages = imapTools.getMessageRange(this.selected.uidList, range, false);
this._server.onUpdate(this.selected.mailbox, {
this._server.onStore(this.selected.mailbox, {
value: flags,
action,
type,

View file

@ -23,7 +23,7 @@ module.exports = {
handler(command, callback) {
// Check if STORE method is set
if (typeof this._server.onUpdate !== 'function') {
if (typeof this._server.onStore !== 'function') {
return callback(null, {
response: 'NO',
message: 'STORE not implemented'
@ -105,7 +105,7 @@ module.exports = {
let messages = imapTools.getMessageRange(this.selected.uidList, range, true);
this._server.onUpdate(this.selected.mailbox, {
this._server.onStore(this.selected.mailbox, {
isUid: true,
value: flags,
action,

View file

@ -307,7 +307,7 @@ module.exports = function (options) {
};
// STORE / UID STORE, updates flags for selected UIDs
server.onUpdate = function (mailbox, update, session, callback) {
server.onStore = function (mailbox, update, session, callback) {
this.logger.debug('[%s] Updating messages in "%s"', session.id, mailbox);
if (!folders.has(mailbox)) {

37
imap.js
View file

@ -385,7 +385,9 @@ server.onStatus = function (path, session, callback) {
}
database.collection('messages').find({
mailbox: mailbox._id,
unseen: true
flags: {
$ne: '\\Seen'
}
}).count((err, unseen) => {
if (err) {
return callback(err);
@ -425,7 +427,7 @@ server.onAppend = function (path, flags, date, raw, session, callback) {
};
// STORE / UID STORE, updates flags for selected UIDs
server.onUpdate = function (path, update, session, callback) {
server.onStore = function (path, update, session, callback) {
this.logger.debug('[%s] Updating messages in "%s"', session.id, path);
let username = session.user.username;
@ -466,6 +468,7 @@ server.onUpdate = function (path, update, session, callback) {
});
}
let flagsupdate = {};
let updated = false;
switch (update.action) {
case 'set':
@ -473,8 +476,11 @@ server.onUpdate = function (path, update, session, callback) {
if (message.flags.length !== update.value.length || update.value.filter(flag => message.flags.indexOf(flag) < 0).length) {
updated = true;
}
// set flags
message.flags = [].concat(update.value);
// set flags
flagsupdate.$set = {
flags: message.flags
};
break;
case 'add':
@ -485,6 +491,13 @@ server.onUpdate = function (path, update, session, callback) {
}
return false;
}));
// add flags
flagsupdate.$addToSet = {
flags: {
$each: update.value
}
};
break;
case 'remove':
@ -495,6 +508,13 @@ server.onUpdate = function (path, update, session, callback) {
updated = true;
return false;
});
// remove flags
flagsupdate.$pull = {
flags: {
$in: update.value
}
};
break;
}
@ -508,12 +528,7 @@ server.onUpdate = function (path, update, session, callback) {
if (updated) {
database.collection('messages').findOneAndUpdate({
_id: message._id
}, {
$set: {
flags: message.flags,
unseen: !message.flags.includes('\\Seen')
}
}, {}, err => {
}, flagsupdate, {}, err => {
if (err) {
return cursor.close(() => callback(err));
}
@ -838,8 +853,7 @@ server.onFetch = function (path, options, session, callback) {
_id: message._id
}, {
$set: {
flags: message.flags,
unseen: false
flags: message.flags
}
}, {}, err => {
if (err) {
@ -1176,7 +1190,6 @@ server.addToMailbox = (username, path, meta, date, flags, raw, callback) => {
internaldate,
headerdate,
flags,
unseen: !flags.includes('\\Seen'),
size,
meta,
modseq: 0,

View file

@ -1,6 +1,6 @@
{
"name": "wildduck",
"version": "1.0.1",
"version": "1.0.2",
"description": "IMAP server built with Node.js and MongoDB",
"main": "index.js",
"scripts": {