|
|
|
@ -4,15 +4,20 @@ var RDB = require('./redis'),
|
|
|
|
|
winston = require('winston'),
|
|
|
|
|
cron = require('cron').CronJob,
|
|
|
|
|
|
|
|
|
|
notifications = {
|
|
|
|
|
init: function() {
|
|
|
|
|
websockets = require('./websockets');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(function(Notifications) {
|
|
|
|
|
|
|
|
|
|
Notifications.init = function() {
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
winston.info('[notifications.init] Registering jobs.');
|
|
|
|
|
}
|
|
|
|
|
new cron('0 0 * * *', Notifications.prune, null, true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
new cron('0 0 * * *', notifications.prune, null, true);
|
|
|
|
|
},
|
|
|
|
|
get: function(nid, uid, callback) {
|
|
|
|
|
Notifications.get = function(nid, uid, callback) {
|
|
|
|
|
RDB.multi()
|
|
|
|
|
.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId')
|
|
|
|
|
.zrank('uid:' + uid + ':notifications:read', nid)
|
|
|
|
@ -35,8 +40,9 @@ var RDB = require('./redis'),
|
|
|
|
|
read: readIdx !== null ? true : false
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
create: function(text, path, uniqueId, callback) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notifications.create = function(text, path, uniqueId, callback) {
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
@ -56,8 +62,9 @@ var RDB = require('./redis'),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
destroy: function(nid) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function destroy(nid) {
|
|
|
|
|
var multi = RDB.multi();
|
|
|
|
|
|
|
|
|
|
multi.del('notifications:' + nid);
|
|
|
|
@ -69,24 +76,22 @@ var RDB = require('./redis'),
|
|
|
|
|
winston.error(err.stack);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
push: function(nid, uids, callback) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifications.push = function(nid, uids, callback) {
|
|
|
|
|
if (!Array.isArray(uids)) uids = [uids];
|
|
|
|
|
|
|
|
|
|
var numUids = uids.length,
|
|
|
|
|
x;
|
|
|
|
|
|
|
|
|
|
notifications.get(nid, null, function(notif_data) {
|
|
|
|
|
Notifications.get(nid, null, function(notif_data) {
|
|
|
|
|
for (x = 0; x < numUids; x++) {
|
|
|
|
|
if (parseInt(uids[x], 10) > 0) {
|
|
|
|
|
(function(uid) {
|
|
|
|
|
notifications.remove_by_uniqueId(notif_data.uniqueId, uid, function() {
|
|
|
|
|
remove_by_uniqueId(notif_data.uniqueId, uid, function() {
|
|
|
|
|
RDB.zadd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
|
|
|
|
|
|
|
|
|
|
global.io.sockets.in('uid_' + uid).emit('event:new_notification');
|
|
|
|
|
|
|
|
|
|
// TODO: moving this require to the top of the file overwrites 'notifications' figure out why -baris
|
|
|
|
|
//require('./websockets').in('uid_' + uid).emit('event:new_notification');
|
|
|
|
|
websockets.in('uid_' + uid).emit('event:new_notification');
|
|
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback(true);
|
|
|
|
@ -96,14 +101,15 @@ var RDB = require('./redis'),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
remove_by_uniqueId: function(uniqueId, uid, callback) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function remove_by_uniqueId(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, uid, function(nid_info) {
|
|
|
|
|
Notifications.get(nid, uid, function(nid_info) {
|
|
|
|
|
if (nid_info.uniqueId === uniqueId) {
|
|
|
|
|
RDB.zrem('uid:' + uid + ':notifications:unread', nid);
|
|
|
|
|
}
|
|
|
|
@ -122,7 +128,7 @@ var RDB = require('./redis'),
|
|
|
|
|
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, uid, function(nid_info) {
|
|
|
|
|
Notifications.get(nid, uid, function(nid_info) {
|
|
|
|
|
if (nid_info && nid_info.uniqueId === uniqueId) {
|
|
|
|
|
RDB.zrem('uid:' + uid + ':notifications:read', nid);
|
|
|
|
|
}
|
|
|
|
@ -142,10 +148,11 @@ var RDB = require('./redis'),
|
|
|
|
|
callback(true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
mark_read: function(nid, uid, callback) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifications.mark_read = function(nid, uid, callback) {
|
|
|
|
|
if (parseInt(uid) > 0) {
|
|
|
|
|
notifications.get(nid, uid, 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) {
|
|
|
|
@ -153,14 +160,15 @@ var RDB = require('./redis'),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mark_read_multiple: function(nids, uid, callback) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifications.mark_read_multiple = function(nids, uid, callback) {
|
|
|
|
|
if (!Array.isArray(nids) && parseInt(nids, 10) > 0) {
|
|
|
|
|
nids = [nids];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.each(nids, function(nid, next) {
|
|
|
|
|
notifications.mark_read(nid, uid, function(err) {
|
|
|
|
|
Notifications.mark_read(nid, uid, function(err) {
|
|
|
|
|
if (!err) {
|
|
|
|
|
next(null);
|
|
|
|
|
}
|
|
|
|
@ -170,23 +178,25 @@ var RDB = require('./redis'),
|
|
|
|
|
callback(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
mark_all_read: function(uid, callback) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notifications.mark_all_read = function(uid, callback) {
|
|
|
|
|
RDB.zrange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nids.length > 0) {
|
|
|
|
|
notifications.mark_read_multiple(nids, uid, function(err) {
|
|
|
|
|
Notifications.mark_read_multiple(nids, uid, function(err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
prune: function(cutoff) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notifications.prune = function(cutoff) {
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
winston.info('[notifications.prune] Removing expired notifications from the database.');
|
|
|
|
|
}
|
|
|
|
@ -240,7 +250,7 @@ var RDB = require('./redis'),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (expired) {
|
|
|
|
|
notifications.destroy(nid);
|
|
|
|
|
destroy(nid);
|
|
|
|
|
numPruned++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -258,16 +268,7 @@ var RDB = require('./redis'),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
init: notifications.init,
|
|
|
|
|
get: notifications.get,
|
|
|
|
|
create: notifications.create,
|
|
|
|
|
push: notifications.push,
|
|
|
|
|
mark_read: notifications.mark_read_multiple,
|
|
|
|
|
mark_all_read: notifications.mark_all_read,
|
|
|
|
|
prune: notifications.prune
|
|
|
|
|
};
|
|
|
|
|
}(exports));
|
|
|
|
|
|
|
|
|
|