From 627c7eab3adbb00e9974697d352bce80acd724bd Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 2 Feb 2023 10:45:07 +0100 Subject: [PATCH] multiuser messaging --- index.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 36095a3..f8adeeb 100644 --- a/index.js +++ b/index.js @@ -119,7 +119,7 @@ app.post('/nickname', (req, res) => { // User is pulling messages app.post('/getChat', (req, res) => { - const { name, id } = req.body; + const { name, from, id } = req.body; for (const u of users) if (u.name === name && u.id === id) { @@ -133,7 +133,7 @@ app.post('/getChat', (req, res) => { for (const u of users) if (fullName in u.chats) { const len = u.chats[fullName].length; - if (len > 0) + if (len > 0 && from.includes(`${u.name}#${u.id}`)) chats[`${u.name}#${u.id}`] = u.chats[fullName].splice(0, len); } @@ -145,7 +145,7 @@ app.post('/getChat', (req, res) => { // Log if messages were delivered if (consoleLogging) - console.log(`Delivered messages to user with ID '${fullName}'.`); + console.log(`Delivered messages to user with ID '${fullName}' from '${from}'.`); // Send back data res.status(200).json(chats); @@ -153,21 +153,23 @@ app.post('/getChat', (req, res) => { // User sent new message app.post('/message', (req, res) => { - const { name, id, recipient, message } = req.body; + const { name, id, to, message } = req.body; // Search for senders' name, save message for (const u of users) if (u.name === name && u.id === id) { - if (!u.chats[recipient]) - u.chats[recipient] = []; - u.chats[recipient].push(message); + for (const recipient of to) { + if (!u.chats[recipient]) + u.chats[recipient] = []; + u.chats[recipient].push(message); + } u.seen = new Date(); break; } // Log message for debugging if (consoleLogging) - console.log(`User with ID '${name}#${id}' sent message '${message}'.`); + console.log(`User with ID '${name}#${id}' sent message '${message}' to '${to}'.`); // Confirm process res.status(200).send();