I think i commented everything now
This commit is contained in:
parent
ba2347966c
commit
f0a1f53c24
32
jsdoc.json
32
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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
"serve-drive": "^5.0.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"docdash": "^2.0.2",
|
||||
"jsdoc": "^4.0.3"
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user