diff --git a/jsdoc.json b/jsdoc.json index 578c098..0eb82a4 100644 --- a/jsdoc.json +++ b/jsdoc.json @@ -11,9 +11,41 @@ "plugins/markdown" ], "opts": { + "template": "node_modules/docdash", "encoding": "utf8", "destination": "docs/", "recurse": 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" + } + } } } \ No newline at end of file diff --git a/package.json b/package.json index 2f25f2b..8278389 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "serve-drive": "^5.0.8" }, "devDependencies": { + "docdash": "^2.0.2", "jsdoc": "^4.0.3" } } diff --git a/src/Client.js b/src/Client.js index 4bac377..e794f3d 100644 --- a/src/Client.js +++ b/src/Client.js @@ -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. + * @emits Client#onMessage + * @emits Client#onFile + * @emits Client#onAudio + * @emits Client#onIcon */ class Client extends EventEmitter { /** @@ -131,19 +135,68 @@ class Client extends EventEmitter { const peerAvatar = messageObj.avatar; const timestamp = messageObj.timestamp; + 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)); if (msgType === "file") { 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)); } 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)); if (msgType === "audio") { 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)); } } diff --git a/src/message/AudioMessage.js b/src/message/AudioMessage.js index fd0dce6..dcd7810 100644 --- a/src/message/AudioMessage.js +++ b/src/message/AudioMessage.js @@ -2,6 +2,19 @@ import Message from "./Message.js"; import b4a from "b4a"; 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) { super("audio", peerName, peerAvatar, topic, timestamp); this.audioUrl = audioUrl; @@ -9,6 +22,11 @@ class AudioMessage extends Message { 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() { return JSON.stringify({ ...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) { 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); diff --git a/src/message/FileMessage.js b/src/message/FileMessage.js index a9be736..f9be0b9 100644 --- a/src/message/FileMessage.js +++ b/src/message/FileMessage.js @@ -2,6 +2,20 @@ import Message from "./Message.js"; import b4a from "b4a"; 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) { super("file", peerName, peerAvatar, topic, timestamp); this.fileName = fileName; @@ -10,6 +24,11 @@ class FileMessage extends Message { 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() { return JSON.stringify({ ...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) { 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); diff --git a/src/message/IconMessage.js b/src/message/IconMessage.js index f3e9bd5..a30ab56 100644 --- a/src/message/IconMessage.js +++ b/src/message/IconMessage.js @@ -2,16 +2,27 @@ import Message from "./Message.js"; import b4a from "b4a"; 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) { super("icon", peerName, peerAvatar, null, timestamp); } - toJsonString() { - return JSON.stringify({ - ...this.toJson() - }); - } - + /** + * @description Creates a new icon message instance. + * @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) { return new IconMessage(bot.botName, b4a.toString(avatarBuffer, 'base64'), Date.now()); } diff --git a/src/message/Message.js b/src/message/Message.js index c1eb170..800a62b 100644 --- a/src/message/Message.js +++ b/src/message/Message.js @@ -36,6 +36,17 @@ class Message { 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; diff --git a/src/message/TextMessage.js b/src/message/TextMessage.js index f446dc9..32064f5 100644 --- a/src/message/TextMessage.js +++ b/src/message/TextMessage.js @@ -1,12 +1,8 @@ import Message from "./Message.js"; -/** - * @description Class for text messages - * @since 1.0 - * @author MiTask - */ class TextMessage extends Message { /** + * @description Creates a new text message. * @since 1.0 * @author MiTask * @constructor @@ -34,11 +30,12 @@ class TextMessage extends Message { } /** + * @description Creates a new text message instance. * @since 1.0 * @author MiTask * @param {Client} bot Bot Client class * @param {String} message Text of the message - * @returns {TextMessage} TextMessage class + * @returns {TextMessage} TextMessage instance */ static new(bot, message) { return new TextMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), message);