Tests for user icons with eventEmitter

This commit is contained in:
Raven Scott 2024-06-08 16:15:55 -04:00
parent c15b298268
commit dd8099649f

13
app.js
View File

@ -66,10 +66,11 @@ async function initialize() {
registerDiv.classList.remove('hidden'); registerDiv.classList.remove('hidden');
} }
eventEmitter.on('onMessage', (messageObj) => { eventEmitter.on('onMessage', async (messageObj) => {
if (messageObj.type === 'icon') { if (messageObj.type === 'icon') {
const username = messageObj.username; const username = messageObj.username;
const avatarBuffer = Buffer.from(messageObj.avatar, 'base64'); const avatarBuffer = Buffer.from(messageObj.avatar, 'base64');
await drive.put(`/icons/${username}.png`, avatarBuffer);
updateIcon(username, avatarBuffer); updateIcon(username, avatarBuffer);
} else { } else {
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar); onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
@ -125,9 +126,9 @@ function registerUser(e) {
const avatarFile = document.querySelector('#avatar-file').files[0]; const avatarFile = document.querySelector('#avatar-file').files[0];
if (avatarFile) { if (avatarFile) {
const reader = new FileReader(); const reader = new FileReader();
reader.onload = (event) => { reader.onload = async (event) => {
const buffer = new Uint8Array(event.target.result); const buffer = new Uint8Array(event.target.result);
drive.put(`/icons/${regUsername}.png`, buffer); await drive.put(`/icons/${regUsername}.png`, buffer);
userAvatar = `http://localhost:1337/icons/${regUsername}.png`; // Set the correct URL userAvatar = `http://localhost:1337/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));
@ -288,11 +289,11 @@ function truncateHash(hash) {
} }
async function updateIcon(username, avatarBuffer) { async function updateIcon(username, avatarBuffer) {
// Update the icon in the local HTML if necessary
// This can be adjusted as per your needs
const userIcon = document.querySelector(`img[src*="${username}.png"]`); const userIcon = document.querySelector(`img[src*="${username}.png"]`);
if (userIcon) { if (userIcon) {
userIcon.src = `http://localhost:1337/icons/${username}.png`; // Ensure the URL is correct const avatarBlob = new Blob([avatarBuffer], { type: 'image/png' });
const avatarUrl = URL.createObjectURL(avatarBlob);
userIcon.src = avatarUrl;
} }
} }