-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathapi.js
More file actions
145 lines (123 loc) · 4.25 KB
/
api.js
File metadata and controls
145 lines (123 loc) · 4.25 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
import { Meteor } from 'meteor/meteor';
import { Email } from 'meteor/email';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import _ from 'underscore';
import s from 'underscore.string';
import juice from 'juice';
import stripHtml from 'string-strip-html';
import { settings } from '../../settings/server';
let contentHeader;
let contentFooter;
let body;
let Settings = {
get: () => {},
};
// define server language for email translations
// @TODO: change TAPi18n.__ function to use the server language by default
let lng = 'en';
settings.get('Language', (key, value) => {
lng = value || 'en';
});
export const replacekey = (str, key, value = '') => str.replace(new RegExp(`(\\[${ key }\\]|__${ key }__)`, 'igm'), s.escapeHTML(value));
export const translate = (str) => str.replace(/\{ ?([^\} ]+)(( ([^\}]+))+)? ?\}/gmi, (match, key) => TAPi18n.__(key, { lng }));
export const replace = function replace(str, data = {}) {
if (!str) {
return '';
}
const options = {
Site_Name: Settings.get('Site_Name'),
Site_URL: Settings.get('Site_Url'),
Site_URL_Slash: Settings.get('Site_Url').replace(/\/?$/, '/'),
...data.name && {
fname: s.strLeft(data.name, ' '),
lname: s.strRightBack(data.name, ' '),
},
...data,
};
return Object.entries(options).reduce((ret, [key, value]) => replacekey(ret, key, value), translate(str));
};
const nonEscapeKeys = ['room_path'];
export const replaceEscaped = (str, data = {}) => replace(str, {
Site_Name: s.escapeHTML(settings.get('Site_Name')),
Site_Url: s.escapeHTML(settings.get('Site_Url')),
...Object.entries(data).reduce((ret, [key, value]) => {
ret[key] = nonEscapeKeys.includes(key) ? value : s.escapeHTML(value);
return ret;
}, {}),
});
export const wrap = (html, data = {}) => {
if (settings.get('email_plain_text_only')) {
return replace(html, data);
}
return replaceEscaped(body.replace('{{body}}', html), data);
};
export const inlinecss = (html) => juice.inlineContent(html, Settings.get('email_style'));
export const getTemplate = (template, fn, escape = true) => {
let html = '';
Settings.get(template, (key, value) => {
html = value || '';
fn(escape ? inlinecss(html) : html);
});
Settings.get('email_style', () => {
fn(escape ? inlinecss(html) : html);
});
};
export const getTemplateWrapped = (template, fn) => {
let html = '';
const wrapInlineCSS = _.debounce(() => fn(wrap(inlinecss(html))), 100);
Settings.get('Email_Header', () => html && wrapInlineCSS());
Settings.get('Email_Footer', () => html && wrapInlineCSS());
Settings.get('email_style', () => html && wrapInlineCSS());
Settings.get(template, (key, value) => {
html = value || '';
return html && wrapInlineCSS();
});
};
export const setSettings = (s) => {
Settings = s;
getTemplate('Email_Header', (value) => {
contentHeader = replace(value || '');
body = inlinecss(`${ contentHeader } {{body}} ${ contentFooter }`);
}, false);
getTemplate('Email_Footer', (value) => {
contentFooter = replace(value || '');
body = inlinecss(`${ contentHeader } {{body}} ${ contentFooter }`);
}, false);
body = inlinecss(`${ contentHeader } {{body}} ${ contentFooter }`);
};
export const rfcMailPatternWithName = /^(?:.*<)?([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)(?:>?)$/;
export const checkAddressFormat = (from) => rfcMailPatternWithName.test(from);
export const sendNoWrap = ({ to, from, replyTo, subject, html, text, headers }) => {
if (!checkAddressFormat(to)) {
return;
}
if (!text) {
text = stripHtml(html);
}
if (settings.get('email_plain_text_only')) {
html = undefined;
}
Meteor.defer(() => Email.send({ to, from, replyTo, subject, html, text, headers }));
};
export const send = ({ to, from, replyTo, subject, html, text, data, headers }) =>
sendNoWrap({
to,
from,
replyTo,
subject: replace(subject, data),
text: text
? replace(text, data)
: stripHtml(replace(html, data)),
html: wrap(html, data),
headers,
});
export const checkAddressFormatAndThrow = (from, func) => {
if (checkAddressFormat(from)) {
return true;
}
throw new Meteor.Error('error-invalid-from-address', 'Invalid from address', {
function: func,
});
};
export const getHeader = () => contentHeader;
export const getFooter = () => contentFooter;