From c3a8305a400113899301c7a7d995f06a075fbc7b Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Thu, 24 Oct 2019 11:35:15 +0200 Subject: [PATCH] Mentions Fix and Test (#11475) * Fix issue with mentioned members not being loaded enough * mentions: add integration tests and fix saved message --- test/api/v3/integration/chat/POST-chat.test.js | 10 ++++++++++ .../members/POST-send_private_message.test.js | 11 +++++++++++ website/server/controllers/api-v3/chat.js | 2 +- website/server/libs/highlightMentions.js | 2 +- 4 files changed, 23 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 bf537ff449..73abe0728c 100644 --- a/test/api/v3/integration/chat/POST-chat.test.js +++ b/test/api/v3/integration/chat/POST-chat.test.js @@ -479,6 +479,16 @@ describe('POST /chat', () => { expect(groupMessages[0].id).to.exist; }); + it('creates a chat with mentions', async () => { + const messageWithMentions = `hi @${member.auth.local.username}`; + const newMessage = await user.post(`/groups/${groupWithChat._id}/chat`, { message: messageWithMentions }); + const groupMessages = await user.get(`/groups/${groupWithChat._id}/chat`); + + expect(newMessage.message.id).to.exist; + expect(newMessage.message.text).to.include(`[@${member.auth.local.username}](/profile/${member._id})`); + 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. diff --git a/test/api/v3/integration/members/POST-send_private_message.test.js b/test/api/v3/integration/members/POST-send_private_message.test.js index 78b5d6467c..47bc0ea574 100644 --- a/test/api/v3/integration/members/POST-send_private_message.test.js +++ b/test/api/v3/integration/members/POST-send_private_message.test.js @@ -135,6 +135,17 @@ describe('POST /members/send-private-message', () => { expect(sendersMessageInSendersInbox).to.exist; }); + it('sends a private message with mentions to a user', async () => { + const receiver = await generateUser(); + + const response = await userToSendMessage.post('/members/send-private-message', { + message: `hi @${receiver.auth.local.username}`, + toUserId: receiver._id, + }); + + expect(response.message.text).to.include(`[@${receiver.auth.local.username}](/profile/${receiver._id})`); + }); + // @TODO waiting for mobile support xit('creates a notification with an excerpt if the message is too long', async () => { const receiver = await generateUser(); diff --git a/website/server/controllers/api-v3/chat.js b/website/server/controllers/api-v3/chat.js index 31af733d75..1a4466e0cb 100644 --- a/website/server/controllers/api-v3/chat.js +++ b/website/server/controllers/api-v3/chat.js @@ -228,7 +228,7 @@ api.postChat = { } const newChatMessage = group.sendChat({ - message: req.body.message, + message, user, flagCount, metaData: null, diff --git a/website/server/libs/highlightMentions.js b/website/server/libs/highlightMentions.js index ef99609a57..b04777ca2d 100644 --- a/website/server/libs/highlightMentions.js +++ b/website/server/libs/highlightMentions.js @@ -10,7 +10,7 @@ export async function highlightMentions (text) { // eslint-disable-line import/p const usernames = mentions.map(mention => mention.substr(1)); members = await User .find({ 'auth.local.username': { $in: usernames }, 'flags.verifiedUsername': true }) - .select(['auth.local.username', '_id', 'preferences.pushNotifications', 'pushDevices']) + .select(['auth.local.username', '_id', 'preferences.pushNotifications', 'pushDevices', 'party', 'guilds']) .lean() .exec(); members.forEach(member => {