digest test

v1.18.x
barisusakli 8 years ago
parent 60b3db7b34
commit f754c8b7df

@ -13,48 +13,54 @@ var emailer = require('../emailer');
var utils = require('../../public/src/utils'); var utils = require('../../public/src/utils');
(function (Digest) { (function (Digest) {
Digest.execute = function (interval) { Digest.execute = function (interval, callback) {
var digestsDisabled = meta.config.disableEmailSubscriptions !== undefined && parseInt(meta.config.disableEmailSubscriptions, 10) === 1; callback = callback || function () {};
var digestsDisabled = parseInt(meta.config.disableEmailSubscriptions, 10) === 1;
if (digestsDisabled) { if (digestsDisabled) {
return winston.verbose('[user/jobs] Did not send digests (' + interval + ') because subscription system is disabled.'); winston.verbose('[user/jobs] Did not send digests (' + interval + ') because subscription system is disabled.');
return callback();
} }
if (!interval) { if (!interval) {
// interval is one of: day, week, month, or year // interval is one of: day, week, month, or year
interval = 'day'; interval = 'day';
} }
var subscribers;
async.parallel({ async.waterfall([
topics: async.apply(topics.getLatestTopics, 0, 0, 9, interval), function (next) {
subscribers: async.apply(Digest.getSubscribers, interval) async.parallel({
}, function (err, data) { topics: async.apply(topics.getLatestTopics, 0, 0, 9, interval),
if (err) { subscribers: async.apply(Digest.getSubscribers, interval)
return winston.error('[user/jobs] Could not send digests (' + interval + '): ' + err.message); }, next);
} },
function (data, next) {
// Fix relative paths in topic data subscribers = data.subscribers;
data.topics.topics = data.topics.topics.map(function (topicObj) { if (!data.subscribers.length) {
var user = topicObj.hasOwnProperty('teaser') && topicObj.teaser !== undefined ? topicObj.teaser.user : topicObj.user; return callback();
if (user && user.picture && utils.isRelativeUrl(user.picture)) {
user.picture = nconf.get('base_url') + user.picture;
} }
return topicObj; // Fix relative paths in topic data
}); data.topics.topics = data.topics.topics.map(function (topicObj) {
var user = topicObj.hasOwnProperty('teaser') && topicObj.teaser !== undefined ? topicObj.teaser.user : topicObj.user;
data.interval = interval; if (user && user.picture && utils.isRelativeUrl(user.picture)) {
user.picture = nconf.get('base_url') + user.picture;
if (data.subscribers.length) {
Digest.send(data, function (err) {
if (err) {
winston.error('[user/jobs] Could not send digests (' + interval + '): ' + err.message);
} else {
winston.info('[user/jobs] Digest (' + interval + ') scheduling completed. ' + data.subscribers.length + ' email(s) sent.');
} }
return topicObj;
}); });
data.interval = interval;
Digest.send(data, next);
}
], function (err) {
if (err) {
winston.error('[user/jobs] Could not send digests (' + interval + '): ' + err.message);
} else { } else {
winston.verbose('[user/jobs] No users subscribing to digest (' + interval + '). Digest not sent.'); winston.info('[user/jobs] Digest (' + interval + ') scheduling completed. ' + subscribers.length + ' email(s) sent.');
} }
callback(err);
}); });
}; };
@ -74,48 +80,50 @@ var utils = require('../../public/src/utils');
}; };
Digest.send = function (data, callback) { Digest.send = function (data, callback) {
if (!data || !data.subscribers || !data.subscribers.length) {
return callback();
}
var now = new Date(); var now = new Date();
user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline'], function (err, users) { async.waterfall([
if (err) { function (next) {
winston.error('[user/jobs] Could not send digests (' + data.interval + '): ' + err.message); user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline'], next);
return callback(err); },
} function (users, next) {
async.eachLimit(users, 100, function (userObj, next) {
async.eachLimit(users, 100, function (userObj, next) { async.waterfall([
user.notifications.getDailyUnread(userObj.uid, function (err, notifications) { function (next) {
if (err) { user.notifications.getDailyUnread(userObj.uid, next);
winston.error('[user/jobs] Could not send digests (' + data.interval + '): ' + err.message); },
return next(err); function (notifications, next) {
} notifications = notifications.filter(Boolean);
// If there are no notifications and no new topics, don't bother sending a digest
notifications = notifications.filter(Boolean); if (!notifications.length && !data.topics.topics.length) {
return next();
// If there are no notifications and no new topics, don't bother sending a digest }
if (!notifications.length && !data.topics.topics.length) {
return next(); notifications.forEach(function (notification) {
} if (notification.image && !notification.image.startsWith('http')) {
notification.image = nconf.get('url') + notification.image;
for(var i = 0; i < notifications.length; ++i) { }
if (notifications[i].image && notifications[i].image.indexOf('http') !== 0) { });
notifications[i].image = nconf.get('url') + notifications[i].image; emailer.send('digest', userObj.uid, {
subject: '[' + meta.config.title + '] [[email:digest.subject, ' + (now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate()) + ']]',
username: userObj.username,
userslug: userObj.userslug,
url: nconf.get('url'),
site_title: meta.config.title || meta.config.browserTitle || 'NodeBB',
notifications: notifications,
recent: data.topics.topics,
interval: data.interval
});
next();
} }
} ], next);
}, next);
emailer.send('digest', userObj.uid, { }
subject: '[' + meta.config.title + '] [[email:digest.subject, ' + (now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate()) + ']]', ], function (err) {
username: userObj.username, callback(err);
userslug: userObj.userslug,
url: nconf.get('url'),
site_title: meta.config.title || meta.config.browserTitle || 'NodeBB',
notifications: notifications,
recent: data.topics.topics,
interval: data.interval
});
next();
});
}, callback);
}); });
}; };

@ -574,6 +574,16 @@ describe('User', function () {
}); });
it('should send digests', function (done) {
User.updateDigestSetting(testUid, 'day', function (err) {
assert.ifError(err);
User.digest.execute('day', function (err) {
assert.ifError(err);
done();
});
});
});
after(function (done) { after(function (done) {

Loading…
Cancel
Save