-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple.js
74 lines (70 loc) · 1.66 KB
/
simple.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
const Message = require("./../message.js");
const path = require("path");
const mjml2html = require("mjml").default;
const assert = require("assert");
module.exports = async function SimpleTemplate(app, data) {
assert(data.text);
assert(data.subject);
assert(data.to);
assert(data.buttons === undefined || Array.isArray(data.buttons));
if (data.buttons === undefined) {
data.buttons = [];
}
const logo_cid = Math.floor(Math.random() * 10e6).toString();
let html;
if (app.ConfigManager.get("core").environment === "production") {
html = get_html(app, data, logo_cid);
} else {
html = "dummy";
}
const text = data.text + "\n\n" + buttons_to_text(data.buttons);
return new Message({
to: data.to,
subject: data.subject,
attachments: [
{
filename: path.basename(app.manifest.logo),
path: app.manifest.logo,
cid: logo_cid,
},
],
text,
html,
});
};
function buttons_to_text(buttons) {
return buttons
.map(button => `\t* ${button.text}: ${button.href}`)
.join("\n");
}
function get_html(app, data, logo_cid) {
const result = mjml2html(`
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-image width="100" src="cid:${logo_cid}"></mj-image>
<mj-divider border-color="${(app.manifest.colors &&
app.manifest.colors.primary) ||
"black"}"></mj-divider>
<mj-text>
<h1>
${data.subject}
</h1>
${data.text}
</mj-text>
${data.buttons.map(
button =>
`<mj-button href="${
button.href
}" font-size="20px" background-color="#0074D9">${
button.text
}</mj-button>`
)}
</mj-column>
</mj-section>
</mj-body>
</mjml>
`);
return result.html;
}