first pass #1249 -- this causes emails to be sent if you have not been to the site in over 24 hours.

v1.18.x
Julian Lam 11 years ago
parent d027355ae9
commit c3a4bcb116

@ -54,7 +54,7 @@ module.exports = function(app, middleware, controllers) {
}); });
app.get('/test', function(req, res) { app.get('/test', function(req, res) {
require('../meta').sounds.init(); user.sendDailyDigests();
res.send(200); res.send(200);
}); });
}); });

@ -8,7 +8,6 @@ var db = require('../database'),
nconf = require('nconf'), nconf = require('nconf'),
user = require('../user'), user = require('../user'),
UserNotifications = require('./notifications'),
topics = require('../topics'), topics = require('../topics'),
emailer = require('../emailer'), emailer = require('../emailer'),
meta = require('../meta'); meta = require('../meta');
@ -23,6 +22,8 @@ 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);
@ -33,29 +34,32 @@ module.exports = function(User) {
}, function(err, data) { }, function(err, data) {
var now = new Date(); var now = new Date();
async.each(data.uids, function(uid, next) { // Consider using eachLimit, but *only* if people complain about email relays choking -- otherwise we're ok.
UserNotifications.getDailyUnread(uid, function(err, notifications) { User.getMultipleUserFields(data.uids, ['uid', 'username', 'lastonline'], function(err, receipients) {
if (!err && notifications && notifications.length) { // Find only those users who have not been online in the past 24 hours
var users = receipients.filter(function(userObj) {
return yesterday > parseInt(userObj.lastonline, 10);
});
async.each(users, 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) { for(var i=0; i<notifications.length; ++i) {
if (notifications[i].image.indexOf('http') !== 0) { if (notifications[i].image.indexOf('http') !== 0) {
notifications[i].image = nconf.get('url') + notifications[i].image; notifications[i].image = nconf.get('url') + notifications[i].image;
} }
} }
user.getUserField(uid, 'username', function(err, username) {
// Send daily digest email // Send daily digest email
// winston.info('[user/notifications] Sending Daily Digest to uid ' + uid); // winston.info('[user/notifications] Sending Daily Digest to uid ' + userObj.uid);
emailer.send('dailydigest', uid, { emailer.send('dailydigest', userObj.uid, {
subject: '[' + meta.config.title + '] Daily Digest for ' + now.getFullYear()+ '/' + (now.getMonth()+1) + '/' + now.getDate(), subject: '[' + meta.config.title + '] Daily Digest for ' + now.getFullYear()+ '/' + (now.getMonth()+1) + '/' + now.getDate(),
username: username, username: userObj.username,
url: nconf.get('url'), url: nconf.get('url'),
site_title: meta.config.title, site_title: meta.config.title,
notifications: notifications, notifications: notifications,
recent: data.recent.topics recent: data.recent.topics
}); });
});
}
next(err); next(err);
}); });
@ -68,6 +72,7 @@ module.exports = function(User) {
} }
}); });
}); });
});
}; };
}; };

Loading…
Cancel
Save