Files
NodeBB/src/user/digest.js

196 lines
5.2 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');
2017-06-23 12:41:40 -04:00
var batch = require('../batch');
2016-04-01 15:12:01 +03:00
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
2017-05-16 17:14:50 -04:00
var Digest = module.exports;
2016-11-23 15:52:35 +03:00
Digest.execute = function (payload, callback) {
2017-05-16 17:14:50 -04:00
callback = callback || function () {};
2014-11-17 13:37:07 -05:00
var digestsDisabled = meta.config.disableEmailSubscriptions === 1;
2017-05-16 17:14:50 -04:00
if (digestsDisabled) {
winston.info('[user/jobs] Did not send digests (' + payload.interval + ') because subscription system is disabled.');
2017-05-16 17:14:50 -04:00
return callback();
}
async.waterfall([
function (next) {
2017-07-06 15:42:37 -04:00
if (payload.subscribers) {
setImmediate(next, undefined, payload.subscribers);
} else {
Digest.getSubscribers(payload.interval, next);
}
2017-05-16 17:14:50 -04:00
},
2017-07-06 15:42:37 -04:00
function (subscribers, next) {
if (!subscribers.length) {
2017-05-16 17:14:50 -04:00
return callback();
}
2017-07-06 15:42:37 -04:00
var data = {
interval: payload.interval,
subscribers: subscribers,
};
2016-11-23 15:52:35 +03:00
2017-05-16 17:14:50 -04:00
Digest.send(data, next);
},
2017-07-06 15:42:37 -04:00
], function (err, count) {
2017-05-16 17:14:50 -04:00
if (err) {
winston.error('[user/jobs] Could not send digests (' + payload.interval + ')', err);
2017-05-16 17:14:50 -04:00
} else {
2017-07-06 15:42:37 -04:00
winston.info('[user/jobs] Digest (' + payload.interval + ') scheduling completed. ' + count + ' email(s) sent.');
2017-05-16 17:14:50 -04:00
}
2016-11-23 15:52:35 +03:00
2017-05-16 17:14:50 -04:00
callback(err);
});
};
2016-11-23 15:52:35 +03:00
2017-05-16 17:14:50 -04:00
Digest.getSubscribers = function (interval, callback) {
async.waterfall([
function (next) {
2017-06-23 12:41:40 -04:00
var subs = [];
batch.processSortedSet('users:joindate', function (uids, next) {
async.waterfall([
function (next) {
user.getMultipleUserSettings(uids, next);
},
function (settings, next) {
const subUids = [];
settings.forEach(function (hash) {
if (hash.dailyDigestFreq === interval) {
subUids.push(hash.uid);
}
});
user.bans.filterBanned(subUids, next);
},
function (uids, next) {
subs = subs.concat(uids);
next();
},
], next);
}, { interval: 1000 }, function (err) {
next(err, subs);
2017-06-23 11:03:54 -04:00
});
},
2017-05-16 17:14:50 -04:00
function (subscribers, next) {
plugins.fireHook('filter:digest.subscribers', {
interval: interval,
subscribers: subscribers,
}, next);
},
function (results, next) {
next(null, results.subscribers);
},
], callback);
};
2014-11-17 13:37:07 -05:00
2017-05-16 17:14:50 -04:00
Digest.send = function (data, callback) {
2018-01-23 12:02:43 -05:00
var emailsSent = 0;
2017-05-16 17:14:50 -04:00
if (!data || !data.subscribers || !data.subscribers.length) {
2018-01-23 12:02:43 -05:00
return callback(null, emailsSent);
2017-05-16 17:14:50 -04:00
}
var now = new Date();
2014-11-17 13:37:07 -05:00
2017-05-16 17:14:50 -04: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) {
2017-07-06 15:42:37 -04:00
async.parallel({
notifications: async.apply(user.notifications.getDailyUnread, userObj.uid),
2018-02-20 12:52:59 -05:00
topics: async.apply(getTermTopics, data.interval, userObj.uid, 0, 9),
2017-07-06 15:42:37 -04:00
}, next);
2017-05-16 17:14:50 -04:00
},
2017-07-06 15:42:37 -04:00
function (data, next) {
var notifications = data.notifications.filter(Boolean);
2017-05-16 17:14:50 -04:00
// If there are no notifications and no new topics, don't bother sending a digest
2018-02-20 12:52:59 -05:00
if (!notifications.length && !data.topics.length) {
2017-05-16 17:14:50 -04:00
return next();
}
2014-11-17 13:37:07 -05:00
2017-05-16 17:14:50 -04:00
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
}
2017-05-16 17:14:50 -04:00
});
2016-11-23 15:52:35 +03:00
2017-07-06 15:42:37 -04:00
// Fix relative paths in topic data
2018-02-20 12:52:59 -05:00
data.topics = data.topics.map(function (topicObj) {
2017-07-06 15:42:37 -04:00
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;
}
return topicObj;
});
2018-01-23 12:02:43 -05:00
emailsSent += 1;
2017-05-16 17:14:50 -04:00
emailer.send('digest', userObj.uid, {
2018-03-20 08:41:49 -04:00
subject: '[[email:digest.subject, ' + (now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate()) + ']]',
2017-05-16 17:14:50 -04:00
username: userObj.username,
userslug: userObj.userslug,
notifications: notifications,
2018-02-20 12:52:59 -05:00
recent: data.topics,
2017-05-16 17:14:50 -04:00
interval: data.interval,
showUnsubscribe: true,
2017-11-10 10:20:47 -05:00
}, function (err) {
if (err) {
winston.error('[user/jobs] Could not send digest email', err);
}
2017-05-16 17:14:50 -04:00
});
next();
},
], next);
}, next);
},
], function (err) {
2018-01-23 12:02:43 -05:00
callback(err, emailsSent);
2017-05-16 17:14:50 -04:00
});
2018-02-20 12:52:59 -05:00
function getTermTopics(term, uid, start, stop, callback) {
2019-02-22 14:39:05 -05:00
const options = {
uid: uid,
start: start,
stop: stop,
term: term,
sort: 'posts',
teaserPost: 'last-post',
};
2018-02-20 12:52:59 -05:00
async.waterfall([
function (next) {
2019-02-22 14:39:05 -05:00
topics.getSortedTopics(options, next);
2018-02-20 12:52:59 -05:00
},
function (data, next) {
if (!data.topics.length) {
2019-02-22 14:39:05 -05:00
topics.getLatestTopics(options, next);
2018-02-20 12:52:59 -05:00
} else {
next(null, data);
}
},
(data, next) => {
2019-02-22 14:39:05 -05:00
data.topics.forEach(function (topicObj) {
if (topicObj && topicObj.teaser && topicObj.teaser.content && topicObj.teaser.content.length > 255) {
topicObj.teaser.content = topicObj.teaser.content.slice(0, 255) + '...';
}
});
2019-02-22 14:39:05 -05:00
next(null, data.topics);
2018-02-20 12:52:59 -05:00
},
], callback);
}
2017-05-16 17:14:50 -04:00
};