-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocessing.js
44 lines (36 loc) · 1.17 KB
/
processing.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
const _ = require('lodash');
const send = require('./send');
let waitUserId = null;
const couples = []; // [['id', 'id'], ['id', 'id']]
module.exports = async ({ user_id: userId, body: text, payload }) => {
userId = +userId;
const index = _.findIndex(couples, arr => _.indexOf(arr, userId) !== -1);
if (index === -1) {
if (!waitUserId || waitUserId === userId) {
waitUserId = userId;
await send(userId, 'Ждём собеседника...');
return;
} else {
couples.push([userId, waitUserId]);
const text = 'Поприветствуйте собеседника.';
await send(userId, text);
await send(waitUserId, text);
waitUserId = null;
return;
}
} else {
const couple = couples[index];
const to = _.indexOf(couple, userId) === 0 ? couple[1] : couple[0];
let button;
try {
button = +JSON.parse(payload).button;
} catch (error) {} // eslint-disable-line
if (button === 1) {
couples.splice(index, 1);
await send(to, 'Собеседник отключился.');
await send(userId, 'Чат остановлен');
return;
}
await send(to, text);
}
};