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 = {};
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;

@ -1,10 +1,10 @@
<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">
<!-- BEGIN notifications -->
<li data-nid="{notifications.nid}">
<li data-nid="{notifications.nid}" class="{notifications.readClass}">
<p class="timestamp">
<span class="timeago" title="{notifications.datetimeISO}"></span>
</p>

@ -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();

@ -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);

@ -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);
});

Loading…
Cancel
Save