updating notifications lib to not expose every method

v1.18.x
Julian Lam 12 years ago
parent cb1258da9c
commit 984ac21e52

@ -1,121 +1,125 @@
var RDB = require('./redis.js'), var RDB = require('./redis.js'),
async = require('async'), async = require('async'),
utils = require('../public/src/utils.js'); utils = require('../public/src/utils.js'),
(function(Notifications) { notifications = {
Notifications.get = function(nid, callback) { get: function(nid, callback) {
RDB.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId', function(err, notification) { RDB.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId', function(err, notification) {
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]
});
}); });
}); },
} create: function(text, score, path, uniqueId, callback) {
/*
Notifications.create = function(text, score, path, uniqueId, callback) { * Score guide:
/* * 0 Low priority messages (probably unused)
* Score guide: * 5 Normal messages
* 0 Low priority messages (probably unused) * 10 High priority messages
* 5 Normal messages *
* 10 High priority messages * 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
* uniqueId is used solely to override stale nids. * (un)read list contains the same uniqueId, it will be removed, and
* If a new nid is pushed to a user and an existing nid in the user's * the new one put in its place.
* (un)read list contains the same uniqueId, it will be removed, and */
* the new one put in its place. RDB.incr('notifications:next_nid', function(err, nid) {
*/ RDB.hmset(
RDB.incr('notifications:next_nid', function(err, nid) { 'notifications:' + nid,
RDB.hmset( 'text', text || '',
'notifications:' + nid, 'score', score || 5,
'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 (status === 'OK') callback(nid);
function(err, status) { });
if (status === 'OK') callback(nid);
}); });
}); },
} push: function(nid, uids, callback) {
if (!Array.isArray(uids)) uids = [uids];
Notifications.push = function(nid, uids, callback) { var numUids = uids.length,
if (!Array.isArray(uids)) uids = [uids]; x;
var numUids = uids.length, notifications.get(nid, function(notif_data) {
x; for(x=0;x<numUids;x++) {
if (parseInt(uids[x]) > 0) {
Notifications.get(nid, function(notif_data) { (function(uid) {
for(x=0;x<numUids;x++) { notifications.remove_by_uniqueId(notif_data.uniqueId, uid, function() {
if (parseInt(uids[x]) > 0) { RDB.zadd('uid:' + uid + ':notifications:unread', notif_data.score, nid);
(function(uid) { RDB.set('uid:' + uid + ':notifications:flag', 1);
Notifications.remove_by_uniqueId(notif_data.uniqueId, uid, function() { global.io.sockets.in('uid_' + uid).emit('event:new_notification');
RDB.zadd('uid:' + uid + ':notifications:unread', notif_data.score, nid); if (callback) callback(true);
RDB.set('uid:' + uid + ':notifications:flag', 1); });
global.io.sockets.in('uid_' + uid).emit('event:new_notification'); })(uids[x]);
if (callback) callback(true); }
});
})(uids[x]);
} }
} });
}); },
} remove_by_uniqueId: function(uniqueId, uid, callback) {
async.parallel([
Notifications.remove_by_uniqueId = function(uniqueId, uid, callback) { function(next) {
async.parallel([ RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
function(next) { if (nids && nids.length > 0) {
RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) { async.each(nids, function(nid, next) {
if (nids && nids.length > 0) { notifications.get(nid, function(nid_info) {
async.each(nids, function(nid, next) { if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:unread', nid);
Notifications.get(nid, function(nid_info) { next();
if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:unread', nid); });
}, function(err) {
next(); next();
}); });
}, function(err) { } else next();
next(); });
}); },
} else next(); function(next) {
}); RDB.zrange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) {
}, if (nids && nids.length > 0) {
function(next) { async.each(nids, function(nid, next) {
RDB.zrange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) { notifications.get(nid, function(nid_info) {
if (nids && nids.length > 0) { if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:read', nid);
async.each(nids, function(nid, next) { next();
Notifications.get(nid, function(nid_info) { });
if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:read', nid); }, function(err) {
next(); next();
}); });
}, function(err) { } else next();
next(); });
}); }
} else next(); ], function(err) {
if (!err) callback(true);
});
},
mark_read: function(nid, uid, callback) {
if (parseInt(uid) > 0) {
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);
}); });
} }
], function(err) { },
if (!err) callback(true); mark_read_multiple: function(nids, uid, callback) {
}); if (!Array.isArray(nids) && parseInt(nids, 10) > 0) nids = [nids];
}
Notifications.mark_read = function(nid, uid, callback) { async.each(nids, function(nid, next) {
if (parseInt(uid) > 0) { notifications.mark_read(nid, uid, function(success) {
Notifications.get(nid, function(notif_data) { if (success) next(null);
RDB.zrem('uid:' + uid + ':notifications:unread', nid); });
RDB.zadd('uid:' + uid + ':notifications:read', notif_data.score, nid); }, function(err) {
if (callback) callback(true); if (callback && !err) callback(true);
}); });
} }
} }
Notifications.mark_read_multiple = function(nids, uid, callback) { module.exports = {
async.each(nids, function(nid, next) { get: notifications.get,
Notifications.mark_read(nid, uid, function(success) { create: notifications.create,
if (success) next(null); push: notifications.push,
}); mark_read: notifications.mark_read_multiple
}, function(err) { }
if (callback && !err) callback(true);
});
}
}(exports));
Loading…
Cancel
Save