From c3a4bcb116f4143f074bfe678baadd22fdd8ba47 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 15 Apr 2014 21:45:36 -0400 Subject: [PATCH] first pass #1249 -- this causes emails to be sent if you have not been to the site in over 24 hours. --- src/routes/debug.js | 2 +- src/user/jobs.js | 53 +++++++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/routes/debug.js b/src/routes/debug.js index 6c03961251..c49dfc7c2b 100644 --- a/src/routes/debug.js +++ b/src/routes/debug.js @@ -54,7 +54,7 @@ module.exports = function(app, middleware, controllers) { }); app.get('/test', function(req, res) { - require('../meta').sounds.init(); + user.sendDailyDigests(); res.send(200); }); }); diff --git a/src/user/jobs.js b/src/user/jobs.js index 602b889039..c28519eecf 100644 --- a/src/user/jobs.js +++ b/src/user/jobs.js @@ -8,7 +8,6 @@ var db = require('../database'), nconf = require('nconf'), user = require('../user'), - UserNotifications = require('./notifications'), topics = require('../topics'), emailer = require('../emailer'), meta = require('../meta'); @@ -23,6 +22,8 @@ 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); @@ -33,39 +34,43 @@ module.exports = function(User) { }, function(err, data) { var now = new Date(); - async.each(data.uids, function(uid, next) { - UserNotifications.getDailyUnread(uid, function(err, notifications) { - if (!err && notifications && notifications.length) { + // Consider using eachLimit, but *only* if people complain about email relays choking -- otherwise we're ok. + User.getMultipleUserFields(data.uids, ['uid', 'username', 'lastonline'], function(err, receipients) { + // 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