mark all notifs as read functionality. CSS tweaks...

v1.18.x
Julian Lam 11 years ago
parent db2917193e
commit 62c85274a3

@ -2,7 +2,8 @@ define(function() {
var Notifications = {}; var Notifications = {};
Notifications.init = function() { Notifications.init = function() {
var listEl = $('.notifications-list'); var listEl = $('.notifications-list'),
markAllReadEl = document.getElementById('mark-all-notifs-read');
$('span.timeago').timeago(); $('span.timeago').timeago();
@ -10,6 +11,20 @@ define(function() {
listEl.on('click', 'li', function(e) { listEl.on('click', 'li', function(e) {
this.querySelector('a').click(); 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; return Notifications;

@ -1,10 +1,10 @@
<h2>[[notifications:title]]</h2> <h2>[[notifications:title]]</h2>
<button type="button" class="btn btn-default">Mark All as Read</button> <button type="button" class="btn btn-default" id="mark-all-notifs-read">Mark All as Read</button>
<ul class="notifications-list"> <ul class="notifications-list">
<!-- BEGIN notifications --> <!-- BEGIN notifications -->
<li data-nid="{notifications.nid}"> <li data-nid="{notifications.nid}" class="{notifications.readClass}">
<p class="timestamp"> <p class="timestamp">
<span class="timeago" title="{notifications.datetimeISO}"></span> <span class="timeago" title="{notifications.datetimeISO}"></span>
</p> </p>

@ -3,15 +3,22 @@ var RDB = require('./redis.js'),
utils = require('../public/src/utils.js'), utils = require('../public/src/utils.js'),
notifications = { notifications = {
get: function(nid, callback) { get: function(nid, uid, callback) {
RDB.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId', function(err, notification) { 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({ callback({
nid: nid, nid: nid,
text: notification[0], text: notification[0],
score: notification[1], score: notification[1],
path: notification[2], path: notification[2],
datetime: notification[3], datetime: notification[3],
uniqueId: notification[4] uniqueId: notification[4],
read: readIdx !== null ? true : false
}); });
}); });
}, },
@ -39,7 +46,7 @@ var RDB = require('./redis.js'),
var numUids = uids.length, var numUids = uids.length,
x; x;
notifications.get(nid, function(notif_data) { notifications.get(nid, null, function(notif_data) {
for (x = 0; x < numUids; x++) { for (x = 0; x < numUids; x++) {
if (parseInt(uids[x]) > 0) { if (parseInt(uids[x]) > 0) {
(function(uid) { (function(uid) {
@ -59,7 +66,7 @@ var RDB = require('./redis.js'),
RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) { RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.each(nids, function(nid, next) { 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); if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:unread', nid);
next(); next();
}); });
@ -73,7 +80,7 @@ var RDB = require('./redis.js'),
RDB.zrange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) { RDB.zrange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) {
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.each(nids, function(nid, next) { 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); if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:read', nid);
next(); next();
}); });
@ -89,7 +96,7 @@ var RDB = require('./redis.js'),
}, },
mark_read: function(nid, uid, callback) { mark_read: function(nid, uid, callback) {
if (parseInt(uid) > 0) { 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.zrem('uid:' + uid + ':notifications:unread', nid);
RDB.zadd('uid:' + uid + ':notifications:read', notif_data.datetime, nid); RDB.zadd('uid:' + uid + ':notifications:read', notif_data.datetime, nid);
if (callback) callback(); if (callback) callback();

@ -31,7 +31,7 @@ Upgrade.upgrade = function() {
async.each(keys, function(key, next) { async.each(keys, function(key, next) {
RDB.zrange(key, 0, -1, function(err, nids) { RDB.zrange(key, 0, -1, function(err, nids) {
async.each(nids, function(nid, next) { 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); RDB.zadd(key, notif_data.datetime, nid, next);
}); });
}, next); }, next);

@ -902,7 +902,7 @@ var utils = require('./../public/src/utils.js'),
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) { async.eachSeries(nids, function(nid, next) {
notifications.get(nid, function(notif_data) { notifications.get(nid, uid, function(notif_data) {
unread.push(notif_data); unread.push(notif_data);
next(); next();
}); });
@ -924,7 +924,7 @@ var utils = require('./../public/src/utils.js'),
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) { async.eachSeries(nids, function(nid, next) {
notifications.get(nid, function(notif_data) { notifications.get(nid, uid, function(notif_data) {
read.push(notif_data); read.push(notif_data);
next(); next();
}); });
@ -959,7 +959,7 @@ var utils = require('./../public/src/utils.js'),
var nids = results[0].concat(results[1]); var nids = results[0].concat(results[1]);
async.map(nids, function(nid, next) { async.map(nids, function(nid, next) {
notifications.get(nid, function(notif_data) { notifications.get(nid, uid, function(notif_data) {
next(null, notif_data); next(null, notif_data);
}); });
}, function(err, notifs) { }, function(err, notifs) {
@ -967,6 +967,7 @@ var utils = require('./../public/src/utils.js'),
return parseInt(b.datetime, 10) - parseInt(a.datetime, 10); return parseInt(b.datetime, 10) - parseInt(a.datetime, 10);
}).map(function(notif) { }).map(function(notif) {
notif.datetimeISO = new Date(parseInt(notif.datetime, 10)).toISOString(); notif.datetimeISO = new Date(parseInt(notif.datetime, 10)).toISOString();
notif.readClass = !notif.read ? 'unread' : '';
return notif; return notif;
}); });
@ -981,7 +982,7 @@ var utils = require('./../public/src/utils.js'),
getUnreadByUniqueId: function(uid, uniqueId, callback) { getUnreadByUniqueId: function(uid, uniqueId, callback) {
RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) { RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
async.filter(nids, function(nid, next) { async.filter(nids, function(nid, next) {
notifications.get(nid, function(notifObj) { notifications.get(nid, uid, function(notifObj) {
if (notifObj.uniqueId === uniqueId) next(true); if (notifObj.uniqueId === uniqueId) next(true);
else next(false); else next(false);
}); });

Loading…
Cancel
Save