implement send to outbox and check outbox periodically

This commit is contained in:
Tankred Hase 2013-10-23 20:46:42 +02:00
parent 8add506135
commit 5b895cb61e
4 changed files with 82 additions and 6 deletions

View File

@ -27,7 +27,8 @@ define([], function() {
port: 465,
host: 'smtp.gmail.com'
}
}
},
checkOutboxInterval: 30000
};
/**

View File

@ -4,7 +4,8 @@ define(function(require) {
var angular = require('angular'),
appController = require('js/app-controller'),
_ = require('underscore'),
emailDao;
config = require('js/app-config').config,
emailDao, senderIntervalId;
//
// Controller
@ -91,6 +92,69 @@ define(function(require) {
}
};
$scope.sendFirstFromOutbox = function() {
var dbType = 'email_OUTBOX',
outbox = _.findWhere($scope.folders, {
type: 'Outbox'
});
checkStorage();
function checkStorage() {
// get last item from outbox
emailDao._devicestorage.listItems(dbType, 0, null, function(err, pending) {
if (err) {
console.error(err);
return;
}
// update outbox folder count
outbox.count = pending.length;
$scope.$apply();
if (pending.length < 1) {
return;
}
// sending the first one pending
send(pending[0]);
});
}
function send(email) {
emailDao.smtpSend(email, function(err) {
if (err) {
console.error(err);
return;
}
removeFromStorage(email.id);
});
}
function removeFromStorage(id) {
if (!id) {
console.error('Cannot remove email from storage without a valid id!');
return;
}
// delete email from local storage
var key = dbType + '_' + id;
emailDao._devicestorage.removeList(key, function(err) {
if (err) {
console.error(err);
return;
}
outbox.count = (outbox.count > 0) ? outbox.count - 1 : outbox.count;
$scope.$apply();
});
}
};
//
// Start
//
initFolders(function(folders) {
$scope.folders = folders;
// select inbox as the current folder on init
@ -113,6 +177,9 @@ define(function(require) {
f.count = 0;
});
// start checking outbox periodically
startOutboxSender();
callback(folders);
$scope.$apply();
});
@ -141,6 +208,11 @@ define(function(require) {
path: 'TRASH'
}]);
}
function startOutboxSender() {
// start periodic checking of outbox
senderIntervalId = setInterval($scope.sendFirstFromOutbox, config.checkOutboxInterval);
}
};
//

View File

@ -76,7 +76,7 @@ define(function(require) {
$scope.ciphertextPreview = (body) ? aes.encrypt(body, key, iv) : '';
};
$scope.sendEmail = function() {
$scope.sendToOutbox = function() {
var to, body, email;
// validate recipients
@ -106,15 +106,18 @@ define(function(require) {
});
});
emailDao.smtpSend(email, function(err) {
// set an id for the email and store in outbox
email.id = util.UUID();
emailDao._devicestorage.storeList([email], 'email_OUTBOX', function(err) {
if (err) {
console.log(err);
console.error(err);
return;
}
var ps = $scope.$parent.$parent;
ps.closeWriter();
ps.$apply();
ps.sendFirstFromOutbox();
});
};
};

View File

@ -37,7 +37,7 @@
</div><!--/.body-->
<div class="send-control">
<button ng-click="sendEmail()" class="btn" ng-disabled="!to" tabindex="5">Send securely</button>
<button ng-click="sendToOutbox()" class="btn" ng-disabled="!to" tabindex="5">Send securely</button>
</div>
</div><!--/.write-view-->