You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/notifications.js

271 lines
6.6 KiB
JavaScript

11 years ago
var async = require('async'),
winston = require('winston'),
cron = require('cron').CronJob,
11 years ago
db = require('./database'),
utils = require('../public/src/utils'),
11 years ago
websockets = require('./websockets');
11 years ago
(function(Notifications) {
11 years ago
Notifications.init = function() {
if (process.env.NODE_ENV === 'development') {
winston.info('[notifications.init] Registering jobs.');
}
new cron('0 0 * * *', Notifications.prune, null, true);
};
11 years ago
11 years ago
Notifications.get = function(nid, uid, callback) {
db.exists('nofitications:' + nid, function(err, exists) {
if(!exists) {
return callback(null);
}
db.sortedSetRank('uid:' + uid + ':notifications:read', nid, function(err, rank) {
11 years ago
db.getObjectFields('notifications:' + nid, ['nid', 'text', 'score', 'path', 'datetime', 'uniqueId'], function(err, notification) {
notification.read = rank !== null ? true:false;
callback(notification);
11 years ago
});
});
});
11 years ago
};
11 years ago
11 years ago
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
* (un)read list contains the same uniqueId, it will be removed, and
* the new one put in its place.
*/
11 years ago
db.incrObjectField('global', 'nextNid', function(err, nid) {
db.setAdd('notifications', nid);
db.setObject('notifications:' + nid, {
11 years ago
text: text || '',
path: path || null,
datetime: Date.now(),
uniqueId: uniqueId || utils.generateUUID()
}, function(err, status) {
if (!err) {
callback(nid);
}
});
11 years ago
});
};
11 years ago
11 years ago
function destroy(nid) {
var multi = RDB.multi();
11 years ago
multi.del('notifications:' + nid);
multi.srem('notifications', nid);
11 years ago
multi.exec(function(err) {
if (err) {
winston.error('Problem deleting expired notifications. Stack follows.');
winston.error(err.stack);
}
});
}
11 years ago
11 years ago
Notifications.push = function(nid, uids, callback) {
11 years ago
if (!Array.isArray(uids)) {
uids = [uids];
}
11 years ago
var numUids = uids.length,
x;
11 years ago
Notifications.get(nid, null, function(notif_data) {
for (x = 0; x < numUids; x++) {
if (parseInt(uids[x], 10) > 0) {
(function(uid) {
remove_by_uniqueId(notif_data.uniqueId, uid, function() {
11 years ago
db.sortedSetAdd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
11 years ago
websockets.in('uid_' + uid).emit('event:new_notification');
if (callback) {
callback(true);
}
});
})(uids[x]);
}
11 years ago
}
});
};
11 years ago
11 years ago
function remove_by_uniqueId(uniqueId, uid, callback) {
async.parallel([
function(next) {
11 years ago
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
11 years ago
if (nids && nids.length > 0) {
async.each(nids, function(nid, next) {
Notifications.get(nid, uid, function(nid_info) {
if (nid_info.uniqueId === uniqueId) {
11 years ago
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid);
11 years ago
}
next();
});
11 years ago
}, function(err) {
next();
11 years ago
});
} else {
next();
}
});
},
function(next) {
11 years ago
db.getSortedSetRange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) {
11 years ago
if (nids && nids.length > 0) {
async.each(nids, function(nid, next) {
Notifications.get(nid, uid, function(nid_info) {
if (nid_info && nid_info.uniqueId === uniqueId) {
11 years ago
db.sortedSetRemove('uid:' + uid + ':notifications:read', nid);
11 years ago
}
next();
});
11 years ago
}, function(err) {
next();
11 years ago
});
} else {
next();
}
});
}
], function(err) {
if (!err) {
callback(true);
}
});
}
11 years ago
11 years ago
Notifications.mark_read = function(nid, uid, callback) {
if (parseInt(uid) > 0) {
11 years ago
Notifications.get(nid, uid, function(notif_data) {
11 years ago
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid);
db.sortedSetAdd('uid:' + uid + ':notifications:read', notif_data.datetime, nid);
if (callback) {
callback();
}
});
}
11 years ago
}
11 years ago
11 years ago
Notifications.mark_read_multiple = function(nids, uid, callback) {
if (!Array.isArray(nids) && parseInt(nids, 10) > 0) {
nids = [nids];
}
11 years ago
async.each(nids, function(nid, next) {
Notifications.mark_read(nid, uid, function(err) {
if (!err) {
next(null);
}
});
11 years ago
}, function(err) {
if (callback) {
callback(err);
}
});
};
11 years ago
11 years ago
Notifications.mark_all_read = function(uid, callback) {
11 years ago
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
11 years ago
if (err) {
return callback(err);
}
11 years ago
if (nids.length > 0) {
Notifications.mark_read_multiple(nids, uid, function(err) {
callback(err);
});
} else {
callback();
}
11 years ago
});
};
11 years ago
11 years ago
Notifications.prune = function(cutoff) {
if (process.env.NODE_ENV === 'development') {
winston.info('[notifications.prune] Removing expired notifications from the database.');
}
11 years ago
var today = new Date(),
numPruned = 0;
11 years ago
if (!cutoff) {
cutoff = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
}
11 years ago
11 years ago
var cutoffTime = cutoff.getTime();
11 years ago
11 years ago
async.parallel({
"inboxes": function(next) {
RDB.keys('uid:*:notifications:unread', next);
},
"nids": function(next) {
11 years ago
db.getSetMembers('notifications', function(err, nids) {
11 years ago
async.filter(nids, function(nid, next) {
11 years ago
db.getObjectField('notifications:' + nid, 'datetime', function(err, datetime) {
11 years ago
if (parseInt(datetime, 10) < cutoffTime) {
next(true);
} else {
next(false);
}
11 years ago
});
11 years ago
}, function(expiredNids) {
next(null, expiredNids);
});
11 years ago
});
}
}, function(err, results) {
if (!err) {
var numInboxes = results.inboxes.length,
x;
11 years ago
11 years ago
async.eachSeries(results.nids, function(nid, next) {
var multi = RDB.multi();
11 years ago
11 years ago
for(x=0;x<numInboxes;x++) {
multi.zscore(results.inboxes[x], nid);
}
11 years ago
multi.exec(function(err, results) {
// If the notification is not present in any inbox, delete it altogether
var expired = results.every(function(present) {
if (present === null) {
return true;
}
});
11 years ago
if (expired) {
destroy(nid);
numPruned++;
}
11 years ago
next();
11 years ago
});
11 years ago
}, function(err) {
11 years ago
if (process.env.NODE_ENV === 'development') {
11 years ago
winston.info('[notifications.prune] Notification pruning completed. ' + numPruned + ' expired notification' + (numPruned !== 1 ? 's' : '') + ' removed.');
11 years ago
}
11 years ago
});
} else {
if (process.env.NODE_ENV === 'development') {
winston.error('[notifications.prune] Ran into trouble pruning expired notifications. Stack trace to follow.');
winston.error(err.stack);
11 years ago
}
11 years ago
}
});
};
11 years ago
11 years ago
}(exports));