fix: Fix agent name in Twitter channel private note acting as a link (#4326)

This commit is contained in:
Sivin Varghese 2022-03-31 20:22:52 +05:30 committed by GitHub
parent eff3a50316
commit 3cd1616df6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 7 deletions

View file

@ -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() {

View file

@ -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

View file

@ -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 =

View file

@ -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) {