fix(message) unlogical functionality within messages

This commit is contained in:
gh0sTedBuddy
2024-04-08 01:08:06 +01:00
parent d7156a1e2d
commit 3b2d899508
2 changed files with 30 additions and 15 deletions

View File

@@ -1,33 +1,43 @@
/**
* Represents a chat message.
* Represents a socket message.
*/
export default class ChatMessage {
export default class Message {
#origin;
#data;
#message;
/**
* Creates an instance of ChatMessage.
* @param {string} rawText - The raw text of the chat message.
* Creates an instance of Message.
* @param {string} rawText - The raw text of the socket message.
*/
constructor(rawText) {
this.#origin = rawText;
try {
this.#data = JSON.parse(rawText.trim());
this.#message = this.#data.message;
} catch(err) {}
} catch(err) {
this.#data = undefined;
this.#message = undefined;
}
}
/**
* Emits the properties of the chat message through the provided WebSocket.
* @param {WebSocket} wetsock - The WebSocket instance to emit the properties through.
* Returns an iterable of the property names in the message data.
* @returns {Generator<string>} - An iterable of property names.
*/
emitProps(wetsock) {
if (!this.#data || typeof this.#data !== 'object') return;
for (const key in this.#data) {
if (typeof this.#data[key] === 'string' && this.#data[key].indexOf(' ') === -1) {
wetsock.emit(`message:${key}:${this.#data[key]}`, this);
*getProps() {
if (!this.#data || typeof(this.#data) != 'object') return;
for (const prop in this.#data) {
yield prop;
}
}
/**
* Retrieves the value of a specific property in the message data.
* @param {string} key - The key of the property.
* @returns {*} - The value associated with the specified key.
*/
get(key) {
return this.#data[key];
}
}

View File

@@ -96,9 +96,14 @@ export default class WetSock {
handleMessage(ws, message) {
const client = this.getValidClient(ws);
if (!client) return;
ws.data.messages = Number(ws.data.messages) + 1;
const chatMessage = new Message(message);
chatMessage.emitProps(this);
for (const prop of chatMessage.getProps()) {
this.emit(`message:${prop}`, client, chatMessage);
const value = chatMessage.get(prop);
if(typeof(value) != 'string' || value.indexOf(' ') < 0) continue;
this.emit(`message:${prop}:${value}`, client, chatMessage);
}
this.emit('message', client, chatMessage);
}