From 7dbcf174295867ee8c0707c57b5f2a4104216117 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Fri, 14 Jun 2024 00:33:19 -0400 Subject: [PATCH] Duplicate message fix --- app.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app.js b/app.js index e5d0b70..8b7e586 100644 --- a/app.js +++ b/app.js @@ -141,7 +141,7 @@ function handleIncomingMessage(messageObj) { const username = messageObj.username; if (messageObj.avatar) { const avatarBuffer = b4a.from(messageObj.avatar, 'base64'); - drive.put(`/icons/${username}.png`, avatarBuffer); + drive.put(`/icons/${username}.png`, avatarBuffer); updateIcon(username, avatarBuffer); } else { console.error('Received icon message with missing avatar data:', messageObj); @@ -157,7 +157,7 @@ function handleIncomingMessage(messageObj) { console.error('Received file message with missing file data or fileName:', messageObj); } } else if (messageObj.type === 'message') { - onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar, messageObj.topic); + onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar, messageObj.topic, messageObj.timestamp); } else if (messageObj.type === 'audio') { const audioBuffer = b4a.from(messageObj.audio, 'base64'); const filePath = `/audio/${Date.now()}.webm`; @@ -206,6 +206,7 @@ async function handleConnection(connection, info) { updatePeerCount(); } + function retryConnection(topicBuffer) { const topic = b4a.toString(topicBuffer, 'hex'); const room = activeRooms.find(room => room.topic === topic); @@ -490,10 +491,11 @@ function sendMessage(e) { document.querySelector('#message').value = ''; const topic = currentTopic(); + const timestamp = Date.now(); console.log('Sending message:', message); // Debugging log - onMessageAdded(config.userName, message, config.userAvatar, topic); + onMessageAdded(config.userName, message, config.userAvatar, topic, timestamp); const messageObj = JSON.stringify({ type: 'message', @@ -501,7 +503,7 @@ function sendMessage(e) { message, avatar: config.userAvatar, topic: topic, - timestamp: Date.now() + timestamp: timestamp }); const peers = [...activeRooms.find(room => room.topic === topic).swarm.connections]; @@ -597,12 +599,13 @@ function scrollToBottom() { container.scrollTop = container.scrollHeight; } -function onMessageAdded(from, message, avatar, topic) { +function onMessageAdded(from, message, avatar, topic, timestamp) { console.log('Adding message:', { from, message, avatar, topic }); // Debugging log const messageObj = { from, message, - avatar + avatar, + timestamp: timestamp || Date.now() }; // Add the message to the store @@ -756,7 +759,7 @@ function renderMessagesForRoom(topic) { // Fetch and render messages for the selected room const messages = getMessagesForRoom(topic); messages.forEach(message => { - onMessageAdded(message.from, message.message, message.avatar, topic); + onMessageAdded(message.from, message.message, message.avatar, topic, message.timestamp); }); } @@ -769,10 +772,10 @@ function addMessageToStore(topic, messageObj) { messagesStore[topic] = []; } - // Check for duplicates + // Check for duplicates using a combination of message content and timestamp const isDuplicate = messagesStore[topic].some(msg => - msg.from === messageObj.from && - msg.message === messageObj.message && + msg.from === messageObj.from && + msg.message === messageObj.message && msg.timestamp === messageObj.timestamp ); @@ -807,4 +810,4 @@ async function connectToAllRooms() { // Call this function when loading the rooms initially renderRoomList(); -initialize(); \ No newline at end of file +initialize();