diff --git a/src/js/controller/mail-list.js b/src/js/controller/mail-list.js index b405161..1a5c3a4 100644 --- a/src/js/controller/mail-list.js +++ b/src/js/controller/mail-list.js @@ -23,6 +23,9 @@ define(function(require) { $scope.selected = email; // set selected in parent scope ro it can be displayed in the read view $scope.$parent.selected = $scope.selected; + + // mark selected message as 'read' + markAsRead(email); }; $scope.synchronize = function() { @@ -175,6 +178,27 @@ define(function(require) { function getFolder() { return $scope.$parent.currentFolder; } + + function markAsRead(email) { + email.unread = false; + + // only update imap state if user is logged in + if (!loggedIn) { + return; + } + + emailDao.imapMarkMessageRead({ + folder: getFolder().path, + uid: email.uid + }, function(err) { + if (err) { + console.log(err); + updateStatus('Error marking read!'); + $scope.$apply(); + return; + } + }); + } }; function createDummyMails(callback) { diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index c59e521..a92b348 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -384,6 +384,24 @@ define(function(require) { }, messageReady); }; + EmailDAO.prototype.imapMarkMessageRead = function(options, callback) { + var self = this; + + // validate options + if (!options.folder || !options.uid) { + callback({ + errMsg: 'Invalid options!' + }); + return; + } + + self._imapClient.updateFlags({ + path: options.folder, + uid: options.uid, + unread: false + }, callback); + }; + // // SMTP Apis // diff --git a/test/new-unit/email-dao-test.js b/test/new-unit/email-dao-test.js index 9053929..0aa4213 100644 --- a/test/new-unit/email-dao-test.js +++ b/test/new-unit/email-dao-test.js @@ -228,7 +228,7 @@ define(function(require) { }); }); - describe('IMAP: get unread messages for folder', function() { + describe('IMAP: get unread message count for folder', function() { it('should work', function(done) { imapClientStub.unreadMessages.yields(); emailDao.unreadMessages(function(err) { @@ -409,8 +409,23 @@ define(function(require) { }); }); }); - }); + + describe('IMAP: mark message as read', function() { + it('should work', function(done) { + imapClientStub.updateFlags.yields(); + + emailDao.imapMarkMessageRead({ + folder: 'asdf', + uid: 1 + }, function(err) { + expect(imapClientStub.updateFlags.calledOnce).to.be.true; + expect(err).to.not.exist; + done(); + }); + }); + }); + }); }); \ No newline at end of file