Files
NodeBB/src/user/digest.js

136 lines
4.1 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
2014-11-17 13:37:07 -05:00
2019-08-06 17:23:50 -04:00
const async = require('async');
const winston = require('winston');
const nconf = require('nconf');
2016-04-01 15:12:01 +03:00
2019-08-06 17:23:50 -04:00
const batch = require('../batch');
const meta = require('../meta');
const user = require('../user');
const topics = require('../topics');
const plugins = require('../plugins');
const emailer = require('../emailer');
const utils = require('../utils');
2014-11-17 13:37:07 -05:00
2019-08-06 17:23:50 -04:00
const Digest = module.exports;
2016-11-23 15:52:35 +03:00
Digest.execute = async function (payload) {
const 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.');
return;
}
let subscribers = payload.subscribers;
if (!subscribers) {
subscribers = await Digest.getSubscribers(payload.interval);
}
if (!subscribers.length) {
return;
}
try {
const count = await Digest.send({
interval: payload.interval,
subscribers: subscribers,
});
winston.info('[user/jobs] Digest (' + payload.interval + ') scheduling completed. ' + count + ' email(s) sent.');
} catch (err) {
winston.error('[user/jobs] Could not send digests (' + payload.interval + ')', err);
throw err;
2017-05-16 17:14:50 -04:00
}
};
2016-11-23 15:52:35 +03:00
Digest.getSubscribers = async function (interval) {
var subscribers = [];
2017-06-23 12:41:40 -04:00
await batch.processSortedSet('users:joindate', async function (uids) {
const settings = await user.getMultipleUserSettings(uids);
let subUids = [];
settings.forEach(function (hash) {
if (hash.dailyDigestFreq === interval) {
subUids.push(hash.uid);
}
});
subUids = await user.bans.filterBanned(subUids);
subscribers = subscribers.concat(subUids);
}, { interval: 1000 });
const results = await plugins.fireHook('filter:digest.subscribers', {
interval: interval,
subscribers: subscribers,
});
return results.subscribers;
2017-05-16 17:14:50 -04:00
};
2014-11-17 13:37:07 -05:00
Digest.send = async function (data) {
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) {
return emailsSent;
2017-05-16 17:14:50 -04:00
}
const now = new Date();
const users = await user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline']);
async.eachLimit(users, 100, async function (userObj) {
2019-08-06 17:23:50 -04:00
let [notifications, topicsData] = await Promise.all([
2019-07-22 18:36:29 -04:00
user.notifications.getUnreadInterval(userObj.uid, data.interval),
getTermTopics(data.interval, userObj.uid, 0, 9),
]);
notifications = notifications.filter(Boolean);
// If there are no notifications and no new topics, don't bother sending a digest
2019-08-06 17:23:50 -04:00
if (!notifications.length && !topicsData.length) {
return;
}
2016-11-23 15:52:35 +03:00
notifications.forEach(function (notification) {
if (notification.image && !notification.image.startsWith('http')) {
notification.image = nconf.get('url') + notification.image;
}
});
2017-07-06 15:42:37 -04:00
// Fix relative paths in topic data
2019-08-06 17:23:50 -04:00
topicsData = topicsData.map(function (topicObj) {
const 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;
});
emailsSent += 1;
emailer.send('digest', userObj.uid, {
subject: '[[email:digest.subject, ' + (now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate()) + ']]',
username: userObj.username,
userslug: userObj.userslug,
notifications: notifications,
2019-08-06 17:23:50 -04:00
recent: topicsData,
interval: data.interval,
showUnsubscribe: true,
}, function (err) {
if (err) {
winston.error('[user/jobs] Could not send digest email', err);
}
});
2017-05-16 17:14:50 -04:00
});
return emailsSent;
};
2018-02-20 12:52:59 -05:00
async function getTermTopics(term, uid, start, stop) {
const options = {
uid: uid,
start: start,
stop: stop,
term: term,
sort: 'posts',
teaserPost: 'last-post',
};
let data = await topics.getSortedTopics(options);
if (!data.topics.length) {
data = await topics.getLatestTopics(options);
2018-02-20 12:52:59 -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) + '...';
}
});
return data.topics;
}