From f6b34e85df3189db822b78e99346d79c75cad23f Mon Sep 17 00:00:00 2001 From: negue Date: Thu, 12 Jul 2018 22:40:04 +0200 Subject: [PATCH] Max 3000 Character Limit on Chat-Messages (#10494) * limit chat length to 3000 * add test --- test/api/v3/integration/chat/POST-chat.test.js | 17 +++++++++++++++++ website/client/components/groups/chat.vue | 9 ++++++++- website/server/models/group.js | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/test/api/v3/integration/chat/POST-chat.test.js b/test/api/v3/integration/chat/POST-chat.test.js index 6a675aab97..3f52127d01 100644 --- a/test/api/v3/integration/chat/POST-chat.test.js +++ b/test/api/v3/integration/chat/POST-chat.test.js @@ -388,6 +388,23 @@ describe('POST /chat', () => { expect(groupMessages[0].id).to.exist; }); + it('creates a chat with a max length of 3000 chars', async () => { + const veryLongMessage = ` + 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789. + THIS PART WON'T BE IN THE MESSAGE (over 3000) + `; + + const newMessage = await user.post(`/groups/${groupWithChat._id}/chat`, { message: veryLongMessage}); + const groupMessages = await user.get(`/groups/${groupWithChat._id}/chat`); + + expect(newMessage.message.id).to.exist; + expect(groupMessages[0].id).to.exist; + + expect(newMessage.message.text.length).to.eql(3000); + expect(newMessage.message.text).to.not.contain('MESSAGE'); + expect(groupMessages[0].text.length).to.eql(3000); + }); + it('creates a chat with user styles', async () => { const mount = 'test-mount'; const pet = 'test-pet'; diff --git a/website/client/components/groups/chat.vue b/website/client/components/groups/chat.vue index 2ba54afb4e..58e16cac0e 100644 --- a/website/client/components/groups/chat.vue +++ b/website/client/components/groups/chat.vue @@ -9,8 +9,10 @@ :class='{"user-entry": newMessage}', @keydown='updateCarretPosition', @keyup.ctrl.enter='sendMessageShortcut()', - @paste='disableMessageSendShortcut()' + @paste='disableMessageSendShortcut()', + maxlength='3000' ) + span {{ currentLength }} / 3000 autocomplete( :text='newMessage', v-on:select="selectedAutocomplete", @@ -62,6 +64,11 @@ }, }; }, + computed: { + currentLength () { + return this.newMessage.length; + }, + }, methods: { // https://medium.com/@_jh3y/how-to-where-s-the-caret-getting-the-xy-position-of-the-caret-a24ba372990a getCoord (e, text) { diff --git a/website/server/models/group.js b/website/server/models/group.js index 5ab0b154c9..790e06c607 100644 --- a/website/server/models/group.js +++ b/website/server/models/group.js @@ -478,7 +478,7 @@ export function chatDefaults (msg, user) { const message = { id, _id: id, - text: msg, + text: msg.substring(0, 3000), timestamp: Number(new Date()), likes: {}, flags: {},