Got rid of userName and userAvatar in exchange of config.userName and config.userAvatar

This commit is contained in:
MrMasrozYTLIVE 2024-06-09 09:32:33 +03:00
parent 89ebe02743
commit d33d31debf

22
app.js
View File

@ -14,8 +14,6 @@ const drive = new Hyperdrive(store);
await drive.ready(); await drive.ready();
let swarm; let swarm;
let userName = 'Anonymous';
let userAvatar = '';
let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {}; let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {};
let peerCount = 0; let peerCount = 0;
let currentRoom = null; let currentRoom = null;
@ -26,7 +24,7 @@ let servePort;
// Object to store all the information we want to save // Object to store all the information we want to save
let config = { let config = {
username: '', userName: 'Anonymous',
userAvatar: '', userAvatar: '',
rooms: [] rooms: []
}; };
@ -90,7 +88,7 @@ async function initialize() {
const filePath = `/files/${file.name}`; const filePath = `/files/${file.name}`;
await drive.put(filePath, buffer); await drive.put(filePath, buffer);
const fileUrl = `http://localhost:${servePort}${filePath}`; const fileUrl = `http://localhost:${servePort}${filePath}`;
sendFileMessage(userName, fileUrl, file.type, userAvatar); sendFileMessage(config.userName, fileUrl, file.type, config.userAvatar);
}; };
reader.readAsArrayBuffer(file); reader.readAsArrayBuffer(file);
} }
@ -121,11 +119,11 @@ async function initialize() {
console.log('Peer connected, current peer count:', peerCount); console.log('Peer connected, current peer count:', peerCount);
// Send the current user's icon to the new peer // 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) { if (iconBuffer) {
const iconMessage = JSON.stringify({ const iconMessage = JSON.stringify({
type: 'icon', type: 'icon',
username: userName, username: config.userName,
avatar: iconBuffer.toString('base64'), avatar: iconBuffer.toString('base64'),
}); });
connection.write(iconMessage); connection.write(iconMessage);
@ -167,8 +165,8 @@ 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);
config.userAvatar = userAvatar = `http://localhost:${servePort}/icons/${regUsername}.png`; // Set the correct URL config.userAvatar = `http://localhost:${servePort}/icons/${regUsername}.png`; // Set the correct URL
registeredUsers[regUsername] = userAvatar; registeredUsers[regUsername] = config.userAvatar;
localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers)); localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers));
continueRegistration(regUsername); continueRegistration(regUsername);
}; };
@ -187,7 +185,7 @@ async function continueRegistration(regUsername) {
return; return;
} }
config.username = userName = regUsername; config.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');
@ -272,13 +270,13 @@ function sendMessage(e) {
const message = document.querySelector('#message').value; const message = document.querySelector('#message').value;
document.querySelector('#message').value = ''; document.querySelector('#message').value = '';
onMessageAdded(userName, message, userAvatar); onMessageAdded(config.userName, message, config.userAvatar);
const messageObj = JSON.stringify({ const messageObj = JSON.stringify({
type: 'message', type: 'message',
name: userName, name: config.userName,
message, message,
avatar: userAvatar, avatar: config.userAvatar,
timestamp: Date.now(), timestamp: Date.now(),
}); });