Made Client.js->setupSwarm() parsing messages more readable

This commit is contained in:
MrMasrozYTLIVE 2024-06-14 20:06:05 +03:00
parent 42dccf9547
commit f706e7d621
6 changed files with 22 additions and 16 deletions

View File

@ -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));
}
});

View File

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