multiuser messaging
This commit is contained in:
parent
d153ad5df8
commit
627c7eab3a
12
index.js
12
index.js
@ -119,7 +119,7 @@ app.post('/nickname', (req, res) => {
|
|||||||
|
|
||||||
// User is pulling messages
|
// User is pulling messages
|
||||||
app.post('/getChat', (req, res) => {
|
app.post('/getChat', (req, res) => {
|
||||||
const { name, id } = req.body;
|
const { name, from, id } = req.body;
|
||||||
|
|
||||||
for (const u of users)
|
for (const u of users)
|
||||||
if (u.name === name && u.id === id) {
|
if (u.name === name && u.id === id) {
|
||||||
@ -133,7 +133,7 @@ app.post('/getChat', (req, res) => {
|
|||||||
for (const u of users)
|
for (const u of users)
|
||||||
if (fullName in u.chats) {
|
if (fullName in u.chats) {
|
||||||
const len = u.chats[fullName].length;
|
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);
|
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
|
// Log if messages were delivered
|
||||||
if (consoleLogging)
|
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
|
// Send back data
|
||||||
res.status(200).json(chats);
|
res.status(200).json(chats);
|
||||||
@ -153,21 +153,23 @@ app.post('/getChat', (req, res) => {
|
|||||||
|
|
||||||
// User sent new message
|
// User sent new message
|
||||||
app.post('/message', (req, res) => {
|
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
|
// Search for senders' name, save message
|
||||||
for (const u of users)
|
for (const u of users)
|
||||||
if (u.name === name && u.id === id) {
|
if (u.name === name && u.id === id) {
|
||||||
|
for (const recipient of to) {
|
||||||
if (!u.chats[recipient])
|
if (!u.chats[recipient])
|
||||||
u.chats[recipient] = [];
|
u.chats[recipient] = [];
|
||||||
u.chats[recipient].push(message);
|
u.chats[recipient].push(message);
|
||||||
|
}
|
||||||
u.seen = new Date();
|
u.seen = new Date();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log message for debugging
|
// Log message for debugging
|
||||||
if (consoleLogging)
|
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
|
// Confirm process
|
||||||
res.status(200).send();
|
res.status(200).send();
|
||||||
|
Loading…
Reference in New Issue
Block a user