From d153ad5df872a6a608b60ca1716e349bf08151e2 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 2 Feb 2023 09:16:41 +0100 Subject: [PATCH] auto delete on inactivity --- index.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index f12ccba..36095a3 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,18 @@ function generatePartialID() { return Math.floor(Math.random()*10000).toString().padStart(4, '0'); } +// Check for user inactivity and delete accordingly +function userInactivity(name, id) { + for (let i = 0; i < users.length; i++) + if (users[i].name === name && users[i].id === id && new Date() - users[i].seen > 86400000) { + if (consoleLogging) + console.log(`Deleting user with ID '${name}#${id}' because of inactivity.`); + + users.splice(i, 1); + break; + } +} + // User data const users = []; @@ -46,8 +58,10 @@ app.post('/connect', (req, res) => { while (!isStringUnique(name_id, `${name}#${id}`)) id = generatePartialID(); users.push({ - socket: req.socket, - date: new Date(), + deletion: setInterval( + ()=>userInactivity(name, id), + 3600000), + seen: new Date(), chats: {}, name, id @@ -95,6 +109,7 @@ app.post('/nickname', (req, res) => { console.log(`(Re-)named user with ID '${oldName}#${id}'.`); u.name = name; u.id = currentID; + u.seen = new Date(); break; } @@ -106,6 +121,12 @@ app.post('/nickname', (req, res) => { app.post('/getChat', (req, res) => { const { name, id } = req.body; + for (const u of users) + if (u.name === name && u.id === id) { + u.seen = new Date(); + break; + } + // Go through users' chats, retrieve messages, save by username let chats = {}; const fullName = `${name}#${id}`; @@ -140,12 +161,13 @@ app.post('/message', (req, res) => { 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 '${name}#${id}' sent message '${message}'.`); + console.log(`User with ID '${name}#${id}' sent message '${message}'.`); // Confirm process res.status(200).send(); @@ -154,7 +176,7 @@ app.post('/message', (req, res) => { // User requesting all names app.get('/getNames', (req, res) => { if (consoleLogging) - console.log('Some user requested all names.'); + console.log('Some user requested all usernames + IDs.'); res.status(200).json(users.map(u => { return { @@ -164,6 +186,39 @@ app.get('/getNames', (req, res) => { })); }); +// User sends activity notification +app.post('/activity', (req, res) => { + const { name, id } = req.body; + + if (consoleLogging) + console.log(`User with ID '${name}#${id}' is online.`); + + for (const u of users) + if (u.name === name && u.id === id) { + u.seen = new Date(); + break; + } + + res.status(200).send(); +}); + +app.post('/login', (req, res) => { + const { name, id } = req.body; + + if (consoleLogging) + console.log(`Some user requested existence of '${name}#${id}'.`); + + let success = false; + for (const u of users) + if (u.name === name && u.id === id) { + success = true; + u.seen = new Date(); + break; + } + + res.status(200).json({success}); +}) + // Internal server error server.on('error', err => { console.error('Internal server error', err);