summaryrefslogtreecommitdiff
path: root/server/hornchat.conversation.message.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-08-10 10:38:44 +0200
committerMinteck <contact@minteck.org>2022-08-10 10:38:44 +0200
commitc6dbf0450566c40efc4a26f4f0717452b6ef95cd (patch)
treeb4be2d508223820d0a77d5a3e35e82684da3b6ec /server/hornchat.conversation.message.js
downloadhornchat-c6dbf0450566c40efc4a26f4f0717452b6ef95cd.tar.gz
hornchat-c6dbf0450566c40efc4a26f4f0717452b6ef95cd.tar.bz2
hornchat-c6dbf0450566c40efc4a26f4f0717452b6ef95cd.zip
Initial commitHEADmane
Diffstat (limited to 'server/hornchat.conversation.message.js')
-rw-r--r--server/hornchat.conversation.message.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/server/hornchat.conversation.message.js b/server/hornchat.conversation.message.js
new file mode 100644
index 0000000..69ced04
--- /dev/null
+++ b/server/hornchat.conversation.message.js
@@ -0,0 +1,56 @@
+const fs = require("fs");
+
+module.exports = (socket, msg) => {
+ if (!msg.data) {
+ socket.send(JSON.stringify({error:"MISSING_OPERAND", fatal: false}));
+ console.log("[" + socket.id + "] Missing 'data' 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;
+ })
+
+ let conversationJSON = JSON.parse(fs.readFileSync(dataPath + "/conversations/" + data[socket.id].conversation + "/conversation.json").toString());
+ let messagesJSON = JSON.parse(fs.readFileSync(dataPath + "/conversations/" + data[socket.id].conversation + "/messages.json").toString());
+
+ msg.data.uuid = require('uuid-v4')();
+ msg.data.id = conversationJSON["counter"] + 1;
+ msg.data.date = new Date().toISOString();
+
+ let validKeys = [];
+ let validDevices = userCredentials.filter((i) => {
+ return i.id === data[socket.id].peer;
+ })[0].devices.map((i) => { return i.id; });
+ for (let keyId of Object.keys(keys[data[socket.id].peer])) {
+ if (validDevices.includes(keyId)) validKeys.push(keyId);
+ }
+
+ msg.data.recipients = validKeys;
+
+ for (let s of otherPeerSockets) {
+ msg.data.status = 1;
+ s.send(JSON.stringify(msg))
+ msg.data.recipients = msg.data.recipients.filter((i) => {
+ return i !== s.authenticated.device;
+ })
+ }
+
+ let msg2 = msg;
+ msg2["_callback"] = true;
+ socket.send(JSON.stringify(msg2));
+
+ messagesJSON[msg.data.uuid] = msg;
+ conversationJSON.counter++;
+ fs.writeFileSync(dataPath + "/conversations/" + data[socket.id].conversation + "/messages.json", JSON.stringify(messagesJSON));
+ fs.writeFileSync(dataPath + "/conversations/" + data[socket.id].conversation + "/conversation.json", JSON.stringify(conversationJSON));
+} \ No newline at end of file