summaryrefslogtreecommitdiff
path: root/server/hornchat.conversation.typing.js
blob: d3c57552863baf1042b6ff489cdcd48f2a46e383 (plain)
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
const fs = require("fs");

module.exports = (socket, msg) => {
    if (!msg.text) {
        socket.send(JSON.stringify({error:"MISSING_OPERAND", fatal: false}));
        console.log("[" + socket.id + "] Missing 'username' value");
        return;
    }

    if (!data[socket.id]) {
        socket.send(JSON.stringify({error:"NOT_STARTED", fatal: false}));
        console.log("[" + socket.id + "] Conversation not started");
        return;
    }

    let otherPeerSockets = Object.keys(data).filter((i) => {
        let user = data[i];
        return !!(user && user.conversation === data[socket.id].conversation && user.peer === socket.authenticated.user);
    }).map((i) => {
        return data[i].socket;
    })

    for (let s of otherPeerSockets) {
        s.send(JSON.stringify({
            type: "typing",
            text: msg.text,
            reply: msg.reply,
            attachments: parseInt(msg.attachments),
            position: msg.position
        }))
    }

    socket.send(JSON.stringify({success: true}));
}