Files
NodeBB/src/user/digest.js

133 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
2014-11-17 13:37:07 -05:00
2016-11-23 17:15:31 +03:00
var async = require('async');
2016-04-01 15:12:01 +03:00
var winston = require('winston');
var nconf = require('nconf');
var db = require('../database');
var meta = require('../meta');
var user = require('../user');
var topics = require('../topics');
var plugins = require('../plugins');
var emailer = require('../emailer');
var utils = require('../utils');
2014-11-17 13:37:07 -05:00
(function (Digest) {
2016-11-23 15:52:35 +03:00
Digest.execute = function (interval, callback) {
callback = callback || function () {};
var digestsDisabled = parseInt(meta.config.disableEmailSubscriptions, 10) === 1;
2014-11-17 13:37:07 -05:00
if (digestsDisabled) {
winston.info('[user/jobs] Did not send digests (' + interval + ') because subscription system is disabled.');
2016-11-23 15:52:35 +03:00
return callback();
2014-11-17 13:37:07 -05:00
}
if (!interval) {
// interval is one of: day, week, month, or year
interval = 'day';
}
2016-11-23 15:52:35 +03:00
var subscribers;
async.waterfall([
function (next) {
async.parallel({
topics: async.apply(topics.getLatestTopics, 0, 0, 9, interval),
2017-02-17 19:31:21 -07:00
subscribers: async.apply(Digest.getSubscribers, interval),
2016-11-23 15:52:35 +03:00
}, next);
},
function (data, next) {
subscribers = data.subscribers;
if (!data.subscribers.length) {
return callback();
2015-02-01 20:21:18 -05:00
}
2016-11-23 15:52:35 +03:00
// 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;
if (user && user.picture && utils.isRelativeUrl(user.picture)) {
user.picture = nconf.get('base_url') + user.picture;
2014-11-17 13:37:07 -05:00
}
2016-11-23 15:52:35 +03:00
return topicObj;
2014-11-17 13:37:07 -05:00
});
2016-11-23 15:52:35 +03:00
data.interval = interval;
Digest.send(data, next);
2017-02-17 19:31:21 -07:00
},
2016-11-23 15:52:35 +03:00
], function (err) {
if (err) {
winston.error('[user/jobs] Could not send digests (' + interval + '): ' + err.message);
2014-11-17 13:37:07 -05:00
} else {
winston.info('[user/jobs] Digest (' + interval + ') scheduling completed. ' + subscribers.length + ' email(s) sent.');
2014-11-17 13:37:07 -05:00
}
2016-11-23 15:52:35 +03:00
callback(err);
2014-11-17 13:37:07 -05:00
});
};
Digest.getSubscribers = function (interval, callback) {
2016-11-23 17:15:31 +03:00
async.waterfall([
function (next) {
db.getSortedSetRange('digest:' + interval + ':uids', 0, -1, next);
},
function (subscribers, next) {
plugins.fireHook('filter:digest.subscribers', {
interval: interval,
2017-02-17 19:31:21 -07:00
subscribers: subscribers,
2016-11-23 17:15:31 +03:00
}, next);
},
function (results, next) {
next(null, results.subscribers);
2017-02-17 19:31:21 -07:00
},
2016-11-23 17:15:31 +03:00
], callback);
2014-11-17 13:37:07 -05:00
};
Digest.send = function (data, callback) {
2016-11-23 15:52:35 +03:00
if (!data || !data.subscribers || !data.subscribers.length) {
return callback();
}
2016-11-23 17:15:31 +03:00
var now = new Date();
2014-11-17 13:37:07 -05:00
2016-11-23 15:52:35 +03:00
async.waterfall([
function (next) {
user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline'], next);
},
function (users, next) {
async.eachLimit(users, 100, function (userObj, next) {
async.waterfall([
function (next) {
user.notifications.getDailyUnread(userObj.uid, next);
},
function (notifications, next) {
notifications = notifications.filter(Boolean);
// 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;
}
});
2016-11-23 15:52:35 +03:00
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,
2017-02-17 19:31:21 -07:00
interval: data.interval,
2016-11-23 15:52:35 +03:00
});
next();
2017-02-17 19:31:21 -07:00
},
2016-11-23 15:52:35 +03:00
], next);
}, next);
2017-02-17 19:31:21 -07:00
},
2016-11-23 15:52:35 +03:00
], function (err) {
callback(err);
2014-11-17 13:37:07 -05:00
});
2014-11-29 12:12:02 -05:00
};
}(module.exports));