mirror of
https://github.com/moparisthebest/mail
synced 2025-01-30 22:50:17 -05:00
review sync
This commit is contained in:
parent
0c1003c48f
commit
548ca655c7
@ -12,9 +12,9 @@ define(function(require) {
|
|||||||
* The local outbox takes care of the emails before they are being sent.
|
* The local outbox takes care of the emails before they are being sent.
|
||||||
* It also checks periodically if there are any mails in the local device storage to be sent.
|
* It also checks periodically if there are any mails in the local device storage to be sent.
|
||||||
*/
|
*/
|
||||||
var OutboxBO = function(email, keychain, devicestorage, invitation) {
|
var OutboxBO = function(emailDao, keychain, devicestorage, invitationDao) {
|
||||||
/** @private */
|
/** @private */
|
||||||
this._email = email;
|
this._emailDao = emailDao;
|
||||||
|
|
||||||
/** @private */
|
/** @private */
|
||||||
this._keychain = keychain;
|
this._keychain = keychain;
|
||||||
@ -24,7 +24,7 @@ define(function(require) {
|
|||||||
|
|
||||||
|
|
||||||
/** @private */
|
/** @private */
|
||||||
this._invitation = invitation;
|
this._invitationDao = invitationDao;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Semaphore-esque flag to avoid 'concurrent' calls to _processOutbox when the timeout fires, but a call is still in process.
|
* Semaphore-esque flag to avoid 'concurrent' calls to _processOutbox when the timeout fires, but a call is still in process.
|
||||||
@ -79,7 +79,7 @@ define(function(require) {
|
|||||||
self._outboxBusy = true;
|
self._outboxBusy = true;
|
||||||
|
|
||||||
// get last item from outbox
|
// get last item from outbox
|
||||||
self._email.list(function(err, pending) {
|
self._emailDao.list(function(err, pending) {
|
||||||
if (err) {
|
if (err) {
|
||||||
self._outboxBusy = false;
|
self._outboxBusy = false;
|
||||||
callback(err);
|
callback(err);
|
||||||
@ -147,7 +147,7 @@ define(function(require) {
|
|||||||
|
|
||||||
// invite the unregistered receivers, if necessary
|
// invite the unregistered receivers, if necessary
|
||||||
function invite(addresses) {
|
function invite(addresses) {
|
||||||
var sender = self._email._account.emailAddress;
|
var sender = self._emailDao._account.emailAddress;
|
||||||
|
|
||||||
var invitationFinished = _.after(addresses.length, function() {
|
var invitationFinished = _.after(addresses.length, function() {
|
||||||
// after all of the invitations are checked and sent (if necessary),
|
// after all of the invitations are checked and sent (if necessary),
|
||||||
@ -158,7 +158,7 @@ define(function(require) {
|
|||||||
addresses.forEach(function(recipient) {
|
addresses.forEach(function(recipient) {
|
||||||
var recipientAddress = recipient.address;
|
var recipientAddress = recipient.address;
|
||||||
|
|
||||||
self._invitation.check({
|
self._invitationDao.check({
|
||||||
recipient: recipientAddress,
|
recipient: recipientAddress,
|
||||||
sender: sender
|
sender: sender
|
||||||
}, function(err, status) {
|
}, function(err, status) {
|
||||||
@ -175,7 +175,7 @@ define(function(require) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// the recipient is not yet invited, so let's do that
|
// the recipient is not yet invited, so let's do that
|
||||||
self._invitation.invite({
|
self._invitationDao.invite({
|
||||||
recipient: recipientAddress,
|
recipient: recipientAddress,
|
||||||
sender: sender
|
sender: sender
|
||||||
}, function(err, status) {
|
}, function(err, status) {
|
||||||
@ -209,7 +209,7 @@ define(function(require) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// send invitation mail
|
// send invitation mail
|
||||||
self._email.send(invitationMail, function(err) {
|
self._emailDao.send(invitationMail, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
self._outboxBusy = false;
|
self._outboxBusy = false;
|
||||||
callback(err);
|
callback(err);
|
||||||
@ -222,7 +222,7 @@ define(function(require) {
|
|||||||
|
|
||||||
function sendEncrypted(email) {
|
function sendEncrypted(email) {
|
||||||
removeFromPendingMails(email);
|
removeFromPendingMails(email);
|
||||||
self._email.encryptedSend(email, function(err) {
|
self._emailDao.encryptedSend(email, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
self._outboxBusy = false;
|
self._outboxBusy = false;
|
||||||
callback(err);
|
callback(err);
|
||||||
|
@ -106,6 +106,10 @@ define(function(require) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleGenerated(generatedKeypair);
|
||||||
|
});
|
||||||
|
|
||||||
|
function handleGenerated(generatedKeypair) {
|
||||||
// import the new key pair into crypto module
|
// import the new key pair into crypto module
|
||||||
self._crypto.importKeys({
|
self._crypto.importKeys({
|
||||||
passphrase: options.passphrase,
|
passphrase: options.passphrase,
|
||||||
@ -132,7 +136,7 @@ define(function(require) {
|
|||||||
};
|
};
|
||||||
self._keychain.putUserKeyPair(newKeypair, callback);
|
self._keychain.putUserKeyPair(newKeypair, callback);
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
EmailDAO.prototype.sync = function(options, callback) {
|
EmailDAO.prototype.sync = function(options, callback) {
|
||||||
@ -166,6 +170,13 @@ define(function(require) {
|
|||||||
// initial filling from local storage is an exception from the normal sync.
|
// initial filling from local storage is an exception from the normal sync.
|
||||||
// after reading from local storage, do imap sync
|
// after reading from local storage, do imap sync
|
||||||
if (!isFolderInitialized) {
|
if (!isFolderInitialized) {
|
||||||
|
initFolderMessages();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doLocalDelta();
|
||||||
|
|
||||||
|
function initFolderMessages() {
|
||||||
folder.messages = [];
|
folder.messages = [];
|
||||||
self._localListMessages({
|
self._localListMessages({
|
||||||
folder: folder.path
|
folder: folder.path
|
||||||
@ -193,12 +204,8 @@ define(function(require) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
doLocalDelta();
|
|
||||||
|
|
||||||
function doLocalDelta() {
|
function doLocalDelta() {
|
||||||
self._localListMessages({
|
self._localListMessages({
|
||||||
folder: folder.path
|
folder: folder.path
|
||||||
@ -232,8 +239,18 @@ define(function(require) {
|
|||||||
uid: message.uid
|
uid: message.uid
|
||||||
};
|
};
|
||||||
|
|
||||||
self._imapDeleteMessage(deleteMe, function() {
|
self._imapDeleteMessage(deleteMe, function(err) {
|
||||||
self._localDeleteMessage(deleteMe, function() {
|
if (err) {
|
||||||
|
callback(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self._localDeleteMessage(deleteMe, function(err) {
|
||||||
|
if (err) {
|
||||||
|
callback(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
after();
|
after();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -336,7 +353,7 @@ define(function(require) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// encrypt and add to folder in memory
|
// decrypt and add to folder in memory
|
||||||
handleMessage(message, function(err, cleartextMessage) {
|
handleMessage(message, function(err, cleartextMessage) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
@ -364,8 +381,7 @@ define(function(require) {
|
|||||||
for (i = a.length - 1; i >= 0; i--) {
|
for (i = a.length - 1; i >= 0; i--) {
|
||||||
msg = a[i];
|
msg = a[i];
|
||||||
exists = _.findWhere(b, {
|
exists = _.findWhere(b, {
|
||||||
uid: msg.uid,
|
uid: msg.uid
|
||||||
subject: msg.subject
|
|
||||||
});
|
});
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
delta.push(msg);
|
delta.push(msg);
|
||||||
@ -450,7 +466,7 @@ define(function(require) {
|
|||||||
}, callback);
|
}, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
EmailDAO.prototype.encryptedSend = function(options, callback) {
|
EmailDAO.prototype.sendEncrypted = function(options, callback) {
|
||||||
var self = this,
|
var self = this,
|
||||||
email = options.email;
|
email = options.email;
|
||||||
|
|
||||||
@ -505,14 +521,12 @@ define(function(require) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.send({
|
self._smtpClient.send(email, callback);
|
||||||
email: email
|
|
||||||
}, callback);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
EmailDAO.prototype.send = function(options, callback) {
|
EmailDAO.prototype.sendPlaintext = function(options, callback) {
|
||||||
this._smtpClient.send(options.email, callback);
|
this._smtpClient.send(options.email, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -577,6 +591,12 @@ define(function(require) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
EmailDAO.prototype._localDeleteMessage = function(options, callback) {
|
EmailDAO.prototype._localDeleteMessage = function(options, callback) {
|
||||||
|
if (!options.folder || !options.uid) {
|
||||||
|
callback({
|
||||||
|
errMsg: 'Invalid options!'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
var dbType = 'email_' + options.folder + '_' + options.uid;
|
var dbType = 'email_' + options.folder + '_' + options.uid;
|
||||||
this._devicestorage.removeList(dbType, callback);
|
this._devicestorage.removeList(dbType, callback);
|
||||||
};
|
};
|
||||||
|
@ -523,6 +523,17 @@ define(function(require) {
|
|||||||
uid: uid
|
uid: uid
|
||||||
}, done);
|
}, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should fail when uid is missing', function(done) {
|
||||||
|
var folder = 'FOLDAAAA';
|
||||||
|
|
||||||
|
dao._localDeleteMessage({
|
||||||
|
folder: folder
|
||||||
|
}, function(err) {
|
||||||
|
expect(err).to.exist;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('sync', function() {
|
describe('sync', function() {
|
||||||
@ -727,11 +738,11 @@ define(function(require) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('send', function() {
|
describe('sendPlaintext', function() {
|
||||||
it('should work', function(done) {
|
it('should work', function(done) {
|
||||||
smtpClientStub.send.withArgs(dummyEncryptedMail).yields();
|
smtpClientStub.send.withArgs(dummyEncryptedMail).yields();
|
||||||
|
|
||||||
dao.send({
|
dao.sendPlaintext({
|
||||||
email: dummyEncryptedMail
|
email: dummyEncryptedMail
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
expect(err).to.not.exist;
|
expect(err).to.not.exist;
|
||||||
@ -741,7 +752,7 @@ define(function(require) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('encryptedSend', function() {
|
describe('sendEncrypted', function() {
|
||||||
it('should work', function(done) {
|
it('should work', function(done) {
|
||||||
var encryptStub = sinon.stub(dao, '_encrypt').yields(null, {});
|
var encryptStub = sinon.stub(dao, '_encrypt').yields(null, {});
|
||||||
keychainStub.getReceiverPublicKey.withArgs(dummyDecryptedMail.to[0].address).yields(null, {
|
keychainStub.getReceiverPublicKey.withArgs(dummyDecryptedMail.to[0].address).yields(null, {
|
||||||
@ -751,7 +762,7 @@ define(function(require) {
|
|||||||
});
|
});
|
||||||
smtpClientStub.send.yields();
|
smtpClientStub.send.yields();
|
||||||
|
|
||||||
dao.encryptedSend({
|
dao.sendEncrypted({
|
||||||
email: dummyDecryptedMail
|
email: dummyDecryptedMail
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
expect(err).to.not.exist;
|
expect(err).to.not.exist;
|
||||||
@ -771,7 +782,7 @@ define(function(require) {
|
|||||||
publicKey: publicKey
|
publicKey: publicKey
|
||||||
});
|
});
|
||||||
|
|
||||||
dao.encryptedSend({
|
dao.sendEncrypted({
|
||||||
email: dummyDecryptedMail
|
email: dummyDecryptedMail
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
expect(err).to.exist;
|
expect(err).to.exist;
|
||||||
@ -787,7 +798,7 @@ define(function(require) {
|
|||||||
var encryptStub = sinon.stub(dao, '_encrypt');
|
var encryptStub = sinon.stub(dao, '_encrypt');
|
||||||
keychainStub.getReceiverPublicKey.withArgs(dummyDecryptedMail.to[0].address).yields({});
|
keychainStub.getReceiverPublicKey.withArgs(dummyDecryptedMail.to[0].address).yields({});
|
||||||
|
|
||||||
dao.encryptedSend({
|
dao.sendEncrypted({
|
||||||
email: dummyDecryptedMail
|
email: dummyDecryptedMail
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
expect(err).to.exist;
|
expect(err).to.exist;
|
||||||
@ -803,7 +814,7 @@ define(function(require) {
|
|||||||
var encryptStub = sinon.stub(dao, '_encrypt');
|
var encryptStub = sinon.stub(dao, '_encrypt');
|
||||||
dummyDecryptedMail.to[0].address = 'asd@asd';
|
dummyDecryptedMail.to[0].address = 'asd@asd';
|
||||||
|
|
||||||
dao.encryptedSend({
|
dao.sendEncrypted({
|
||||||
email: dummyDecryptedMail
|
email: dummyDecryptedMail
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
expect(err).to.exist;
|
expect(err).to.exist;
|
||||||
@ -819,7 +830,7 @@ define(function(require) {
|
|||||||
var encryptStub = sinon.stub(dao, '_encrypt');
|
var encryptStub = sinon.stub(dao, '_encrypt');
|
||||||
dummyDecryptedMail.from[0].address = 'asd@asd';
|
dummyDecryptedMail.from[0].address = 'asd@asd';
|
||||||
|
|
||||||
dao.encryptedSend({
|
dao.sendEncrypted({
|
||||||
email: dummyDecryptedMail
|
email: dummyDecryptedMail
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
expect(err).to.exist;
|
expect(err).to.exist;
|
||||||
@ -835,7 +846,7 @@ define(function(require) {
|
|||||||
var encryptStub = sinon.stub(dao, '_encrypt');
|
var encryptStub = sinon.stub(dao, '_encrypt');
|
||||||
delete dummyDecryptedMail.to;
|
delete dummyDecryptedMail.to;
|
||||||
|
|
||||||
dao.encryptedSend({
|
dao.sendEncrypted({
|
||||||
email: dummyDecryptedMail
|
email: dummyDecryptedMail
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
expect(err).to.exist;
|
expect(err).to.exist;
|
||||||
@ -851,7 +862,7 @@ define(function(require) {
|
|||||||
var encryptStub = sinon.stub(dao, '_encrypt');
|
var encryptStub = sinon.stub(dao, '_encrypt');
|
||||||
delete dummyDecryptedMail.from;
|
delete dummyDecryptedMail.from;
|
||||||
|
|
||||||
dao.encryptedSend({
|
dao.sendEncrypted({
|
||||||
email: dummyDecryptedMail
|
email: dummyDecryptedMail
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
expect(err).to.exist;
|
expect(err).to.exist;
|
||||||
|
@ -29,10 +29,10 @@ define(function(require) {
|
|||||||
describe('init', function() {
|
describe('init', function() {
|
||||||
it('should work', function() {
|
it('should work', function() {
|
||||||
expect(outbox).to.exist;
|
expect(outbox).to.exist;
|
||||||
expect(outbox._email).to.equal(emailDaoStub);
|
expect(outbox._emailDao).to.equal(emailDaoStub);
|
||||||
expect(outbox._keychain).to.equal(keychainStub);
|
expect(outbox._keychain).to.equal(keychainStub);
|
||||||
expect(outbox._devicestorage).to.equal(devicestorageStub);
|
expect(outbox._devicestorage).to.equal(devicestorageStub);
|
||||||
expect(outbox._invitation).to.equal(invitationDaoStub);
|
expect(outbox._invitationDao).to.equal(invitationDaoStub);
|
||||||
expect(outbox._outboxBusy).to.be.false;
|
expect(outbox._outboxBusy).to.be.false;
|
||||||
expect(outbox.pendingEmails).to.be.empty;
|
expect(outbox.pendingEmails).to.be.empty;
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user