From 866ee4d565b3a6f88b2db0e0e699132b37375990 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 25 Nov 2022 08:36:29 +0100 Subject: [PATCH 1/7] fix(rocketchat): Replace invalid link characters --- back/models/chat.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/back/models/chat.js b/back/models/chat.js index 95a1e2c29..19f73ca4a 100644 --- a/back/models/chat.js +++ b/back/models/chat.js @@ -4,4 +4,21 @@ module.exports = Self => { require('../methods/chat/sendCheckingPresence')(Self); require('../methods/chat/notifyIssues')(Self); require('../methods/chat/sendQueued')(Self); + + Self.observe('before save', async function(ctx) { + if (!ctx.isNewInstance) return; + + let {message} = ctx.instance; + if (!message) return; + + const parts = message.match(/(?<=\[)[A-Za-z0-9()*\s]*(?=])/g) || []; + const replacedParts = parts.map(part => { + return part.replace(/[*()]/g, ''); + }); + + for (const [index, part] of parts.entries()) + message = message.replace(part, replacedParts[index]); + + ctx.instance.message = message; + }); }; From db64ba48fc21ff4939b6f895dd67a56c70949ab2 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 25 Nov 2022 08:45:26 +0100 Subject: [PATCH 2/7] fix(rocketchat): Removed parenthesis --- back/methods/chat/notifyIssues.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/back/methods/chat/notifyIssues.js b/back/methods/chat/notifyIssues.js index 902ee59cd..3c4706d07 100644 --- a/back/methods/chat/notifyIssues.js +++ b/back/methods/chat/notifyIssues.js @@ -32,7 +32,7 @@ module.exports = Self => { let message = $t(`There's a new urgent ticket:`); const ostUri = 'https://cau.verdnatura.es/scp/tickets.php?id='; tickets.forEach(ticket => { - message += `\r\n[ID: *${ticket.number}* - ${ticket.subject} (@${ticket.username})](${ostUri + ticket.id})`; + message += `\r\n[ID: *${ticket.number}* - ${ticket.subject} @${ticket.username}](${ostUri + ticket.id})`; }); const department = await models.Department.findOne({ @@ -42,7 +42,5 @@ module.exports = Self => { if (channelName) return Self.send(ctx, `#${channelName}`, `@all ➔ ${message}`); - - return; }; }; From 7ea4d9c2e18df2b64326d138837a33f7a9543b37 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 25 Nov 2022 09:18:21 +0100 Subject: [PATCH 3/7] fix(rocketchat): Regex fix --- back/models/chat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/models/chat.js b/back/models/chat.js index 19f73ca4a..f45d15180 100644 --- a/back/models/chat.js +++ b/back/models/chat.js @@ -11,7 +11,7 @@ module.exports = Self => { let {message} = ctx.instance; if (!message) return; - const parts = message.match(/(?<=\[)[A-Za-z0-9()*\s]*(?=])/g) || []; + const parts = message.match(/(?<=\[).*(?=])/g); const replacedParts = parts.map(part => { return part.replace(/[*()]/g, ''); }); From 6af7da4793aded6719f073ba16779ff1349d5e24 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 25 Nov 2022 11:13:26 +0100 Subject: [PATCH 4/7] Updated regex --- back/models/chat.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/back/models/chat.js b/back/models/chat.js index f45d15180..5eb986a2c 100644 --- a/back/models/chat.js +++ b/back/models/chat.js @@ -11,9 +11,9 @@ module.exports = Self => { let {message} = ctx.instance; if (!message) return; - const parts = message.match(/(?<=\[).*(?=])/g); + const parts = message.match(/(?<=\[)[a-zA-Z0-9!@#$%^&*()={};':"\\|,.<>/?\s]*(?=])/g); const replacedParts = parts.map(part => { - return part.replace(/[*()]/g, ''); + return part.replace(/[!@#$%^&*()={};':"\\|,.<>/?]/g, ''); }); for (const [index, part] of parts.entries()) From 6b949223357fa43d0b77cbb4e5f90e3be5fc9f13 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 25 Nov 2022 12:12:48 +0100 Subject: [PATCH 5/7] Fixes --- back/models/chat.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/back/models/chat.js b/back/models/chat.js index 5eb986a2c..b75900ebd 100644 --- a/back/models/chat.js +++ b/back/models/chat.js @@ -12,6 +12,8 @@ module.exports = Self => { if (!message) return; const parts = message.match(/(?<=\[)[a-zA-Z0-9!@#$%^&*()={};':"\\|,.<>/?\s]*(?=])/g); + if (!parts) return; + const replacedParts = parts.map(part => { return part.replace(/[!@#$%^&*()={};':"\\|,.<>/?]/g, ''); }); From 2210f8970c25913d4c658d57df812b896267a369 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 25 Nov 2022 12:26:58 +0100 Subject: [PATCH 6/7] Fixes --- back/methods/chat/notifyIssues.js | 2 +- back/models/chat.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/back/methods/chat/notifyIssues.js b/back/methods/chat/notifyIssues.js index 3c4706d07..f618a6fb1 100644 --- a/back/methods/chat/notifyIssues.js +++ b/back/methods/chat/notifyIssues.js @@ -32,7 +32,7 @@ module.exports = Self => { let message = $t(`There's a new urgent ticket:`); const ostUri = 'https://cau.verdnatura.es/scp/tickets.php?id='; tickets.forEach(ticket => { - message += `\r\n[ID: *${ticket.number}* - ${ticket.subject} @${ticket.username}](${ostUri + ticket.id})`; + message += `\r\n[ID: ${ticket.number} - ${ticket.subject} @${ticket.username}](${ostUri + ticket.id})`; }); const department = await models.Department.findOne({ diff --git a/back/models/chat.js b/back/models/chat.js index b75900ebd..a18edbd3f 100644 --- a/back/models/chat.js +++ b/back/models/chat.js @@ -11,11 +11,11 @@ module.exports = Self => { let {message} = ctx.instance; if (!message) return; - const parts = message.match(/(?<=\[)[a-zA-Z0-9!@#$%^&*()={};':"\\|,.<>/?\s]*(?=])/g); + const parts = message.match(/(?<=\[)[a-zA-Z0-9_\-+!@#$%^&*()={};':"\\|,.<>/?\s]*(?=])/g); if (!parts) return; const replacedParts = parts.map(part => { - return part.replace(/[!@#$%^&*()={};':"\\|,.<>/?]/g, ''); + return part.replace(/[!$%^&*()={};':"\\,.<>/?]/g, ''); }); for (const [index, part] of parts.entries()) From ca0164ff18b13b659482b1ec7ab9df83b1fcfdb5 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 25 Nov 2022 12:56:25 +0100 Subject: [PATCH 7/7] unit test updated --- back/methods/chat/spec/notifyIssue.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/methods/chat/spec/notifyIssue.spec.js b/back/methods/chat/spec/notifyIssue.spec.js index e20d43142..1aab51793 100644 --- a/back/methods/chat/spec/notifyIssue.spec.js +++ b/back/methods/chat/spec/notifyIssue.spec.js @@ -27,7 +27,7 @@ describe('Chat notifyIssue()', () => { subject: 'Issue title'} ]); // eslint-disable-next-line max-len - const expectedMessage = `@all ➔ There's a new urgent ticket:\r\n[ID: *00001* - Issue title (@batman)](https://cau.verdnatura.es/scp/tickets.php?id=1)`; + const expectedMessage = `@all ➔ There's a new urgent ticket:\r\n[ID: 00001 - Issue title @batman](https://cau.verdnatura.es/scp/tickets.php?id=1)`; const department = await app.models.Department.findById(departmentId); let orgChatName = department.chatName;