forked from snxraven/LinkUp-P2P-Chat
Started working on saving the user data on actions like registration, joining and leaving rooms. Lacks of loading on start up.
This commit is contained in:
parent
2fa431886c
commit
3947e78aef
30
app.js
30
app.js
@ -5,6 +5,7 @@ import ServeDrive from 'serve-drive';
|
|||||||
import Hyperdrive from 'hyperdrive';
|
import Hyperdrive from 'hyperdrive';
|
||||||
import Corestore from 'corestore';
|
import Corestore from 'corestore';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
const storagePath = `./storage/storage_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
|
const storagePath = `./storage/storage_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
|
||||||
const store = new Corestore(storagePath);
|
const store = new Corestore(storagePath);
|
||||||
@ -23,6 +24,13 @@ const eventEmitter = new EventEmitter();
|
|||||||
// Define servePort at the top level
|
// Define servePort at the top level
|
||||||
let servePort;
|
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 to get a random port between 1337 and 2223
|
||||||
function getRandomPort() {
|
function getRandomPort() {
|
||||||
return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152;
|
return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152;
|
||||||
@ -136,7 +144,7 @@ function registerUser(e) {
|
|||||||
reader.onload = async (event) => {
|
reader.onload = async (event) => {
|
||||||
const buffer = new Uint8Array(event.target.result);
|
const buffer = new Uint8Array(event.target.result);
|
||||||
await drive.put(`/icons/${regUsername}.png`, buffer);
|
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;
|
registeredUsers[regUsername] = userAvatar;
|
||||||
localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers));
|
localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers));
|
||||||
continueRegistration(regUsername);
|
continueRegistration(regUsername);
|
||||||
@ -156,13 +164,15 @@ async function continueRegistration(regUsername) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
userName = regUsername;
|
config.username = userName = regUsername;
|
||||||
setupDiv.classList.remove('hidden');
|
setupDiv.classList.remove('hidden');
|
||||||
document.querySelector('#register').classList.add('hidden');
|
document.querySelector('#register').classList.add('hidden');
|
||||||
loadingDiv.classList.add('hidden');
|
loadingDiv.classList.add('hidden');
|
||||||
|
|
||||||
const randomTopic = crypto.randomBytes(32);
|
const randomTopic = crypto.randomBytes(32);
|
||||||
document.querySelector('#chat-room-topic').innerText = truncateHash(b4a.toString(randomTopic, 'hex'));
|
document.querySelector('#chat-room-topic').innerText = truncateHash(b4a.toString(randomTopic, 'hex'));
|
||||||
|
|
||||||
|
writeJsonToFile("./config.json", config);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createChatRoom() {
|
async function createChatRoom() {
|
||||||
@ -207,6 +217,9 @@ function addRoomToList(topic) {
|
|||||||
roomItem.dataset.topic = topic;
|
roomItem.dataset.topic = topic;
|
||||||
roomItem.addEventListener('click', () => switchRoom(topic));
|
roomItem.addEventListener('click', () => switchRoom(topic));
|
||||||
roomList.appendChild(roomItem);
|
roomList.appendChild(roomItem);
|
||||||
|
|
||||||
|
config.rooms.push(topic);
|
||||||
|
writeJsonToFile("./config.json", config);
|
||||||
}
|
}
|
||||||
|
|
||||||
function switchRoom(topic) {
|
function switchRoom(topic) {
|
||||||
@ -226,6 +239,9 @@ function leaveRoom() {
|
|||||||
}
|
}
|
||||||
document.querySelector('#chat').classList.add('hidden');
|
document.querySelector('#chat').classList.add('hidden');
|
||||||
document.querySelector('#setup').classList.remove('hidden');
|
document.querySelector('#setup').classList.remove('hidden');
|
||||||
|
|
||||||
|
config.rooms = config.rooms.filter(e => e !== currentRoom.topic);
|
||||||
|
writeJsonToFile("./config.json", config);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendMessage(e) {
|
function sendMessage(e) {
|
||||||
@ -301,6 +317,9 @@ async function updateIcon(username, avatarBuffer) {
|
|||||||
const avatarBlob = new Blob([avatarBuffer], { type: 'image/png' });
|
const avatarBlob = new Blob([avatarBuffer], { type: 'image/png' });
|
||||||
const avatarUrl = URL.createObjectURL(avatarBlob);
|
const avatarUrl = URL.createObjectURL(avatarBlob);
|
||||||
userIcon.src = avatarUrl;
|
userIcon.src = avatarUrl;
|
||||||
|
|
||||||
|
config.userAvatar = avatarUrl;
|
||||||
|
writeJsonToFile("./config.json", config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,4 +335,11 @@ function toggleSetupView() {
|
|||||||
setupDiv.classList.toggle('hidden');
|
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();
|
initialize();
|
||||||
|
Loading…
Reference in New Issue
Block a user