Skip to content

Commit b58d5b7

Browse files
authored
Fix some users not getting censored on ban/mute (#339)
* fix: some users not being censored on ban/mute * refactor: make ChatUser fields more consistent * refactor: move ChatUser fields to class body * chore: rename 'prettyNick' to 'displayName' * fix: use 'username' in the event message dataset
1 parent b1f243c commit b58d5b7

11 files changed

+97
-64
lines changed

assets/chat/js/chat.js

+29-33
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,8 @@ class Chat {
634634
const nicks = [...(this.settings.get('highlightnicks') || [])].filter(
635635
(a) => a !== '',
636636
);
637-
this.regexhighlightself = this.user.nick
638-
? new RegExp(`\\b(?:${this.user.nick})\\b`, 'i')
637+
this.regexhighlightself = this.user.displayName
638+
? new RegExp(`\\b(?:${this.user.displayName})\\b`, 'i')
639639
: null;
640640
this.regexhighlightcustom =
641641
cust.length > 0 ? new RegExp(`\\b(?:${cust.join('|')})\\b`, 'i') : null;
@@ -718,18 +718,15 @@ class Chat {
718718
message.slashme =
719719
message.message.substring(0, 4).toLowerCase() === '/me ';
720720
// check if this is the current users message
721-
message.isown =
722-
message.user.username.toLowerCase() ===
723-
this.user.username.toLowerCase();
721+
message.isown = message.user.username === this.user.username;
724722
// get mentions from message
725723
message.mentioned = Chat.extractNicks(message.message).filter((a) =>
726724
this.users.has(a.toLowerCase()),
727725
);
728726
// set tagged state
729-
message.tag = this.taggednicks.get(message.user.nick.toLowerCase());
727+
message.tag = this.taggednicks.get(message.user.username);
730728
// set tagged note
731-
message.title =
732-
this.taggednotes.get(message.user.nick.toLowerCase()) || '';
729+
message.title = this.taggednotes.get(message.user.username) || '';
733730
}
734731

735732
// Populate highlight for this $message
@@ -739,8 +736,7 @@ class Chat {
739736
win.lastmessage &&
740737
!win.lastmessage.target &&
741738
win.lastmessage.user &&
742-
win.lastmessage.user.username.toLowerCase() ===
743-
message.user.username.toLowerCase();
739+
win.lastmessage.user.username === message.user.username;
744740
// set highlighted state
745741
message.highlighted = this.shouldHighlightMessage(message);
746742
}
@@ -754,7 +750,7 @@ class Chat {
754750
win.addMessage(this, message);
755751

756752
// Hide the message if the user is ignored
757-
if (message.user && this.ignored(message.user.nick, message.message)) {
753+
if (message.user && this.ignored(message.user.username, message.message)) {
758754
message.ignore();
759755
}
760756

@@ -767,7 +763,7 @@ class Chat {
767763
!message.ignored
768764
) {
769765
Chat.showNotification(
770-
`${message.user.username} said ...`,
766+
`${message.user.displayName} said ...`,
771767
message.message,
772768
message.timestamp.valueOf(),
773769
this.settings.get('notificationtimeout'),
@@ -780,7 +776,7 @@ class Chat {
780776
resolveMessage(nick, str) {
781777
for (const message of this.unresolved) {
782778
if (
783-
this.user.username.toLowerCase() === nick.toLowerCase() &&
779+
this.user.username === nick.toLowerCase() &&
784780
message.message === str
785781
) {
786782
this.unresolved.splice(this.unresolved.indexOf(message), 1);
@@ -871,16 +867,16 @@ class Chat {
871867

872868
censor(nick) {
873869
for (const message of this.mainwindow.messages) {
874-
if (message.user?.username === nick) {
870+
if (message.user?.username === nick.toLowerCase()) {
875871
message.censor(parseInt(this.settings.get('showremoved') || '1', 10));
876872
}
877873
}
878874

879875
this.mainwindow.update();
880876
}
881877

882-
ignored(nick, text = null) {
883-
const ignore = this.ignoring.has(nick.toLowerCase());
878+
ignored(username, text = null) {
879+
const ignore = this.ignoring.has(username);
884880
if (!ignore && text !== null) {
885881
return (
886882
(this.settings.get('ignorementions') &&
@@ -929,7 +925,7 @@ class Chat {
929925

930926
setDefaultPlaceholderText() {
931927
const placeholderText = this.authenticated
932-
? `Write something ${this.user.username} ...`
928+
? `Write something ${this.user.displayName} ...`
933929
: `Write something ...`;
934930
this.input.attr('placeholder', placeholderText);
935931
}
@@ -975,7 +971,9 @@ class Chat {
975971
if (data.recipient) {
976972
users.push(this.addUser(data.recipient));
977973
}
978-
users.forEach((u) => this.autocomplete.add(u.nick, false, Date.now()));
974+
users.forEach((u) =>
975+
this.autocomplete.add(u.username, false, Date.now()),
976+
);
979977
}
980978
}
981979

@@ -1013,7 +1011,7 @@ class Chat {
10131011
onNAMES(data) {
10141012
MessageBuilder.status(
10151013
`Connected as ${
1016-
this.authenticated ? this.user.username : 'Guest'
1014+
this.authenticated ? this.user.displayName : 'Guest'
10171015
}. Serving ${data.connectioncount || 0} connections and ${
10181016
data.users.length
10191017
} users.`,
@@ -1080,14 +1078,14 @@ class Chat {
10801078
onVOTECAST(data) {
10811079
const usr = this.users.get(data.nick.toLowerCase());
10821080
this.chatpoll.castVote(data, usr);
1083-
if (data.nick.toLowerCase() === this.user.nick.toLowerCase()) {
1081+
if (data.nick.toLowerCase() === this.user.username) {
10841082
this.chatpoll.markVote(data.vote);
10851083
}
10861084
}
10871085

10881086
onMUTE(data) {
10891087
// data.data is the nick which has been banned
1090-
if (this.user.username.toLowerCase() === data.data.toLowerCase()) {
1088+
if (this.user.username === data.data.toLowerCase()) {
10911089
MessageBuilder.command(
10921090
`You have been muted by ${data.nick}.`,
10931091
data.timestamp,
@@ -1109,7 +1107,7 @@ class Chat {
11091107
}
11101108

11111109
onUNMUTE(data) {
1112-
if (this.user.username.toLowerCase() === data.data.toLowerCase()) {
1110+
if (this.user.username === data.data.toLowerCase()) {
11131111
MessageBuilder.command(
11141112
`You have been unmuted by ${data.nick}.`,
11151113
data.timestamp,
@@ -1126,7 +1124,7 @@ class Chat {
11261124

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

11441142
onUNBAN(data) {
1145-
if (this.user.username.toLowerCase() === data.data.toLowerCase()) {
1143+
if (this.user.username === data.data.toLowerCase()) {
11461144
MessageBuilder.command(
11471145
`You have been unbanned by ${data.nick}.`,
11481146
data.timestamp,
@@ -1302,7 +1300,7 @@ class Chat {
13021300
} else {
13031301
conv.unread += 1;
13041302
}
1305-
this.replyusername = user.username;
1303+
this.replyusername = user.displayName;
13061304
this.menus.get('whisper-users').redraw();
13071305
this.redrawWindowIndicators();
13081306
}
@@ -1501,9 +1499,7 @@ class Chat {
15011499
).into(this);
15021500
}
15031501
} else if (
1504-
parts.some(
1505-
(username) => username.toLowerCase() === this.user.nick.toLowerCase(),
1506-
)
1502+
parts.some((username) => username.toLowerCase() === this.user.username)
15071503
) {
15081504
MessageBuilder.info("You can't add yourself to your ignore list.").into(
15091505
this,
@@ -1726,7 +1722,7 @@ class Chat {
17261722
cmdWHISPER(parts) {
17271723
if (!parts[0] || !nickregex.test(parts[0])) {
17281724
MessageBuilder.error('Invalid nick - /msg nick message').into(this);
1729-
} else if (parts[0].toLowerCase() === this.user.username.toLowerCase()) {
1725+
} else if (parts[0].toLowerCase() === this.user.username) {
17301726
MessageBuilder.error('Cannot send a message to yourself').into(this);
17311727
} else {
17321728
const data = parts.slice(1, parts.length).join(' ');
@@ -1767,7 +1763,7 @@ class Chat {
17671763
return;
17681764
}
17691765
const n = parts[0].toLowerCase();
1770-
if (n === this.user.username.toLowerCase()) {
1766+
if (n === this.user.username) {
17711767
MessageBuilder.error('Cannot tag yourself').into(this);
17721768
return;
17731769
}
@@ -1980,7 +1976,7 @@ class Chat {
19801976
const win = this.getActiveWindow();
19811977
const lastuser =
19821978
win.lastmessage && win.lastmessage.user
1983-
? win.lastmessage.user.username
1979+
? win.lastmessage.user.displayName
19841980
: null;
19851981
const username =
19861982
this.replyusername !== null && this.replyusername !== ''
@@ -2211,7 +2207,7 @@ class Chat {
22112207
const win = new ChatWindow(
22122208
normalized,
22132209
'chat-output-whisper',
2214-
user.nick,
2210+
user.displayName,
22152211
).into(this);
22162212
let once = true;
22172213
win.on('show', () => {
@@ -2220,7 +2216,7 @@ class Chat {
22202216
MessageBuilder.info(`Messages between you and ${nick}`).into(this, win);
22212217
fetch(
22222218
`${this.config.api.base}/api/messages/usr/${encodeURIComponent(
2223-
user.nick,
2219+
user.displayName,
22242220
)}/inbox`,
22252221
{ credentials: 'include' },
22262222
)

assets/chat/js/menus/ChatUserInfoMenu.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export default class ChatUserInfoMenu extends ChatMenuFloating {
207207
[
208208
this.clickedNick,
209209
providedDuration,
210-
`${this.clickedNick} banned by ${this.chat.user.nick}.`,
210+
`${this.clickedNick} banned by ${this.chat.user.displayName}.`,
211211
],
212212
'IPBAN',
213213
);
@@ -233,7 +233,7 @@ export default class ChatUserInfoMenu extends ChatMenuFloating {
233233
const selectedUser = [...message[0].querySelectorAll('.user')].find(
234234
(user) => user.innerText.toLowerCase() === this.clickedNick.toLowerCase(),
235235
);
236-
const prettyNick = selectedUser.innerText;
236+
const displayName = selectedUser.innerText;
237237
const tagNote = this.chat.taggednotes.get(this.clickedNick);
238238
const usernameFeatures = selectedUser.classList.value;
239239

@@ -265,7 +265,7 @@ export default class ChatUserInfoMenu extends ChatMenuFloating {
265265
this.flairSubheader.style.display = 'none';
266266
}
267267

268-
const messageList = this.createMessages();
268+
const messageList = this.createMessages(displayName);
269269
if (messageList.length === 0) {
270270
this.messagesList.toggleClass('hidden', true);
271271
this.messagesSubheader.style.display = 'none';
@@ -282,7 +282,7 @@ export default class ChatUserInfoMenu extends ChatMenuFloating {
282282
this.messagesContainer.empty();
283283
this.flairList.empty();
284284

285-
this.header.text(prettyNick);
285+
this.header.text(displayName);
286286
this.header.addClass(usernameFeatures);
287287
this.flairList.append(featuresList);
288288
messageList.forEach((element) => {
@@ -320,7 +320,7 @@ export default class ChatUserInfoMenu extends ChatMenuFloating {
320320
return features !== '' ? `<span class="features">${features}</span>` : '';
321321
}
322322

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

335334
// Create a new `ChatUser` to remove username styles for a cleaner look.
336335
const msg = MessageBuilder.message(text, new ChatUser(nick));

assets/chat/js/menus/ChatUserMenu.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ export default class ChatUserMenu extends ChatMenu {
235235
addElement(messageUser, sort = false) {
236236
const user = new ChatUser(messageUser);
237237
const label =
238-
!user.username || user.username === '' ? 'Anonymous' : user.username;
238+
!user.displayName || user.displayName === ''
239+
? 'Anonymous'
240+
: user.displayName;
239241
const features =
240242
user.features.length === 0 ? 'nofeature' : user.features.join(' ');
241243
const usr = $(

assets/chat/js/menus/ChatWhisperUsers.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ export default class ChatWhisperUsers extends ChatMenu {
6767
const user = this.chat.users.get(nick.toLowerCase()) || new ChatUser(nick);
6868
this.usersEl.append(`
6969
<li class="conversation unread-${unread}">
70-
<a style="flex: 1;" data-username="${user.nick.toLowerCase()}" class="user">${
71-
user.nick
72-
}</a>
70+
<a style="flex: 1;" data-username="${user.username}" class="user">${user.displayName}</a>
7371
<span class="badge">${unread}</span>
74-
<a data-username="${user.nick.toLowerCase()}" title="Hide" class="remove"></a>
72+
<a data-username="${user.username}" title="Hide" class="remove"></a>
7573
</li>
7674
`);
7775
}

assets/chat/js/messages/ChatDonationMessage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class ChatDonationMessage extends ChatEventMessage {
3434

3535
user.title = this.title;
3636
user.classList.add(colorFlair?.name);
37-
user.innerText = this.user.username;
37+
user.innerText = this.user.displayName;
3838

3939
eventTemplate.querySelector('.event-info').append(
4040
user,

assets/chat/js/messages/ChatEventMessage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class ChatEventMessage extends ChatMessage {
1717
?.content.cloneNode(true).firstElementChild;
1818

1919
if (this.user && this.user.username)
20-
eventTemplate.dataset.username = this.user.username.toLowerCase();
20+
eventTemplate.dataset.username = this.user.username;
2121
if (this.mentioned && this.mentioned.length > 0)
2222
eventTemplate.dataset.mentioned = this.mentioned.join(' ').toLowerCase();
2323
if (this.slashme) eventTemplate.classList.add('msg-me');

assets/chat/js/messages/ChatUserMessage.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class ChatUserMessage extends ChatMessage {
3636
if (this.id) attr['data-id'] = this.id;
3737
if (this.user && this.user.username) {
3838
classes.push(...this.user.features);
39-
attr['data-username'] = this.user.username.toLowerCase();
39+
attr['data-username'] = this.user.username;
4040
}
4141
if (this.mentioned && this.mentioned.length > 0)
4242
attr['data-mentioned'] = this.mentioned.join(' ').toLowerCase();
@@ -56,7 +56,7 @@ export default class ChatUserMessage extends ChatMessage {
5656
const colorFlair = usernameColorFlair(chat.flairs, this.user);
5757
const user = `${this.buildFeatures(this.user, chat)} <a title="${
5858
this.title
59-
}" class="user ${colorFlair?.name}">${this.user.username}</a>`;
59+
}" class="user ${colorFlair?.name}">${this.user.displayName}</a>`;
6060
return this.wrap(
6161
`${this.buildTime()} ${user}<span class="ctrl">${ctrl}</span> ${this.buildMessageTxt(
6262
chat,

assets/chat/js/messages/subscriptions/ChatSubscriptionMessage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class ChatSubscriptionMessage extends ChatEventMessage {
4040
?.content.cloneNode(true).firstElementChild;
4141
user.title = this.title;
4242
user.classList.add(colorFlair?.name);
43-
user.innerText = this.user.username;
43+
user.innerText = this.user.displayName;
4444

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

assets/chat/js/mutedtimer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class MutedTimer {
7474

7575
getPlaceholderText() {
7676
return `Sorry, ${
77-
this.chat.user.username
77+
this.chat.user.displayName
7878
}, you are muted. You can chat again ${this.getReadableDuration()}.`;
7979
}
8080

0 commit comments

Comments
 (0)