From f07218921696e014493a4b023d63f51a38bacf6c Mon Sep 17 00:00:00 2001 From: Gabriel Delavald Date: Wed, 18 Apr 2018 16:50:03 -0300 Subject: [PATCH] [FIX] Links being embedded inside of blockquotes (#10496) * Avoid embedding links inside blockquotes and remove doubles --- .../server/functions/sendMessage.js | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/rocketchat-lib/server/functions/sendMessage.js b/packages/rocketchat-lib/server/functions/sendMessage.js index baf979687e52..63992c29cc14 100644 --- a/packages/rocketchat-lib/server/functions/sendMessage.js +++ b/packages/rocketchat-lib/server/functions/sendMessage.js @@ -26,14 +26,30 @@ RocketChat.sendMessage = function(user, message, room, upsert = false) { } if (message.parseUrls !== false) { - const urls = message.msg.match(/([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g); - + const urlRegex = /([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g; + const urls = message.msg.match(urlRegex); if (urls) { - message.urls = urls.map(function(url) { - return { - url - }; - }); + // ignoredUrls contain blocks of quotes with urls inside + const ignoredUrls = message.msg.match(/(?:(?:\`{1,3})(?:[\n\r]*?.*?)*?)(([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\(\)\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?)(?:(?:[\n\r]*.*?)*?(?:\`{1,3}))/gm); + if (ignoredUrls) { + ignoredUrls.forEach((url) => { + const shouldBeIgnored = url.match(urlRegex); + if (shouldBeIgnored) { + shouldBeIgnored.forEach((match) => { + const matchIndex = urls.indexOf(match); + urls.splice(matchIndex, 1); + }); + } + }); + } + if (urls) { + // use the Set to remove duplicity, so it doesn't embed the same link twice + message.urls = [...new Set(urls)].map(function(url) { + return { + url + }; + }); + } } }