diff options
author | Minteck <contact@minteck.org> | 2022-08-10 10:38:44 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-08-10 10:38:44 +0200 |
commit | c6dbf0450566c40efc4a26f4f0717452b6ef95cd (patch) | |
tree | b4be2d508223820d0a77d5a3e35e82684da3b6ec /server/hornchat.keyserver.disconnect.js | |
download | hornchat-c6dbf0450566c40efc4a26f4f0717452b6ef95cd.tar.gz hornchat-c6dbf0450566c40efc4a26f4f0717452b6ef95cd.tar.bz2 hornchat-c6dbf0450566c40efc4a26f4f0717452b6ef95cd.zip |
Diffstat (limited to 'server/hornchat.keyserver.disconnect.js')
-rw-r--r-- | server/hornchat.keyserver.disconnect.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/server/hornchat.keyserver.disconnect.js b/server/hornchat.keyserver.disconnect.js new file mode 100644 index 0000000..b1d1b35 --- /dev/null +++ b/server/hornchat.keyserver.disconnect.js @@ -0,0 +1,46 @@ +const fs = require("fs"); +const _list = require('./hornchat.keyserver.list'); + +module.exports = (socket, data) => { + console.log("[" + socket.id + "] Disconnect method"); + + if (!data.device) { + socket.send(JSON.stringify({error: "MISSING_OPERAND", fatal: false})); + console.log("[" + socket.id + "] Missing 'device' value"); + return; + } + + let candidateDevices = userCredentials.filter((i) => { + return i.id === socket.authenticated.user; + })[0].devices.filter((i) => { + return i.id === data.device; + }); + + if (candidateDevices.length !== 1) { + socket.send(JSON.stringify({error:"INVALID_DEVICE", fatal: false})); + console.log("[" + socket.id + "] Invalid 'device' value"); + return; + } + + global.userCredentials = userCredentials.map((i) => { + if (i.id === socket.authenticated.user) { + i.devices = i.devices.map((j) => { + if (j.id === data.device) { + return null; + } else { + return j; + } + }).filter((j) => { + return j !== null; + }) + + return i; + } else { + return i; + } + }) + fs.writeFileSync(dataPath + "/users.json", JSON.stringify(userCredentials, null, 2)); + fs.writeFileSync(dataPath + "/keys.json", JSON.stringify(keys, null, 2)); + + _list(socket, data); +}
\ No newline at end of file |