auto delete on inactivity
This commit is contained in:
parent
6809200911
commit
d153ad5df8
63
index.js
63
index.js
@ -34,6 +34,18 @@ function generatePartialID() {
|
|||||||
return Math.floor(Math.random()*10000).toString().padStart(4, '0');
|
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
|
// User data
|
||||||
const users = [];
|
const users = [];
|
||||||
@ -46,8 +58,10 @@ app.post('/connect', (req, res) => {
|
|||||||
while (!isStringUnique(name_id, `${name}#${id}`))
|
while (!isStringUnique(name_id, `${name}#${id}`))
|
||||||
id = generatePartialID();
|
id = generatePartialID();
|
||||||
users.push({
|
users.push({
|
||||||
socket: req.socket,
|
deletion: setInterval(
|
||||||
date: new Date(),
|
()=>userInactivity(name, id),
|
||||||
|
3600000),
|
||||||
|
seen: new Date(),
|
||||||
chats: {},
|
chats: {},
|
||||||
name,
|
name,
|
||||||
id
|
id
|
||||||
@ -95,6 +109,7 @@ app.post('/nickname', (req, res) => {
|
|||||||
console.log(`(Re-)named user with ID '${oldName}#${id}'.`);
|
console.log(`(Re-)named user with ID '${oldName}#${id}'.`);
|
||||||
u.name = name;
|
u.name = name;
|
||||||
u.id = currentID;
|
u.id = currentID;
|
||||||
|
u.seen = new Date();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,6 +121,12 @@ app.post('/nickname', (req, res) => {
|
|||||||
app.post('/getChat', (req, res) => {
|
app.post('/getChat', (req, res) => {
|
||||||
const { name, id } = req.body;
|
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
|
// Go through users' chats, retrieve messages, save by username
|
||||||
let chats = {};
|
let chats = {};
|
||||||
const fullName = `${name}#${id}`;
|
const fullName = `${name}#${id}`;
|
||||||
@ -140,12 +161,13 @@ app.post('/message', (req, res) => {
|
|||||||
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();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log message for debugging
|
// Log message for debugging
|
||||||
if (consoleLogging)
|
if (consoleLogging)
|
||||||
console.log(`User '${name}#${id}' sent message '${message}'.`);
|
console.log(`User with ID '${name}#${id}' sent message '${message}'.`);
|
||||||
|
|
||||||
// Confirm process
|
// Confirm process
|
||||||
res.status(200).send();
|
res.status(200).send();
|
||||||
@ -154,7 +176,7 @@ app.post('/message', (req, res) => {
|
|||||||
// User requesting all names
|
// User requesting all names
|
||||||
app.get('/getNames', (req, res) => {
|
app.get('/getNames', (req, res) => {
|
||||||
if (consoleLogging)
|
if (consoleLogging)
|
||||||
console.log('Some user requested all names.');
|
console.log('Some user requested all usernames + IDs.');
|
||||||
|
|
||||||
res.status(200).json(users.map(u => {
|
res.status(200).json(users.map(u => {
|
||||||
return {
|
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
|
// Internal server error
|
||||||
server.on('error', err => {
|
server.on('error', err => {
|
||||||
console.error('Internal server error', err);
|
console.error('Internal server error', err);
|
||||||
|
Loading…
Reference in New Issue
Block a user