-
Notifications
You must be signed in to change notification settings - Fork 14
/
handleMessage.js
337 lines (299 loc) · 11.2 KB
/
handleMessage.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
'use strict';
const db = require('ep_etherpad-lite/node/db/DB');
const email = require('emailjs');
const randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString;
const settings = require('ep_etherpad-lite/node/utils/Settings');
const util = require('util');
const validator = require('validator');
const SMTPClient = email.SMTPClient;
const pluginSettings = settings.ep_email_notifications;
const fromName = (pluginSettings && pluginSettings.fromName)
? pluginSettings.fromName : 'Etherpad';
const fromEmail = (pluginSettings && pluginSettings.fromEmail)
? pluginSettings.fromEmail : 'pad@etherpad.org';
const urlToPads = (pluginSettings && pluginSettings.urlToPads)
? pluginSettings.urlToPads : 'http://beta.etherpad.org/p/';
const emailServer = (pluginSettings && pluginSettings.emailServer)
? pluginSettings.emailServer : {host: '127.0.0.1'};
if (!pluginSettings) {
console.warn('Settings for ep_email_notifications plugin are missing in settings.json file');
}
// Connect to the email server --
// This might not be the ideal place to connect but it stops us having lots of connections
const server = new SMTPClient(emailServer);
const padUrl = (padId) => urlToPads + encodeURIComponent(padId);
// When a new message comes in from the client - FML this is ugly
exports.handleMessage = async (hookName, context) => {
if (!context.message || !context.message.data) return;
if (context.message.data.type === 'USERINFO_UPDATE') {
// if it's a request to update an authors email
if (!pluginSettings) {
context.client.json.send({
type: 'COLLABROOM',
data: {
type: 'emailNotificationMissingParams',
payload: true,
},
});
console.error('Settings for ep_email_notifications plugin are missing in settings.json file');
return [null];
// don't run onto passing colorId or anything else to the message handler
}
if (!context.message.data.userInfo || !context.message.data.userInfo.email) return;
console.debug(context.message);
// does email (Un)Subscription already exist for this email address?
const userIds = await db.get(`emailSubscription:${context.message.data.padId}`);
console.debug('emailSubscription');
let alreadyExists = false;
await Promise.all(Object.keys(userIds || {}).map(async (user) => {
console.debug('UserIds subscribed by email to this pad:', userIds);
if (user !== context.message.data.userInfo.email) return;
// If we already have this email registered for this pad
// This user ID is already assigned to this padId so don't do
// anything except tell the user they are already subscribed somehow..
alreadyExists = true;
if (context.message.data.userInfo.email_option === 'subscribe') {
// Subscription process
await subscriptionEmail(
context,
context.message.data.userInfo.email,
alreadyExists,
context.message.data.userInfo,
context.message.data.padId);
} else if (context.message.data.userInfo.email_option === 'unsubscribe') {
// Unsubscription process
await unsubscriptionEmail(
context,
alreadyExists,
context.message.data.userInfo,
context.message.data.padId);
}
}));
if (alreadyExists) return;
if (context.message.data.userInfo.email_option === 'subscribe') {
// Subscription process
await subscriptionEmail(
context,
context.message.data.userInfo.email,
alreadyExists,
context.message.data.userInfo,
context.message.data.padId);
} else if (context.message.data.userInfo.email_option === 'unsubscribe') {
// Unsubscription process
await unsubscriptionEmail(
context,
alreadyExists,
context.message.data.userInfo,
context.message.data.padId);
}
return [null];
// don't run onto passing colorId or anything else to the message handler
} else if (context.message.data.type === 'USERINFO_GET') {
// A request to find datas for a userId
if (!context.message.data.userInfo || !context.message.data.userInfo.userId) return;
console.debug(context.message);
// does email Subscription already exist for this UserId?
const userIds = await db.get(`emailSubscription:${context.message.data.padId}`);
let userIdFound = false;
for (const user of Object.keys(userIds || {})) {
if (userIds[user].authorId !== context.message.data.userInfo.userId) continue;
// if we find the same Id in the Db as the one used by the user
console.debug('Options for this pad ', userIds[user].authorId, ' found in the Db');
userIdFound = true;
// Request user subscription info process
sendUserInfo(context, userIdFound, user, userIds[user]);
}
if (!userIdFound) {
// Request user subscription info process
sendUserInfo(context, userIdFound, '', '');
}
return [null];
}
};
/**
* Subscription process
*/
const subscriptionEmail = async (context, email, emailFound, userInfo, padId) => {
const validatesAsEmail = validator.isEmail(email);
const subscribeId = randomString(25);
if (!emailFound && validatesAsEmail) {
// Subscription -> Go for it
console.debug('Subscription: Wrote to the database and sent client a positive response ',
context.message.data.userInfo.email);
await setAuthorEmailRegistered(userInfo, userInfo.userId, subscribeId, padId);
console.debug('emailSubSucc');
context.client.json.send({
type: 'COLLABROOM',
data: {
type: 'emailSubscriptionSuccess',
payload: {
formName: userInfo.formName,
success: true,
},
},
});
// Send mail to user with the link for validation
let message;
try {
message = await util.promisify(server.send.bind(server))({
text: 'Please click on this link in order to validate your subscription to the pad ' +
`${padId}\n${padUrl(padId)}/subscribe=${subscribeId}`,
from: `${fromName} <${fromEmail}>`,
to: userInfo.email,
subject: `Email subscription confirmation for pad ${padId}`,
});
} catch (err) {
console.error(err);
return;
}
console.log(message);
} else if (!validatesAsEmail) {
// Subscription -> failed coz mail malformed.. y'know in general fuck em!
console.debug('Dropped email subscription due to malformed email address');
context.client.json.send({
type: 'COLLABROOM',
data: {
type: 'emailSubscriptionSuccess',
payload: {
type: 'malformedEmail',
formName: userInfo.formName,
success: false,
},
},
});
} else {
// Subscription -> failed coz email already subscribed for this pad
console.debug('email ', context.message.data.userInfo.email,
'already subscribed to ', context.message.data.padId, ' so sending message to client');
context.client.json.send({
type: 'COLLABROOM',
data: {
type: 'emailSubscriptionSuccess',
payload: {
type: 'alreadyRegistered',
formName: userInfo.formName,
success: false,
},
},
});
}
};
/**
* UnsUbscription process
*/
const unsubscriptionEmail = async (context, emailFound, userInfo, padId) => {
const unsubscribeId = randomString(25);
if (emailFound) {
// Unsubscription -> Go for it
console.debug('Unsubscription: Remove from the database and sent client a positive response ',
context.message.data.userInfo.email);
await unsetAuthorEmailRegistered(userInfo, userInfo.userId, unsubscribeId, padId);
context.client.json.send({
type: 'COLLABROOM',
data: {
type: 'emailUnsubscriptionSuccess',
payload: {
formName: userInfo.formName,
success: true,
},
},
});
// Send mail to user with the link for validation
let message;
try {
message = await util.promisify(server.send.bind(server))({
text: 'Please click on this link in order to validate your unsubscription to the pad ' +
`${padId}\n${padUrl(padId)}/unsubscribe=${unsubscribeId}`,
from: `${fromName} <${fromEmail}>`,
to: userInfo.email,
subject: `Email unsubscription confirmation for pad ${padId}`,
});
} catch (err) {
console.error(err);
return;
}
console.log(message);
} else {
// Unsubscription -> Send failed as email not found
console.debug(
'Unsubscription: Send client a negative response ', context.message.data.userInfo.email);
context.client.json.send({
type: 'COLLABROOM',
data: {
type: 'emailUnsubscriptionSuccess',
payload: {
formName: userInfo.formName,
success: false,
},
},
});
}
};
/**
* Request user subscription info process
*/
const sendUserInfo = (context, emailFound, email, userInfo) => {
const {onStart = true, onEnd = false} = userInfo;
if (emailFound) {
// We send back the options associated to this userId
context.client.json.send({
type: 'COLLABROOM',
data: {
type: 'emailNotificationGetUserInfo',
payload: {
email,
onStart,
onEnd,
formName: context.message.data.userInfo.formName,
success: true,
},
},
});
} else {
// No options set for this userId
context.client.json.send({
type: 'COLLABROOM',
data: {
type: 'emailNotificationGetUserInfo',
payload: {
formName: context.message.data.userInfo.formName,
success: false,
},
},
});
}
};
/**
* Database manipulation
*/
// Write email, options, authorId and pendingId to the database
const setAuthorEmailRegistered = async (userInfo, authorId, subscribeId, padId) => {
const timestamp = Date.now();
const registered = {
authorId,
onStart: userInfo.email_onStart,
onEnd: userInfo.email_onEnd,
subscribeId,
timestamp,
};
console.debug('registered', registered, ' to ', padId);
// Here we have to basically hack a new value into the database, this isn't clean or polite.
const value = await db.get(`emailSubscription:${padId}`) || {}; // get the current value
if (!value.pending) value.pending = {};
// add the registered values to the pending section of the object
value.pending[userInfo.email] = registered;
// Write the modified datas back in the Db
await db.set(`emailSubscription:${padId}`, value); // stick it in the database
};
// Write email, authorId and pendingId to the database
const unsetAuthorEmailRegistered = async (userInfo, authorId, unsubscribeId, padId) => {
const timestamp = Date.now();
console.debug('unregistered', userInfo.email, ' to ', padId);
const value = await db.get(`emailSubscription:${padId}`); // get the current value
// if the pending section doesn't exist yet for this padId, we create it (this shouldn't happen)
if (!value.pending) value.pending = {};
// add the registered values to the pending section of the object
value.pending[userInfo.email] = {authorId, unsubscribeId, timestamp};
// Write the modified datas back in the Db
await db.set(`emailSubscription:${padId}`, value);
};