Skip to content

Commit

Permalink
fix: Fix agent name in Twitter channel private note acting as a link (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsivin committed Mar 31, 2022
1 parent eff3a50 commit 3cd1616
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/shared/helpers/MessageFormatter.js
Expand Up @@ -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;

Expand All @@ -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
Expand Down
30 changes: 28 additions & 2 deletions app/javascript/shared/helpers/specs/MessageFormatter.spec.js
Expand Up @@ -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(
'<p><a href="http://twitter.com/chatwootapp" target="_blank" rel="noreferrer nofollow noopener">@chatwootapp</a> is an opensource tool thanks @longnonexistenttwitterusername</p>'
);
});

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(
'<p><a href="https://twitter.com/hashtag/chatwootapp" target="_blank" rel="noreferrer nofollow noopener">#chatwootapp</a> is an opensource tool</p>'
);
});
});

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 =
Expand Down
8 changes: 6 additions & 2 deletions app/javascript/shared/mixins/messageFormatterMixin.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 3cd1616

Please sign in to comment.