From e55fb4370365c036adf69b377bfb45bb8be7f8f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 8 Dec 2018 19:36:38 -0500 Subject: [PATCH] fix: #7086 --- public/language/en-GB/error.json | 2 +- src/messaging/create.js | 5 ++++- src/messaging/edit.js | 4 +++- test/messaging.js | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/public/language/en-GB/error.json b/public/language/en-GB/error.json index 6308bb6183..f9ec3635b1 100644 --- a/public/language/en-GB/error.json +++ b/public/language/en-GB/error.json @@ -144,7 +144,7 @@ "chat-edit-duration-expired": "You are only allowed to edit chat messages for %1 second(s) after posting", "chat-delete-duration-expired": "You are only allowed to delete chat messages for %1 second(s) after posting", "chat-deleted-already": "This chat message has already been deleted.", - "chat-restored'already": "This chat message has already been restored.", + "chat-restored-already": "This chat message has already been restored.", "already-voting-for-this-post": "You have already voted for this post.", "reputation-system-disabled": "Reputation system is disabled.", diff --git a/src/messaging/create.js b/src/messaging/create.js index 19f2e69128..b7be32164d 100644 --- a/src/messaging/create.js +++ b/src/messaging/create.js @@ -36,7 +36,10 @@ module.exports = function (Messaging) { return callback(err); } - content = String(data.content); + content = String(data.content).trim(); + if (!content) { + return callback(new Error('[[error:invalid-chat-message]]')); + } var maximumChatMessageLength = (meta.config.maximumChatMessageLength || 1000); if (content.length > maximumChatMessageLength) { diff --git a/src/messaging/edit.js b/src/messaging/edit.js index d91a4a4f1d..5189a29f5d 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -19,7 +19,9 @@ module.exports = function (Messaging) { if (raw === content) { return callback(); } - + if (!String(content).trim()) { + return callback(new Error('[[error:invalid-chat-message]]')); + } Messaging.setMessageFields(mid, { content: content, edited: Date.now(), diff --git a/test/messaging.js b/test/messaging.js index b80a3c5fe7..fa6bc96cf7 100644 --- a/test/messaging.js +++ b/test/messaging.js @@ -276,6 +276,13 @@ describe('Messaging Library', function () { }); }); + it('should fail to send chat if content is empty', function (done) { + socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: ' ' }, function (err) { + assert.equal(err.message, '[[error:invalid-chat-message]]'); + done(); + }); + }); + it('should send a message to a room', function (done) { socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: 'first chat message' }, function (err, messageData) { assert.ifError(err); @@ -554,6 +561,13 @@ describe('Messaging Library', function () { }); }); + it('should fail to edit message if new content is empty string', function (done) { + socketModules.chats.edit({ uid: fooUid }, { mid: 5, roomId: roomId, message: ' ' }, function (err) { + assert.equal(err.message, '[[error:invalid-chat-message]]'); + done(); + }); + }); + it('should fail to edit message if not own message', function (done) { socketModules.chats.edit({ uid: herpUid }, { mid: 5, roomId: roomId, message: 'message edited' }, function (err) { assert.equal(err.message, '[[error:cant-edit-chat-message]]');