closed #368 - notifications now no longer need scores

v1.18.x
Julian Lam 11 years ago
parent d583122c64
commit 585e07bc79

@ -15,29 +15,22 @@ var RDB = require('./redis.js'),
}); });
}); });
}, },
create: function(text, score, path, uniqueId, callback) { create: function(text, path, uniqueId, callback) {
/* /**
* Score guide:
* 0 Low priority messages (probably unused)
* 5 Normal messages
* 10 High priority messages
*
* uniqueId is used solely to override stale nids. * uniqueId is used solely to override stale nids.
* If a new nid is pushed to a user and an existing nid in the user's * If a new nid is pushed to a user and an existing nid in the user's
* (un)read list contains the same uniqueId, it will be removed, and * (un)read list contains the same uniqueId, it will be removed, and
* the new one put in its place. * the new one put in its place.
*/ */
RDB.incr('notifications:next_nid', function(err, nid) { RDB.incr('notifications:next_nid', function(err, nid) {
RDB.hmset( RDB.hmset('notifications:' + nid, {
'notifications:' + nid, text: text || '',
'text', text || '', path: path || null,
'score', score || 5, datetime: Date.now(),
'path', path || null, uniqueId: uniqueId || utils.generateUUID()
'datetime', Date.now(), }, function(err, status) {
'uniqueId', uniqueId || utils.generateUUID(), if (!err) callback(nid);
function(err, status) { });
if (status === 'OK') callback(nid);
});
}); });
}, },
push: function(nid, uids, callback) { push: function(nid, uids, callback) {
@ -51,8 +44,8 @@ var RDB = require('./redis.js'),
if (parseInt(uids[x]) > 0) { if (parseInt(uids[x]) > 0) {
(function(uid) { (function(uid) {
notifications.remove_by_uniqueId(notif_data.uniqueId, uid, function() { notifications.remove_by_uniqueId(notif_data.uniqueId, uid, function() {
RDB.zadd('uid:' + uid + ':notifications:unread', notif_data.score, nid); RDB.zadd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
global.io.sockets. in ('uid_' + uid).emit('event:new_notification'); global.io.sockets.in('uid_' + uid).emit('event:new_notification');
if (callback) callback(true); if (callback) callback(true);
}); });
})(uids[x]); })(uids[x]);
@ -98,7 +91,7 @@ var RDB = require('./redis.js'),
if (parseInt(uid) > 0) { if (parseInt(uid) > 0) {
notifications.get(nid, function(notif_data) { notifications.get(nid, 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.score, nid); RDB.zadd('uid:' + uid + ':notifications:read', notif_data.datetime, nid);
if (callback) callback(); if (callback) callback();
}); });
} }

@ -276,7 +276,7 @@ var RDB = require('./redis.js'),
topics.getTopicField(tid, 'title', function(err, title) { topics.getTopicField(tid, 'title', function(err, title) {
topics.getTeaser(tid, function(err, teaser) { topics.getTeaser(tid, function(err, teaser) {
if (!err) { if (!err) {
notifications.create('<strong>' + teaser.username + '</strong> has posted a reply to: "<strong>' + title + '</strong>"', null, nconf.get('relative_path') + '/topic/' + tid, 'topic:' + tid, function(nid) { notifications.create('<strong>' + teaser.username + '</strong> has posted a reply to: "<strong>' + title + '</strong>"', nconf.get('relative_path') + '/topic/' + tid, 'topic:' + tid, function(nid) {
next(null, nid); next(null, nid);
}); });
} else next(err); } else next(err);

@ -578,7 +578,7 @@ var utils = require('./../public/src/utils.js'),
topics.getTopicField(tid, 'slug', function(err, slug) { topics.getTopicField(tid, 'slug', function(err, slug) {
var message = '<strong>' + username + '</strong> made a new post'; var message = '<strong>' + username + '</strong> made a new post';
notifications.create(message, 5, nconf.get('relative_path') + '/topic/' + slug + '#' + pid, 'topic:' + tid, function(nid) { notifications.create(message, nconf.get('relative_path') + '/topic/' + slug + '#' + pid, 'topic:' + tid, function(nid) {
notifications.push(nid, followers); notifications.push(nid, followers);
}); });
}); });
@ -888,7 +888,7 @@ var utils = require('./../public/src/utils.js'),
async.parallel({ async.parallel({
unread: function(next) { unread: function(next) {
RDB.zrevrangebyscore('uid:' + uid + ':notifications:unread', 10, 0, function(err, nids) { RDB.zrevrange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
// @todo handle err // @todo handle err
var unread = []; var unread = [];
@ -910,7 +910,7 @@ var utils = require('./../public/src/utils.js'),
}); });
}, },
read: function(next) { read: function(next) {
RDB.zrevrangebyscore('uid:' + uid + ':notifications:read', 10, 0, function(err, nids) { RDB.zrevrange('uid:' + uid + ':notifications:read', 0, 10, function(err, nids) {
// @todo handle err // @todo handle err
var read = []; var read = [];
@ -932,22 +932,6 @@ var utils = require('./../public/src/utils.js'),
}); });
} }
}, function(err, notifications) { }, function(err, notifications) {
// While maintaining score sorting, sort by time
var readCount = notifications.read.length,
unreadCount = notifications.unread.length;
notifications.read.sort(function(a, b) {
if (a.score === b.score) {
return (a.datetime - b.datetime) > 0 ? -1 : 1;
}
});
notifications.unread.sort(function(a, b) {
if (a.score === b.score) {
return (a.datetime - b.datetime) > 0 ? -1 : 1;
}
});
// Limit the number of notifications to `maxNotifs`, prioritising unread notifications // Limit the number of notifications to `maxNotifs`, prioritising unread notifications
if (notifications.read.length + notifications.unread.length > maxNotifs) { if (notifications.read.length + notifications.unread.length > maxNotifs) {
notifications.read.length = maxNotifs - notifications.unread.length; notifications.read.length = maxNotifs - notifications.unread.length;

@ -580,7 +580,7 @@ module.exports.init = function(io) {
notifText = 'New message from <strong>' + username + '</strong>'; notifText = 'New message from <strong>' + username + '</strong>';
if (!isUserOnline(touid)) { if (!isUserOnline(touid)) {
notifications.create(notifText, 5, 'javascript:app.openChat(&apos;' + username + '&apos;, ' + uid + ');', 'notification_' + uid + '_' + touid, function(nid) { notifications.create(notifText, 'javascript:app.openChat(&apos;' + username + '&apos;, ' + uid + ');', 'notification_' + uid + '_' + touid, function(nid) {
notifications.push(nid, [touid], function(success) { notifications.push(nid, [touid], function(success) {
}); });

Loading…
Cancel
Save