auto delete on inactivity

This commit is contained in:
Baipyrus 2023-02-02 09:16:40 +01:00
parent 586cb1738c
commit 5980709885

View File

@ -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;