-
Notifications
You must be signed in to change notification settings - Fork 14
/
handleMessage.js
91 lines (82 loc) · 2.46 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
'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');
const db = require('ep_etherpad-lite/node/db/DB').db;
// Remove cache for this procedure
db.dbSettings.cache = 0;
const saveRoomTitle = (padId, message) => {
db.set(`title:${padId}`, message);
};
const sendToRoom = (message, msg) => {
// This is bad.. We have to do it because ACE hasn't redrawn by the time the chat has arrived
setTimeout(() => {
padMessageHandler.handleCustomObjectMessage(msg, false, () => {
// TODO: Error handling.
});
}
, 100);
};
/*
* Handle incoming messages from clients
*/
exports.handleMessage = async (hookName, context, cb) => {
// Firstly ignore any request that aren't about chat
let message = false;
if (context.message && context.message.type && context.message.type === 'COLLABROOM') {
if (context.message.data && context.message.data.type &&
context.message.data.type === 'title') {
message = context.message.data;
}
}
if (!message) return;
/* **
* What's available in a message?
* - action -- The action IE chatPosition
* - padId -- The padId of the pad both authors are on
* - targetAuthorId -- The Id of the author this user wants to talk to
* - message -- the actual message
* - myAuthorId -- The Id of the author who is trying to talk to the targetAuthorId
** */
if (message.action === 'sendTitleMessage') {
const authorName = await authorManager.getAuthorName(message.myAuthorId); // Get the authorname
const msg = {
type: 'COLLABROOM',
data: {
type: 'CUSTOM',
payload: {
action: 'recieveTitleMessage',
authorId: message.myAuthorId,
authorName,
padId: message.padId,
message: message.message,
},
},
};
sendToRoom(message, msg);
saveRoomTitle(message.padId, message.message);
return null; // handled by plugin
}
};
exports.clientVars = (hook, pad, callback) => {
const padId = pad.pad.id;
db.get(`title:${padId}`, (err, value) => {
const msg = {
type: 'COLLABROOM',
data: {
type: 'CUSTOM',
payload: {
action: 'recieveTitleMessage',
padId,
message: value,
},
},
};
sendToRoom(false, msg);
});
callback();
};