Up to date fork #1

Merged
MiTask merged 33 commits from snxraven/LinkUp-P2P-Chat:main into main 2024-06-14 04:32:25 -04:00
Showing only changes of commit 7dbcf17429 - Show all commits

25
app.js
View File

@ -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();
initialize();