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; #origin;
#data; #data;
#message; #message;
/** /**
* Creates an instance of ChatMessage. * Creates an instance of Message.
* @param {string} rawText - The raw text of the chat message. * @param {string} rawText - The raw text of the socket message.
*/ */
constructor(rawText) { constructor(rawText) {
this.#origin = rawText; this.#origin = rawText;
try { try {
this.#data = JSON.parse(rawText.trim()); this.#data = JSON.parse(rawText.trim());
this.#message = this.#data.message; 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. * Returns an iterable of the property names in the message data.
* @param {WebSocket} wetsock - The WebSocket instance to emit the properties through. * @returns {Generator<string>} - An iterable of property names.
*/ */
emitProps(wetsock) { *getProps() {
if (!this.#data || typeof this.#data !== 'object') return; if (!this.#data || typeof(this.#data) != 'object') return;
for (const key in this.#data) { for (const prop in this.#data) {
if (typeof this.#data[key] === 'string' && this.#data[key].indexOf(' ') === -1) { yield prop;
wetsock.emit(`message:${key}:${this.#data[key]}`, this);
}
} }
} }
/**
* 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) { handleMessage(ws, message) {
const client = this.getValidClient(ws); const client = this.getValidClient(ws);
if (!client) return; if (!client) return;
ws.data.messages = Number(ws.data.messages) + 1;
const chatMessage = new Message(message); 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); this.emit('message', client, chatMessage);
} }