From 3947e78aef9c6968ebcaefd7e1c52c01d56d6b4a Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:23:45 +0300 Subject: [PATCH 01/14] Started working on saving the user data on actions like registration, joining and leaving rooms. Lacks of loading on start up. --- app.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 4c9a540..db1e214 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,7 @@ import ServeDrive from 'serve-drive'; import Hyperdrive from 'hyperdrive'; import Corestore from 'corestore'; import { EventEmitter } from 'events'; +import fs from "fs"; const storagePath = `./storage/storage_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`; const store = new Corestore(storagePath); @@ -23,6 +24,13 @@ const eventEmitter = new EventEmitter(); // Define servePort at the top level let servePort; +// Object to store all the information we want to save +let config = { + username: '', + userAvatar: '', + rooms: [] +}; + // Function to get a random port between 1337 and 2223 function getRandomPort() { return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152; @@ -136,7 +144,7 @@ function registerUser(e) { reader.onload = async (event) => { const buffer = new Uint8Array(event.target.result); await drive.put(`/icons/${regUsername}.png`, buffer); - userAvatar = `http://localhost:${servePort}/icons/${regUsername}.png`; // Set the correct URL + config.userAvatar = userAvatar = `http://localhost:${servePort}/icons/${regUsername}.png`; // Set the correct URL registeredUsers[regUsername] = userAvatar; localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers)); continueRegistration(regUsername); @@ -156,13 +164,15 @@ async function continueRegistration(regUsername) { return; } - userName = regUsername; + config.username = userName = regUsername; setupDiv.classList.remove('hidden'); document.querySelector('#register').classList.add('hidden'); loadingDiv.classList.add('hidden'); const randomTopic = crypto.randomBytes(32); document.querySelector('#chat-room-topic').innerText = truncateHash(b4a.toString(randomTopic, 'hex')); + + writeJsonToFile("./config.json", config); } async function createChatRoom() { @@ -207,6 +217,9 @@ function addRoomToList(topic) { roomItem.dataset.topic = topic; roomItem.addEventListener('click', () => switchRoom(topic)); roomList.appendChild(roomItem); + + config.rooms.push(topic); + writeJsonToFile("./config.json", config); } function switchRoom(topic) { @@ -226,6 +239,9 @@ function leaveRoom() { } document.querySelector('#chat').classList.add('hidden'); document.querySelector('#setup').classList.remove('hidden'); + + config.rooms = config.rooms.filter(e => e !== currentRoom.topic); + writeJsonToFile("./config.json", config); } function sendMessage(e) { @@ -301,6 +317,9 @@ async function updateIcon(username, avatarBuffer) { const avatarBlob = new Blob([avatarBuffer], { type: 'image/png' }); const avatarUrl = URL.createObjectURL(avatarBlob); userIcon.src = avatarUrl; + + config.userAvatar = avatarUrl; + writeJsonToFile("./config.json", config); } } @@ -316,4 +335,11 @@ function toggleSetupView() { setupDiv.classList.toggle('hidden'); } +function writeJsonToFile(filePath, data) { + fs.writeFile(filePath, JSON.stringify(data), (err) => { + if (err) return console.error(err); + console.log("File has been created"); + }); +} + initialize(); -- 2.39.3 From d33d31debf76a9328ecf7f7e992fc8b7b192e0cc Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:32:33 +0300 Subject: [PATCH 02/14] Got rid of userName and userAvatar in exchange of config.userName and config.userAvatar --- app.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index 3fa777f..18aceb7 100644 --- a/app.js +++ b/app.js @@ -14,8 +14,6 @@ const drive = new Hyperdrive(store); await drive.ready(); let swarm; -let userName = 'Anonymous'; -let userAvatar = ''; let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {}; let peerCount = 0; let currentRoom = null; @@ -26,7 +24,7 @@ let servePort; // Object to store all the information we want to save let config = { - username: '', + userName: 'Anonymous', userAvatar: '', rooms: [] }; @@ -90,7 +88,7 @@ async function initialize() { const filePath = `/files/${file.name}`; await drive.put(filePath, buffer); const fileUrl = `http://localhost:${servePort}${filePath}`; - sendFileMessage(userName, fileUrl, file.type, userAvatar); + sendFileMessage(config.userName, fileUrl, file.type, config.userAvatar); }; reader.readAsArrayBuffer(file); } @@ -121,11 +119,11 @@ async function initialize() { console.log('Peer connected, current peer count:', peerCount); // Send the current user's icon to the new peer - const iconBuffer = await drive.get(`/icons/${userName}.png`); + const iconBuffer = await drive.get(`/icons/${config.userName}.png`); if (iconBuffer) { const iconMessage = JSON.stringify({ type: 'icon', - username: userName, + username: config.userName, avatar: iconBuffer.toString('base64'), }); connection.write(iconMessage); @@ -167,8 +165,8 @@ function registerUser(e) { reader.onload = async (event) => { const buffer = new Uint8Array(event.target.result); await drive.put(`/icons/${regUsername}.png`, buffer); - config.userAvatar = userAvatar = `http://localhost:${servePort}/icons/${regUsername}.png`; // Set the correct URL - registeredUsers[regUsername] = userAvatar; + config.userAvatar = `http://localhost:${servePort}/icons/${regUsername}.png`; // Set the correct URL + registeredUsers[regUsername] = config.userAvatar; localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers)); continueRegistration(regUsername); }; @@ -187,7 +185,7 @@ async function continueRegistration(regUsername) { return; } - config.username = userName = regUsername; + config.username = regUsername; setupDiv.classList.remove('hidden'); document.querySelector('#register').classList.add('hidden'); loadingDiv.classList.add('hidden'); @@ -272,13 +270,13 @@ function sendMessage(e) { const message = document.querySelector('#message').value; document.querySelector('#message').value = ''; - onMessageAdded(userName, message, userAvatar); + onMessageAdded(config.userName, message, config.userAvatar); const messageObj = JSON.stringify({ type: 'message', - name: userName, + name: config.userName, message, - avatar: userAvatar, + avatar: config.userAvatar, timestamp: Date.now(), }); -- 2.39.3 From 1271c4857145ab1464de20f0bf62867c68b03fd6 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:44:52 +0300 Subject: [PATCH 03/14] Mostly finished loading. User avatar stuff is left to do :sob: --- app.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 18aceb7..7098498 100644 --- a/app.js +++ b/app.js @@ -24,7 +24,7 @@ let servePort; // Object to store all the information we want to save let config = { - userName: 'Anonymous', + userName: '', userAvatar: '', rooms: [] }; @@ -95,8 +95,13 @@ async function initialize() { }); } + readConfigFromFile(); + config.rooms.forEach(room => { + addRoomToList(room); + }); + const registerDiv = document.querySelector('#register'); - if (registerDiv) { + if (registerDiv && !config.userName) { registerDiv.classList.remove('hidden'); } @@ -185,7 +190,7 @@ async function continueRegistration(regUsername) { return; } - config.username = regUsername; + config.userName = regUsername; setupDiv.classList.remove('hidden'); document.querySelector('#register').classList.add('hidden'); loadingDiv.classList.add('hidden'); @@ -193,7 +198,7 @@ async function continueRegistration(regUsername) { const randomTopic = crypto.randomBytes(32); document.querySelector('#chat-room-topic').innerText = truncateHash(b4a.toString(randomTopic, 'hex')); - writeJsonToFile("./config.json", config); + writeConfigToFile("./config.json"); } async function createChatRoom() { @@ -240,7 +245,7 @@ function addRoomToList(topic) { roomList.appendChild(roomItem); config.rooms.push(topic); - writeJsonToFile("./config.json", config); + writeConfigToFile("./config.json"); } function switchRoom(topic) { @@ -262,7 +267,7 @@ function leaveRoom() { document.querySelector('#setup').classList.remove('hidden'); config.rooms = config.rooms.filter(e => e !== currentRoom.topic); - writeJsonToFile("./config.json", config); + writeConfigToFile("./config.json"); } function sendMessage(e) { @@ -397,7 +402,7 @@ async function updateIcon(username, avatarBuffer) { userIcon.src = avatarUrl; config.userAvatar = avatarUrl; - writeJsonToFile("./config.json", config); + writeConfigToFile("./config.json"); } } @@ -413,11 +418,18 @@ function toggleSetupView() { setupDiv.classList.toggle('hidden'); } -function writeJsonToFile(filePath, data) { - fs.writeFile(filePath, JSON.stringify(data), (err) => { +function writeConfigToFile(filePath) { + fs.writeFile(filePath, JSON.stringify(config), (err) => { if (err) return console.error(err); console.log("File has been created"); }); } +function readConfigFromFile(filePath) { + fs.readFile(filePath, 'utf8', (err, data) => { + if(err) return console.error("Error while reading config.json! ", err); + config = JSON.parse(data); + }); +} + initialize(); -- 2.39.3 From a9f50e892bdf6451ad70534b3ad72846f27b718e Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:46:42 +0300 Subject: [PATCH 04/14] Oopsie checking if config exists didnt work (It is done by checking if username is not empty lol) --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 7098498..dc224a3 100644 --- a/app.js +++ b/app.js @@ -101,7 +101,7 @@ async function initialize() { }); const registerDiv = document.querySelector('#register'); - if (registerDiv && !config.userName) { + if (registerDiv && config.userName !== '') { registerDiv.classList.remove('hidden'); } -- 2.39.3 From a3fbe7434a654d409b4c47f10ecb35de3b0c0cb0 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:50:05 +0300 Subject: [PATCH 05/14] Hmm will it work this way? --- app.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index dc224a3..3be3a05 100644 --- a/app.js +++ b/app.js @@ -95,13 +95,16 @@ async function initialize() { }); } - readConfigFromFile(); - config.rooms.forEach(room => { - addRoomToList(room); - }); + const configExists = fs.existsSync("./config.json"); + if (configExists) { + readConfigFromFile(); + config.rooms.forEach(room => { + addRoomToList(room); + }); + } const registerDiv = document.querySelector('#register'); - if (registerDiv && config.userName !== '') { + if (registerDiv && !configExists) { registerDiv.classList.remove('hidden'); } -- 2.39.3 From 8ca1cc2b8f2e934abc8d22a0961c22520f4e4515 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:53:11 +0300 Subject: [PATCH 06/14] I just forgot to read from the file LOL --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 3be3a05..856985f 100644 --- a/app.js +++ b/app.js @@ -97,7 +97,7 @@ async function initialize() { const configExists = fs.existsSync("./config.json"); if (configExists) { - readConfigFromFile(); + readConfigFromFile("./config.json"); config.rooms.forEach(room => { addRoomToList(room); }); -- 2.39.3 From b577ff320723b2348f1315ff7b5b78a20b40cac4 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:54:30 +0300 Subject: [PATCH 07/14] It doesnt want to use config for some reason. Time to debug stuff --- app.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app.js b/app.js index 856985f..796369d 100644 --- a/app.js +++ b/app.js @@ -96,8 +96,10 @@ async function initialize() { } const configExists = fs.existsSync("./config.json"); + console.log("Config exists:", configExists) if (configExists) { readConfigFromFile("./config.json"); + console.log("Config:", config) config.rooms.forEach(room => { addRoomToList(room); }); -- 2.39.3 From 2befd340393f6fd3732d0285e4c5e0733db6de00 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:56:12 +0300 Subject: [PATCH 08/14] Ohh reading is async, thats why it didnt work --- app.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 796369d..2370063 100644 --- a/app.js +++ b/app.js @@ -96,10 +96,9 @@ async function initialize() { } const configExists = fs.existsSync("./config.json"); - console.log("Config exists:", configExists) if (configExists) { - readConfigFromFile("./config.json"); - console.log("Config:", config) + await readConfigFromFile("./config.json"); + console.log("Read config from file:", config) config.rooms.forEach(room => { addRoomToList(room); }); @@ -430,9 +429,9 @@ function writeConfigToFile(filePath) { }); } -function readConfigFromFile(filePath) { - fs.readFile(filePath, 'utf8', (err, data) => { - if(err) return console.error("Error while reading config.json! ", err); +async function readConfigFromFile(filePath) { + await fs.readFile(filePath, 'utf8', (err, data) => { + if(err) return console.log("Error while reading config.json! ", err); config = JSON.parse(data); }); } -- 2.39.3 From 4a881ed721258e05b33996a7bd95148ec6a5df78 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:57:11 +0300 Subject: [PATCH 09/14] Its actually not the reason... --- app.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app.js b/app.js index 2370063..58063d1 100644 --- a/app.js +++ b/app.js @@ -432,6 +432,7 @@ function writeConfigToFile(filePath) { async function readConfigFromFile(filePath) { await fs.readFile(filePath, 'utf8', (err, data) => { if(err) return console.log("Error while reading config.json! ", err); + console.log(data) config = JSON.parse(data); }); } -- 2.39.3 From 13e402092070cb88b5e7c4d2c2fab30bb44ad124 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:58:35 +0300 Subject: [PATCH 10/14] It just doesnt want to put proper data to the config for some strange reason --- app.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 58063d1..9fc6448 100644 --- a/app.js +++ b/app.js @@ -432,8 +432,9 @@ function writeConfigToFile(filePath) { async function readConfigFromFile(filePath) { await fs.readFile(filePath, 'utf8', (err, data) => { if(err) return console.log("Error while reading config.json! ", err); - console.log(data) - config = JSON.parse(data); + config.userName = data.userName; + config.userAvatar = data.userAvatar; + config.rooms = data.rooms; }); } -- 2.39.3 From f5f8d0ce190812f6577552168ed371659b39bb9d Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 10:00:01 +0300 Subject: [PATCH 11/14] And I honestly have no idea why it doesnt want to work --- app.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app.js b/app.js index 9fc6448..1b62e10 100644 --- a/app.js +++ b/app.js @@ -432,6 +432,9 @@ function writeConfigToFile(filePath) { async function readConfigFromFile(filePath) { await fs.readFile(filePath, 'utf8', (err, data) => { if(err) return console.log("Error while reading config.json! ", err); + console.log("1:", data); + data = JSON.parse(data); + console.log("2:", data); config.userName = data.userName; config.userAvatar = data.userAvatar; config.rooms = data.rooms; -- 2.39.3 From f6f804ab399a8bde7cb903edfb55b1b4c3d1d13f Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 10:01:01 +0300 Subject: [PATCH 12/14] It actually works now but i want to get it to work properly --- app.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 1b62e10..28aa808 100644 --- a/app.js +++ b/app.js @@ -431,13 +431,9 @@ function writeConfigToFile(filePath) { async function readConfigFromFile(filePath) { await fs.readFile(filePath, 'utf8', (err, data) => { - if(err) return console.log("Error while reading config.json! ", err); - console.log("1:", data); + if(err) return console.log("Error while reading config.json! ", err) data = JSON.parse(data); - console.log("2:", data); - config.userName = data.userName; - config.userAvatar = data.userAvatar; - config.rooms = data.rooms; + config = data; }); } -- 2.39.3 From f3b47558218135c165d9d8c87d21804ab9e6c5af Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 10:03:23 +0300 Subject: [PATCH 13/14] Very strange stuff tbh --- app.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/app.js b/app.js index 28aa808..9a3dd18 100644 --- a/app.js +++ b/app.js @@ -97,7 +97,7 @@ async function initialize() { const configExists = fs.existsSync("./config.json"); if (configExists) { - await readConfigFromFile("./config.json"); + config = JSON.parse(fs.readFileSync("./config.json", 'utf8')); console.log("Read config from file:", config) config.rooms.forEach(room => { addRoomToList(room); @@ -429,12 +429,4 @@ function writeConfigToFile(filePath) { }); } -async function readConfigFromFile(filePath) { - await fs.readFile(filePath, 'utf8', (err, data) => { - if(err) return console.log("Error while reading config.json! ", err) - data = JSON.parse(data); - config = data; - }); -} - initialize(); -- 2.39.3 From 0077989253a5d956372f174904b104718db50b98 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Sun, 9 Jun 2024 10:08:29 +0300 Subject: [PATCH 14/14] It acutally works just fine now yay --- app.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 9a3dd18..8ceceb6 100644 --- a/app.js +++ b/app.js @@ -100,7 +100,7 @@ async function initialize() { config = JSON.parse(fs.readFileSync("./config.json", 'utf8')); console.log("Read config from file:", config) config.rooms.forEach(room => { - addRoomToList(room); + addRoomToListWithoutWritingToConfig(room); }); } @@ -252,6 +252,15 @@ function addRoomToList(topic) { writeConfigToFile("./config.json"); } +function addRoomToListWithoutWritingToConfig(topic) { + const roomList = document.querySelector('#room-list'); + const roomItem = document.createElement('li'); + roomItem.textContent = truncateHash(topic); + roomItem.dataset.topic = topic; + roomItem.addEventListener('click', () => switchRoom(topic)); + roomList.appendChild(roomItem); +} + function switchRoom(topic) { const topicBuffer = b4a.from(topic, 'hex'); joinSwarm(topicBuffer); -- 2.39.3