From 033330fd0423027b4de9b8bf903ca7031527bc5a Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 20:46:59 +0300 Subject: [PATCH 01/17] started working on parsing peers & topics they're currently in on swarm update event using 'swarm.peers' --- chatBot/includes/client.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 09964f5..5d68b3f 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -20,7 +20,11 @@ class Client extends EventEmitter { }); this.swarm.on('update', () => { - console.log(`Peers count: ${this.swarm.connections.size}`); + console.log(`Peers count: ${this.swarm.connections.size} (${this.swarm.peers.size})`); + + this.swarm.peers.forEach((peer, peerInfo) => { + console.log(`Peer ${peer} is in topic(s) ${[...peerInfo.topics].map(topic => topic.toString('hex'))}`); + }); }); } From 66d684e473f718faaf9da0c95b5d4aa1d47c341f Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 20:56:02 +0300 Subject: [PATCH 02/17] Filter all the peerInfo topics that are undefined and then map all of them to hex string --- chatBot/includes/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 5d68b3f..33ffb91 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -23,7 +23,7 @@ class Client extends EventEmitter { console.log(`Peers count: ${this.swarm.connections.size} (${this.swarm.peers.size})`); this.swarm.peers.forEach((peer, peerInfo) => { - console.log(`Peer ${peer} is in topic(s) ${[...peerInfo.topics].map(topic => topic.toString('hex'))}`); + console.log(`Peer ${peer} is in topic(s) ${[peerInfo.topics].filter(topic => topic).map(topic => topic.toString('hex'))}`); }); }); } From acb5609d0395e70b54ae8ff95a715b172e0f4969 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 20:57:32 +0300 Subject: [PATCH 03/17] Its not working :sob: time to debug --- chatBot/includes/client.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 33ffb91..69e2ae6 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -20,10 +20,12 @@ class Client extends EventEmitter { }); this.swarm.on('update', () => { - console.log(`Peers count: ${this.swarm.connections.size} (${this.swarm.peers.size})`); + console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); this.swarm.peers.forEach((peer, peerInfo) => { - console.log(`Peer ${peer} is in topic(s) ${[peerInfo.topics].filter(topic => topic).map(topic => topic.toString('hex'))}`); + console.log([peer]); + console.log([peerInfo.topics]); + console.log(`Peer ${[peer]} is in topic(s) ${[peerInfo.topics].filter(topic => topic).map(topic => topic.toString('hex'))}`); }); }); } From 230cdd1321e6deb1f6ba3d53ddc3a6faf0282b43 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 20:58:59 +0300 Subject: [PATCH 04/17] Huh? Is it actually inverted?? --- chatBot/includes/client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 69e2ae6..d55aa8c 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -24,8 +24,8 @@ class Client extends EventEmitter { this.swarm.peers.forEach((peer, peerInfo) => { console.log([peer]); - console.log([peerInfo.topics]); - console.log(`Peer ${[peer]} is in topic(s) ${[peerInfo.topics].filter(topic => topic).map(topic => topic.toString('hex'))}`); + console.log([peerInfo]); + console.log(`Peer ${[peer]} is in topic(s) ${[peer.topics].filter(topic => topic).map(topic => topic.toString('hex'))}`); }); }); } From 340362872fe8fe3765651f409f18f8476cf01c6a Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:00:22 +0300 Subject: [PATCH 05/17] Lol it actually was inverted for some reason --- chatBot/includes/client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index d55aa8c..eba3dc5 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -22,10 +22,10 @@ class Client extends EventEmitter { this.swarm.on('update', () => { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); - this.swarm.peers.forEach((peer, peerInfo) => { + this.swarm.peers.forEach((peerInfo, peer) => { console.log([peer]); console.log([peerInfo]); - console.log(`Peer ${[peer]} is in topic(s) ${[peer.topics].filter(topic => topic).map(topic => topic.toString('hex'))}`); + console.log(`Peer ${[peer]} is in topic(s) [${[peer.topics].filter(topic => topic).map(topic => topic.toString('hex')).join(", ")}]`); }); }); } From edd3f632cf663d2ab5422b71ec418bb54d663563 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:02:01 +0300 Subject: [PATCH 06/17] Now its time to debug peer topics since its empty right now --- chatBot/includes/client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index eba3dc5..8465b4c 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -23,8 +23,8 @@ class Client extends EventEmitter { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); this.swarm.peers.forEach((peerInfo, peer) => { - console.log([peer]); - console.log([peerInfo]); + console.log([peer.topics]) + console.log([peer.topics.filter(topic => topic)]) console.log(`Peer ${[peer]} is in topic(s) [${[peer.topics].filter(topic => topic).map(topic => topic.toString('hex')).join(", ")}]`); }); }); From 1dca21fe75611210fead5ef0dbb45607e2eb9306 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:03:01 +0300 Subject: [PATCH 07/17] Did an oopsie with array.filter ([array.filter] instead of [array].filter) --- chatBot/includes/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 8465b4c..c86affa 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -24,7 +24,7 @@ class Client extends EventEmitter { this.swarm.peers.forEach((peerInfo, peer) => { console.log([peer.topics]) - console.log([peer.topics.filter(topic => topic)]) + console.log([peer.topics].filter(topic => topic)) console.log(`Peer ${[peer]} is in topic(s) [${[peer.topics].filter(topic => topic).map(topic => topic.toString('hex')).join(", ")}]`); }); }); From 79bdd04810781e4ca4ffe5ffda7e3188cf09f0bb Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:06:09 +0300 Subject: [PATCH 08/17] Lmfao i did peer.topics --- chatBot/includes/client.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index c86affa..5d7a67a 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -23,9 +23,7 @@ class Client extends EventEmitter { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); this.swarm.peers.forEach((peerInfo, peer) => { - console.log([peer.topics]) - console.log([peer.topics].filter(topic => topic)) - console.log(`Peer ${[peer]} is in topic(s) [${[peer.topics].filter(topic => topic).map(topic => topic.toString('hex')).join(", ")}]`); + console.log(`Peer ${[peer]} is in topic(s) [${[peerInfo.topics].filter(topic => topic).map(topic => topic.toString()).join(", ")}]`); }); }); } From 3b7f0fbd1d722eba8d4cfaf45613c2bf4c33e9ae Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:09:33 +0300 Subject: [PATCH 09/17] replaced topic.toString with b4a.toString(topic) --- chatBot/includes/client.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 5d7a67a..18b2fea 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) { @@ -23,7 +24,7 @@ class Client extends EventEmitter { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); this.swarm.peers.forEach((peerInfo, peer) => { - console.log(`Peer ${[peer]} is in topic(s) [${[peerInfo.topics].filter(topic => topic).map(topic => topic.toString()).join(", ")}]`); + console.log(`Peer ${[peer]} is in topic(s) [${[peerInfo.topics].filter(topic => topic).map(topic => b4a.toString(topic, 'hex')).join(", ")}]`); }); }); } From e396bcbb0be75b942b24db12bde57e53b06e1314 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:10:25 +0300 Subject: [PATCH 10/17] b4a doesnt work its time to debug again --- chatBot/includes/client.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 18b2fea..d03d411 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -24,7 +24,10 @@ class Client extends EventEmitter { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); this.swarm.peers.forEach((peerInfo, peer) => { - console.log(`Peer ${[peer]} is in topic(s) [${[peerInfo.topics].filter(topic => topic).map(topic => b4a.toString(topic, 'hex')).join(", ")}]`); + console.log(`Peer ${[peer]} is in topic(s) [${[peerInfo.topics].filter(topic => topic).map(topic => { + console.log(topic) + b4a.toString(topic, 'hex') + }).join(", ")}]`); }); }); } From 2ca07e46c3c6202957e6c37c281d07e20795399d Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:11:46 +0300 Subject: [PATCH 11/17] so topics in .map() are array. Is it array that always has only one buffer or can have lots of them??? --- chatBot/includes/client.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index d03d411..2c7a0b9 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -26,7 +26,8 @@ class Client extends EventEmitter { this.swarm.peers.forEach((peerInfo, peer) => { console.log(`Peer ${[peer]} is in topic(s) [${[peerInfo.topics].filter(topic => topic).map(topic => { console.log(topic) - b4a.toString(topic, 'hex') + return "test" + // b4a.toString(topic, 'hex') }).join(", ")}]`); }); }); From 27b35283042b8030f278b50cf796c12063b153fb Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:13:52 +0300 Subject: [PATCH 12/17] It can have multiple topics :( time to figure a way to do it then ig --- chatBot/includes/client.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 2c7a0b9..aa2532a 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -25,9 +25,7 @@ class Client extends EventEmitter { this.swarm.peers.forEach((peerInfo, peer) => { console.log(`Peer ${[peer]} is in topic(s) [${[peerInfo.topics].filter(topic => topic).map(topic => { - console.log(topic) - return "test" - // b4a.toString(topic, 'hex') + console.log(topic.map(top => b4a.toString(top, 'hex'))) }).join(", ")}]`); }); }); From 4ec0be2774a09d638c7d893af362c01c392b370b Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:17:16 +0300 Subject: [PATCH 13/17] I think i got it to work lol --- chatBot/includes/client.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index aa2532a..0047d63 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -23,10 +23,15 @@ class Client extends EventEmitter { this.swarm.on('update', () => { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); - this.swarm.peers.forEach((peerInfo, peer) => { - console.log(`Peer ${[peer]} is in topic(s) [${[peerInfo.topics].filter(topic => topic).map(topic => { - console.log(topic.map(top => b4a.toString(top, 'hex'))) - }).join(", ")}]`); + 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(topic => topic) + .map(topic => topic.map(top => b4a.toString(top, 'hex'))) + .join(", "); + + console.log(`Peer ${peer} is in topic(s) [${peerTopics}]`); }); }); } From 56d8a8091ed2a1275ba9e208fbe30c9a0de1d129 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:18:33 +0300 Subject: [PATCH 14/17] Yeah it actually works :yay: --- chatBot/includes/client.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index 0047d63..ae51599 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -27,11 +27,11 @@ class Client extends EventEmitter { // 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(topic => topic) - .map(topic => topic.map(top => b4a.toString(top, 'hex'))) - .join(", "); + const peerTopics = [peerInfo.topics] + .filter(topics => topics) + .map(topics => topics.map(topic => b4a.toString(topic, 'hex'))); - console.log(`Peer ${peer} is in topic(s) [${peerTopics}]`); + console.log(`Peer ${peer} is in topic(s) [${peerTopics.join(", ")}]`); }); }); } From 7275b6994863e317821870db0cda0604be5c73c3 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:23:59 +0300 Subject: [PATCH 15/17] Added onIcon and onFile events. onMessage is now being called only on messageType == message. Removed debug code from swarm.on('update'). --- chatBot/bot.js | 1 - chatBot/includes/client.js | 9 ++++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index b2844c3..c1359b9 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -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/client.js b/chatBot/includes/client.js index ae51599..426b094 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -13,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}`) @@ -30,8 +35,6 @@ class Client extends EventEmitter { const peerTopics = [peerInfo.topics] .filter(topics => topics) .map(topics => topics.map(topic => b4a.toString(topic, 'hex'))); - - console.log(`Peer ${peer} is in topic(s) [${peerTopics.join(", ")}]`); }); }); } From 92ba56b9fc6a16829ceefb1112c0ac0f34ce29ec Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:25:41 +0300 Subject: [PATCH 16/17] Added topic to file attachment json --- app.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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(), }); From edf61f0462339d9798288d14c45a53ef58964b9d Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:34:50 +0300 Subject: [PATCH 17/17] Started working on proper classes for messages (files and text), user peer class, chatroom class --- chatBot/bot.js | 2 +- chatBot/includes/{chatroom.js => ChatRoom.js} | 2 -- chatBot/includes/{client.js => Client.js} | 0 chatBot/includes/FileMessage.js | 12 ++++++++++++ chatBot/includes/TextMessage.js | 10 ++++++++++ chatBot/includes/UserPeer.js | 10 ++++++++++ 6 files changed, 33 insertions(+), 3 deletions(-) rename chatBot/includes/{chatroom.js => ChatRoom.js} (81%) rename chatBot/includes/{client.js => Client.js} (100%) create mode 100644 chatBot/includes/FileMessage.js create mode 100644 chatBot/includes/TextMessage.js create mode 100644 chatBot/includes/UserPeer.js diff --git a/chatBot/bot.js b/chatBot/bot.js index c1359b9..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 diff --git a/chatBot/includes/chatroom.js b/chatBot/includes/ChatRoom.js similarity index 81% 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 100% rename from chatBot/includes/client.js rename to chatBot/includes/Client.js 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