mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
closed #2421
This commit is contained in:
@@ -17,7 +17,7 @@
|
|||||||
"digest.latest_topics": "Latest topics from %1",
|
"digest.latest_topics": "Latest topics from %1",
|
||||||
"digest.cta": "Click here to visit %1",
|
"digest.cta": "Click here to visit %1",
|
||||||
"digest.unsub.info": "This digest was sent to you due to your subscription settings.",
|
"digest.unsub.info": "This digest was sent to you due to your subscription settings.",
|
||||||
"digest.daily.no_topics": "There have been no active topics in the past day",
|
"digest.no_topics": "There have been no active topics in the past %1",
|
||||||
|
|
||||||
"notif.chat.subject": "New chat message received from %1",
|
"notif.chat.subject": "New chat message received from %1",
|
||||||
"notif.chat.cta": "Click here to continue the conversation",
|
"notif.chat.cta": "Click here to continue the conversation",
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ var async = require('async'),
|
|||||||
User.email = require('./user/email');
|
User.email = require('./user/email');
|
||||||
User.notifications = require('./user/notifications');
|
User.notifications = require('./user/notifications');
|
||||||
User.reset = require('./user/reset');
|
User.reset = require('./user/reset');
|
||||||
|
User.digest = require('./user/digest');
|
||||||
|
|
||||||
require('./user/auth')(User);
|
require('./user/auth')(User);
|
||||||
require('./user/create')(User);
|
require('./user/create')(User);
|
||||||
|
|||||||
110
src/user/digest.js
Normal file
110
src/user/digest.js
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
var async = require('async'),
|
||||||
|
winston = require('winston'),
|
||||||
|
nconf = require('nconf'),
|
||||||
|
|
||||||
|
db = require('../database'),
|
||||||
|
meta = require('../meta'),
|
||||||
|
user = require('../user'),
|
||||||
|
topics = require('../topics'),
|
||||||
|
batch = require('../batch'),
|
||||||
|
emailer = require('../emailer');
|
||||||
|
|
||||||
|
module.exports = (function(Digest) {
|
||||||
|
Digest.execute = function(interval) {
|
||||||
|
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) {
|
||||||
|
if (err) {
|
||||||
|
return winston.error('[user/jobs] Could not send digests (' + interval + '): ' + err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
data.interval = interval;
|
||||||
|
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
winston.verbose('[user/jobs] No users subscribing to digest (' + interval + '). Digest not sent.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Digest.getSubscribers = function(interval, callback) {
|
||||||
|
async.waterfall([
|
||||||
|
async.apply(db.getSortedSetRange, 'users:joindate', 0, -1),
|
||||||
|
async.apply(user.getMultipleUserSettings)
|
||||||
|
], function(err, userSettings) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
var subscribed = userSettings.filter(function(setting) {
|
||||||
|
return setting.dailyDigestFreq === interval;
|
||||||
|
}).map(function(setting) {
|
||||||
|
return setting.uid;
|
||||||
|
});
|
||||||
|
|
||||||
|
callback(null, subscribed);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Digest.send = function(data, callback) {
|
||||||
|
var now = new Date();
|
||||||
|
|
||||||
|
user.getMultipleUserFields(data.subscribers, ['uid', 'username', 'lastonline'], function(err, users) {
|
||||||
|
if (err) {
|
||||||
|
winston.error('[user/jobs] Could not send digests (' + interval + '): ' + err.message);
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
async.eachLimit(users, 100, function(userObj, next) {
|
||||||
|
user.notifications.getDailyUnread(userObj.uid, function(err, notifications) {
|
||||||
|
if (err) {
|
||||||
|
winston.error('[user/jobs] Could not send digests (' + interval + '): ' + err.message);
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
notifications = notifications.filter(Boolean);
|
||||||
|
|
||||||
|
for(var i=0; i<notifications.length; ++i) {
|
||||||
|
if (notifications[i].image.indexOf('http') !== 0) {
|
||||||
|
notifications[i].image = nconf.get('url') + notifications[i].image;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
emailer.send('digest', userObj.uid, {
|
||||||
|
subject: '[' + meta.config.title + '] Digest for ' + now.getFullYear()+ '/' + (now.getMonth()+1) + '/' + now.getDate(),
|
||||||
|
username: userObj.username,
|
||||||
|
url: nconf.get('url'),
|
||||||
|
site_title: meta.config.title || meta.config.browserTitle || 'NodeBB',
|
||||||
|
notifications: notifications,
|
||||||
|
recent: data.topics.topics,
|
||||||
|
interval: data.interval
|
||||||
|
});
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}, callback);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Digest;
|
||||||
|
})({});
|
||||||
103
src/user/jobs.js
103
src/user/jobs.js
@@ -1,104 +1,29 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var db = require('../database'),
|
var winston = require('winston'),
|
||||||
async = require('async'),
|
|
||||||
winston = require('winston'),
|
|
||||||
cronJob = require('cron').CronJob,
|
cronJob = require('cron').CronJob,
|
||||||
nconf = require('nconf'),
|
|
||||||
|
|
||||||
user = require('../user'),
|
user = require('../user'),
|
||||||
topics = require('../topics'),
|
meta = require('../meta');
|
||||||
emailer = require('../emailer'),
|
|
||||||
meta = require('../meta'),
|
|
||||||
batch = require('../batch');
|
|
||||||
|
|
||||||
module.exports = function(User) {
|
module.exports = function(User) {
|
||||||
User.startJobs = function() {
|
User.startJobs = function() {
|
||||||
winston.verbose('[user.startJobs] Registering User Jobs');
|
|
||||||
|
|
||||||
new cronJob('0 0 17 * * *', function() {
|
new cronJob('0 0 17 * * *', function() {
|
||||||
User.sendDailyDigests();
|
winston.verbose('[user.startJobs] Digest job (daily) scheduled.');
|
||||||
|
User.digest.execute('day');
|
||||||
|
}, null, true);
|
||||||
|
|
||||||
|
new cronJob('0 0 17 * * 0', function() {
|
||||||
|
winston.verbose('[user.startJobs] Digest job (weekly) scheduled.');
|
||||||
|
User.digest.execute('week');
|
||||||
|
}, null, true);
|
||||||
|
|
||||||
|
new cronJob('0 0 17 1 * *', function() {
|
||||||
|
winston.verbose('[user.startJobs] Digest job (monthly) scheduled.');
|
||||||
|
User.digest.execute('month');
|
||||||
}, null, true);
|
}, null, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
User.sendDailyDigests = function() {
|
|
||||||
var digestsDisabled = meta.config.disableEmailSubscriptions !== undefined && parseInt(meta.config.disableEmailSubscriptions, 10) === 1;
|
|
||||||
if (digestsDisabled) {
|
|
||||||
return winston.verbose('[user/jobs] Did not send daily digests because subscription system is disabled.');
|
|
||||||
}
|
|
||||||
|
|
||||||
topics.getLatestTopics(0, 0, 9, 'day', function(err, data) {
|
|
||||||
if (err) {
|
|
||||||
return winston.error('[user/jobs] Could not send daily digests: ' + err.message);
|
|
||||||
}
|
|
||||||
|
|
||||||
batch.processSortedSet('users:joindate', function(uids, next) {
|
|
||||||
User.getMultipleUserSettings(uids, function(err, userSettings) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var subscribed = userSettings.filter(function(setting) {
|
|
||||||
return setting.dailyDigestFreq === 'daily';
|
|
||||||
}).map(function(setting) {
|
|
||||||
return setting.uid;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!subscribed.length) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
sendEmails(subscribed, data.topics, next);
|
|
||||||
});
|
|
||||||
}, function(err) {
|
|
||||||
if (err) {
|
|
||||||
winston.error('[user/jobs] Could not send daily digests: ' + err.message);
|
|
||||||
} else {
|
|
||||||
winston.verbose('[user/jobs] Daily Digests sent!');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function sendEmails(uids, recentTopics, callback) {
|
|
||||||
var now = new Date();
|
|
||||||
|
|
||||||
User.getMultipleUserFields(uids, ['uid', 'username', 'lastonline'], function(err, users) {
|
|
||||||
if (err) {
|
|
||||||
winston.error('[user/jobs] Could not send daily digests: ' + err.message);
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
async.eachLimit(users, 100, function(userObj, next) {
|
|
||||||
user.notifications.getDailyUnread(userObj.uid, function(err, notifications) {
|
|
||||||
if (err) {
|
|
||||||
winston.error('[user/jobs] Could not send daily digests: ' + err.message);
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
notifications = notifications.filter(Boolean);
|
|
||||||
|
|
||||||
for(var i=0; i<notifications.length; ++i) {
|
|
||||||
if (notifications[i].image.indexOf('http') !== 0) {
|
|
||||||
notifications[i].image = nconf.get('url') + notifications[i].image;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
emailer.send('dailydigest', userObj.uid, {
|
|
||||||
subject: '[' + meta.config.title + '] Daily Digest for ' + now.getFullYear()+ '/' + (now.getMonth()+1) + '/' + now.getDate(),
|
|
||||||
username: userObj.username,
|
|
||||||
url: nconf.get('url'),
|
|
||||||
site_title: meta.config.title || meta.config.browserTitle || 'NodeBB',
|
|
||||||
notifications: notifications,
|
|
||||||
recent: recentTopics
|
|
||||||
});
|
|
||||||
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
}, callback);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
<!-- END recent -->
|
<!-- END recent -->
|
||||||
<!-- ELSE -->
|
<!-- ELSE -->
|
||||||
<li style="text-decoration: none; list-style-type: none; padding-bottom: 0.5em; font-style: italic;">
|
<li style="text-decoration: none; list-style-type: none; padding-bottom: 0.5em; font-style: italic;">
|
||||||
[[email:digest.daily.no_topics]]
|
[[email:digest.no_topics, {interval}]]
|
||||||
</li>
|
</li>
|
||||||
<!-- ENDIF recent.length -->
|
<!-- ENDIF recent.length -->
|
||||||
</ul>
|
</ul>
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
* {recent.title} ({url}/topic/{recent.slug})
|
* {recent.title} ({url}/topic/{recent.slug})
|
||||||
<!-- END recent -->
|
<!-- END recent -->
|
||||||
<!-- ELSE -->
|
<!-- ELSE -->
|
||||||
* [[email:digest.daily.no_topics]]
|
* [[email:digest.no_topics, {interval}]]
|
||||||
<!-- ENDIF recent.length -->
|
<!-- ENDIF recent.length -->
|
||||||
|
|
||||||
[[email:digest.cta, {site_title}]]: {url}
|
[[email:digest.cta, {site_title}]]: {url}
|
||||||
Reference in New Issue
Block a user