diff --git a/src/user/jobs.js b/src/user/jobs.js index 2e62a9387d..7659a87aea 100644 --- a/src/user/jobs.js +++ b/src/user/jobs.js @@ -22,8 +22,6 @@ module.exports = function(User) { }; User.sendDailyDigests = function() { - var yesterday = Date.now() - (1000*60*60*24); - async.parallel({ recent: function(next) { topics.getLatestTopics(0, 0, 10, 'day', next); @@ -32,65 +30,70 @@ module.exports = function(User) { db.getSortedSetRange('users:joindate', 0, -1, next); } }, function(err, data) { - var now = new Date(); + if (err) { + return winston.error('[user/jobs] Could not send daily digests: ' + err.message); + } - async.parallel({ - recipients: function(next) { - User.getMultipleUserFields(data.uids, ['uid', 'username', 'lastonline'], next); - }, - userSettings: function(next) { - User.getMultipleUserSettings(data.uids, next); + User.getMultipleUserSettings(data.uids, function(err, userSettings) { + if (err) { + return winston.error('[user/jobs] Could not send daily digests: ' + err.message); } - }, function(err, users) { - var recipients = users.recipients, - userSettings = users.userSettings, - subscribed; - // Find uids subscribed to daily digest emails - subscribed = userSettings.filter(function(setting) { + var subscribed = userSettings.filter(function(setting) { return setting.dailyDigestFreq === 'daily'; }).map(function(setting) { return setting.uid; }); - // Find only those users who have not been online in the past 24 hours - users = recipients.filter(function(userObj) { - return subscribed.indexOf(userObj.uid) !== -1 && yesterday > parseInt(userObj.lastonline, 10); - }); - - // Consider using eachLimit, but *only* if people complain about email relays choking -- otherwise we're ok. - async.eachLimit(users, 100, function(userObj, next) { - user.notifications.getDailyUnread(userObj.uid, function(err, notifications) { - // Turn relative URLs into absolute ones - for(var i=0; i