Files
NodeBB/src/user/digest.js

123 lines
3.9 KiB
JavaScript
Raw Normal View History

2014-11-17 13:37:07 -05:00
"use strict";
2016-04-01 15:12:01 +03:00
var async = require('async');
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('../../public/src/utils');
2014-11-17 13:37:07 -05:00
(function (Digest) {
Digest.execute = function (interval) {
2014-11-17 13:37:07 -05:00
var digestsDisabled = meta.config.disableEmailSubscriptions !== undefined && parseInt(meta.config.disableEmailSubscriptions, 10) === 1;
if (digestsDisabled) {
return winston.verbose('[user/jobs] Did not send digests (' + interval + ') because subscription system is disabled.');
}
if (!interval) {
// interval is one of: day, week, month, or year
interval = 'day';
}
async.parallel({
topics: async.apply(topics.getLatestTopics, 0, 0, 9, interval),
subscribers: async.apply(Digest.getSubscribers, interval)
}, function (err, data) {
2014-11-17 13:37:07 -05:00
if (err) {
return winston.error('[user/jobs] Could not send digests (' + interval + '): ' + err.message);
}
2015-02-01 20:21:18 -05:00
// Fix relative paths in topic data
data.topics.topics = data.topics.topics.map(function (topicObj) {
2015-06-08 13:55:14 -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;
2015-02-01 20:21:18 -05:00
}
return topicObj;
});
2014-11-17 13:37:07 -05:00
data.interval = interval;
if (data.subscribers.length) {
Digest.send(data, function (err) {
2014-11-17 13:37:07 -05:00
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.');
2014-11-17 13:37:07 -05:00
}
});
} else {
winston.verbose('[user/jobs] No users subscribing to digest (' + interval + '). Digest not sent.');
}
});
};
Digest.getSubscribers = function (interval, callback) {
db.getSortedSetRange('digest:' + interval + ':uids', 0, -1, function (err, subscribers) {
2016-08-16 19:46:59 +02:00
if (err) {
return callback(err);
}
plugins.fireHook('filter:digest.subscribers', {
interval: interval,
subscribers: subscribers
}, function (err, returnData) {
callback(err, returnData.subscribers);
});
});
2014-11-17 13:37:07 -05:00
};
Digest.send = function (data, callback) {
2014-11-17 13:37:07 -05:00
var now = new Date();
user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline'], function (err, users) {
2014-11-17 13:37:07 -05:00
if (err) {
2014-11-29 12:12:02 -05:00
winston.error('[user/jobs] Could not send digests (' + data.interval + '): ' + err.message);
2014-11-17 13:37:07 -05:00
return callback(err);
}
async.eachLimit(users, 100, function (userObj, next) {
user.notifications.getDailyUnread(userObj.uid, function (err, notifications) {
2014-11-17 13:37:07 -05:00
if (err) {
2014-11-29 12:12:02 -05:00
winston.error('[user/jobs] Could not send digests (' + data.interval + '): ' + err.message);
2014-11-17 13:37:07 -05:00
return next(err);
}
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();
}
2016-10-13 11:42:29 +02:00
for(var i = 0; i < notifications.length; ++i) {
Squashed commit of the following: commit 56582bc9eee5d81a01f42a28808b617b9c96873a Author: Julian Lam <julian@designcreateplay.com> Date: Tue Oct 27 05:21:11 2015 -0400 added missing template commit 6462a1626e7d8d77210b6e10eace5c9214335f33 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Oct 27 05:19:07 2015 -0400 sitemap index commit 3cfd56f1fbc8e03405dc394375bf5ff6eef21322 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Oct 27 04:47:52 2015 -0400 sitemap routes, controllers, and library methods for pages, categories, and topics commit e58e07c0881bdbe16d503b4679b85f761b02163c Author: Julian Lam <julian@designcreateplay.com> Date: Tue Oct 27 04:07:39 2015 -0400 added groups to sitemap commit 7ee584b6327ea79f4b1f465c32da28d01c75908f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Oct 27 01:43:06 2015 -0400 If notification dropdown is double-clicked, all notifications are marked read commit 488f147bef395233edb697a3895df0da7ee5ed0d Author: barisusakli <barisusakli@gmail.com> Date: Mon Oct 26 22:39:19 2015 -0400 closes #3781 commit 5e1bd58a022b1aaeedd7ab83c3655b3c16c611a0 Author: barisusakli <barisusakli@gmail.com> Date: Mon Oct 26 22:28:30 2015 -0400 closes #3782 commit 57d39802671612185e86977b5c237f850fe80206 Author: barisusakli <barisusakli@gmail.com> Date: Mon Oct 26 22:16:08 2015 -0400 closes #3790 commit 555c5b82da3ee30eab3fb21b2474eba834556741 Author: barisusakli <barisusakli@gmail.com> Date: Mon Oct 26 21:19:20 2015 -0400 check user settings commit 5454862c1c81525bb096466a547557cfb0f9a2b7 Author: barisusakli <barisusakli@gmail.com> Date: Mon Oct 26 20:26:02 2015 -0400 wait for all callbacks when creating tags commit 051c5077eb49937a8cac33b6b689c0902acfca51 Merge: 839fd93 e0e04ef Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Mon Oct 26 09:54:12 2015 -0400 Merge pull request #3792 from drlogout/master Fixed wrong method name in socket.io/groups.js from isAdmin to isAdmi… commit e0e04ef8920e61f3bf411903b5a5c1f5198e1698 Author: Christian Nolte <hello@noltech.net> Date: Mon Oct 26 14:50:32 2015 +0100 Fixed wrong method name in socket.io/groups.js from isAdmin to isAdministrator commit 839fd935ada8d6c88eb38adbd7435679a9103875 Author: barisusakli <barisusakli@gmail.com> Date: Sun Oct 25 21:54:35 2015 -0400 add back thread tools filter commit 37060bf1a3b75f6d74af9501344c4bd06d343db2 Merge: 5820a19 bf918bd Author: Barış Soner Uşaklı <barisusakli@gmail.com> Date: Sun Oct 25 18:13:06 2015 -0400 Merge pull request #3787 from cubehouse/patch-1 Upgrade script fails on some consoles commit 5820a193f60bcf0398ab1f981192cc1908f277cc Author: barisusakli <barisusakli@gmail.com> Date: Sun Oct 25 17:04:46 2015 -0400 closes #3789 commit 0d88d52557bf187c3b4d2b78f6da49d52952e9d1 Author: barisusakli <barisusakli@gmail.com> Date: Sun Oct 25 17:03:33 2015 -0400 up theme commit 9bc43ba5e1e8ca145615bd07fff1560bc7f3eb6e Author: barisusakli <barisusakli@gmail.com> Date: Sun Oct 25 16:57:42 2015 -0400 closes #3788 commit aafd4b698456864fb41c365d3202a7890643fd72 Author: barisusakli <barisusakli@gmail.com> Date: Sun Oct 25 15:56:17 2015 -0400 closes #3786 commit bf918bd016f645b9d307e4314943f787082b8627 Author: James Holding <cubehouse@users.noreply.github.com> Date: Sun Oct 25 10:14:00 2015 +0000 Upgrade script fails on some consoles The upgrade script errors/fails on some consoles if the stdout.columns isn't set (my console did this when upgrading a Docker instance of NodeBB). Checking for stdout.columns before using, falling back to a couple of spaces for slightly prettiness if we can't work out the console width.
2015-10-27 05:25:14 -04:00
if (notifications[i].image && notifications[i].image.indexOf('http') !== 0) {
2014-11-17 13:37:07 -05:00
notifications[i].image = nconf.get('url') + notifications[i].image;
}
}
emailer.send('digest', userObj.uid, {
2016-10-13 11:42:29 +02:00
subject: '[' + meta.config.title + '] [[email:digest.subject, ' + (now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate()) + ']]',
2014-11-17 13:37:07 -05:00
username: userObj.username,
userslug: userObj.userslug,
2014-11-17 13:37:07 -05:00
url: nconf.get('url'),
site_title: meta.config.title || meta.config.browserTitle || 'NodeBB',
notifications: notifications,
recent: data.topics.topics,
interval: data.interval
});
next();
});
}, callback);
});
2014-11-29 12:12:02 -05:00
};
2014-11-17 13:37:07 -05:00
2014-11-29 12:12:02 -05:00
}(module.exports));