diff --git a/.gitignore b/.gitignore index 981f7b6..4330556 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/ package-lock.json -node_modules \ No newline at end of file +node_modules +docs \ No newline at end of file diff --git a/jsdoc.json b/jsdoc.json index b305cb3..578c098 100644 --- a/jsdoc.json +++ b/jsdoc.json @@ -14,25 +14,6 @@ "encoding": "utf8", "destination": "docs/", "recurse": true, - "verbose": true, - "template": "better-docs" - }, - "templates": { - "cleverLinks": false, - "monospaceLinks": false, - "search": true, - "better-docs": { - "name": "Sample Documentation", - "title": "Test", // HTML title - "css": "style.css", - "trackingCode": "tracking-code-which-will-go-to-the-HEAD", - "hideGenerator": false, - "navLinks": [ - { - "label": "Git", - "href": "https://git.ssh.surf/MiTask/LinkUp-Bot-Lib" - } - ] - } + "verbose": true } } \ No newline at end of file diff --git a/package.json b/package.json index e3b41f3..2f25f2b 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "main": "src/Client.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "generate-docs": "jsdoc --configure jsdoc.json" }, "keywords": [], "author": "", diff --git a/src/Client.js b/src/Client.js index d926531..4bac377 100644 --- a/src/Client.js +++ b/src/Client.js @@ -13,9 +13,15 @@ import fs from 'fs'; import ServeDrive from 'serve-drive'; /** - * @author Raven + * 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. */ class Client extends EventEmitter { + /** + * @param {String} botName The name of the bot. + * @since 1.0 + * @constructor + * @author snxraven + */ constructor(botName) { super(); if (!botName) return console.error("Bot Name is not defined!"); @@ -55,6 +61,11 @@ class Client extends EventEmitter { }); } + /** + * @description Initializes the ServeDrive for serving files and audio. + * @since 1.0 + * @author snxraven + */ async initializeServeDrive() { try { this.servePort = this.getRandomPort(); @@ -68,11 +79,22 @@ class Client extends EventEmitter { console.error('Error initializing ServeDrive:', error); } } - + /** + * @description Returns a random port number. + * @since 1.0 + * @author snxraven + * @return {Number} Random port number. + */ getRandomPort() { return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152; } + /** + * @description Fetches and sets the bot's avatar from a local file. + * @param {String} filePath path to the local avatar file. + * @since 1.0 + * @author snxraven + */ async fetchAvatar(filePath) { try { await this.drive.ready(); @@ -87,6 +109,11 @@ class Client extends EventEmitter { } } + /** + * @description Sets up the Hyperswarm network and connection handlers. + * @since 1.0 + * @author snxraven + */ setupSwarm() { this.swarm.on('connection', (peer) => { // Send the cached icon message to the new peer @@ -132,10 +159,16 @@ class Client extends EventEmitter { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); }); } - + /** + * @description Joins a specified chat room. + * @since 1.0 + * @author snxraven + * @param {String} chatRoomID Chat room topic string + */ joinChatRoom(chatRoomID) { if (!chatRoomID || typeof chatRoomID !== 'string') { - return console.error("Invalid chat room ID!"); + console.error("Invalid chat room ID!"); + return; } this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms @@ -147,11 +180,24 @@ class Client extends EventEmitter { }); } + /** + * @description Sends a text message. + * @since 1.0 + * @author MiTask + * @param {String} message Text message to send to the bot's current chat room. + */ sendTextMessage(message) { console.log(`Preparing to send text message: ${message}`); this.sendMessage(TextMessage.new(this, message)); } + /** + * @description Sends a file message. + * @since 1.0 + * @author snxraven + * @param {String} filePath Path to the file to send. + * @param {String} fileType Type of the file to send. + */ async sendFileMessage(filePath, fileType) { try { await this.drive.ready(); @@ -166,6 +212,13 @@ class Client extends EventEmitter { } } + /** + * @description Sends an audio message. + * @since 1.0 + * @author snxraven + * @param {String} filePath Path to the audio file to send. + * @param {String} audioType Type of the audio file to send. + */ async sendAudioMessage(filePath, audioType) { try { await this.drive.ready(); @@ -180,6 +233,13 @@ class Client extends EventEmitter { } } + + /** + * @description Sends a generic message. + * @since 1.0 + * @author MiTask + * @param {Message} message Message class (TextMessage, FileMessage or AudioMessage) + */ sendMessage(message) { if (!(message instanceof Message)) { console.error(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message); @@ -205,6 +265,11 @@ class Client extends EventEmitter { } } + /** + * @description Disconnects the bot and shuts down the Hyperswarm network. + * @since 1.0 + * @author snxraven + */ async destroy() { await this.swarm.destroy(); console.log(`Bot ${this.botName} disconnected.`); diff --git a/src/message/Message.js b/src/message/Message.js index fbb2fff..c1eb170 100644 --- a/src/message/Message.js +++ b/src/message/Message.js @@ -1,4 +1,19 @@ +/** + * @description Base class for all messages + * @since 1.0 + * @author MiTask + */ class Message { + /** + * @since 1.0 + * @author MiTask + * @constructor + * @param {String} messageType Type of the message (text, file, audio, icon) + * @param {String} peerName Peer username + * @param {String} peerAvatar Peer avatar URL + * @param {String} topic Chat room topic string + * @param {Number} timestamp UNIX Timestamp + */ constructor(messageType, peerName, peerAvatar, topic, timestamp) { this.type = messageType; this.peerName = peerName; @@ -7,6 +22,11 @@ class Message { this.timestamp = timestamp; } + /** + * @since 1.0 + * @author MiTask + * @returns {{name: String, topic: String, avatar: String, type: String, timestamp: Number}} JSON Object with all of the information about the message + */ toJson() { return { type: this.type, diff --git a/src/message/TextMessage.js b/src/message/TextMessage.js index f4a68c0..f446dc9 100644 --- a/src/message/TextMessage.js +++ b/src/message/TextMessage.js @@ -1,11 +1,31 @@ import Message from "./Message.js"; +/** + * @description Class for text messages + * @since 1.0 + * @author MiTask + */ class TextMessage extends 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} message Text of the message + */ constructor(peerName, peerAvatar, topic, timestamp, message) { super("message", peerName, peerAvatar, topic, timestamp); this.message = message; } + /** + * @since 1.0 + * @author MiTask + * @returns {String} JSON String with all of the information about the message + */ toJsonString() { return JSON.stringify({ ...this.toJson(), @@ -13,6 +33,13 @@ class TextMessage extends Message { }); } + /** + * @since 1.0 + * @author MiTask + * @param {Client} bot Bot Client class + * @param {String} message Text of the message + * @returns {TextMessage} TextMessage class + */ static new(bot, message) { return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message); }