closed #2421
parent
2e2995d3bd
commit
d51683f62e
@ -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;
|
||||||
|
})({});
|
@ -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);
|
}, 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) {
|
new cronJob('0 0 17 * * 0', function() {
|
||||||
return setting.dailyDigestFreq === 'daily';
|
winston.verbose('[user.startJobs] Digest job (weekly) scheduled.');
|
||||||
}).map(function(setting) {
|
User.digest.execute('week');
|
||||||
return setting.uid;
|
}, null, true);
|
||||||
});
|
|
||||||
|
|
||||||
if (!subscribed.length) {
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
sendEmails(subscribed, data.topics, next);
|
new cronJob('0 0 17 1 * *', function() {
|
||||||
});
|
winston.verbose('[user.startJobs] Digest job (monthly) scheduled.');
|
||||||
}, function(err) {
|
User.digest.execute('month');
|
||||||
if (err) {
|
}, null, true);
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue