forked from getprodigy/harbormaster-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.js
executable file
·273 lines (247 loc) · 7.08 KB
/
email.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* eslint no-param-reassign: 0,
function-paren-newline: 0,
import/no-unresolved: 0,
no-underscore-dangle: 0 */
/* globals $H */
const name = 'email';
const pkgs = [
'email-validator',
'js-htmlencode',
'debug',
'lodash',
];
let log;
let error;
let _;
let encode;
let Shipments;
const renderInput = (values) => {
values = values || {};
return /*html*/`
<style>
.harbor textarea.email-list {
resize: vertical;
min-height: initial;
}
.harbor textarea.email-body {
min-height: 150px;
}
</style>
<p>Note: Duplicates email addresses aren't allowed within individual recipient types (To, CC, BCC).</p>
<p>To insert a value from a prior harbor's shipment manifest into the email use double brackets, e.g. <code>[[timestamp]]</code>, or for an unparsed value, use triple brackets, e.g. <code>[[[prior_manifest]]]</code></p>
<label>From:
<span class="address-field">
<input
type=email
name=fromEmail
class="from-email"
placeholder="harbormaster@localhost"
required
value="${values.fromEmail || ''}"
>
</span>
</label>
<label>To (one email per line):
<textarea
name=toEmailList
class="to-email-list email-list"
placeholder="foo@bar.com\nbaz@qux.net"
required
>${values.toEmailList || ''}</textarea>
</label>
<label>CC (one email per line):
<textarea
name=toCCList
class="to-cc-list email-list"
placeholder="foo@bar.com\nbaz@qux.net"
>${values.toCCList || ''}</textarea>
</label>
<label>BCC (one email per line):
<textarea
name=toBCCList
class="to-bcc-list email-list"
placeholder="foo@bar.com\nbaz@qux.net"
>${values.toBCCList || ''}</textarea>
</label>
<label>Reply To (one email per line):
<textarea
name=replyTo
class="to-email-list email-list"
placeholder="foo@bar.com\nbaz@qux.net"
>${values.replyTo || ''}</textarea>
</label>
<label>Subject:
<input
type=text
name=subject
class="email-subject"
placeholder="(no subject)"
value="${values.subject ? encode(values.subject) : ''}"
>
</label>
<label>Body (supports HTML):
<textarea
name=rawText
class="email-body email-list"
placeholder="(empty)"
required
>${values.rawText ? encode(values.rawText) : ''}</textarea>
</label>
<label>Include prior manifest?
<input
type=checkbox
name=includePriorManifest
class="include-prior-manifest"
${values.includePriorManifest ? 'checked' : ''}
>
</label>
`;
};
const renderWorkPreview = (manifest) => {
let includePriorManifest = false;
if (manifest.includePriorManifest) includePriorManifest = true;
return /*html*/`
<figure>
<figcaption>An email will be sent with the following details:</figcaption>
<p>From: <code>${manifest.fromEmail}</code></p>
<p>To: <code>${manifest.toEmailList}</code></p>
<p>CC: <code>${manifest.toCCList}</code></p>
<p>BCC: <code>${manifest.toBCCList}</code></p>
<p>Reply To: <code>${manifest.replyTo || manifest.fromEmail}</code></p>
<p>Subject: <code>${manifest.subject}</code></p>
<p>Body: <code>${manifest.rawText}</code></p>
<hr>
<p>Include prior manifest: ${includePriorManifest}</p>
</figure>
`;
};
const register = (lanes, users, harbors, shipments) => {
Shipments = shipments;
return { name, pkgs };
};
const hasDupes = list => {
if (
_.uniq( //4. remove duplicate dupe entries
_.filter( //3. filter by keys with more than one occurance
_.groupBy( //2. group by email as keys
_.flattenDeep(list), //1. flatten nested list
(n) => n
),
(n) => n.length > 1,
)
).length
) return true;
return false;
}
const checkDupes = (values) => {
const toList = values.toEmailList.split('\n');
const ccList = values.toCCList.length ?
values.toCCList.split('\n') :
[];
const bccList = values.toBCCList ?
values.toBCCList.split('\n') :
[];
if (hasDupes(toList) || hasDupes(ccList) || hasDupes(bccList)) {
return false;
}
return true;
};
const checkBody = (values) => {
if (values.rawText.length) return true;
return false;
};
const update = (lane, values) => {
if (
checkDupes(values) && checkBody(values)
) {
return true;
}
return false;
};
const fillReferenceText = (manifest, text) => {
const referenceRegex = /\[\[([a-zA-Z0-9_.:-]+)\]\]/g;
const strictReferenceRegex = /\[\[\[([a-zA-Z0-9_.:-]+)\]\]\]/g;
const priorExitCodeRegex = /\[\[prior_exit_code\]\]/g;
const priorFinishedRegex = /\[\[prior_finished\]\]/g;
const referencedValueText = text.replace(
strictReferenceRegex,
(match, target) => {
const value = JSON.stringify(_.get(manifest, target), null, '\t');
return value;
}
)
.replace(
priorExitCodeRegex,
(match, target) => {
if (!manifest.prior_manifest) return false;
const prior_shipment = Shipments.findOne(
manifest.prior_manifest.shipment_id
);
return prior_shipment.exit_code;
}
)
.replace(
priorFinishedRegex,
(match, target) => {
if (!manifest.prior_manifest) return false;
const prior_shipment = Shipments.findOne(
manifest.prior_manifest.shipment_id
);
return prior_shipment.finished;
}
)
.replace(
referenceRegex,
(match, target) => {
const value = _.get(manifest, target);
return value;
}
)
;
return referencedValueText;
};
const work = (lane, manifest) => {
let exitCode = 1;
if (manifest.includePriorManifest && manifest.prior_manifest) {
const priorManifestJson = JSON.stringify(
manifest.prior_manifest, null, '\t',
);
manifest.rawText += `\nPrior manifest:\n${priorManifestJson}`;
}
const referencedSubject = fillReferenceText(manifest, manifest.subject);
const referencedText = fillReferenceText(manifest, manifest.rawText);
try {
H.Email.send({
from: manifest.fromEmail,
to: manifest.toEmailList.split('\n'),
cc: manifest.toCCList.split('\n'),
bcc: manifest.toBCCList.split('\n'),
replyTo: manifest.replyTo.split('\n'),
subject: referencedSubject || manifest.subject,
html: referencedText || manifest.rawText,
});
exitCode = 0;
} catch (err) {
error(JSON.stringify(err, null, '\t'));
const shipment = Shipments.findOne(manifest.shipment_id);
const key = new Date();
shipment.stderr[key] = err;
Shipments.update(shipment._id, { $set: { stderr: shipment.stderr } });
manifest.error = err;
}
$H.call('Lanes#end_shipment', lane, exitCode, manifest);
};
module.exports = {
render_input: renderInput,
render_work_preview: renderWorkPreview,
register,
update,
work,
next: () => {
_ = require('lodash');
encode = require('js-htmlencode').htmlEncode;
log = require('debug')(`${name}:log`);
error = require('debug')(`${name}:error`);
log('Email harbor is ready.');
},
};