diff --git a/public/src/forum/notifications.js b/public/src/forum/notifications.js
index 38bb3c80a2..de1e053871 100644
--- a/public/src/forum/notifications.js
+++ b/public/src/forum/notifications.js
@@ -2,7 +2,8 @@ define(function() {
var Notifications = {};
Notifications.init = function() {
- var listEl = $('.notifications-list');
+ var listEl = $('.notifications-list'),
+ markAllReadEl = document.getElementById('mark-all-notifs-read');
$('span.timeago').timeago();
@@ -10,6 +11,20 @@ define(function() {
listEl.on('click', 'li', function(e) {
this.querySelector('a').click();
});
+
+ // Mark all as read button
+ $(markAllReadEl).click(function() {
+ socket.emit('api:notifications.mark_all_read', {}, function() {
+ ajaxify.go('notifications');
+ app.alert({
+ alert_id: "notifications:mark_all_read",
+ title: "All Notifications Read",
+ message: "Successfully marked all notifications read",
+ type: 'success',
+ timeout: 2500
+ })
+ });
+ });
}
return Notifications;
diff --git a/public/templates/notifications.tpl b/public/templates/notifications.tpl
index d509293009..beac732ef1 100644
--- a/public/templates/notifications.tpl
+++ b/public/templates/notifications.tpl
@@ -1,10 +1,10 @@
[[notifications:title]]
-
+
- -
+
-
diff --git a/src/notifications.js b/src/notifications.js
index d2e1477aac..d6b2791028 100644
--- a/src/notifications.js
+++ b/src/notifications.js
@@ -3,17 +3,24 @@ var RDB = require('./redis.js'),
utils = require('../public/src/utils.js'),
notifications = {
- get: function(nid, callback) {
- RDB.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId', function(err, notification) {
- callback({
- nid: nid,
- text: notification[0],
- score: notification[1],
- path: notification[2],
- datetime: notification[3],
- uniqueId: notification[4]
+ get: function(nid, uid, callback) {
+ RDB.multi()
+ .hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId')
+ .zrank('uid:' + uid + ':notifications:read', nid)
+ .exec(function(err, results) {
+ var notification = results[0]
+ readIdx = results[1];
+
+ callback({
+ nid: nid,
+ text: notification[0],
+ score: notification[1],
+ path: notification[2],
+ datetime: notification[3],
+ uniqueId: notification[4],
+ read: readIdx !== null ? true : false
+ });
});
- });
},
create: function(text, path, uniqueId, callback) {
/**
@@ -39,7 +46,7 @@ var RDB = require('./redis.js'),
var numUids = uids.length,
x;
- notifications.get(nid, function(notif_data) {
+ notifications.get(nid, null, function(notif_data) {
for (x = 0; x < numUids; x++) {
if (parseInt(uids[x]) > 0) {
(function(uid) {
@@ -59,7 +66,7 @@ var RDB = require('./redis.js'),
RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
if (nids && nids.length > 0) {
async.each(nids, function(nid, next) {
- notifications.get(nid, function(nid_info) {
+ notifications.get(nid, uid, function(nid_info) {
if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:unread', nid);
next();
});
@@ -73,7 +80,7 @@ var RDB = require('./redis.js'),
RDB.zrange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) {
if (nids && nids.length > 0) {
async.each(nids, function(nid, next) {
- notifications.get(nid, function(nid_info) {
+ notifications.get(nid, uid, function(nid_info) {
if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:read', nid);
next();
});
@@ -89,7 +96,7 @@ var RDB = require('./redis.js'),
},
mark_read: function(nid, uid, callback) {
if (parseInt(uid) > 0) {
- notifications.get(nid, function(notif_data) {
+ notifications.get(nid, uid, function(notif_data) {
RDB.zrem('uid:' + uid + ':notifications:unread', nid);
RDB.zadd('uid:' + uid + ':notifications:read', notif_data.datetime, nid);
if (callback) callback();
diff --git a/src/upgrade.js b/src/upgrade.js
index 153c7c0399..9a6faed535 100644
--- a/src/upgrade.js
+++ b/src/upgrade.js
@@ -31,7 +31,7 @@ Upgrade.upgrade = function() {
async.each(keys, function(key, next) {
RDB.zrange(key, 0, -1, function(err, nids) {
async.each(nids, function(nid, next) {
- notifications.get(nid, function(notif_data) {
+ notifications.get(nid, null, function(notif_data) {
RDB.zadd(key, notif_data.datetime, nid, next);
});
}, next);
diff --git a/src/user.js b/src/user.js
index 9a0092a21f..47e86e69f1 100644
--- a/src/user.js
+++ b/src/user.js
@@ -902,7 +902,7 @@ var utils = require('./../public/src/utils.js'),
if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) {
- notifications.get(nid, function(notif_data) {
+ notifications.get(nid, uid, function(notif_data) {
unread.push(notif_data);
next();
});
@@ -924,7 +924,7 @@ var utils = require('./../public/src/utils.js'),
if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) {
- notifications.get(nid, function(notif_data) {
+ notifications.get(nid, uid, function(notif_data) {
read.push(notif_data);
next();
});
@@ -959,7 +959,7 @@ var utils = require('./../public/src/utils.js'),
var nids = results[0].concat(results[1]);
async.map(nids, function(nid, next) {
- notifications.get(nid, function(notif_data) {
+ notifications.get(nid, uid, function(notif_data) {
next(null, notif_data);
});
}, function(err, notifs) {
@@ -967,6 +967,7 @@ var utils = require('./../public/src/utils.js'),
return parseInt(b.datetime, 10) - parseInt(a.datetime, 10);
}).map(function(notif) {
notif.datetimeISO = new Date(parseInt(notif.datetime, 10)).toISOString();
+ notif.readClass = !notif.read ? 'unread' : '';
return notif;
});
@@ -981,7 +982,7 @@ var utils = require('./../public/src/utils.js'),
getUnreadByUniqueId: function(uid, uniqueId, callback) {
RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
async.filter(nids, function(nid, next) {
- notifications.get(nid, function(notifObj) {
+ notifications.get(nid, uid, function(notifObj) {
if (notifObj.uniqueId === uniqueId) next(true);
else next(false);
});