Skip to content

Node.js based forum software built for the modern web

closes #1813

barisusakli943f3814e5bb
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<notifications.length; ++i) {
- if (notifications[i].image.indexOf('http') !== 0) {
- notifications[i].image = nconf.get('url') + notifications[i].image;
- }
- }
+ sendEmails(subscribed, data.recent.topics);
+ });
+ });
+ };
- // Send daily digest email
- // winston.info('[user/notifications] Sending Daily Digest to uid ' + userObj.uid);
- emailer.send('dailydigest', userObj.uid, {
- subject: '[' + meta.config.title + '] Daily Digest for ' + now.getFullYear()+ '/' + (now.getMonth()+1) + '/' + now.getDate(),
- username: userObj.username,
- url: nconf.get('url'),
- site_title: meta.config.title || meta.config.browserTitle || 'NodeBB',
- notifications: notifications,
- recent: data.recent.topics
- });
+ function sendEmails(uids, recentTopics) {
+ var now = new Date();
- next(err);
- });
- }, function(err) {
- // When finished...
- if (!err) {
- winston.info('[user/jobs] Daily Digests sent!');
- } else {
+ User.getMultipleUserFields(uids, ['uid', 'username', 'lastonline'], function(err, users) {
+ if (err) {
+ return winston.error('[user/jobs] Could not send daily digests: ' + err.message);
+ }
+ // 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) {
+ if (err) {
winston.error('[user/jobs] Could not send daily digests: ' + err.message);
+ return next(err);
+ }
+ // Turn relative URLs into absolute ones
+ for(var i=0; i<notifications.length; ++i) {
+ if (notifications[i].image.indexOf('http') !== 0) {
+ notifications[i].image = nconf.get('url') + notifications[i].image;
+ }
}
+
+ // Send daily digest email
+ // winston.info('[user/notifications] Sending Daily Digest to uid ' + userObj.uid);
+ emailer.send('dailydigest', userObj.uid, {
+ subject: '[' + meta.config.title + '] Daily Digest for ' + now.getFullYear()+ '/' + (now.getMonth()+1) + '/' + now.getDate(),
+ username: userObj.username,
+ url: nconf.get('url'),
+ site_title: meta.config.title || meta.config.browserTitle || 'NodeBB',
+ notifications: notifications,
+ recent: recentTopics
+ });
+
+ next();
});
+ }, function(err) {
+ // When finished...
+ if (!err) {
+ winston.info('[user/jobs] Daily Digests sent!');
+ } else {
+ winston.error('[user/jobs] Could not send daily digests: ' + err.message);
+ }
});
});
- };
+ }
+
};