updating notifications lib to not expose every method

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

@ -1,9 +1,9 @@
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,
@ -14,9 +14,8 @@ var RDB = require('./redis.js'),
uniqueId: notification[4] uniqueId: notification[4]
}); });
}); });
} },
create: function(text, score, path, uniqueId, callback) {
Notifications.create = function(text, score, path, uniqueId, callback) {
/* /*
* Score guide: * Score guide:
* 0 Low priority messages (probably unused) * 0 Low priority messages (probably unused)
@ -40,19 +39,18 @@ var RDB = require('./redis.js'),
if (status === 'OK') callback(nid); if (status === 'OK') callback(nid);
}); });
}); });
} },
push: function(nid, uids, callback) {
Notifications.push = function(nid, uids, callback) {
if (!Array.isArray(uids)) uids = [uids]; if (!Array.isArray(uids)) uids = [uids];
var numUids = uids.length, var numUids = uids.length,
x; x;
Notifications.get(nid, function(notif_data) { notifications.get(nid, 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) {
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.score, nid);
RDB.set('uid:' + uid + ':notifications:flag', 1); RDB.set('uid:' + uid + ':notifications:flag', 1);
global.io.sockets.in('uid_' + uid).emit('event:new_notification'); global.io.sockets.in('uid_' + uid).emit('event:new_notification');
@ -62,15 +60,14 @@ var RDB = require('./redis.js'),
} }
} }
}); });
} },
remove_by_uniqueId: function(uniqueId, uid, callback) {
Notifications.remove_by_uniqueId = function(uniqueId, uid, callback) {
async.parallel([ async.parallel([
function(next) { function(next) {
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, 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();
}); });
@ -84,7 +81,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, 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();
}); });
@ -97,25 +94,32 @@ var RDB = require('./redis.js'),
], function(err) { ], function(err) {
if (!err) callback(true); if (!err) callback(true);
}); });
} },
mark_read: function(nid, uid, callback) {
Notifications.mark_read = function(nid, uid, callback) {
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.score, nid);
if (callback) callback(true); if (callback) callback(true);
}); });
} }
} },
mark_read_multiple: function(nids, uid, callback) {
if (!Array.isArray(nids) && parseInt(nids, 10) > 0) nids = [nids];
Notifications.mark_read_multiple = function(nids, uid, callback) {
async.each(nids, function(nid, next) { async.each(nids, function(nid, next) {
Notifications.mark_read(nid, uid, function(success) { notifications.mark_read(nid, uid, function(success) {
if (success) next(null); if (success) next(null);
}); });
}, function(err) { }, function(err) {
if (callback && !err) callback(true); if (callback && !err) callback(true);
}); });
} }
}(exports)); }
module.exports = {
get: notifications.get,
create: notifications.create,
push: notifications.push,
mark_read: notifications.mark_read_multiple
}
Loading…
Cancel
Save