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