diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.js index 4d9fffcde0..6e36cf7296 100644 --- a/public/src/modules/notifications.js +++ b/public/src/modules/notifications.js @@ -37,7 +37,8 @@ define('notifications', ['sounds'], function(sound) { }); notifList.on('click', '[data-nid]', function() { - var nid = $(this).attr('data-nid'); + var nid = this.getAttribute('data-nid'); + socket.emit('notifications.markRead', nid, function(err) { if (err) { app.alertError(err.message); @@ -54,6 +55,25 @@ define('notifications', ['sounds'], function(sound) { }); }); + notifList.on('click', '.mark-read', function(e) { + var anchorEl = $(this.parentNode), + parentEl = anchorEl.parent(), + nid = anchorEl.attr('data-nid'), + unread = parentEl.hasClass('unread'); + + e.preventDefault(); + e.stopPropagation(); + + socket.emit('notifications.mark' + (unread ? 'Read' : 'Unread'), nid, function(err) { + if (err) { + app.alertError(err.message); + } + + parentEl.toggleClass('unread'); + increaseNotifCount(unread ? -1 : 1); + }); + }); + function updateNotifCount(count) { if (count > 0) { notifIcon.removeClass('fa-bell-o').addClass('fa-bell'); @@ -67,8 +87,8 @@ define('notifications', ['sounds'], function(sound) { Tinycon.setBubble(count); }; - function increaseNotifCount() { - var count = parseInt(notifIcon.attr('data-content'), 10) + 1; + function increaseNotifCount(delta) { + var count = parseInt(notifIcon.attr('data-content'), 10) + delta; updateNotifCount(count); } @@ -94,7 +114,7 @@ define('notifications', ['sounds'], function(sound) { ajaxify.refresh(); } - increaseNotifCount(); + increaseNotifCount(1); sound.play('notification'); }); diff --git a/src/notifications.js b/src/notifications.js index dc3aae7ea2..0008fb1f69 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -223,6 +223,22 @@ var async = require('async'), Notifications.markReadMultiple([nid], uid, callback); }; + Notifications.markUnread = function(nid, uid, callback) { + callback = callback || function() {}; + if (!parseInt(uid, 10) || !nid) { + return callback(); + } + + db.getObjectField(nid, 'datetime', function(err, datetime) { + datetime = datetime || Date.now(); + + async.parallel([ + async.apply(db.sortedSetRemove, 'uid:' + uid + ':notifications:read', nid), + async.apply(db.sortedSetAdd, 'uid:' + uid + ':notifications:unread', datetime, nid) + ], callback); + }); + }; + Notifications.markReadMultiple = function(nids, uid, callback) { callback = callback || function() {}; if (!Array.isArray(nids) || !nids.length) { diff --git a/src/socket.io/notifications.js b/src/socket.io/notifications.js index ebd633b080..cb93e1ca45 100644 --- a/src/socket.io/notifications.js +++ b/src/socket.io/notifications.js @@ -24,6 +24,10 @@ SocketNotifs.markRead = function(socket, nid, callback) { notifications.markRead(nid, socket.uid, callback); }; +SocketNotifs.markUnread = function(socket, nid, callback) { + notifications.markUnread(nid, socket.uid, callback); +}; + SocketNotifs.markAllRead = function(socket, data, callback) { notifications.markAllRead(socket.uid, callback); }; diff --git a/src/user/notifications.js b/src/user/notifications.js index ccadf4f999..523b9e4903 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -76,7 +76,7 @@ var async = require('async'), deletedNids.push(nids[index]); } else { notification.read = read; - notification.readClass = !notification.read ? 'label-warning' : ''; + notification.readClass = !notification.read ? 'unread' : ''; } });