Skip to content

Commit

Permalink
refactor: move is msg continued logic into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
vyneer committed Dec 27, 2023
1 parent 58caf23 commit e5086c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
7 changes: 1 addition & 6 deletions assets/chat/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,12 +756,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.lastmessage &&
!win.lastmessage.target &&
win.lastmessage.user &&
(!win.lastmessage.ignored || win.lastmessage.continued) && // messages should not appear as "continued" if the previous message is ignored and was the start of the thread
win.lastmessage.user.username === message.user.username;
message.continued = win.isContinued(message);
// set highlighted state
message.highlighted = this.shouldHighlightMessage(message);
}
Expand Down
25 changes: 16 additions & 9 deletions assets/chat/js/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,7 @@ class ChatWindow extends EventEmitter {
message.ignore(chat.ignored(username, message.message));
message.highlight(chat.shouldHighlightMessage(message));
if (message.type === MessageTypes.USER) {
const lastMessage = this.messages[i - 1];
message.setContinued(
lastMessage &&
!lastMessage.target &&
lastMessage.user &&
(!lastMessage.ignored || lastMessage.continued) && // messages should not appear as "continued" if the previous message is ignored and was the start of the thread
lastMessage.user.username === username,
);

message.setContinued(this.isContinued(message, this.messages[i - 1]));
message.setTag(chat.taggednicks.get(username));
}
message.setTagTitle(chat.taggednotes.get(username));
Expand All @@ -160,6 +152,21 @@ class ChatWindow extends EventEmitter {
this.messages = this.messages.filter((m) => m !== this.lastmessage);
}

/**
* @param {ChatMessage} message
* @param {ChatMessage} lastMessage
* @returns {boolean}
*/
isContinued(message, lastMessage = this.lastmessage) {
return (
lastMessage &&
!lastMessage.target &&
lastMessage.user &&
(!lastMessage.ignored || lastMessage.continued) && // messages should not appear as "continued" if the previous message is ignored and was the start of the thread
lastMessage.user.username === message.user.username
);
}

cleanupThrottle = throttle(50, this.cleanup);
}

Expand Down

0 comments on commit e5086c5

Please sign in to comment.