multiuser messaging

This commit is contained in:
Baipyrus 2023-02-02 10:45:07 +01:00
parent d153ad5df8
commit 627c7eab3a

View File

@ -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();