From 3cd1616df624f25205c338398466e978fcee0db9 Mon Sep 17 00:00:00 2001 From: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Date: Thu, 31 Mar 2022 20:22:52 +0530 Subject: [PATCH] fix: Fix agent name in Twitter channel private note acting as a link (#4326) --- .../widgets/conversation/Message.vue | 6 +++- .../shared/helpers/MessageFormatter.js | 5 ++-- .../helpers/specs/MessageFormatter.spec.js | 30 +++++++++++++++++-- .../shared/mixins/messageFormatterMixin.js | 8 +++-- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/app/javascript/dashboard/components/widgets/conversation/Message.vue b/app/javascript/dashboard/components/widgets/conversation/Message.vue index 51d82ff8c..4035a62d0 100644 --- a/app/javascript/dashboard/components/widgets/conversation/Message.vue +++ b/app/javascript/dashboard/components/widgets/conversation/Message.vue @@ -207,7 +207,11 @@ export default { } } return ( - this.formatMessage(this.data.content, this.isATweet) + botMessageContent + this.formatMessage( + this.data.content, + this.isATweet, + this.data.private + ) + botMessageContent ); }, contentAttributes() { diff --git a/app/javascript/shared/helpers/MessageFormatter.js b/app/javascript/shared/helpers/MessageFormatter.js index 3bc89ec2f..260436da4 100644 --- a/app/javascript/shared/helpers/MessageFormatter.js +++ b/app/javascript/shared/helpers/MessageFormatter.js @@ -13,8 +13,9 @@ const TWITTER_HASH_REPLACEMENT = const USER_MENTIONS_REGEX = /mention:\/\/(user|team)\/(\d+)\/(.+)/gm; class MessageFormatter { - constructor(message, isATweet = false) { + constructor(message, isATweet = false, isAPrivateNote = false) { this.message = DOMPurify.sanitize(escapeHtml(message || '')); + this.isAPrivateNote = isAPrivateNote; this.isATweet = isATweet; this.marked = marked; @@ -35,7 +36,7 @@ class MessageFormatter { } formatMessage() { - if (this.isATweet) { + if (this.isATweet && !this.isAPrivateNote) { const withUserName = this.message.replace( TWITTER_USERNAME_REGEX, TWITTER_USERNAME_REPLACEMENT diff --git a/app/javascript/shared/helpers/specs/MessageFormatter.spec.js b/app/javascript/shared/helpers/specs/MessageFormatter.spec.js index 4e13fe832..1f2209b71 100644 --- a/app/javascript/shared/helpers/specs/MessageFormatter.spec.js +++ b/app/javascript/shared/helpers/specs/MessageFormatter.spec.js @@ -36,19 +36,45 @@ describe('#MessageFormatter', () => { it('should add links to @mentions', () => { const message = '@chatwootapp is an opensource tool thanks @longnonexistenttwitterusername'; - expect(new MessageFormatter(message, true).formattedMessage).toMatch( + expect( + new MessageFormatter(message, true, false).formattedMessage + ).toMatch( '
@chatwootapp is an opensource tool thanks @longnonexistenttwitterusername
' ); }); it('should add links to #tags', () => { const message = '#chatwootapp is an opensource tool'; - expect(new MessageFormatter(message, true).formattedMessage).toMatch( + expect( + new MessageFormatter(message, true, false).formattedMessage + ).toMatch( '#chatwootapp is an opensource tool
' ); }); }); + describe('private notes', () => { + it('should return the same string if not tags or @mentions', () => { + const message = 'Chatwoot is an opensource tool'; + expect(new MessageFormatter(message).formattedMessage).toMatch(message); + }); + + it('should add links to @mentions', () => { + const message = + '@chatwootapp is an opensource tool thanks @longnonexistenttwitterusername'; + expect( + new MessageFormatter(message, false, true).formattedMessage + ).toMatch(message); + }); + + it('should add links to #tags', () => { + const message = '#chatwootapp is an opensource tool'; + expect( + new MessageFormatter(message, false, true).formattedMessage + ).toMatch(message); + }); + }); + describe('plain text content', () => { it('returns the plain text without HTML', () => { const message = diff --git a/app/javascript/shared/mixins/messageFormatterMixin.js b/app/javascript/shared/mixins/messageFormatterMixin.js index 21096409d..7767e7c76 100644 --- a/app/javascript/shared/mixins/messageFormatterMixin.js +++ b/app/javascript/shared/mixins/messageFormatterMixin.js @@ -3,8 +3,12 @@ import DOMPurify from 'dompurify'; export default { methods: { - formatMessage(message, isATweet) { - const messageFormatter = new MessageFormatter(message, isATweet); + formatMessage(message, isATweet, isAPrivateNote) { + const messageFormatter = new MessageFormatter( + message, + isATweet, + isAPrivateNote + ); return messageFormatter.formattedMessage; }, getPlainText(message, isATweet) {