Skip to content

Commit b1da2a6

Browse files
committed
Trim messages rather than have them disappear into the void
1 parent cc272c6 commit b1da2a6

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

javascript/features/communication/message_filter.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const REMOVE_REPLACEMENT_QUERY = `
3232
LIMIT
3333
1`;
3434

35+
// Maximum length of a message in main chat, in number of characters.
36+
const kMaximumMessageLength = 122;
37+
3538
// Minimum message length before considering recapitalization.
3639
const kRecapitalizeMinimumMessageLength = 10;
3740

@@ -117,6 +120,12 @@ export class MessageFilter {
117120
message = this.applyReplacement(message, replacement);
118121
}
119122

123+
// (3) Cap the length of a message to a determined maximum, as messages otherwise would
124+
// disappear into the void with no information given to the sending player at all.
125+
const maximumLength = kMaximumMessageLength - player.name.length;
126+
if (message.length > maximumLength)
127+
message = this.trimMessage(message, maximumLength);
128+
120129
return message;
121130
}
122131

@@ -210,6 +219,23 @@ export class MessageFilter {
210219
});
211220
}
212221

222+
// Trims the given |message| to the given |maximumLength|. We'll find the closest word from
223+
// that position and break there when it's close enough, otherwise apply a hard break.
224+
trimMessage(message, maximumLength) {
225+
const kCutoffText = '...';
226+
227+
// Determines exactly where the |message| should be cut.
228+
const messageCutoffIndex = maximumLength - kCutoffText.length;
229+
const messageCutoffWhitespace = message.lastIndexOf(' ', messageCutoffIndex);
230+
231+
// If the last whitespace character is within 8 characters of the message length limit, cut
232+
// there. Otherwise cut the |message| exactly at the limit.
233+
if (messageCutoffIndex - messageCutoffWhitespace <= 8)
234+
return message.substring(0, messageCutoffWhitespace) + kCutoffText;
235+
else
236+
return message.substring(0, messageCutoffIndex) + kCutoffText;
237+
}
238+
213239
// ---------------------------------------------------------------------------------------------
214240

215241
// Loads the replacements from the database, once a connection has been established. Mocked out

javascript/features/communication/message_filter.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ describe('MessageFilter', (it, beforeEach) => {
4545
assert.equal(filter.filter(gunther, 'Hey Luce!'), 'Hey Luce!');
4646
});
4747

48+
it('should limit the length of a message to 128-[len(nickname)] characters', assert => {
49+
const longText =
50+
'..................................................................................' +
51+
'...........................................';
52+
53+
const longWords =
54+
'fall marsh book waste dare proof jump graze ring tower shed lease doll trunk lake ' +
55+
'agony year lemon cake essay lock heart tent';
56+
57+
// (1) Cut with the nickname "Russell", giving a maximum length of 115.
58+
assert.equal(filter.filter(gunther, longText).length, 115);
59+
assert.equal(filter.filter(gunther, longWords).length, 112); // essay...
60+
61+
gunther.name = 'OwmKwrjkRmPlwQjaQaEhZoSy';
62+
63+
// (2) Cut with the nickname "OwmKwrjkRmPlwQjaQaEhZoSy", giving a maximum length of 98.
64+
assert.equal(filter.filter(gunther, longText).length, 98);
65+
assert.equal(filter.filter(gunther, longWords).length, 95); // year...
66+
});
67+
4868
it('should be able to completely recapitalize a sentence', assert => {
4969
// (1) Remove excess exclamation and question marks
5070
assert.equal(filter.recapitalize('HUH??!!'), 'Huh?!');

javascript/features/communication_commands/nuwani_commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export class NuwaniCommands {
108108
if (!this.nuwaniPlayers_.has(nickname)) {
109109
this.nuwaniPlayers_.set(nickname, new class {
110110
account = { mutedUntil: null };
111+
name = nickname;
111112

112113
sendMessage(message) {
113114
contextMap.get(nickname).respond(

0 commit comments

Comments
 (0)