-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
Copy pathemail.js
194 lines (160 loc) · 5.7 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
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import s from 'underscore.string';
import * as Mailer from '../../../../mailer';
import { settings } from '../../../../settings';
import { roomTypes } from '../../../../utils';
import { metrics } from '../../../../metrics';
import { callbacks } from '../../../../callbacks';
import { getURL } from '../../../../utils/server';
let advice = '';
let goToMessage = '';
Meteor.startup(() => {
settings.get('email_style', function() {
goToMessage = Mailer.inlinecss('<p><a class=\'btn\' href="[room_path]">{Offline_Link_Message}</a></p>');
});
Mailer.getTemplate('Email_Footer_Direct_Reply', (value) => {
advice = value;
});
});
function getEmailContent({ message, user, room }) {
const lng = (user && user.language) || settings.get('Language') || 'en';
const roomName = s.escapeHTML(`#${ roomTypes.getRoomName(room.t, room) }`);
const userName = s.escapeHTML(settings.get('UI_Use_Real_Name') ? message.u.name || message.u.username : message.u.username);
const header = TAPi18n.__(room.t === 'd' ? 'User_sent_a_message_to_you' : 'User_sent_a_message_on_channel', {
username: userName,
channel: roomName,
lng,
});
if (message.msg !== '') {
if (!settings.get('Email_notification_show_message')) {
return header;
}
let messageContent = s.escapeHTML(message.msg);
if (message.t === 'e2e') {
messageContent = TAPi18n.__('Encrypted_message', { lng });
}
message = callbacks.run('renderMessage', message);
if (message.tokens && message.tokens.length > 0) {
message.tokens.forEach((token) => {
token.text = token.text.replace(/([^\$])(\$[^\$])/gm, '$1$$$2');
messageContent = messageContent.replace(token.token, token.text);
});
}
return `${ header }:<br/><br/>${ messageContent.replace(/\n/gm, '<br/>') }`;
}
if (message.file) {
const fileHeader = TAPi18n.__(room.t === 'd' ? 'User_uploaded_a_file_to_you' : 'User_uploaded_a_file_on_channel', {
username: userName,
channel: roomName,
lng,
});
if (!settings.get('Email_notification_show_message')) {
return fileHeader;
}
let content = `${ s.escapeHTML(message.file.name) }`;
if (message.attachments && message.attachments.length === 1 && message.attachments[0].description !== '') {
content += `<br/><br/>${ s.escapeHTML(message.attachments[0].description) }`;
}
return `${ fileHeader }:<br/><br/>${ content }`;
}
if (!settings.get('Email_notification_show_message')) {
return header;
}
if (Array.isArray(message.attachments) && message.attachments.length > 0) {
const [attachment] = message.attachments;
let content = '';
if (attachment.title) {
content += `${ s.escapeHTML(attachment.title) }<br/>`;
}
if (attachment.text) {
content += `${ s.escapeHTML(attachment.text) }<br/>`;
}
return `${ header }:<br/><br/>${ content }`;
}
return header;
}
const getButtonUrl = (room, subscription, message) => {
const path = `${ s.ltrim(roomTypes.getRelativePath(room.t, subscription), '/') }?msg=${ message._id }`;
return getURL(path, {
full: true,
cloud: settings.get('Offline_Message_Use_DeepLink'),
cloud_route: 'room',
cloud_params: {
rid: room._id,
mid: message._id,
},
});
};
export function sendEmail({ message, user, subscription, room, emailAddress, hasMentionToUser }) {
const username = settings.get('UI_Use_Real_Name') ? message.u.name || message.u.username : message.u.username;
let subjectKey = 'Offline_Mention_All_Email';
if (room.t === 'd') {
subjectKey = 'Offline_DM_Email';
} else if (hasMentionToUser) {
subjectKey = 'Offline_Mention_Email';
}
const emailSubject = Mailer.replace(settings.get(subjectKey), {
user: username,
room: roomTypes.getRoomName(room.t, room),
});
const content = getEmailContent({
message,
user,
room,
});
const room_path = getButtonUrl(room, subscription, message);
const email = {
to: emailAddress,
subject: emailSubject,
html: content + goToMessage + (settings.get('Direct_Reply_Enable') ? advice : ''),
data: {
room_path,
},
};
email.from = `${ String(username).replace(/@/g, '%40').replace(/[<>,]/g, '') } <${ settings.get('From_Email') }>`;
// If direct reply enabled, email content with headers
if (settings.get('Direct_Reply_Enable')) {
const replyto = settings.get('Direct_Reply_ReplyTo') || settings.get('Direct_Reply_Username');
email.headers = {
// Reply-To header with format "username+messageId@domain"
'Reply-To': `${ replyto.split('@')[0].split(settings.get('Direct_Reply_Separator'))[0] }${ settings.get('Direct_Reply_Separator') }${ message._id }@${ replyto.split('@')[1] }`,
};
}
metrics.notificationsSent.inc({ notification_type: 'email' });
return Mailer.send(email);
}
export function shouldNotifyEmail({
disableAllMessageNotifications,
statusConnection,
emailNotifications,
isHighlighted,
hasMentionToUser,
hasMentionToAll,
hasReplyToThread,
roomType,
}) {
// email notifications are disabled globally
if (!settings.get('Accounts_AllowEmailNotifications')) {
return false;
}
// use connected (don't need to send him an email)
if (statusConnection === 'online') {
return false;
}
// user/room preference to nothing
if (emailNotifications === 'nothing') {
return false;
}
// no user or room preference
if (emailNotifications == null) {
if (disableAllMessageNotifications && !isHighlighted && !hasMentionToUser && !hasReplyToThread) {
return false;
}
// default server preference is disabled
if (settings.get('Accounts_Default_User_Preferences_emailNotificationMode') === 'nothing') {
return false;
}
}
return roomType === 'd' || isHighlighted || emailNotifications === 'all' || hasMentionToUser || hasReplyToThread || (!disableAllMessageNotifications && hasMentionToAll);
}