From 5980709885390ff23ca7ba77717ab1cf5b380216 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 2 Feb 2023 09:16:40 +0100 Subject: [PATCH] auto delete on inactivity --- js/networking.js | 60 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/js/networking.js b/js/networking.js index b57c831..91f6d69 100644 --- a/js/networking.js +++ b/js/networking.js @@ -1,11 +1,11 @@ // Global networking variables -let pretext = { +const pretext = { original: "root@baipyr.us:~# ", current: "root@baipyr.us:~# " }; const userData = { history: [], chatPull: null }; -let chatMode = "default"; +let chatMode = "default", activityNotify; if (window.localStorage.getItem("id") === null) window.localStorage.setItem("id", ""); @@ -13,9 +13,53 @@ if (window.localStorage.getItem("name") === null) window.localStorage.setItem("name", ""); if (window.localStorage.getItem("connected") === null) window.localStorage.setItem("connected", ""); -else if (!!window.localStorage.getItem("connected")) + +function setNotification() { + return setInterval(()=>{ + fetch('/activity', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + name: window.localStorage.getItem("name"), + id: window.localStorage.getItem("id") + }) + }).then(res => {}); + }, 60000); +} + +if (window.localStorage.getItem("id") !== "" && window.localStorage.getItem("name") !== "") { pretext.original = pretext.current = `${window.localStorage.getItem("name")}@baipyr.us:~# `; + fetch('/login', { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + name: window.localStorage.getItem("name"), + id: window.localStorage.getItem("id") + }) + }).then(res => { + if (res.status === 200) + return res.json(); + }).then(res => { + if (res === undefined) + return; + if (res.success) + activityNotify = setNotification(); + else { + window.localStorage.setItem("id", ""); + window.localStorage.setItem("name", ""); + window.localStorage.setItem("connected", ""); + pretext.original = pretext.current = "root@baipyr.us:~# "; + cmd_clear(); + } + }); +} + // Tell server to disconnect function disconnect() { @@ -23,7 +67,6 @@ function disconnect() { fetch('/disconnect', { method: 'POST', headers: { - 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ @@ -50,6 +93,7 @@ function connect(name) { body: JSON.stringify({ name }) }).then(res => { if (res.status === 200) { + activityNotify = setNotification(); window.localStorage.setItem("connected", "1"); return res.json(); } @@ -137,7 +181,6 @@ function directMessage(name, message) { fetch('/message', { method: 'POST', headers: { - 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ @@ -174,7 +217,12 @@ function requestUsernames() { // Ping host for two-way-delay function requestPing() { const startTime = new Date(); - fetch('/ping').then(res => { + fetch('/ping', { + method: 'GET', + headers: { + 'Accept': 'application/json', + } + }).then(res => { if (res.status !== 200) return; const diff = new Date() - startTime;