From 7251af56e39cb993ce7cc3ebf464f290f9c63cdc Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 7 Aug 2013 22:42:34 -0400 Subject: [PATCH] refactored notifications library to mark all notifs read when the menu is opened (closes #134) --- public/src/forum/footer.js | 27 ++++++++++++--------------- src/notifications.js | 22 +++++++++++++++++----- src/user.js | 16 ---------------- src/websockets.js | 16 ++++++---------- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/public/src/forum/footer.js b/public/src/forum/footer.js index 9c683c13df..e422356ded 100644 --- a/public/src/forum/footer.js +++ b/public/src/forum/footer.js @@ -85,10 +85,16 @@ // Notifications dropdown var notifContainer = document.getElementsByClassName('notifications')[0], notifTrigger = notifContainer.querySelector('a'), - notifList = document.getElementById('notif-list'); + notifList = document.getElementById('notif-list'), + notifIcon = document.querySelector('.notifications a i'); notifTrigger.addEventListener('click', function(e) { e.preventDefault(); - if (notifContainer.className.indexOf('open') === -1) socket.emit('api:notifications.get'); + if (notifContainer.className.indexOf('open') === -1) { + socket.emit('api:notifications.get'); + socket.emit('api:notifications.mark_all_read', null, function() { + notifIcon.className = 'icon-circle-blank'; + }); + } }); notifList.addEventListener('click', function(e) { var target; @@ -101,16 +107,15 @@ var nid = parseInt(target.getAttribute('data-nid')); if (nid > 0) socket.emit('api:notifications.mark_read', nid); } - }) + }); socket.on('api:notifications.get', function(data) { var notifFrag = document.createDocumentFragment(), notifEl = document.createElement('li'), - notifIcon = document.querySelector('.notifications a i'), numRead = data.read.length, numUnread = data.unread.length, x; notifList.innerHTML = ''; - if (data.read.length + data.unread.length > 0) { + if ((data.read.length + data.unread.length) > 0) { for(x=0;x 0) notifIcon.className = 'icon-circle active'; - else notifIcon.className = 'icon-circle-blank'; - } + if (data.unread.length > 0) notifIcon.className = 'icon-circle active'; + else notifIcon.className = 'icon-circle-blank'; }); socket.on('event:new_notification', function() { document.querySelector('.notifications a i').className = 'icon-circle active'; }); - socket.emit('api:notifications.hasFlag'); socket.on('chatMessage', function(data) { diff --git a/src/notifications.js b/src/notifications.js index 12a42dc300..3e251628f5 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -100,7 +100,7 @@ var RDB = require('./redis.js'), notifications.get(nid, function(notif_data) { RDB.zrem('uid:' + uid + ':notifications:unread', nid); RDB.zadd('uid:' + uid + ':notifications:read', notif_data.score, nid); - if (callback) callback(true); + if (callback) callback(); }); } }, @@ -108,11 +108,22 @@ var RDB = require('./redis.js'), if (!Array.isArray(nids) && parseInt(nids, 10) > 0) nids = [nids]; async.each(nids, function(nid, next) { - notifications.mark_read(nid, uid, function(success) { - if (success) next(null); + notifications.mark_read(nid, uid, function(err) { + if (!err) next(null); }); }, function(err) { - if (callback && !err) callback(true); + if (callback) callback(err); + }); + }, + mark_all_read: function(uid, callback) { + RDB.zrange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) { + if (err) return callback(err); + + if (nids.length > 0) { + notifications.mark_read_multiple(nids, uid, function(err) { + callback(err); + }); + } else callback(); }); } } @@ -121,5 +132,6 @@ module.exports = { get: notifications.get, create: notifications.create, push: notifications.push, - mark_read: notifications.mark_read_multiple + mark_read: notifications.mark_read_multiple, + mark_all_read: notifications.mark_all_read } \ No newline at end of file diff --git a/src/user.js b/src/user.js index bf96034650..78b01f5f9c 100644 --- a/src/user.js +++ b/src/user.js @@ -1057,22 +1057,6 @@ var utils = require('./../public/src/utils.js'), }, getUnreadCount: function(uid, callback) { RDB.zcount('uid:' + uid + ':notifications:unread', 0, 10, callback); - }, - hasFlag: function(uid, callback) { - RDB.get('uid:1:notifications:flag', function(err, flag) { - if (err) { - RDB.handle(err); - } - - callback(flag === 1); - }); - }, - removeFlag: function(uid) { - RDB.del('uid:' + uid + ':notifications:flag', function(err) { - if (err) { - RDB.handle(err); - } - }); } } }(exports)); diff --git a/src/websockets.js b/src/websockets.js index 1263533ab3..5a7bfc062b 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -409,20 +409,16 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), }); }); - socket.on('api:notifications.hasFlag', function(data) { - user.notifications.hasFlag(uid, function(flag) { - socket.emit('api:notifications.hasFlag', flag); - }); - }); - - socket.on('api:notifications.removeFlag', function() { - user.notifications.removeFlag(uid); - }); - socket.on('api:notifications.mark_read', function(nid) { notifications.mark_read(nid, uid); }); + socket.on('api:notifications.mark_all_read', function(data, callback) { + notifications.mark_all_read(uid, function(err) { + if (!err) callback(); + }); + }); + socket.on('api:categories.getRecentReplies', function(tid) { categories.getRecentReplies(tid, 4, function(replies) { socket.emit('api:categories.getRecentReplies', replies);