This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
/
email.js
231 lines (208 loc) · 5.74 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
/* eslint
no-param-reassign: 0,
function-paren-newline: 0,
global-require: 0,
no-underscore-dangle: 0,
import/no-unresolved: 0 */
/* globals $H */
const name = 'email';
const pkgs = [
'email-validator',
'js-htmlencode',
'debug',
'lodash',
'email-me-when',
];
let error;
let _;
let encode;
let Shipments;
const renderInput = (values) => {
values = values || {};
return `
<style>
.email-list {
resize: vertical;
}
</style>
<p>Note: Duplicates email addresses aren't allowed among recipients.</p>
<label>From:
<span class="address-field">
<input
type=email
name=fromEmail
class="from-email"
placeholder="harbormaster@localhost"
required
value="harbormaster@localhost"
>
</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 (raw text):
<textarea
name=rawText
class="email-raw-text 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 `
<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 next = () => {
error = require('debug')(`${name}:error`);
_ = require('lodash');
encode = require('js-htmlencode').htmlEncode;
};
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 (
_.uniq(_.flatten(_.filter(
_.groupBy(toList, (n) => n),
(n) => n.length > 1,
))).length ||
_.uniq(_.flatten(_.filter(
_.groupBy(ccList, (n) => n),
(n) => n.length > 1,
))).length ||
_.uniq(_.flatten(_.filter(
_.groupBy(bccList, (n) => n),
(n) => n.length > 1,
))).length
) {
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 referencedValueText = text.replace(referenceRegex, (match, target) => {
const value = _.get(manifest, target);
if (value) return JSON.stringify(value, null, '\t');
return '';
});
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,
text: referencedText || manifest.rawText,
});
exitCode = 0;
} catch (err) {
error(JSON.stringify(err, null, '\t'));
const shipment = Shipments.findOne(manifest.shipment_id);
shipment.stderr.push(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,
};