From f706e7d621534ad12a81644fdba7754ba0d5e729 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Fri, 14 Jun 2024 20:06:05 +0300 Subject: [PATCH] Made Client.js->setupSwarm() parsing messages more readable --- chatBot/includes/Client.js | 31 +++++++++++-------- .../includes/{ => message}/AudioMessage.js | 0 chatBot/includes/{ => message}/FileMessage.js | 0 chatBot/includes/{ => message}/IconMessage.js | 7 +++-- chatBot/includes/{ => message}/Message.js | 0 chatBot/includes/{ => message}/TextMessage.js | 0 6 files changed, 22 insertions(+), 16 deletions(-) rename chatBot/includes/{ => message}/AudioMessage.js (100%) rename chatBot/includes/{ => message}/FileMessage.js (100%) rename chatBot/includes/{ => message}/IconMessage.js (51%) rename chatBot/includes/{ => message}/Message.js (100%) rename chatBot/includes/{ => message}/TextMessage.js (100%) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index d44982e..c1da18e 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -1,11 +1,11 @@ import Hyperswarm from 'hyperswarm'; import EventEmitter from 'node:events'; import b4a from "b4a"; -import TextMessage from "./TextMessage.js"; -import FileMessage from "./FileMessage.js"; -import AudioMessage from "./AudioMessage.js"; -import Message from "./Message.js"; -import IconMessage from "./IconMessage.js"; +import TextMessage from "./message/TextMessage.js"; +import FileMessage from "./message/FileMessage.js"; +import AudioMessage from "./message/AudioMessage.js"; +import Message from "./message/Message.js"; +import IconMessage from "./message/IconMessage.js"; class Client extends EventEmitter { constructor(botName) { @@ -52,17 +52,22 @@ class Client extends EventEmitter { if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room this.currentTopic = messageObj.topic; // Set the current topic from the incoming message - if (messageObj.type === "message") - this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.message)); + const msgType = messageObj.type; + const peerName = messageObj.name; + const peerAvatar = messageObj.avatar; + const timestamp = messageObj.timestamp; - if (messageObj.type === "file") - this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.avatar, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType)); + if (msgType === "message") + this.emit('onMessage', peer, new TextMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.message)); - if (messageObj.type === "icon") - this.emit('onIcon', peer, messageObj); + if (msgType === "file") + this.emit('onFile', peer, new FileMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType)); - if (messageObj.type === "audio") - this.emit('onAudio', peer, new AudioMessage(messageObj.name, messageObj.avatar, messageObj.avatar, messageObj.topic, messageObj.timestamp, messageObj.audio, messageObj.audioType)); + if (msgType === "icon") + this.emit('onIcon', peer, new IconMessage(peerName, peerAvatar, timestamp)); + + if (msgType === "audio") + this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.audio, messageObj.audioType)); } }); diff --git a/chatBot/includes/AudioMessage.js b/chatBot/includes/message/AudioMessage.js similarity index 100% rename from chatBot/includes/AudioMessage.js rename to chatBot/includes/message/AudioMessage.js diff --git a/chatBot/includes/FileMessage.js b/chatBot/includes/message/FileMessage.js similarity index 100% rename from chatBot/includes/FileMessage.js rename to chatBot/includes/message/FileMessage.js diff --git a/chatBot/includes/IconMessage.js b/chatBot/includes/message/IconMessage.js similarity index 51% rename from chatBot/includes/IconMessage.js rename to chatBot/includes/message/IconMessage.js index a0c5cc3..bb4afc4 100644 --- a/chatBot/includes/IconMessage.js +++ b/chatBot/includes/message/IconMessage.js @@ -1,8 +1,9 @@ import Message from "./Message.js"; +import b4a from "b4a"; class IconMessage extends Message { - constructor(peerName, avatarBuffer, topic, timestamp) { - super("icon", peerName, avatarBuffer, topic, timestamp); + constructor(peerName, peerAvatar, topic, timestamp) { + super("icon", peerName, peerAvatar, topic, timestamp); } toJsonString() { @@ -12,7 +13,7 @@ class IconMessage extends Message { } static new(bot, avatarBuffer) { - return new IconMessage(bot.botName, avatarBuffer, bot.currentTopic, Date.now()); + return new IconMessage(bot.botName, b4a.toString(avatarBuffer, 'base64'), Date.now()); } } diff --git a/chatBot/includes/Message.js b/chatBot/includes/message/Message.js similarity index 100% rename from chatBot/includes/Message.js rename to chatBot/includes/message/Message.js diff --git a/chatBot/includes/TextMessage.js b/chatBot/includes/message/TextMessage.js similarity index 100% rename from chatBot/includes/TextMessage.js rename to chatBot/includes/message/TextMessage.js