daily digest emails! resolved #326

v1.18.x
Julian Lam 11 years ago
parent e11efad0b7
commit 2b8a7c73ac

@ -123,7 +123,6 @@ function start() {
webserver = require('./src/webserver'), webserver = require('./src/webserver'),
sockets = require('./src/socket.io'), sockets = require('./src/socket.io'),
plugins = require('./src/plugins'), plugins = require('./src/plugins'),
notifications = require('./src/notifications'),
upgrade = require('./src/upgrade'); upgrade = require('./src/upgrade');
templates.setGlobal('relative_path', nconf.get('relative_path')); templates.setGlobal('relative_path', nconf.get('relative_path'));
@ -141,8 +140,6 @@ function start() {
webserver.init(); webserver.init();
}); });
notifications.init();
process.on('SIGTERM', shutdown); process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown); process.on('SIGINT', shutdown);
process.on('SIGHUP', restart); process.on('SIGHUP', restart);

@ -31,7 +31,7 @@ var bcrypt = require('bcryptjs'),
require('./user/delete')(User); require('./user/delete')(User);
require('./user/settings')(User); require('./user/settings')(User);
require('./user/search')(User); require('./user/search')(User);
require('./user/jobs')(User);
User.getUserField = function(uid, field, callback) { User.getUserField = function(uid, field, callback) {
db.getObjectField('user:' + uid, field, callback); db.getObjectField('user:' + uid, field, callback);

@ -0,0 +1,66 @@
'use strict';
var db = require('../database'),
async = require('async'),
winston = require('winston'),
cronJob = require('cron').CronJob,
nconf = require('nconf'),
user = require('../user'),
UserNotifications = require('./notifications'),
topics = require('../topics'),
emailer = require('../emailer'),
meta = require('../meta');
module.exports = function(User) {
User.startJobs = function() {
winston.info('[user.startJobs] Registering User Jobs');
new cronJob('0 0 17 * * *', function() {
User.sendDailyDigests();
}, null, true);
};
User.sendDailyDigests = function() {
async.parallel({
recent: function(next) {
topics.getLatestTopics(0, 0, 10, 'day', next);
},
uids: function(next) {
db.getSortedSetRange('users:joindate', 0, -1, next);
}
}, function(err, data) {
var now = new Date();
async.each(data.uids, function(uid, next) {
UserNotifications.getDailyUnread(uid, function(err, notifications) {
if (!err && notifications && notifications.length) {
user.getUserField(uid, 'username', function(err, username) {
// Send daily digest email
// winston.info('[user/notifications] Sending Daily Digest to uid ' + uid);
emailer.send('dailydigest', uid, {
subject: '[' + meta.config.title + '] Daily Digest for ' + now.getFullYear()+ '/' + (now.getMonth()+1) + '/' + now.getDate(),
username: username,
url: nconf.get('url'),
site_title: meta.config.title,
notifications: notifications,
recent: data.recent.topics
});
});
}
next(err);
});
}, function(err) {
// When finished...
if (!err) {
winston.info('[user/notifications] Daily Digests sent!');
} else {
winston.error('[user/notifications] Could not send daily digests: ' + err.message);
}
});
});
};
};

@ -5,14 +5,13 @@ var async = require('async'),
nconf = require('nconf'), nconf = require('nconf'),
winston = require('winston'), winston = require('winston'),
user = require('./../user'), user = require('../user'),
utils = require('./../../public/src/utils'), utils = require('../../public/src/utils'),
db = require('./../database'), db = require('../database'),
notifications = require('./../notifications'), notifications = require('../notifications'),
topics = require('./../topics'); topics = require('../topics');
(function(UserNotifications) { (function(UserNotifications) {
UserNotifications.get = function(uid, callback) { UserNotifications.get = function(uid, callback) {
function getNotifications(set, start, stop, iterator, done) { function getNotifications(set, start, stop, iterator, done) {
db.getSortedSetRevRange(set, start, stop, function(err, nids) { db.getSortedSetRevRange(set, start, stop, function(err, nids) {
@ -110,7 +109,18 @@ var async = require('async'),
}); });
}); });
}); });
};
UserNotifications.getDailyUnread = function(uid, callback) {
var now = Date.now(),
yesterday = now - (1000*60*60*24); // Approximate, can be more or less depending on time changes, makes no difference really.
db.getSortedSetRangeByScore(['uid:' + uid + ':notifications:unread', yesterday, now], function(err, nids) {
async.map(nids, function(nid, next) {
notifications.get(nid, uid, function(notif_data) {
next(null, notif_data);
});
}, callback);
});
}; };
UserNotifications.getUnreadCount = function(uid, callback) { UserNotifications.getUnreadCount = function(uid, callback) {

@ -12,6 +12,8 @@ var path = require('path'),
db = require('./database'), db = require('./database'),
auth = require('./routes/authentication'), auth = require('./routes/authentication'),
meta = require('./meta'), meta = require('./meta'),
user = require('./user'),
notifications = require('./notifications'),
logger = require('./logger'), logger = require('./logger'),
middleware = require('./middleware'), middleware = require('./middleware'),
routes = require('./routes'), routes = require('./routes'),
@ -34,6 +36,8 @@ if(nconf.get('ssl')) {
logger.init(app); logger.init(app);
auth.registerApp(app); auth.registerApp(app);
emailer.registerApp(app); emailer.registerApp(app);
notifications.init();
user.startJobs();
async.series({ async.series({
themesData: meta.themes.get, themesData: meta.themes.get,

Loading…
Cancel
Save