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] 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();