Skip to content

Commit

Permalink
Fix continue and combo from message history
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitchdev committed Nov 18, 2024
1 parent 3d346cf commit 6759c29
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
27 changes: 15 additions & 12 deletions assets/chat/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
} */

Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}
}

Expand Down
24 changes: 21 additions & 3 deletions assets/chat/js/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6759c29

Please sign in to comment.