Skip to content

Commit

Permalink
[FIX] Message_AllowedMaxSize fails for emoji sequences (#10431)
Browse files Browse the repository at this point in the history
Closes #10422 

This pr ensures emojis are treated as single characters when determining if message size exceeds allowed limit, for better experience.

Taking same example as mentioned in issue, if max allowed size is 10
![image](https://user-images.githubusercontent.com/23701803/38641610-32369d6e-3df5-11e8-8bfb-011734eb50cf.png)

now works and has effective size 10.
  • Loading branch information
c0dzilla authored and ggazzo committed Jun 12, 2018
1 parent 0bd6de6 commit b681db6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"emailreplyparser": "^0.0.5",
"file-type": "^8.0.0",
"filesize": "^3.6.1",
"grapheme-splitter": "^1.0.2",
"gridfs-stream": "^1.1.1",
"he": "^1.1.1",
"highlight.js": "^9.12.0",
Expand Down
24 changes: 24 additions & 0 deletions packages/rocketchat-lib/lib/MessageProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import GraphemeSplitter from 'grapheme-splitter';

const splitter = new GraphemeSplitter();

export const messageProperties = {

length: (message => {
return splitter.countGraphemes(message);
}),

messageWithoutEmojiShortnames: (message => {
return message.replace(/:\w+:/gm, (match) => {
if (RocketChat.emoji.list[match] !== undefined) {
return ' ';
}
return match;
});
})
};

// check for tests
if (typeof RocketChat !== 'undefined') {
RocketChat.messageProperties = messageProperties;
}
1 change: 1 addition & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Package.onUse(function(api) {
api.addFiles('lib/RoomTypesCommon.js');
api.addFiles('lib/slashCommand.js');
api.addFiles('lib/Message.js');
api.addFiles('lib/MessageProperties.js');
api.addFiles('lib/messageBox.js');
api.addFiles('lib/MessageTypes.js');
api.addFiles('lib/templateVarHandler.js');
Expand Down
12 changes: 8 additions & 4 deletions packages/rocketchat-lib/server/methods/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ Meteor.methods({
message.ts = new Date();
}

if (message.msg && message.msg.length > RocketChat.settings.get('Message_MaxAllowedSize')) {
throw new Meteor.Error('error-message-size-exceeded', 'Message size exceeds Message_MaxAllowedSize', {
method: 'sendMessage'
});
if (message.msg) {
const adjustedMessage = RocketChat.messageProperties.messageWithoutEmojiShortnames(message.msg);

if (RocketChat.messageProperties.length(adjustedMessage) > RocketChat.settings.get('Message_MaxAllowedSize')) {
throw new Meteor.Error('error-message-size-exceeded', 'Message size exceeds Message_MaxAllowedSize', {
method: 'sendMessage'
});
}
}

const user = RocketChat.models.Users.findOneById(Meteor.userId(), {
Expand Down
18 changes: 18 additions & 0 deletions packages/rocketchat-lib/tests/server.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'babel-polyfill';
import assert from 'assert';

import PasswordPolicyClass from '../server/lib/PasswordPolicyClass';
import {messageProperties} from '../lib/MessageProperties';

describe('PasswordPolicyClass', () => {
describe('Default options', () => {
Expand Down Expand Up @@ -204,3 +205,20 @@ describe('PasswordPolicyClass', () => {
});
});
});

const messages = {
'Sample Message': 14,
'Sample 1 ⛳': 10,
'Sample 2 ❤': 10,
'Sample 3 ⛳❤⛳❤': 13
};

describe('Message Properties', () => {
describe('Check Message Length', () => {
Object.keys(messages).forEach((objectKey) => {
it('should treat emojis as single characters', () => {
assert.equal(messageProperties.length(objectKey), messages[objectKey]);
});
});
});
});
3 changes: 2 additions & 1 deletion packages/rocketchat-ui/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ this.ChatMessages = class ChatMessages {
}

isMessageTooLong(message) {
return message && message.length > this.messageMaxSize;
const adjustedMessage = RocketChat.messageProperties.messageWithoutEmojiShortnames(message);
return RocketChat.messageProperties.length(adjustedMessage) > this.messageMaxSize && message;
}

isEmpty() {
Expand Down

0 comments on commit b681db6

Please sign in to comment.