Skip to content
Permalink
Browse files
Trim messages rather than have them disappear into the void
  • Loading branch information
RussellLVP committed Jul 15, 2020
1 parent cc272c6 commit b1da2a6d6181eac092e99e8cb27092086c9b1362
Showing 3 changed files with 47 additions and 0 deletions.
@@ -32,6 +32,9 @@ const REMOVE_REPLACEMENT_QUERY = `
LIMIT
1`;

// Maximum length of a message in main chat, in number of characters.
const kMaximumMessageLength = 122;

// Minimum message length before considering recapitalization.
const kRecapitalizeMinimumMessageLength = 10;

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

// (3) Cap the length of a message to a determined maximum, as messages otherwise would
// disappear into the void with no information given to the sending player at all.
const maximumLength = kMaximumMessageLength - player.name.length;
if (message.length > maximumLength)
message = this.trimMessage(message, maximumLength);

return message;
}

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

// Trims the given |message| to the given |maximumLength|. We'll find the closest word from
// that position and break there when it's close enough, otherwise apply a hard break.
trimMessage(message, maximumLength) {
const kCutoffText = '...';

// Determines exactly where the |message| should be cut.
const messageCutoffIndex = maximumLength - kCutoffText.length;
const messageCutoffWhitespace = message.lastIndexOf(' ', messageCutoffIndex);

// If the last whitespace character is within 8 characters of the message length limit, cut
// there. Otherwise cut the |message| exactly at the limit.
if (messageCutoffIndex - messageCutoffWhitespace <= 8)
return message.substring(0, messageCutoffWhitespace) + kCutoffText;
else
return message.substring(0, messageCutoffIndex) + kCutoffText;
}

// ---------------------------------------------------------------------------------------------

// Loads the replacements from the database, once a connection has been established. Mocked out
@@ -45,6 +45,26 @@ describe('MessageFilter', (it, beforeEach) => {
assert.equal(filter.filter(gunther, 'Hey Luce!'), 'Hey Luce!');
});

it('should limit the length of a message to 128-[len(nickname)] characters', assert => {
const longText =
'..................................................................................' +
'...........................................';

const longWords =
'fall marsh book waste dare proof jump graze ring tower shed lease doll trunk lake ' +
'agony year lemon cake essay lock heart tent';

// (1) Cut with the nickname "Russell", giving a maximum length of 115.
assert.equal(filter.filter(gunther, longText).length, 115);
assert.equal(filter.filter(gunther, longWords).length, 112); // essay...

gunther.name = 'OwmKwrjkRmPlwQjaQaEhZoSy';

// (2) Cut with the nickname "OwmKwrjkRmPlwQjaQaEhZoSy", giving a maximum length of 98.
assert.equal(filter.filter(gunther, longText).length, 98);
assert.equal(filter.filter(gunther, longWords).length, 95); // year...
});

it('should be able to completely recapitalize a sentence', assert => {
// (1) Remove excess exclamation and question marks
assert.equal(filter.recapitalize('HUH??!!'), 'Huh?!');
@@ -108,6 +108,7 @@ export class NuwaniCommands {
if (!this.nuwaniPlayers_.has(nickname)) {
this.nuwaniPlayers_.set(nickname, new class {
account = { mutedUntil: null };
name = nickname;

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

0 comments on commit b1da2a6

Please sign in to comment.