-
Notifications
You must be signed in to change notification settings - Fork 15
/
handleMessage.js
66 lines (58 loc) · 1.96 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
'use strict';
/** *
*
* Responsible for negotiating messages between two clients
*
****/
const authorManager = require('ep_etherpad-lite/node/db/AuthorManager');
const padMessageHandler = require('ep_etherpad-lite/node/handler/PadMessageHandler');
/*
* Handle incoming messages from clients
*/
exports.handleMessage = async (hookName, context) => {
// Firstly ignore any request that aren't about cursor
const {message: {type, data = {}} = {}} = context || {};
if (type !== 'COLLABROOM' || data.type !== 'cursor') return;
const message = data;
/** *
What's available in a message?
* action -- The action IE cursorPosition
* padId -- The padId of the pad both authors are on
* targetAuthorId -- The Id of the author this user wants to talk to
* locationX and location Y are the locations. // TODO make this one object or a touple
* myAuthorId -- The Id of the author who is trying to talk to the targetAuthorId
***/
if (message.action === 'cursorPosition') {
const authorName = await authorManager.getAuthorName(message.myAuthorId);
const msg = {
type: 'COLLABROOM',
data: {
type: 'CUSTOM',
payload: {
action: 'cursorPosition',
authorId: message.myAuthorId,
authorName,
padId: message.padId,
locationX: message.locationX,
locationY: message.locationY,
},
},
};
sendToRoom(message, msg);
}
return null; // null prevents Etherpad from attempting to process the message any further.
};
const sendToRoom = (message, msg) => {
// Todo write some buffer handling for protection and to stop DDoS
// myAuthorId exists in message.
const bufferAllows = true;
if (bufferAllows) {
// We have to do this because the editor hasn't redrawn by the time the cursor has arrived
setTimeout(() => {
padMessageHandler.handleCustomObjectMessage(msg, false, () => {
// TODO: Error handling.
});
}
, 500);
}
};