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
2 changed files with 74 additions and 71 deletions
Showing only changes of commit b50bd0bbab - Show all commits

116
app.js
View File

@ -130,6 +130,7 @@ async function initialize() {
if (messageObj.type === 'icon') { if (messageObj.type === 'icon') {
const username = messageObj.username; const username = messageObj.username;
const topic = messageObj.topic;
if (messageObj.avatar) { if (messageObj.avatar) {
const avatarBuffer = b4a.from(messageObj.avatar, 'base64'); const avatarBuffer = b4a.from(messageObj.avatar, 'base64');
await drive.put(`/icons/${username}.png`, avatarBuffer); await drive.put(`/icons/${username}.png`, avatarBuffer);
@ -137,6 +138,7 @@ async function initialize() {
} else { } else {
console.error('Received icon message with missing avatar data:', messageObj); console.error('Received icon message with missing avatar data:', messageObj);
} }
// updatePeerCount(topic);
} else if (messageObj.type === 'file') { } else if (messageObj.type === 'file') {
if (messageObj.file && messageObj.fileName) { if (messageObj.file && messageObj.fileName) {
const fileBuffer = b4a.from(messageObj.file, 'base64'); const fileBuffer = b4a.from(messageObj.file, 'base64');
@ -161,20 +163,38 @@ async function initialize() {
swarm.on('connection', async (connection, info) => { swarm.on('connection', async (connection, info) => {
try { try {
const discovery = [...discoveryTopicsMap.entries()].find(([key, value]) => key.id === info.id); connection.on('data', (data) => {
const topic = discovery ? discovery[1] : null; const messageObj = JSON.parse(data.toString());
if (messageObj.type === 'icon' && messageObj.topic) {
connection.topic = messageObj.topic;
if (!peerCounts[messageObj.topic]) {
peerCounts[messageObj.topic] = 0;
}
peerCounts[messageObj.topic]++;
updateHeaderPeerCount(messageObj.topic);
}
eventEmitter.emit('onMessage', messageObj);
});
if (!topic) { connection.on('close', () => {
console.error('No topic found in connection info:', info); const topic = connection.topic;
return; if (topic && peerCounts[topic]) {
} peerCounts[topic]--;
updateHeaderPeerCount(topic);
console.log('Peer disconnected, current peer count:', peerCounts[topic]);
}
});
if (!peerCounts[topic]) { // Sending current user's topic to the new peer
peerCounts[topic] = 0; const currentTopic = document.querySelector('#chat-room-topic').innerText;
connection.topic = currentTopic;
if (!peerCounts[currentTopic]) {
peerCounts[currentTopic] = 0;
} }
peerCounts[topic]++; peerCounts[currentTopic]++;
updatePeerCount(topic); updateHeaderPeerCount(currentTopic);
console.log('Peer connected, current peer count:', peerCounts[topic]); console.log('Peer connected, current peer count:', peerCounts[currentTopic]);
// 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/${config.userName}.png`); const iconBuffer = await drive.get(`/icons/${config.userName}.png`);
@ -183,21 +203,17 @@ async function initialize() {
type: 'icon', type: 'icon',
username: config.userName, username: config.userName,
avatar: b4a.toString(iconBuffer, 'base64'), avatar: b4a.toString(iconBuffer, 'base64'),
topic topic: currentTopic
});
connection.write(iconMessage);
} else {
const iconMessage = JSON.stringify({
type: 'icon',
username: config.userName,
topic: currentTopic
}); });
connection.write(iconMessage); connection.write(iconMessage);
} }
connection.on('data', (data) => {
const messageObj = JSON.parse(data.toString());
eventEmitter.emit('onMessage', messageObj);
});
connection.on('close', () => {
peerCounts[topic]--;
updatePeerCount(topic);
console.log('Peer disconnected, current peer count:', peerCounts[topic]);
});
} catch (error) { } catch (error) {
console.error('Error handling connection:', error); console.error('Error handling connection:', error);
} }
@ -601,27 +617,6 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar, topic) {
} }
} }
function updatePeerCount(topic) {
let peerCountElement = document.querySelector(`#peers-count-${topic}`);
if (!peerCountElement) {
const roomItem = document.querySelector(`#room-list li[data-topic="${topic}"]`);
if (roomItem) {
peerCountElement = document.createElement('span');
peerCountElement.id = `peers-count-${topic}`;
// roomItem.appendChild(peerCountElement);
}
}
if (peerCountElement) {
peerCountElement.textContent = ` (${peerCounts[topic]})`; // Display the peer count for the specific topic
}
// Update the header peer count if the current room matches the topic
const currentTopic = document.querySelector('#chat-room-topic').innerText;
if (currentTopic === topic) {
updateHeaderPeerCount(topic);
}
}
function updateHeaderPeerCount(topic) { function updateHeaderPeerCount(topic) {
const peerCountElement = document.querySelector('#peers-count'); const peerCountElement = document.querySelector('#peers-count');
if (peerCountElement) { if (peerCountElement) {
@ -637,12 +632,13 @@ function scrollToBottom() {
function onMessageAdded(from, message, avatar, topic) { function onMessageAdded(from, message, avatar, topic) {
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
}; };
// Add the message to the store // Add the message to the store
addMessageToStore(topic, messageObj); addMessageToStore(topic, messageObj);
@ -667,19 +663,24 @@ function onMessageAdded(from, message, avatar, topic) {
const $text = document.createElement('div'); const $text = document.createElement('div');
$text.classList.add('message-text'); $text.classList.add('message-text');
const md = window.markdownit({ if (typeof message === 'string') {
highlight: function (str, lang) { const md = window.markdownit({
if (lang && hljs.getLanguage(lang)) { highlight: function (str, lang) {
try { if (lang && hljs.getLanguage(lang)) {
return hljs.highlight(str, { language: lang }).value; try {
} catch (__) {} return hljs.highlight(str, { language: lang }).value;
} catch (__) {}
}
return ''; // use external default escaping
} }
return ''; // use external default escaping });
}
});
const markdownContent = md.render(message); const markdownContent = md.render(message);
$text.innerHTML = markdownContent; $text.innerHTML = markdownContent;
} else {
console.error('Message content is not a string:', message);
$text.textContent = 'Invalid message content.';
}
$content.appendChild($header); $content.appendChild($header);
$content.appendChild($text); $content.appendChild($text);
@ -799,7 +800,6 @@ function renderMessagesForRoom(topic) {
onMessageAdded(message.from, message.message, message.avatar, topic); onMessageAdded(message.from, message.message, message.avatar, topic);
}); });
} }
function getMessagesForRoom(topic) { function getMessagesForRoom(topic) {
return messagesStore[topic] || []; return messagesStore[topic] || [];
} }
@ -814,4 +814,4 @@ function addMessageToStore(topic, messageObj) {
// Call this function when loading the rooms initially // Call this function when loading the rooms initially
renderRoomList(); renderRoomList();
initialize(); initialize();

View File

@ -5,6 +5,7 @@ import 'dotenv/config';
// Create a new instance of the chatBot class with a valid botName // Create a new instance of the chatBot class with a valid botName
const botName = process.env.BOT_NAME; // Replace 'MyBot' with the desired bot name const botName = process.env.BOT_NAME; // Replace 'MyBot' with the desired bot name
const linkupRoomId = process.env.LINKUP_ROOM_ID; // Use the environment variable for room ID
// Load commands from the 'commands' directory // Load commands from the 'commands' directory
const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname); const commandsDir = path.join(new URL('./commands/', import.meta.url).pathname);
@ -41,29 +42,31 @@ loadCommands().then(commands => {
bot.on('onMessage', (peer, message) => { bot.on('onMessage', (peer, message) => {
console.log(message); console.log(message);
console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); console.log(`Message received from ${message.peerName}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`);
// Check if the message starts with a command prefix // Handle all messages as potential AI commands
if (message.message.startsWith('!')) { const userMessage = message.message;
// Extract the command and arguments
const [command, ...args] = message.message.slice(1).split(' ');
// Find the corresponding command handler // Find the corresponding command handler (assuming the AI command handler is in 'commands/ai.js')
const commandHandler = commands[command.toLowerCase()]; const commandHandler = commands['ai'];
// If the command exists, execute its handler // If the command exists, execute its handler
if (commandHandler && typeof commandHandler.handler === 'function') { if (commandHandler && typeof commandHandler.handler === 'function') {
commandHandler.handler(bot, args, message); commandHandler.handler(bot, [userMessage], message);
}
} }
}); });
bot.on('onBotJoinRoom', () => { bot.on('onBotJoinRoom', () => {
console.log("Bot is ready!"); console.log("Bot is ready!");
bot.sendTextMessage(process.env.ON_READY_MESSAGE); // Include topic in the message sent when the bot joins the room
bot.sendTextMessage({
text: process.env.ON_READY_MESSAGE,
topic: linkupRoomId
});
}); });
bot.joinChatRoom(process.env.LINKUP_ROOM_ID); // Ensure the bot joins the chat room with the topic information
bot.joinChatRoom(linkupRoomId);
}).catch(error => { }).catch(error => {
console.error('Error loading commands:', error); console.error('Error loading commands:', error);
}); });