From 45504dd49bd4e0a1eb874f9bf7175d90f4a7cfdf Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Fri, 22 Nov 2024 08:57:47 +1300 Subject: [PATCH 1/7] Append message when received --- assets/chat/js/chat.js | 24 ++++++++++----------- assets/chat/js/window.js | 45 ++++++---------------------------------- 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index a046aa39..d7f33ab8 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -757,16 +757,15 @@ class Chat { addMessage(message, win = null) { // eslint-disable-next-line no-param-reassign if (win === null) win = this.mainwindow; - const previousMessage = win.getPreviousMessage(message); // Break the current combo if this message is not an emote // We don't need to check what type the current message is, we just know that its a new message, so the combo is invalid. if ( - previousMessage && - previousMessage.type === MessageTypes.EMOTE && - previousMessage.emotecount > 1 + win.lastmessage && + win.lastmessage.type === MessageTypes.EMOTE && + win.lastmessage.emotecount > 1 ) - previousMessage.completeCombo(); + win.lastmessage.completeCombo(); // Populate the tag and mentioned users for this $message. if ( @@ -799,7 +798,7 @@ class Chat { // Populate highlight for this $message if (message.type === MessageTypes.USER) { // check if the last message was from the same user - message.continued = win.isContinued(message, previousMessage); + message.continued = win.isContinued(message); // set highlighted state message.highlighted = this.shouldHighlightMessage(message); } @@ -1144,24 +1143,23 @@ class Chat { const usr = this.users.get(data.nick.toLowerCase()); const win = this.mainwindow; const message = MessageBuilder.message(data.data, usr, data.timestamp); - const previousMessage = win.getPreviousMessage(message); const isCombo = this.emoteService.canUserUseEmote(usr, textonly) && - this.removeSlashCmdFromText(previousMessage?.message) === textonly; + this.removeSlashCmdFromText(win.lastmessage?.message) === textonly; - if (isCombo && previousMessage?.type === MessageTypes.EMOTE) { - previousMessage.incEmoteCount(); + if (isCombo && win.lastmessage?.type === MessageTypes.EMOTE) { + win.lastmessage.incEmoteCount(); if (this.user.equalWatching(usr.watching)) { - previousMessage.ui.classList.toggle('watching-same', true); + win.lastmessage.ui.classList.toggle('watching-same', true); } this.mainwindow.update(); return; } - if (isCombo && previousMessage?.type === MessageTypes.USER) { - win.removeMessage(previousMessage); + if (isCombo && win.lastmessage?.type === MessageTypes.USER) { + win.removeLastMessage(); const msg = MessageBuilder.emote(textonly, data.timestamp, 2).into(this); if (this.user.equalWatching(usr.watching)) { diff --git a/assets/chat/js/window.js b/assets/chat/js/window.js index 5d1fce4d..12a2c769 100644 --- a/assets/chat/js/window.js +++ b/assets/chat/js/window.js @@ -81,47 +81,14 @@ class ChatWindow extends EventEmitter { message.ui = message.html(chat); message.afterRender(chat); - // Get index of where the message should be based on timestamp. - const index = this.getMessageIndex(message); - - /** - * If the index of the message is 0 then prepend. - * If it's equal the length of the array then append. - * Otherwise insert at index. - */ - if (index === 0) { - this.lines.prepend(message.ui); - this.messages.unshift(message); - } else if (index === this.messages.length) { - this.lines.append(message.ui); - this.messages.push(message); - this.lastmessage = message; - } else { - this.lines.insertBefore(message.ui, this.messages[index].ui); - this.messages.splice(index, 0, message); - } + this.lines.append(message.ui); + this.messages.push(message); + this.lastmessage = message; this.linecount += 1; this.cleanupThrottle(); } - getMessageIndex(message) { - return ( - this.messages.findLastIndex( - (m) => m.timestamp.valueOf() <= message.timestamp.valueOf(), - ) + 1 - ); - } - - getPreviousMessage(message) { - const index = this.getMessageIndex(message); - if (index === 0) { - return null; - } - - return this.messages[index - 1]; - } - containsMessage(message) { return this.messages.find((msg) => msg.md5 === message.md5); } @@ -212,9 +179,9 @@ class ChatWindow extends EventEmitter { } } - removeMessage(message) { - message.remove(); - this.messages = this.messages.filter((m) => m !== message); + removeLastMessage() { + this.lastmessage.remove(); + this.messages = this.messages.filter((m) => m !== this.lastmessage); } /** From 70949385745752e9e6caa5ac4f7a07ff841f55d4 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Fri, 22 Nov 2024 08:58:32 +1300 Subject: [PATCH 2/7] Move welcome to chat message --- assets/chat/js/chat.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index d7f33ab8..97add46e 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -82,6 +82,7 @@ class Chat { this.subonlyicon = null; this.loginscrn = null; this.loadingscrn = null; + this.showwelcome = true; this.showmotd = true; this.subonly = false; this.authenticated = false; @@ -550,7 +551,6 @@ class Chat { this.mainwindow.update(true); this.setDefaultPlaceholderText(); - MessageBuilder.status(this.config.welcomeMessage).into(this); return Promise.resolve(this); } @@ -1092,6 +1092,11 @@ class Chat { } onNAMES(data) { + if (this.showwelcome) { + MessageBuilder.status(this.config.welcomeMessage).into(this); + this.showwelcome = false; + } + MessageBuilder.status( `Connected as ${ this.authenticated ? this.user.displayName : 'Guest' @@ -1099,6 +1104,7 @@ class Chat { data.users.length } users.`, ).into(this); + if (this.showmotd) { this.cmdHINT([Math.floor(Math.random() * hintstrings.size)]); this.showmotd = false; From 20dc0c6d75d5670ff72013732cdb9efec7b2da36 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Fri, 22 Nov 2024 09:00:04 +1300 Subject: [PATCH 3/7] Check if message exists onMsg instead of addMessage --- assets/chat/js/chat.js | 3 +++ assets/chat/js/window.js | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index 97add46e..65563a3c 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -1148,7 +1148,10 @@ class Chat { const textonly = this.removeSlashCmdFromText(data.data); const usr = this.users.get(data.nick.toLowerCase()); const win = this.mainwindow; + const message = MessageBuilder.message(data.data, usr, data.timestamp); + if (win.containsMessage(message)) return; + const isCombo = this.emoteService.canUserUseEmote(usr, textonly) && this.removeSlashCmdFromText(win.lastmessage?.message) === textonly; diff --git a/assets/chat/js/window.js b/assets/chat/js/window.js index 12a2c769..711915ed 100644 --- a/assets/chat/js/window.js +++ b/assets/chat/js/window.js @@ -75,9 +75,6 @@ class ChatWindow extends EventEmitter { } addMessage(chat, message) { - // Return if message is already in chat. - if (this.containsMessage(message)) return; - message.ui = message.html(chat); message.afterRender(chat); From 503db3bdba2f8389d99ff1e160e75f68c7a07ce3 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Fri, 22 Nov 2024 09:01:14 +1300 Subject: [PATCH 4/7] Add md5 list to EmoteMessage --- assets/chat/js/chat.js | 10 ++++++++-- assets/chat/js/messages/ChatEmoteMessage.js | 8 ++++++-- assets/chat/js/messages/MessageBuilder.js | 4 ++-- assets/chat/js/window.js | 7 ++++++- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index 65563a3c..1a3ab212 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -1157,7 +1157,7 @@ class Chat { this.removeSlashCmdFromText(win.lastmessage?.message) === textonly; if (isCombo && win.lastmessage?.type === MessageTypes.EMOTE) { - win.lastmessage.incEmoteCount(); + win.lastmessage.add(message.md5); if (this.user.equalWatching(usr.watching)) { win.lastmessage.ui.classList.toggle('watching-same', true); @@ -1168,8 +1168,14 @@ class Chat { } if (isCombo && win.lastmessage?.type === MessageTypes.USER) { + const lastMessageMd5 = win.lastmessage.md5; win.removeLastMessage(); - const msg = MessageBuilder.emote(textonly, data.timestamp, 2).into(this); + const msg = MessageBuilder.emote( + textonly, + data.timestamp, + [lastMessageMd5, message.md5], + 2, + ).into(this); if (this.user.equalWatching(usr.watching)) { msg.ui.classList.add('watching-same'); diff --git a/assets/chat/js/messages/ChatEmoteMessage.js b/assets/chat/js/messages/ChatEmoteMessage.js index 38d5fc4a..bdb8fe19 100644 --- a/assets/chat/js/messages/ChatEmoteMessage.js +++ b/assets/chat/js/messages/ChatEmoteMessage.js @@ -27,8 +27,11 @@ function ChatEmoteMessageCount(message) { const ChatEmoteMessageCountThrottle = throttle(63, ChatEmoteMessageCount); export default class ChatEmoteMessage extends ChatMessage { - constructor(emote, timestamp, count = 1) { + md5List = []; + + constructor(emote, timestamp, md5List, count = 1) { super(emote, timestamp, MessageTypes.EMOTE); + this.md5List = md5List; this.emotecount = count; this.emoteFormatter = new EmoteFormatter(); } @@ -62,7 +65,8 @@ export default class ChatEmoteMessage extends ChatMessage { this.ui.append(this.text.get(0), this.combo.get(0)); } - incEmoteCount() { + add(md5) { + this.md5List.push(md5); this.emotecount += 1; ChatEmoteMessageCountThrottle(this); } diff --git a/assets/chat/js/messages/MessageBuilder.js b/assets/chat/js/messages/MessageBuilder.js index 5349d760..6f333096 100644 --- a/assets/chat/js/messages/MessageBuilder.js +++ b/assets/chat/js/messages/MessageBuilder.js @@ -41,8 +41,8 @@ export default class MessageBuilder { return new ChatUserMessage(message, user, timestamp); } - static emote(emote, timestamp, count = 1) { - return new ChatEmoteMessage(emote, timestamp, count); + static emote(emote, timestamp, md5List, count = 1) { + return new ChatEmoteMessage(emote, timestamp, md5List, count); } static whisper(message, user, target, timestamp = null, id = null) { diff --git a/assets/chat/js/window.js b/assets/chat/js/window.js index 711915ed..16d1f021 100644 --- a/assets/chat/js/window.js +++ b/assets/chat/js/window.js @@ -87,7 +87,12 @@ class ChatWindow extends EventEmitter { } containsMessage(message) { - return this.messages.find((msg) => msg.md5 === message.md5); + return this.messages.find((msg) => { + if (msg.type === MessageTypes.EMOTE) { + return msg.md5List.includes(message.md5); + } + return msg.md5 === message.md5; + }); } getlines(sel) { From 2bff2f0e4aadb7b68bd8daa1142da443721373cf Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Fri, 22 Nov 2024 09:54:25 +1300 Subject: [PATCH 5/7] Store messages in EmoteMessage not just md5 --- assets/chat/js/chat.js | 8 ++++---- assets/chat/js/messages/ChatEmoteMessage.js | 18 ++++++++++++------ assets/chat/js/messages/MessageBuilder.js | 4 ++-- assets/chat/js/window.js | 2 +- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index 1a3ab212..dfb9b023 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -1157,7 +1157,7 @@ class Chat { this.removeSlashCmdFromText(win.lastmessage?.message) === textonly; if (isCombo && win.lastmessage?.type === MessageTypes.EMOTE) { - win.lastmessage.add(message.md5); + win.lastmessage.add(message); if (this.user.equalWatching(usr.watching)) { win.lastmessage.ui.classList.toggle('watching-same', true); @@ -1168,12 +1168,12 @@ class Chat { } if (isCombo && win.lastmessage?.type === MessageTypes.USER) { - const lastMessageMd5 = win.lastmessage.md5; + const lastMessage = win.lastmessage; win.removeLastMessage(); const msg = MessageBuilder.emote( textonly, - data.timestamp, - [lastMessageMd5, message.md5], + lastMessage.timestamp, + [lastMessage, message], 2, ).into(this); diff --git a/assets/chat/js/messages/ChatEmoteMessage.js b/assets/chat/js/messages/ChatEmoteMessage.js index bdb8fe19..37c61a9a 100644 --- a/assets/chat/js/messages/ChatEmoteMessage.js +++ b/assets/chat/js/messages/ChatEmoteMessage.js @@ -27,12 +27,14 @@ function ChatEmoteMessageCount(message) { const ChatEmoteMessageCountThrottle = throttle(63, ChatEmoteMessageCount); export default class ChatEmoteMessage extends ChatMessage { - md5List = []; + messages = []; - constructor(emote, timestamp, md5List, count = 1) { + emotecount = 0; + + constructor(emote, timestamp, messages) { super(emote, timestamp, MessageTypes.EMOTE); - this.md5List = md5List; - this.emotecount = count; + this.messages = messages; + this.emotecount = messages.length; this.emoteFormatter = new EmoteFormatter(); } @@ -65,12 +67,16 @@ export default class ChatEmoteMessage extends ChatMessage { this.ui.append(this.text.get(0), this.combo.get(0)); } - add(md5) { - this.md5List.push(md5); + add(message) { + this.messages.push(message); this.emotecount += 1; ChatEmoteMessageCountThrottle(this); } + containsMessage(message) { + return this.messages.find((msg) => msg.md5 === message.md5); + } + completeCombo() { ChatEmoteMessageCount(this); this.combo.attr('class', `${this.combo.attr('class')} combo-complete`); diff --git a/assets/chat/js/messages/MessageBuilder.js b/assets/chat/js/messages/MessageBuilder.js index 6f333096..93185b5e 100644 --- a/assets/chat/js/messages/MessageBuilder.js +++ b/assets/chat/js/messages/MessageBuilder.js @@ -41,8 +41,8 @@ export default class MessageBuilder { return new ChatUserMessage(message, user, timestamp); } - static emote(emote, timestamp, md5List, count = 1) { - return new ChatEmoteMessage(emote, timestamp, md5List, count); + static emote(emote, timestamp, messages) { + return new ChatEmoteMessage(emote, timestamp, messages); } static whisper(message, user, target, timestamp = null, id = null) { diff --git a/assets/chat/js/window.js b/assets/chat/js/window.js index 16d1f021..b76f7bcf 100644 --- a/assets/chat/js/window.js +++ b/assets/chat/js/window.js @@ -89,7 +89,7 @@ class ChatWindow extends EventEmitter { containsMessage(message) { return this.messages.find((msg) => { if (msg.type === MessageTypes.EMOTE) { - return msg.md5List.includes(message.md5); + return msg.containsMessage(message); } return msg.md5 === message.md5; }); From dc5def35fee8fa512327b05409093c70f2c34f25 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Fri, 22 Nov 2024 09:55:59 +1300 Subject: [PATCH 6/7] Revert Move welcome to chat message --- assets/chat/js/chat.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index dfb9b023..0eeb4449 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -82,7 +82,6 @@ class Chat { this.subonlyicon = null; this.loginscrn = null; this.loadingscrn = null; - this.showwelcome = true; this.showmotd = true; this.subonly = false; this.authenticated = false; @@ -551,6 +550,7 @@ class Chat { this.mainwindow.update(true); this.setDefaultPlaceholderText(); + MessageBuilder.status(this.config.welcomeMessage).into(this); return Promise.resolve(this); } @@ -1092,11 +1092,6 @@ class Chat { } onNAMES(data) { - if (this.showwelcome) { - MessageBuilder.status(this.config.welcomeMessage).into(this); - this.showwelcome = false; - } - MessageBuilder.status( `Connected as ${ this.authenticated ? this.user.displayName : 'Guest' @@ -1104,7 +1099,6 @@ class Chat { data.users.length } users.`, ).into(this); - if (this.showmotd) { this.cmdHINT([Math.floor(Math.random() * hintstrings.size)]); this.showmotd = false; From ac24da8f72f10571a77509f70d95c4d79938ca3d Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Fri, 22 Nov 2024 10:08:58 +1300 Subject: [PATCH 7/7] Remove unused parameter --- assets/chat/js/chat.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index 0eeb4449..b0e669c8 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -1164,12 +1164,10 @@ class Chat { if (isCombo && win.lastmessage?.type === MessageTypes.USER) { const lastMessage = win.lastmessage; win.removeLastMessage(); - const msg = MessageBuilder.emote( - textonly, - lastMessage.timestamp, - [lastMessage, message], - 2, - ).into(this); + const msg = MessageBuilder.emote(textonly, lastMessage.timestamp, [ + lastMessage, + message, + ]).into(this); if (this.user.equalWatching(usr.watching)) { msg.ui.classList.add('watching-same');