diff --git a/app.js b/app.js index 5a5a527..6c3dbf7 100644 --- a/app.js +++ b/app.js @@ -307,7 +307,7 @@ function sendMessage(e) { message, avatar: config.userAvatar, topic: b4a.toString(currentRoom.topic, 'hex'), - peers: peersPublicKeys, + peers: peersPublicKeys, // Deprecated. To be deleted in future updates timestamp: Date.now(), readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp }); @@ -327,6 +327,7 @@ function sendFileMessage(name, fileUrl, fileType, avatar) { fileUrl, fileType, avatar, + topic: b4a.toString(currentRoom.topic, 'hex'), timestamp: Date.now(), }); diff --git a/chatBot/bot.js b/chatBot/bot.js index b2844c3..2dcb9ba 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import Client from './includes/client.js'; // Adjust the import path as necessary +import Client from './includes/Client.js'; // Adjust the import path as necessary import 'dotenv/config'; // Create a new instance of the chatBot class with a valid botName @@ -39,7 +39,6 @@ loadCommands().then(commands => { // We use Event Emitter here to handle new messages bot.on('onMessage', (peer, message) => { - if (message.type == "icon") return; console.log(message); console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); diff --git a/chatBot/includes/chatroom.js b/chatBot/includes/ChatRoom.js similarity index 98% rename from chatBot/includes/chatroom.js rename to chatBot/includes/ChatRoom.js index 89b2afe..0686013 100644 --- a/chatBot/includes/chatroom.js +++ b/chatBot/includes/ChatRoom.js @@ -3,8 +3,6 @@ class ChatRoom { this.topic = topic; this.peers = peers; } - - } export default ChatRoom; \ No newline at end of file diff --git a/chatBot/includes/client.js b/chatBot/includes/Client.js similarity index 67% rename from chatBot/includes/client.js rename to chatBot/includes/Client.js index 09964f5..426b094 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/Client.js @@ -1,5 +1,6 @@ import Hyperswarm from 'hyperswarm'; import EventEmitter from 'node:events' +import b4a from "b4a"; class Client extends EventEmitter { constructor(botName) { @@ -12,7 +13,12 @@ class Client extends EventEmitter { setupSwarm() { this.swarm.on('connection', (peer) => { - peer.on('data', message => this.emit('onMessage', peer, JSON.parse(message.toString()))); + peer.on('data', message => { + if(message.type === "message") this.emit('onMessage', peer, JSON.parse(message.toString())); + if(message.type === "icon") this.emit('onIcon', peer, JSON.parse(message.toString())); + if(message.type === "file") this.emit('onFile', peer, JSON.parse(message.toString())); + }); + peer.on('error', e => { this.emit('onError', e); console.error(`Connection error: ${e}`) @@ -20,7 +26,16 @@ class Client extends EventEmitter { }); this.swarm.on('update', () => { - console.log(`Peers count: ${this.swarm.connections.size}`); + console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); + + this.swarm.peers.forEach((peerInfo, peerId) => { + // Please do not try to understand what is going on here. I have no idea anyway. But it surprisingly works + + const peer = [peerId]; + const peerTopics = [peerInfo.topics] + .filter(topics => topics) + .map(topics => topics.map(topic => b4a.toString(topic, 'hex'))); + }); }); } diff --git a/chatBot/includes/FileMessage.js b/chatBot/includes/FileMessage.js new file mode 100644 index 0000000..55d58b4 --- /dev/null +++ b/chatBot/includes/FileMessage.js @@ -0,0 +1,12 @@ +class FileMessage { + public FileMessage(chatRoom, userPeer, fileName, fileUrl, fileType, timestamp) { + this.chatRoom = chatRoom; + this.userPeer = userPeer; + this.fileName = fileName; + this.fileUrl = fileUrl; + this.fileType = fileType; + this.timestamp = timestamp; + } +} + +export default FileMessage; \ No newline at end of file diff --git a/chatBot/includes/TextMessage.js b/chatBot/includes/TextMessage.js new file mode 100644 index 0000000..f3a8021 --- /dev/null +++ b/chatBot/includes/TextMessage.js @@ -0,0 +1,10 @@ +class TextMessage { + public TextMessage(chatRoom, userPeer, message, timestamp) { + this.chatRoom = chatRoom; + this.userPeer = userPeer; + this.message = message; + this.timestamp = timestamp; + } +} + +export default TextMessage; \ No newline at end of file diff --git a/chatBot/includes/UserPeer.js b/chatBot/includes/UserPeer.js new file mode 100644 index 0000000..9546499 --- /dev/null +++ b/chatBot/includes/UserPeer.js @@ -0,0 +1,10 @@ +class UserPeer { + public UserPeer(peer, topics, username, avatar) { + this.peer = peer; + this.topics = topics; + this.username = username; + this.avatar = avatar; + } +} + +export default UserPeer; \ No newline at end of file