-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspread.js
More file actions
179 lines (139 loc) · 4.42 KB
/
Copy pathspread.js
File metadata and controls
179 lines (139 loc) · 4.42 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var _ = require('underscore');
var async = require('async');
var moment = require('moment');
var config = require('../../config');
var mongo = require('../db/mongo')(config);
var mandrill = require('node-mandrill')(config.mandrill.token);
function spread(interval, recipients, callback) {
var current = moment().format('YYYY-MM-DD');
async.waterfall([
read,
prepare,
emails,
send
], callback);
function read(callback) {
mongo.pulse.findOne({date: current, interval: interval}, function (err, pulse) {
if (err) {
return callback(err);
}
if (!pulse) {
return callback({message: 'failed to get pulse', date: current});
}
callback(null, pulse.results);
});
}
function prepare(results, callback) {
var grouped = _.groupBy(results, function (item) {
return item.type;
});
var titles = Object.keys(grouped).map(function (key) {
return { name: 'BEST_OF_' + key.toUpperCase() + '_TITLE', content: either(_.first(grouped[key]), 'title', 'authorName') };
});
var descriptions = Object.keys(grouped).map(function (key) {
return { name: 'BEST_OF_' + key.toUpperCase() + '_DESCRIPTION', content: truncate(_.first(grouped[key]).description || '', 240) };
});
var urls = Object.keys(grouped).map(function (key) {
return { name: 'BEST_OF_' + key.toUpperCase() + '_URL', content: _.first(grouped[key]).url };
});
var likes = Object.keys(grouped).map(function (key) {
return { name: 'BEST_OF_' + key.toUpperCase() + '_LIKES', content: _.first(grouped[key]).likes };
});
var thumbnails = Object.keys(grouped).map(function (key) {
return { name: 'BEST_OF_' + key.toUpperCase() + '_THUMBNAIL', content: _.first(grouped[key]).thumbnail };
});
var mores = [
{ name: 'MORE', content: Math.floor((Math.random() * 20) + 24) }
];
var fields = _.union(titles, descriptions, urls, likes, thumbnails, mores);
var subject = createSubject(titles);
callback(null, {fields: fields, subject: subject});
function createSubject(titles) {
var titles = titles.map(function (title) {
return title.content;
}).join(', ');
return 'Likeastore: ' + titles + ' and more..';
}
}
function emails(message, callback) {
var users = {};
if (recipients === 'devs') {
return callback(null, {'info@likeastore.com': 1}, message);
} else if (recipients === 'users') {
return findUsers();
}
function findUsers() {
mongo.users.find({unsubscribed: {$exists: false}, firstTimeUser: {$exists: false}}).forEach(function (err, user) {
if (err) {
return callback(err);
}
if (!user) {
return callback(null, users, message);
}
users[user.email] = user._id.toString();
});
}
}
function send(users, message, callback) {
var emails = Object.keys(users);
var merge = emails.map(function (email) {
return {rcpt: email, vars: [{name: 'userid', content: users[email]}]};
});
async.map(splitToChunks(emails), pushToMandrill, callback);
function pushToMandrill(emailsChunk, callback) {
var to = emailsChunk.map(function (email) {
return { email: email };
});
mandrill('/messages/send-template', {
template_name: 'likeastore-pulse-weekly',
template_content: [],
message: {
global_merge_vars: message.fields,
subject: message.subject,
auto_html: null,
to: to,
preserve_recipients: false,
merge_vars: merge
},
}, callback);
}
}
function splitToChunks(arr) {
var chunks = [], size = 1024;
while (arr.length > 0) {
chunks.push(arr.splice(0, size));
}
return chunks;
}
function either() {
var o = arguments[0];
var p = Array.prototype.slice.call(arguments, 1);
return p.reduce(function (p, c) {
return o[p] || o[c];
});
}
function truncate(text, length, end) {
if (isNaN(length)) {
length = 100;
}
if (!end || typeof end !== 'string') {
end = "...";
}
if (text.length <= length || text.length - end.length <= length) {
return text;
} else {
return text.substring(0, length - end.length) + end;
}
}
}
function define(agenda) {
agenda.define('send weekly pulse developers', function (job, callback) {
spread('week', 'devs', callback);
});
agenda.define('send weekly pulse users', function (job, callback) {
spread('week', 'users', callback);
});
agenda.schedule('friday at 2pm', 'send weekly pulse developers').repeatEvery('1 week').save();
agenda.schedule('saturday at 5am', 'send weekly pulse users').repeatEvery('1 week').save();
}
module.exports = define;