mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
refactored notifications library to mark all notifs read when the menu is
opened (closes #134)
This commit is contained in:
@@ -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<numUnread;x++) {
|
||||
notifEl.setAttribute('data-nid', data.unread[x].nid);
|
||||
notifEl.className = 'unread';
|
||||
@@ -129,20 +134,12 @@
|
||||
}
|
||||
notifList.appendChild(notifFrag);
|
||||
|
||||
socket.emit('api:notifications.removeFlag');
|
||||
notifIcon.className = 'icon-circle-blank';
|
||||
});
|
||||
socket.on('api:notifications.hasFlag', function(flag) {
|
||||
var notifIcon = document.querySelector('.notifications a i');
|
||||
if(notifIcon) {
|
||||
if (flag > 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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
16
src/user.js
16
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));
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user