initial group chat functionality

This commit is contained in:
Baipyrus 2023-02-03 13:57:30 +01:00
parent 627c7eab3a
commit d672692854

View File

@ -47,8 +47,8 @@ function userInactivity(name, id) {
}
// User data
const users = [];
// User and chat data
const users = [], chats = [];
// Incoming user connection
app.post('/connect', (req, res) => {
// Generate id, save user
@ -219,7 +219,55 @@ app.post('/login', (req, res) => {
}
res.status(200).json({success});
})
});
app.post('/chatInit', (req, res) => {
const { name, id, chat } = req.body;
const fullName = `${name}#${id}`;
let chatExists = false;
for (const c of chats)
if (c.name === chat) {
chatExists = true;
if (!c.users[fullName])
c.users[fullName] = [];
break;
}
if (!chatExists)
chats.push({
users: {},
name: chat
});
// Log message for debugging
if (consoleLogging)
console.log(`User with ID '${name}#${id}' joined chat '${chat}'.`);
res.status(200).send();
});
app.post('/sendGroup', (req, res) => {
const { name, id, message, chat } = req.body;
for (const c of chats)
if (c.name === chat) {
c.users[`${name}#${id}`].push(message);
break;
}
for (const u of users)
if (u.name === name && u.id === id) {
u.seen = new Date();
break;
}
// Log message for debugging
if (consoleLogging)
console.log(`User with ID '${name}#${id}' sent message '${message}' in '${chat}'.`);
res.status(200).send();
});
// Internal server error
server.on('error', err => {