/
digest.js
157 lines (140 loc) · 4.34 KB
/
digest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
var async = require('async');
var winston = require('winston');
var nconf = require('nconf');
var batch = require('../batch');
var meta = require('../meta');
var user = require('../user');
var topics = require('../topics');
var plugins = require('../plugins');
var emailer = require('../emailer');
var utils = require('../utils');
var Digest = module.exports;
Digest.execute = function (payload, callback) {
callback = callback || function () {};
var digestsDisabled = parseInt(meta.config.disableEmailSubscriptions, 10) === 1;
if (digestsDisabled) {
winston.info('[user/jobs] Did not send digests (' + payload.interval + ') because subscription system is disabled.');
return callback();
}
async.waterfall([
function (next) {
if (payload.subscribers) {
setImmediate(next, undefined, payload.subscribers);
} else {
Digest.getSubscribers(payload.interval, next);
}
},
function (subscribers, next) {
if (!subscribers.length) {
return callback();
}
var data = {
interval: payload.interval,
subscribers: subscribers,
};
Digest.send(data, next);
},
], function (err, count) {
if (err) {
winston.error('[user/jobs] Could not send digests (' + payload.interval + ')', err);
} else {
winston.info('[user/jobs] Digest (' + payload.interval + ') scheduling completed. ' + count + ' email(s) sent.');
}
callback(err);
});
};
Digest.getSubscribers = function (interval, callback) {
async.waterfall([
function (next) {
var subs = [];
batch.processSortedSet('users:joindate', function (uids, next) {
async.waterfall([
function (next) {
user.getMultipleUserSettings(uids, next);
},
function (settings, next) {
settings.forEach(function (hash) {
if (hash.dailyDigestFreq === interval) {
subs.push(hash.uid);
}
});
next();
},
], next);
}, { interval: 1000 }, function (err) {
next(err, subs);
});
},
function (subscribers, next) {
plugins.fireHook('filter:digest.subscribers', {
interval: interval,
subscribers: subscribers,
}, next);
},
function (results, next) {
next(null, results.subscribers);
},
], callback);
};
Digest.send = function (data, callback) {
var emailsSent = 0;
if (!data || !data.subscribers || !data.subscribers.length) {
return callback(null, emailsSent);
}
var now = new Date();
async.waterfall([
function (next) {
user.getUsersFields(data.subscribers, ['uid', 'username', 'userslug', 'lastonline'], next);
},
function (users, next) {
async.eachLimit(users, 100, function (userObj, next) {
async.waterfall([
function (next) {
async.parallel({
notifications: async.apply(user.notifications.getDailyUnread, userObj.uid),
topics: async.apply(topics.getPopular, data.interval, userObj.uid, 10),
}, next);
},
function (data, next) {
var notifications = data.notifications.filter(Boolean);
// If there are no notifications and no new topics, don't bother sending a digest
if (!notifications.length && !data.topics.length) {
return next();
}
notifications.forEach(function (notification) {
if (notification.image && !notification.image.startsWith('http')) {
notification.image = nconf.get('url') + notification.image;
}
});
// Fix relative paths in topic data
data.topics = data.topics.map(function (topicObj) {
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;
}
return topicObj;
});
emailsSent += 1;
emailer.send('digest', userObj.uid, {
subject: '[' + meta.config.title + '] [[email:digest.subject, ' + (now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate()) + ']]',
username: userObj.username,
userslug: userObj.userslug,
notifications: notifications,
recent: data.topics,
interval: data.interval,
showUnsubscribe: true,
}, function (err) {
if (err) {
winston.error('[user/jobs] Could not send digest email', err);
}
});
next();
},
], next);
}, next);
},
], function (err) {
callback(err, emailsSent);
});
};