I think i commented everything now

This commit is contained in:
MrMasrozYTLIVE 2024-06-15 18:15:54 +03:00
parent ba2347966c
commit f0a1f53c24
8 changed files with 175 additions and 12 deletions

View File

@ -11,9 +11,41 @@
"plugins/markdown" "plugins/markdown"
], ],
"opts": { "opts": {
"template": "node_modules/docdash",
"encoding": "utf8", "encoding": "utf8",
"destination": "docs/", "destination": "docs/",
"recurse": true, "recurse": true,
"verbose": true "verbose": true
},
"markdown": {
"parser": "gfm",
"hardwrap": true,
"idInHeadings": true
},
"templates": {
"cleverLinks": false,
"monospaceLinks": false,
"default": {
"outputSourceFiles": true,
"includeDate": false,
"useLongnameInNav": true
}
},
"docdash": {
"static": true,
"sort": true,
"search": true,
"collapse": true,
"typedefs": true,
"removeQuotes": "none",
"wrap": true,
"menu": {
"Git Repository": {
"href":"https://git.ssh.surf/mitask/linkup-bot-lib",
"target":"_blank",
"class":"menu-item",
"id":"gitrepo_link"
}
}
} }
} }

View File

@ -18,6 +18,7 @@
"serve-drive": "^5.0.8" "serve-drive": "^5.0.8"
}, },
"devDependencies": { "devDependencies": {
"docdash": "^2.0.2",
"jsdoc": "^4.0.3" "jsdoc": "^4.0.3"
} }
} }

View File

@ -14,6 +14,10 @@ import ServeDrive from 'serve-drive';
/** /**
* This class is the core component of the bot system. It handles connections to the Hyperswarm network, manages message sending and receiving, and emits events for various actions. * This class is the core component of the bot system. It handles connections to the Hyperswarm network, manages message sending and receiving, and emits events for various actions.
* @emits Client#onMessage
* @emits Client#onFile
* @emits Client#onAudio
* @emits Client#onIcon
*/ */
class Client extends EventEmitter { class Client extends EventEmitter {
/** /**
@ -131,19 +135,68 @@ class Client extends EventEmitter {
const peerAvatar = messageObj.avatar; const peerAvatar = messageObj.avatar;
const timestamp = messageObj.timestamp; const timestamp = messageObj.timestamp;
if (msgType === "message") if (msgType === "message")
/**
* Triggered when a new message is received.
*
* @event Client#onMessage
* @property peer - HyperSwarm peer object
* @property {TextMessage} textMessage -Class with all of the information about received text message
* @example
* const bot = new Client("MyBot");
* bot.on('onMessage', (peer, message) => {
* console.log(`Message from ${message.peerName}: ${message.message}`);
* });
*/
this.emit('onMessage', peer, new TextMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.message)); this.emit('onMessage', peer, new TextMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.message));
if (msgType === "file") { if (msgType === "file") {
const fileBuffer = await this.drive.get(`/files/${messageObj.fileName}`); const fileBuffer = await this.drive.get(`/files/${messageObj.fileName}`);
/**
* Triggered when a file message is received.
*
* @event Client#onFile
* @property peer - HyperSwarm peer object
* @property {FileMessage} fileMessage - Class with all of the information about received file
* @example
* const bot = new Client("MyBot");
* bot.on('onFile', (peer, message) => {
* console.log(`Received file from ${message.peerName}`);
* });
*/
this.emit('onFile', peer, new FileMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.fileName, `http://localhost:${this.servePort}/files/${messageObj.fileName}`, messageObj.fileType, messageObj.fileData)); this.emit('onFile', peer, new FileMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.fileName, `http://localhost:${this.servePort}/files/${messageObj.fileName}`, messageObj.fileType, messageObj.fileData));
} }
if (msgType === "icon") if (msgType === "icon")
/**
* Triggered when an icon message is received.
*
* @event Client#onIcon
* @property peer - HyperSwarm peer object
* @property {IconMessage} iconMessage - Class with all of the information about received peer icon
* @example
* const bot = new Client("MyBot");
* bot.on('onIcon', (peer, message) => {
* console.log(`Received new Icon from ${message.peerName}`);
* });
*/
this.emit('onIcon', peer, new IconMessage(peerName, peerAvatar, timestamp)); this.emit('onIcon', peer, new IconMessage(peerName, peerAvatar, timestamp));
if (msgType === "audio") { if (msgType === "audio") {
const audioBuffer = await this.drive.get(`/audio/${messageObj.audioName}`); const audioBuffer = await this.drive.get(`/audio/${messageObj.audioName}`);
/**
* Triggered when an audio message is received.
*
* @event Client#onAudio
* @property peer - HyperSwarm peer object
* @property {AudioMessage} audioMessage - Class with all of the information about received audio file
* @example
* const bot = new Client("MyBot");
* bot.on('onAudio', (peer, message) => {
* console.log(`Received audio file from ${message.peerName}`);
* });
*/
this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, `http://localhost:${this.servePort}/audio/${messageObj.audioName}`, messageObj.audioType, messageObj.audioData)); this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, `http://localhost:${this.servePort}/audio/${messageObj.audioName}`, messageObj.audioType, messageObj.audioData));
} }
} }

View File

@ -2,6 +2,19 @@ import Message from "./Message.js";
import b4a from "b4a"; import b4a from "b4a";
class AudioMessage extends Message { class AudioMessage extends Message {
/**
* @description Creates a new Audio message.
* @since 1.0
* @author MiTask
* @constructor
* @param {String} peerName Peer username
* @param {String} peerAvatar Peer avatar URL
* @param {String} topic Chat room topic string
* @param {Number} timestamp UNIX Timestamp
* @param {String} audioUrl URL to the audio file
* @param {String} audioType Type of the audio file
* @param {String} audioData Audio file data in base64 String format
*/
constructor(peerName, peerAvatar, topic, timestamp, audioUrl, audioType, audioData) { constructor(peerName, peerAvatar, topic, timestamp, audioUrl, audioType, audioData) {
super("audio", peerName, peerAvatar, topic, timestamp); super("audio", peerName, peerAvatar, topic, timestamp);
this.audioUrl = audioUrl; this.audioUrl = audioUrl;
@ -9,6 +22,11 @@ class AudioMessage extends Message {
this.audioData = audioData; // Add audio data property this.audioData = audioData; // Add audio data property
} }
/**
* @since 1.0
* @author MiTask
* @returns {String} JSON String with all of the information about the message
*/
toJsonString() { toJsonString() {
return JSON.stringify({ return JSON.stringify({
...this.toJson(), ...this.toJson(),
@ -18,6 +36,16 @@ class AudioMessage extends Message {
}); });
} }
/**
* @description Creates a new audio message instance.
* @since 1.0
* @author MiTask
* @param {Client} bot Bot Client class
* @param {String} audioUrl URL to the audio file
* @param {String} audioType Type of the audio file
* @param {Buffer} audioBuffer Audio file data
* @returns {AudioMessage} AudioMessage instance.
*/
static new(bot, audioUrl, audioType, audioBuffer) { static new(bot, audioUrl, audioType, audioBuffer) {
const audioData = b4a.toString(audioBuffer, 'base64'); // Convert audio buffer to base64 const audioData = b4a.toString(audioBuffer, 'base64'); // Convert audio buffer to base64
return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType, audioData); return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType, audioData);

View File

@ -2,6 +2,20 @@ import Message from "./Message.js";
import b4a from "b4a"; import b4a from "b4a";
class FileMessage extends Message { class FileMessage extends Message {
/**
* @description Creates a new file message.
* @since 1.0
* @author MiTask
* @constructor
* @param {String} peerName Peer username
* @param {String} peerAvatar Peer avatar URL
* @param {String} topic Chat room topic string
* @param {Number} timestamp UNIX Timestamp
* @param {String} fileName File name
* @param {String} fileUrl URL to the file
* @param {String} fileType Type of the file
* @param {String} fileData File data in base64 String format
*/
constructor(peerName, peerAvatar, topic, timestamp, fileName, fileUrl, fileType, fileData) { constructor(peerName, peerAvatar, topic, timestamp, fileName, fileUrl, fileType, fileData) {
super("file", peerName, peerAvatar, topic, timestamp); super("file", peerName, peerAvatar, topic, timestamp);
this.fileName = fileName; this.fileName = fileName;
@ -10,6 +24,11 @@ class FileMessage extends Message {
this.fileData = fileData; // Add file data property this.fileData = fileData; // Add file data property
} }
/**
* @since 1.0
* @author MiTask
* @returns {String} JSON String with all of the information about the message
*/
toJsonString() { toJsonString() {
return JSON.stringify({ return JSON.stringify({
...this.toJson(), ...this.toJson(),
@ -20,6 +39,17 @@ class FileMessage extends Message {
}); });
} }
/**
* @description Creates a new file message instance.
* @since 1.0
* @author MiTask
* @param {Client} bot Bot Client class
* @param {String} fileName File name
* @param {String} fileUrl URL to the file
* @param {String} fileType Type of the file
* @param {Buffer} fileBuffer File data
* @returns {FileMessage} FileMessage instance.
*/
static new(bot, fileName, fileUrl, fileType, fileBuffer) { static new(bot, fileName, fileUrl, fileType, fileBuffer) {
const fileData = b4a.toString(fileBuffer, 'base64'); // Convert file buffer to base64 const fileData = b4a.toString(fileBuffer, 'base64'); // Convert file buffer to base64
return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType, fileData); return new FileMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), fileName, fileUrl, fileType, fileData);

View File

@ -2,16 +2,27 @@ import Message from "./Message.js";
import b4a from "b4a"; import b4a from "b4a";
class IconMessage extends Message { class IconMessage extends Message {
/**
* @description Creates a new icon message.
* @since 1.0
* @author MiTask
* @constructor
* @param {String} peerName Peer username
* @param {String} peerAvatar Peer avatar URL
* @param {Number} timestamp UNIX Timestamp
*/
constructor(peerName, peerAvatar, timestamp) { constructor(peerName, peerAvatar, timestamp) {
super("icon", peerName, peerAvatar, null, timestamp); super("icon", peerName, peerAvatar, null, timestamp);
} }
toJsonString() { /**
return JSON.stringify({ * @description Creates a new icon message instance.
...this.toJson() * @since 1.0
}); * @author MiTask
} * @param {Client} bot Bot Client class
* @param {String} avatarBuffer Bot Avatar buffer
* @returns {IconMessage} IconMessage instance
*/
static new(bot, avatarBuffer) { static new(bot, avatarBuffer) {
return new IconMessage(bot.botName, b4a.toString(avatarBuffer, 'base64'), Date.now()); return new IconMessage(bot.botName, b4a.toString(avatarBuffer, 'base64'), Date.now());
} }

View File

@ -36,6 +36,17 @@ class Message {
timestamp: this.timestamp timestamp: this.timestamp
}; };
} }
/**
* @since 1.0
* @author MiTask
* @returns {String} JSON String with all of the information about the message
*/
toJsonString() {
return JSON.stringify({
...this.toJson()
});
}
} }
export default Message; export default Message;

View File

@ -1,12 +1,8 @@
import Message from "./Message.js"; import Message from "./Message.js";
/**
* @description Class for text messages
* @since 1.0
* @author MiTask
*/
class TextMessage extends Message { class TextMessage extends Message {
/** /**
* @description Creates a new text message.
* @since 1.0 * @since 1.0
* @author MiTask * @author MiTask
* @constructor * @constructor
@ -34,11 +30,12 @@ class TextMessage extends Message {
} }
/** /**
* @description Creates a new text message instance.
* @since 1.0 * @since 1.0
* @author MiTask * @author MiTask
* @param {Client} bot Bot Client class * @param {Client} bot Bot Client class
* @param {String} message Text of the message * @param {String} message Text of the message
* @returns {TextMessage} TextMessage class * @returns {TextMessage} TextMessage instance
*/ */
static new(bot, message) { static new(bot, message) {
return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message); return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message);