v1.18.x
barisusakli 11 years ago
parent 49d29db347
commit 943f3814e5

@ -22,8 +22,6 @@ module.exports = function(User) {
}; };
User.sendDailyDigests = function() { User.sendDailyDigests = function() {
var yesterday = Date.now() - (1000*60*60*24);
async.parallel({ async.parallel({
recent: function(next) { recent: function(next) {
topics.getLatestTopics(0, 0, 10, 'day', next); topics.getLatestTopics(0, 0, 10, 'day', next);
@ -32,35 +30,40 @@ module.exports = function(User) {
db.getSortedSetRange('users:joindate', 0, -1, next); db.getSortedSetRange('users:joindate', 0, -1, next);
} }
}, function(err, data) { }, function(err, data) {
var now = new Date(); if (err) {
return winston.error('[user/jobs] Could not send daily digests: ' + err.message);
}
async.parallel({ User.getMultipleUserSettings(data.uids, function(err, userSettings) {
recipients: function(next) { if (err) {
User.getMultipleUserFields(data.uids, ['uid', 'username', 'lastonline'], next); return winston.error('[user/jobs] Could not send daily digests: ' + err.message);
},
userSettings: function(next) {
User.getMultipleUserSettings(data.uids, next);
} }
}, function(err, users) {
var recipients = users.recipients,
userSettings = users.userSettings,
subscribed;
// Find uids subscribed to daily digest emails var subscribed = userSettings.filter(function(setting) {
subscribed = userSettings.filter(function(setting) {
return setting.dailyDigestFreq === 'daily'; return setting.dailyDigestFreq === 'daily';
}).map(function(setting) { }).map(function(setting) {
return setting.uid; return setting.uid;
}); });
// Find only those users who have not been online in the past 24 hours sendEmails(subscribed, data.recent.topics);
users = recipients.filter(function(userObj) { });
return subscribed.indexOf(userObj.uid) !== -1 && yesterday > parseInt(userObj.lastonline, 10);
}); });
};
function sendEmails(uids, recentTopics) {
var now = new Date();
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. // Consider using eachLimit, but *only* if people complain about email relays choking -- otherwise we're ok.
async.eachLimit(users, 100, function(userObj, next) { async.eachLimit(users, 100, function(userObj, next) {
user.notifications.getDailyUnread(userObj.uid, function(err, notifications) { 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 // Turn relative URLs into absolute ones
for(var i=0; i<notifications.length; ++i) { for(var i=0; i<notifications.length; ++i) {
if (notifications[i].image.indexOf('http') !== 0) { if (notifications[i].image.indexOf('http') !== 0) {
@ -76,10 +79,10 @@ module.exports = function(User) {
url: nconf.get('url'), url: nconf.get('url'),
site_title: meta.config.title || meta.config.browserTitle || 'NodeBB', site_title: meta.config.title || meta.config.browserTitle || 'NodeBB',
notifications: notifications, notifications: notifications,
recent: data.recent.topics recent: recentTopics
}); });
next(err); next();
}); });
}, function(err) { }, function(err) {
// When finished... // When finished...
@ -90,7 +93,7 @@ module.exports = function(User) {
} }
}); });
}); });
}); }
};
}; };

Loading…
Cancel
Save