Duplicate message fix
This commit is contained in:
parent
98aa79f075
commit
7dbcf17429
19
app.js
19
app.js
@ -141,7 +141,7 @@ function handleIncomingMessage(messageObj) {
|
|||||||
const username = messageObj.username;
|
const username = messageObj.username;
|
||||||
if (messageObj.avatar) {
|
if (messageObj.avatar) {
|
||||||
const avatarBuffer = b4a.from(messageObj.avatar, 'base64');
|
const avatarBuffer = b4a.from(messageObj.avatar, 'base64');
|
||||||
drive.put(`/icons/${username}.png`, avatarBuffer);
|
drive.put(`/icons/${username}.png`, avatarBuffer);
|
||||||
updateIcon(username, avatarBuffer);
|
updateIcon(username, avatarBuffer);
|
||||||
} else {
|
} else {
|
||||||
console.error('Received icon message with missing avatar data:', messageObj);
|
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);
|
console.error('Received file message with missing file data or fileName:', messageObj);
|
||||||
}
|
}
|
||||||
} else if (messageObj.type === 'message') {
|
} 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') {
|
} else if (messageObj.type === 'audio') {
|
||||||
const audioBuffer = b4a.from(messageObj.audio, 'base64');
|
const audioBuffer = b4a.from(messageObj.audio, 'base64');
|
||||||
const filePath = `/audio/${Date.now()}.webm`;
|
const filePath = `/audio/${Date.now()}.webm`;
|
||||||
@ -206,6 +206,7 @@ async function handleConnection(connection, info) {
|
|||||||
|
|
||||||
updatePeerCount();
|
updatePeerCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
function retryConnection(topicBuffer) {
|
function retryConnection(topicBuffer) {
|
||||||
const topic = b4a.toString(topicBuffer, 'hex');
|
const topic = b4a.toString(topicBuffer, 'hex');
|
||||||
const room = activeRooms.find(room => room.topic === topic);
|
const room = activeRooms.find(room => room.topic === topic);
|
||||||
@ -490,10 +491,11 @@ function sendMessage(e) {
|
|||||||
document.querySelector('#message').value = '';
|
document.querySelector('#message').value = '';
|
||||||
|
|
||||||
const topic = currentTopic();
|
const topic = currentTopic();
|
||||||
|
const timestamp = Date.now();
|
||||||
|
|
||||||
console.log('Sending message:', message); // Debugging log
|
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({
|
const messageObj = JSON.stringify({
|
||||||
type: 'message',
|
type: 'message',
|
||||||
@ -501,7 +503,7 @@ function sendMessage(e) {
|
|||||||
message,
|
message,
|
||||||
avatar: config.userAvatar,
|
avatar: config.userAvatar,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
timestamp: Date.now()
|
timestamp: timestamp
|
||||||
});
|
});
|
||||||
|
|
||||||
const peers = [...activeRooms.find(room => room.topic === topic).swarm.connections];
|
const peers = [...activeRooms.find(room => room.topic === topic).swarm.connections];
|
||||||
@ -597,12 +599,13 @@ function scrollToBottom() {
|
|||||||
container.scrollTop = container.scrollHeight;
|
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
|
console.log('Adding message:', { from, message, avatar, topic }); // Debugging log
|
||||||
const messageObj = {
|
const messageObj = {
|
||||||
from,
|
from,
|
||||||
message,
|
message,
|
||||||
avatar
|
avatar,
|
||||||
|
timestamp: timestamp || Date.now()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add the message to the store
|
// Add the message to the store
|
||||||
@ -756,7 +759,7 @@ function renderMessagesForRoom(topic) {
|
|||||||
// Fetch and render messages for the selected room
|
// Fetch and render messages for the selected room
|
||||||
const messages = getMessagesForRoom(topic);
|
const messages = getMessagesForRoom(topic);
|
||||||
messages.forEach(message => {
|
messages.forEach(message => {
|
||||||
onMessageAdded(message.from, message.message, message.avatar, topic);
|
onMessageAdded(message.from, message.message, message.avatar, topic, message.timestamp);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -769,7 +772,7 @@ function addMessageToStore(topic, messageObj) {
|
|||||||
messagesStore[topic] = [];
|
messagesStore[topic] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for duplicates
|
// Check for duplicates using a combination of message content and timestamp
|
||||||
const isDuplicate = messagesStore[topic].some(msg =>
|
const isDuplicate = messagesStore[topic].some(msg =>
|
||||||
msg.from === messageObj.from &&
|
msg.from === messageObj.from &&
|
||||||
msg.message === messageObj.message &&
|
msg.message === messageObj.message &&
|
||||||
|
Loading…
Reference in New Issue
Block a user