Skip to content

Commit

Permalink
refactor: make ChatUser fields more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
vyneer committed Sep 20, 2023
1 parent 8158412 commit 087c882
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 60 deletions.
60 changes: 28 additions & 32 deletions assets/chat/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ class Chat {
const nicks = [...(this.settings.get('highlightnicks') || [])].filter(
(a) => a !== '',
);
this.regexhighlightself = this.user.nick
? new RegExp(`\\b(?:${this.user.nick})\\b`, 'i')
this.regexhighlightself = this.user.displayName
? new RegExp(`\\b(?:${this.user.displayName})\\b`, 'i')
: null;
this.regexhighlightcustom =
cust.length > 0 ? new RegExp(`\\b(?:${cust.join('|')})\\b`, 'i') : null;
Expand Down Expand Up @@ -718,18 +718,15 @@ class Chat {
message.slashme =
message.message.substring(0, 4).toLowerCase() === '/me ';
// check if this is the current users message
message.isown =
message.user.username.toLowerCase() ===
this.user.username.toLowerCase();
message.isown = message.user.username === this.user.username;
// get mentions from message
message.mentioned = Chat.extractNicks(message.message).filter((a) =>
this.users.has(a.toLowerCase()),
);
// set tagged state
message.tag = this.taggednicks.get(message.user.nick.toLowerCase());
message.tag = this.taggednicks.get(message.user.username);
// set tagged note
message.title =
this.taggednotes.get(message.user.nick.toLowerCase()) || '';
message.title = this.taggednotes.get(message.user.username) || '';
}

// Populate highlight for this $message
Expand All @@ -739,8 +736,7 @@ class Chat {
win.lastmessage &&
!win.lastmessage.target &&
win.lastmessage.user &&
win.lastmessage.user.username.toLowerCase() ===
message.user.username.toLowerCase();
win.lastmessage.user.username === message.user.username;
// set highlighted state
message.highlighted = this.shouldHighlightMessage(message);
}
Expand All @@ -754,7 +750,7 @@ class Chat {
win.addMessage(this, message);

// Hide the message if the user is ignored
if (message.user && this.ignored(message.user.nick, message.message)) {
if (message.user && this.ignored(message.user.username, message.message)) {
message.ignore();
}

Expand All @@ -767,7 +763,7 @@ class Chat {
!message.ignored
) {
Chat.showNotification(
`${message.user.username} said ...`,
`${message.user.displayName} said ...`,
message.message,
message.timestamp.valueOf(),
this.settings.get('notificationtimeout'),
Expand All @@ -780,7 +776,7 @@ class Chat {
resolveMessage(nick, str) {
for (const message of this.unresolved) {
if (
this.user.username.toLowerCase() === nick.toLowerCase() &&
this.user.username === nick.toLowerCase() &&
message.message === str
) {
this.unresolved.splice(this.unresolved.indexOf(message), 1);
Expand Down Expand Up @@ -879,8 +875,8 @@ class Chat {
this.mainwindow.update();
}

ignored(nick, text = null) {
const ignore = this.ignoring.has(nick.toLowerCase());
ignored(username, text = null) {
const ignore = this.ignoring.has(username);
if (!ignore && text !== null) {
return (
(this.settings.get('ignorementions') &&
Expand Down Expand Up @@ -929,7 +925,7 @@ class Chat {

setDefaultPlaceholderText() {
const placeholderText = this.authenticated
? `Write something ${this.user.username} ...`
? `Write something ${this.user.displayName} ...`
: `Write something ...`;
this.input.attr('placeholder', placeholderText);
}
Expand Down Expand Up @@ -975,7 +971,9 @@ class Chat {
if (data.recipient) {
users.push(this.addUser(data.recipient));
}
users.forEach((u) => this.autocomplete.add(u.nick, false, Date.now()));
users.forEach((u) =>
this.autocomplete.add(u.username, false, Date.now()),
);
}
}

Expand Down Expand Up @@ -1013,7 +1011,7 @@ class Chat {
onNAMES(data) {
MessageBuilder.status(
`Connected as ${
this.authenticated ? this.user.username : 'Guest'
this.authenticated ? this.user.displayName : 'Guest'
}. Serving ${data.connectioncount || 0} connections and ${
data.users.length
} users.`,
Expand Down Expand Up @@ -1080,14 +1078,14 @@ class Chat {
onVOTECAST(data) {
const usr = this.users.get(data.nick.toLowerCase());
this.chatpoll.castVote(data, usr);
if (data.nick.toLowerCase() === this.user.nick.toLowerCase()) {
if (data.nick.toLowerCase() === this.user.username) {
this.chatpoll.markVote(data.vote);
}
}

onMUTE(data) {
// data.data is the nick which has been banned
if (this.user.username.toLowerCase() === data.data.toLowerCase()) {
if (this.user.username === data.data.toLowerCase()) {
MessageBuilder.command(
`You have been muted by ${data.nick}.`,
data.timestamp,
Expand All @@ -1109,7 +1107,7 @@ class Chat {
}

onUNMUTE(data) {
if (this.user.username.toLowerCase() === data.data.toLowerCase()) {
if (this.user.username === data.data.toLowerCase()) {
MessageBuilder.command(
`You have been unmuted by ${data.nick}.`,
data.timestamp,
Expand All @@ -1126,7 +1124,7 @@ class Chat {

onBAN(data) {
// data.data is the nick which has been banned, no info about duration
if (this.user.username.toLowerCase() === data.data.toLowerCase()) {
if (this.user.username === data.data.toLowerCase()) {
MessageBuilder.command(
`You have been banned by ${data.nick}. Check your profile for more information.`,
data.timestamp,
Expand All @@ -1142,7 +1140,7 @@ class Chat {
}

onUNBAN(data) {
if (this.user.username.toLowerCase() === data.data.toLowerCase()) {
if (this.user.username === data.data.toLowerCase()) {
MessageBuilder.command(
`You have been unbanned by ${data.nick}.`,
data.timestamp,
Expand Down Expand Up @@ -1302,7 +1300,7 @@ class Chat {
} else {
conv.unread += 1;
}
this.replyusername = user.username;
this.replyusername = user.displayName;
this.menus.get('whisper-users').redraw();
this.redrawWindowIndicators();
}
Expand Down Expand Up @@ -1501,9 +1499,7 @@ class Chat {
).into(this);
}
} else if (
parts.some(
(username) => username.toLowerCase() === this.user.nick.toLowerCase(),
)
parts.some((username) => username.toLowerCase() === this.user.username)
) {
MessageBuilder.info("You can't add yourself to your ignore list.").into(
this,
Expand Down Expand Up @@ -1726,7 +1722,7 @@ class Chat {
cmdWHISPER(parts) {
if (!parts[0] || !nickregex.test(parts[0])) {
MessageBuilder.error('Invalid nick - /msg nick message').into(this);
} else if (parts[0].toLowerCase() === this.user.username.toLowerCase()) {
} else if (parts[0].toLowerCase() === this.user.username) {
MessageBuilder.error('Cannot send a message to yourself').into(this);
} else {
const data = parts.slice(1, parts.length).join(' ');
Expand Down Expand Up @@ -1767,7 +1763,7 @@ class Chat {
return;
}
const n = parts[0].toLowerCase();
if (n === this.user.username.toLowerCase()) {
if (n === this.user.username) {
MessageBuilder.error('Cannot tag yourself').into(this);
return;
}
Expand Down Expand Up @@ -1980,7 +1976,7 @@ class Chat {
const win = this.getActiveWindow();
const lastuser =
win.lastmessage && win.lastmessage.user
? win.lastmessage.user.username
? win.lastmessage.user.displayName
: null;
const username =
this.replyusername !== null && this.replyusername !== ''
Expand Down Expand Up @@ -2211,7 +2207,7 @@ class Chat {
const win = new ChatWindow(
normalized,
'chat-output-whisper',
user.nick,
user.displayName,
).into(this);
let once = true;
win.on('show', () => {
Expand All @@ -2220,7 +2216,7 @@ class Chat {
MessageBuilder.info(`Messages between you and ${nick}`).into(this, win);
fetch(
`${this.config.api.base}/api/messages/usr/${encodeURIComponent(
user.nick,
user.displayName,
)}/inbox`,
{ credentials: 'include' },
)
Expand Down
7 changes: 3 additions & 4 deletions assets/chat/js/menus/ChatUserInfoMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export default class ChatUserInfoMenu extends ChatMenuFloating {
[
this.clickedNick,
providedDuration,
`${this.clickedNick} banned by ${this.chat.user.nick}.`,
`${this.clickedNick} banned by ${this.chat.user.displayName}.`,
],
'IPBAN',
);
Expand Down Expand Up @@ -265,7 +265,7 @@ export default class ChatUserInfoMenu extends ChatMenuFloating {
this.flairSubheader.style.display = 'none';
}

const messageList = this.createMessages();
const messageList = this.createMessages(prettyNick);
if (messageList.length === 0) {
this.messagesList.toggleClass('hidden', true);
this.messagesSubheader.style.display = 'none';
Expand Down Expand Up @@ -320,7 +320,7 @@ export default class ChatUserInfoMenu extends ChatMenuFloating {
return features !== '' ? `<span class="features">${features}</span>` : '';
}

createMessages() {
createMessages(nick) {
const displayedMessages = [];
if (this.messageArray.length > 0) {
let nextMsg = this.messageArray[0].next('.msg-continue');
Expand All @@ -330,7 +330,6 @@ export default class ChatUserInfoMenu extends ChatMenuFloating {
}
this.messageArray.forEach((element) => {
const text = element.find('.text')[0].innerText;
const nick = element.data('username');

// Create a new `ChatUser` to remove username styles for a cleaner look.
const msg = MessageBuilder.message(text, new ChatUser(nick));
Expand Down
4 changes: 3 additions & 1 deletion assets/chat/js/menus/ChatUserMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ export default class ChatUserMenu extends ChatMenu {
addElement(messageUser, sort = false) {
const user = new ChatUser(messageUser);
const label =
!user.username || user.username === '' ? 'Anonymous' : user.username;
!user.displayName || user.displayName === ''
? 'Anonymous'
: user.displayName;
const features =
user.features.length === 0 ? 'nofeature' : user.features.join(' ');
const usr = $(
Expand Down
6 changes: 2 additions & 4 deletions assets/chat/js/menus/ChatWhisperUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ export default class ChatWhisperUsers extends ChatMenu {
const user = this.chat.users.get(nick.toLowerCase()) || new ChatUser(nick);
this.usersEl.append(`
<li class="conversation unread-${unread}">
<a style="flex: 1;" data-username="${user.nick.toLowerCase()}" class="user">${
user.nick
}</a>
<a style="flex: 1;" data-username="${user.username}" class="user">${user.displayName}</a>
<span class="badge">${unread}</span>
<a data-username="${user.nick.toLowerCase()}" title="Hide" class="remove"></a>
<a data-username="${user.username}" title="Hide" class="remove"></a>
</li>
`);
}
Expand Down
2 changes: 1 addition & 1 deletion assets/chat/js/messages/ChatDonationMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class ChatDonationMessage extends ChatEventMessage {

user.title = this.title;
user.classList.add(colorFlair?.name);
user.innerText = this.user.username;
user.innerText = this.user.displayName;

eventTemplate.querySelector('.event-info').append(
user,
Expand Down
4 changes: 2 additions & 2 deletions assets/chat/js/messages/ChatEventMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export default class ChatEventMessage extends ChatMessage {
.querySelector('#event-template')
?.content.cloneNode(true).firstElementChild;

if (this.user && this.user.username)
eventTemplate.dataset.username = this.user.username.toLowerCase();
if (this.user && this.user.displayName)
eventTemplate.dataset.username = this.user.displayName;
if (this.mentioned && this.mentioned.length > 0)
eventTemplate.dataset.mentioned = this.mentioned.join(' ').toLowerCase();
if (this.slashme) eventTemplate.classList.add('msg-me');
Expand Down
4 changes: 2 additions & 2 deletions assets/chat/js/messages/ChatUserMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class ChatUserMessage extends ChatMessage {
if (this.id) attr['data-id'] = this.id;
if (this.user && this.user.username) {
classes.push(...this.user.features);
attr['data-username'] = this.user.username.toLowerCase();
attr['data-username'] = this.user.username;
}
if (this.mentioned && this.mentioned.length > 0)
attr['data-mentioned'] = this.mentioned.join(' ').toLowerCase();
Expand All @@ -56,7 +56,7 @@ export default class ChatUserMessage extends ChatMessage {
const colorFlair = usernameColorFlair(chat.flairs, this.user);
const user = `${this.buildFeatures(this.user, chat)} <a title="${
this.title
}" class="user ${colorFlair?.name}">${this.user.username}</a>`;
}" class="user ${colorFlair?.name}">${this.user.displayName}</a>`;
return this.wrap(
`${this.buildTime()} ${user}<span class="ctrl">${ctrl}</span> ${this.buildMessageTxt(
chat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class ChatSubscriptionMessage extends ChatEventMessage {
?.content.cloneNode(true).firstElementChild;
user.title = this.title;
user.classList.add(colorFlair?.name);
user.innerText = this.user.username;
user.innerText = this.user.displayName;

const tierLabel = this.tierLabel ?? `Tier ${this.tier}`;

Expand Down
2 changes: 1 addition & 1 deletion assets/chat/js/mutedtimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MutedTimer {

getPlaceholderText() {
return `Sorry, ${
this.chat.user.username
this.chat.user.displayName
}, you are muted. You can chat again ${this.getReadableDuration()}.`;
}

Expand Down
Loading

0 comments on commit 087c882

Please sign in to comment.