From 6759c2972db5f60817a7140107f770473c3c6d31 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Mon, 18 Nov 2024 18:48:06 +1300 Subject: [PATCH 1/6] Fix continue and combo from message history --- assets/chat/js/chat.js | 27 +++++++++++++++------------ assets/chat/js/window.js | 24 +++++++++++++++++++++--- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index d01aa2f0..c789407e 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -757,15 +757,16 @@ class Chat { addMessage(message, win = null) { // eslint-disable-next-line no-param-reassign if (win === null) win = this.mainwindow; + const lastmessage = 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 ( - win.lastmessage && - win.lastmessage.type === MessageTypes.EMOTE && - win.lastmessage.emotecount > 1 + lastmessage && + lastmessage.type === MessageTypes.EMOTE && + lastmessage.emotecount > 1 ) - win.lastmessage.completeCombo(); + lastmessage.completeCombo(); // Populate the tag and mentioned users for this $message. if ( @@ -804,7 +805,7 @@ class Chat { } // This looks odd, although it would be a correct implementation - /* else if(win.lastmessage && win.lastmessage.type === message.type && [MessageTypes.ERROR,MessageTypes.INFO,MessageTypes.COMMAND,MessageTypes.STATUS].indexOf(message.type)){ + /* else if(lastmessage && lastmessage.type === message.type && [MessageTypes.ERROR,MessageTypes.INFO,MessageTypes.COMMAND,MessageTypes.STATUS].indexOf(message.type)){ message.continued = true } */ @@ -1147,23 +1148,25 @@ 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); + const lastmessage = win.getPreviousMessage(message); const isCombo = this.emoteService.canUserUseEmote(usr, textonly) && - this.removeSlashCmdFromText(win.lastmessage?.message) === textonly; + this.removeSlashCmdFromText(lastmessage?.message) === textonly; - if (isCombo && win.lastmessage?.type === MessageTypes.EMOTE) { - win.lastmessage.incEmoteCount(); + if (isCombo && lastmessage?.type === MessageTypes.EMOTE) { + lastmessage.incEmoteCount(); if (this.user.equalWatching(usr.watching)) { - win.lastmessage.ui.classList.toggle('watching-same', true); + lastmessage.ui.classList.toggle('watching-same', true); } this.mainwindow.update(); return; } - if (isCombo && win.lastmessage?.type === MessageTypes.USER) { - win.removeLastMessage(); + if (isCombo && lastmessage?.type === MessageTypes.USER) { + win.removeMessage(lastmessage); const msg = MessageBuilder.emote(textonly, data.timestamp, 2).into(this); if (this.user.equalWatching(usr.watching)) { @@ -1173,7 +1176,7 @@ class Chat { } if (!this.resolveMessage(data.nick, data.data)) { - MessageBuilder.message(data.data, usr, data.timestamp).into(this); + message.into(this); } } diff --git a/assets/chat/js/window.js b/assets/chat/js/window.js index b1b5a1ff..ae3d6788 100644 --- a/assets/chat/js/window.js +++ b/assets/chat/js/window.js @@ -82,9 +82,7 @@ class ChatWindow extends EventEmitter { message.afterRender(chat); // Get index of where the message should be based on timestamp. - const index = this.messages.findLastIndex( - (m) => m.timestamp.valueOf() <= message.timestamp.valueOf(), - ); + const index = this.getMessageIndex(message); /** * If message index is < 0 then add message to the top of chat. @@ -110,6 +108,21 @@ class ChatWindow extends EventEmitter { this.cleanupThrottle(); } + getMessageIndex(message) { + return this.messages.findLastIndex( + (m) => m.timestamp.valueOf() <= message.timestamp.valueOf(), + ); + } + + getPreviousMessage(message) { + const index = this.getMessageIndex(message); + if (index < 0 || index > this.messages.length) { + return null; + } + + return this.messages[index]; + } + containsMessage(message) { return this.messages.find((msg) => msg.md5 === message.md5); } @@ -205,6 +218,11 @@ class ChatWindow extends EventEmitter { this.messages = this.messages.filter((m) => m !== this.lastmessage); } + removeMessage(message) { + message.remove(); + this.messages = this.messages.filter((m) => m !== message); + } + /** * @param {ChatMessage} message * @param {ChatMessage} lastMessage From 0d3357964867b5872bb483a43aefb5a754453ffa Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Mon, 18 Nov 2024 18:50:46 +1300 Subject: [PATCH 2/6] use correct lastmessage for isContinued --- assets/chat/js/chat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index c789407e..eb3e7918 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -799,7 +799,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); + message.continued = win.isContinued(message, lastmessage); // set highlighted state message.highlighted = this.shouldHighlightMessage(message); } From 642d0157d201d6cd18063bbbe2bf07155c2435f8 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Mon, 18 Nov 2024 18:55:30 +1300 Subject: [PATCH 3/6] Remove unused removeLastMessage --- assets/chat/js/window.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/assets/chat/js/window.js b/assets/chat/js/window.js index ae3d6788..db9b9b07 100644 --- a/assets/chat/js/window.js +++ b/assets/chat/js/window.js @@ -213,11 +213,6 @@ class ChatWindow extends EventEmitter { } } - removeLastMessage() { - this.lastmessage.remove(); - this.messages = this.messages.filter((m) => m !== this.lastmessage); - } - removeMessage(message) { message.remove(); this.messages = this.messages.filter((m) => m !== message); From a1392f6c2dbaae9263ea8595946a5cf7c683c224 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Mon, 18 Nov 2024 19:38:23 +1300 Subject: [PATCH 4/6] Make index more readable. --- assets/chat/js/window.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/assets/chat/js/window.js b/assets/chat/js/window.js index db9b9b07..5d1fce4d 100644 --- a/assets/chat/js/window.js +++ b/assets/chat/js/window.js @@ -85,23 +85,20 @@ class ChatWindow extends EventEmitter { const index = this.getMessageIndex(message); /** - * If message index is < 0 then add message to the top of chat. - * - * If message index + 1 >= the length of messages array, - * it is a new message so add to bottom of chat. - * - * Otherwise the message is inbetween so insert in correct place. + * 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) { + if (index === 0) { this.lines.prepend(message.ui); this.messages.unshift(message); - } else if (index + 1 >= this.messages.length) { + } 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 + 1].ui); - this.messages.splice(index + 1, 0, message); + this.lines.insertBefore(message.ui, this.messages[index].ui); + this.messages.splice(index, 0, message); } this.linecount += 1; @@ -109,18 +106,20 @@ class ChatWindow extends EventEmitter { } getMessageIndex(message) { - return this.messages.findLastIndex( - (m) => m.timestamp.valueOf() <= message.timestamp.valueOf(), + return ( + this.messages.findLastIndex( + (m) => m.timestamp.valueOf() <= message.timestamp.valueOf(), + ) + 1 ); } getPreviousMessage(message) { const index = this.getMessageIndex(message); - if (index < 0 || index > this.messages.length) { + if (index === 0) { return null; } - return this.messages[index]; + return this.messages[index - 1]; } containsMessage(message) { From 6435944fba1a6c09c57b33a4462bbc9b06f60862 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Mon, 18 Nov 2024 19:41:03 +1300 Subject: [PATCH 5/6] Use previousMessage instead of lastmessage --- assets/chat/js/chat.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index eb3e7918..29be24fb 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -757,16 +757,16 @@ class Chat { addMessage(message, win = null) { // eslint-disable-next-line no-param-reassign if (win === null) win = this.mainwindow; - const lastmessage = win.getPreviousMessage(message); + 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 ( - lastmessage && - lastmessage.type === MessageTypes.EMOTE && - lastmessage.emotecount > 1 + previousMessage && + previousMessage.type === MessageTypes.EMOTE && + previousMessage.emotecount > 1 ) - lastmessage.completeCombo(); + previousMessage.completeCombo(); // Populate the tag and mentioned users for this $message. if ( @@ -799,7 +799,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, lastmessage); + message.continued = win.isContinued(message, previousMessage); // set highlighted state message.highlighted = this.shouldHighlightMessage(message); } @@ -1149,24 +1149,24 @@ class Chat { const usr = this.users.get(data.nick.toLowerCase()); const win = this.mainwindow; const message = MessageBuilder.message(data.data, usr, data.timestamp); - const lastmessage = win.getPreviousMessage(message); + const previousMessage = win.getPreviousMessage(message); const isCombo = this.emoteService.canUserUseEmote(usr, textonly) && - this.removeSlashCmdFromText(lastmessage?.message) === textonly; + this.removeSlashCmdFromText(previousMessage?.message) === textonly; - if (isCombo && lastmessage?.type === MessageTypes.EMOTE) { - lastmessage.incEmoteCount(); + if (isCombo && previousMessage?.type === MessageTypes.EMOTE) { + previousMessage.incEmoteCount(); if (this.user.equalWatching(usr.watching)) { - lastmessage.ui.classList.toggle('watching-same', true); + previousMessage.ui.classList.toggle('watching-same', true); } this.mainwindow.update(); return; } - if (isCombo && lastmessage?.type === MessageTypes.USER) { - win.removeMessage(lastmessage); + if (isCombo && previousMessage?.type === MessageTypes.USER) { + win.removeMessage(previousMessage); const msg = MessageBuilder.emote(textonly, data.timestamp, 2).into(this); if (this.user.equalWatching(usr.watching)) { From fb64611eb77ed9bc8f90e9acb07e511694be5af9 Mon Sep 17 00:00:00 2001 From: Mitchdev Date: Mon, 18 Nov 2024 19:41:33 +1300 Subject: [PATCH 6/6] Remove unused --- assets/chat/js/chat.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index 29be24fb..a046aa39 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -804,11 +804,6 @@ class Chat { message.highlighted = this.shouldHighlightMessage(message); } - // This looks odd, although it would be a correct implementation - /* else if(lastmessage && lastmessage.type === message.type && [MessageTypes.ERROR,MessageTypes.INFO,MessageTypes.COMMAND,MessageTypes.STATUS].indexOf(message.type)){ - message.continued = true - } */ - // The point where we actually add the message dom win.addMessage(this, message);