-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandleMessage.js
More file actions
148 lines (127 loc) · 4.18 KB
/
handleMessage.js
File metadata and controls
148 lines (127 loc) · 4.18 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
146
147
148
'use strict';
const padManager = require('ep_etherpad-lite/node/db/PadManager');
const padMessageHandler = require('ep_etherpad-lite/node/handler/PadMessageHandler');
const db = require('ep_etherpad-lite/node/db/DB');
const settingsStr = require('ep_etherpad-lite/node/utils/Settings');
const exportTxt = require('ep_etherpad-lite/node/utils/ExportTxt');
const settings = settingsStr.ep_git_commit_saved_revision;
if (!settings) return console.error('No ep_git_commit_saved_revision settings, see the README.md');
exports.handleMessage = (hook_name, context, callback) => {
// Firstly ignore any request that aren't about chat
let isgitcommitMessage = false;
if (context) {
if (context.message && context.message) {
if (context.message.type === 'COLLABROOM') {
if (context.message.data) {
if (context.message.data.type) {
if (context.message.data.type === 'gitcommit') {
isgitcommitMessage = true;
}
}
}
}
}
}
if (!isgitcommitMessage) {
callback(false);
}
if (isgitcommitMessage) {
const message = context.message.data;
if (message.action === 'sendgitcommitMessage') {
saveRoomgitcommit(message.padId, message.message);
}
callback([null]);
}
};
const saveRoomgitcommit = (padId, message) => {
// do the git logic here
// saving to database just for posterity.. ueberdb2 v6 is promise-only,
// so awaiting (or .catch'ing) surfaces failures instead of producing an
// unhandled rejection.
db.set(`gitcommit:${padId}`, message).catch((err) => {
console.error('ep_git_commit_saved_revision db.set failed:', err);
});
// handle the actual event
doEvent(padId, message);
};
const doInit = () => {
const path = settings.path; // Path IE "/home/etherpad/var/git"
const initCommandStr = settings.initCommand; // IE "git init \"${REPO_PATH}\"
const initCommand = eval(`\`${initCommandStr}\``); /* eslint-disable-line no-eval */
console.debug('initCommand', initCommand);
// make path if it doesn't exists
const fs = require('fs');
const dir = path;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
// execute a command in that folder
const {exec} = require('child_process');
exec(initCommand, {cwd: path}, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
};
const doEvent = (padId, message) => {
// IE "git -C \"${REPO_PATH} add <PADNAME.txt> &&
// git -C \"${REPO_PATH}\" commit -m \"${COMMIT_MESSAGE}\""
let saveCommand = settings.saveCommand;
const path = settings.path; // Path IE "/home/etherpad/var/git"
const saveCommandStr = settings.saveCommand; // IE "git init \"${REPO_PATH}\"
saveCommand = eval(`\`${saveCommandStr}\``); /* eslint-disable-line no-eval */
// make path if it doesn't exists
const fs = require('fs');
const dir = path;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
// Write a text file to the path
exports.getAndWrite = async () => {
const pad = await padManager.getPad(padId);
const padText = exportTxt.getTXTFromAtext(pad, pad.atext);
fs.writeFile(`${path}/${padId}.txt`, padText, (err) => {
if (err) return console.log(err);
});
// execute a command in that folder
const {exec} = require('child_process');
exec(saveCommand, {cwd: path}, (error, stdout, stderr) => {
if (!error) tellRoom(padId, true);
if (error) {
tellRoom(padId, false);
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
});
};
exports.getAndWrite();
};
const tellRoom = (padId, value) => {
// Tells people present on the pad that a git commit was made for this docucment.
const msg = {
type: 'COLLABROOM',
data: {
type: 'CUSTOM',
payload: {
action: 'recievegitcommitMessage',
padId,
message: value,
},
},
};
padMessageHandler.handleCustomObjectMessage(msg, false, () => {
// TODO: Something?
});
};
// Doing initialization
doInit();