[WO-661] Introduce API to move message

This commit is contained in:
Felix Hammerl 2014-11-04 20:31:09 +01:00
parent a65435a771
commit 541b35818b
3 changed files with 1709 additions and 1579 deletions

View File

@ -630,6 +630,60 @@ EmailDAO.prototype.setFlags = function(options, callback) {
}
};
/**
* Moves a message to another folder
*
* @param {Object} options.folder The origin folder
* @param {Object} options.destination The destination folder
* @param {Object} options.message The message that should be moved
* @param {Function} callback(error) Invoked when the message was moved, or an error occurred
*/
EmailDAO.prototype.moveMessage = function(options, callback) {
var self = this,
folder = options.folder,
destination = options.destination,
message = options.message;
self.busy();
if (!self._account.online) {
// no action if we're not online
done({
errMsg: 'Client is currently offline!',
code: 42
});
return;
}
folder.messages.splice(folder.messages.indexOf(message), 1);
// delete from IMAP
self._imapMoveMessage({
folder: folder,
destination: destination,
uid: message.uid
}, function(err) {
if (err) {
// re-add the message to the folder in case of an error, only makes sense if IMAP errors
folder.messages.unshift(message);
done(err);
return;
}
// delete from local indexed db, will be synced when new folder is opened
self._localDeleteMessage({
folder: folder,
uid: message.uid
}, done);
});
function done(err) {
self.done(); // stop the spinner
updateUnreadCount(folder); // update the unread count, if necessary
callback(err);
}
};
/**
* Streams message content
* @param {Object} options.message The message for which to retrieve the body
@ -1089,7 +1143,6 @@ EmailDAO.prototype.encrypt = function(options, callback) {
self.done();
callback(err);
});
};
@ -1552,14 +1605,30 @@ EmailDAO.prototype._imapDeleteMessage = function(options, callback) {
return;
}
// move the message to the trash folder
this._imapClient.moveMessage({
path: options.folder.path,
destination: trash.path,
this._imapMoveMessage({
folder: options.folder,
destination: trash,
uid: options.uid
}, callback);
};
/**
* Move stuff around on the server
*
* @param {String} options.folder The folder
* @param {Number} options.destination The destination folder
* @param {String} options.uid the message's uid
* @param {Function} callback (error) The callback when the message is moved
*/
EmailDAO.prototype._imapMoveMessage = function(options, callback) {
this._imapClient.moveMessage({
path: options.folder.path,
destination: options.destination.path,
uid: options.uid
}, callback);
};
/**
* Get list messsage headers without the body
*
@ -1724,7 +1793,6 @@ EmailDAO.prototype._uploadToSent = function(options, callback) {
//
//
// Helper Functions

File diff suppressed because one or more lines are too long

View File

@ -132,7 +132,7 @@ describe('Email DAO unit tests', function() {
mailreader.parse.restore();
});
describe('public API', function() {
describe('#init', function() {
var initFoldersStub;
@ -839,6 +839,83 @@ describe('Email DAO unit tests', function() {
});
});
describe('#moveMessage', function() {
var localDeleteStub, imapMoveStub, message;
beforeEach(function() {
localDeleteStub = sinon.stub(dao, '_localDeleteMessage');
imapMoveStub = sinon.stub(dao, '_imapMoveMessage');
message = {
uid: 123
};
inboxFolder.messages.push(message);
});
it('should move a message to a destination folder', function(done) {
imapMoveStub.withArgs({
folder: inboxFolder,
destination: sentFolder,
uid: message.uid
}).yieldsAsync();
localDeleteStub.withArgs({
folder: inboxFolder,
uid: message.uid
}).yieldsAsync();
dao.moveMessage({
folder: inboxFolder,
destination: sentFolder,
message: message
}, function(err) {
expect(err).to.not.exist;
expect(imapMoveStub.calledOnce).to.be.true;
expect(localDeleteStub.calledOnce).to.be.true;
expect(inboxFolder.messages).to.not.contain(message);
done();
});
});
it('should not a message if IMAP errors', function(done) {
imapMoveStub.withArgs({
folder: inboxFolder,
destination: sentFolder,
uid: message.uid
}).yieldsAsync(new Error());
dao.moveMessage({
folder: inboxFolder,
destination: sentFolder,
message: message
}, function(err) {
expect(err).to.exist;
expect(imapMoveStub.calledOnce).to.be.true;
expect(localDeleteStub.called).to.be.false;
expect(inboxFolder.messages).to.contain(message);
done();
});
});
it('should fail at delete from imap in offline', function(done) {
account.online = false;
dao.moveMessage({
folder: inboxFolder,
destination: sentFolder,
message: message
}, function(err) {
expect(err).to.exist;
expect(imapMoveStub.called).to.be.false;
expect(localDeleteStub.called).to.be.false;
expect(inboxFolder.messages).to.contain(message);
done();
});
});
});
describe('#getBody', function() {
var localListStub, localStoreStub, imapGetStub, uid;
@ -1760,7 +1837,6 @@ describe('Email DAO unit tests', function() {
});
});
});
});
describe('event handlers', function() {
@ -2147,6 +2223,22 @@ describe('Email DAO unit tests', function() {
});
});
describe('#_imapMoveMessage', function() {
it('should move a message to a destination folder', function(done) {
imapClientStub.moveMessage.withArgs({
path: inboxFolder.path,
destination: sentFolder.path,
uid: 123
}).yieldsAsync();
dao._imapMoveMessage({
folder: inboxFolder,
destination: sentFolder,
uid: 123
}, done);
});
});
describe('#_imapDeleteMessage', function() {
var uid = 1337;