From 8027c69c1c781025c488ce82c2394ed2ca0bff1a Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Tue, 29 Jun 2021 14:32:57 +0530 Subject: [PATCH] chore: Change the notification text for mention notification type (#2534) --- app/helpers/message_format_helper.rb | 6 +++++ app/models/notification.rb | 3 ++- lib/regex_helper.rb | 1 + spec/helpers/message_format_helper_spec.rb | 11 +++++++++ spec/models/notification_spec.rb | 27 +++++++++++++++++++--- 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 app/helpers/message_format_helper.rb create mode 100644 spec/helpers/message_format_helper_spec.rb diff --git a/app/helpers/message_format_helper.rb b/app/helpers/message_format_helper.rb new file mode 100644 index 000000000..e27e61fce --- /dev/null +++ b/app/helpers/message_format_helper.rb @@ -0,0 +1,6 @@ +module MessageFormatHelper + include RegexHelper + def transform_user_mention_content(message_content) + message_content.gsub(MENTION_REGEX, '\1') + end +end diff --git a/app/models/notification.rb b/app/models/notification.rb index a92faf67e..752d03100 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -23,6 +23,7 @@ # class Notification < ApplicationRecord + include MessageFormatHelper belongs_to :account belongs_to :user @@ -73,7 +74,7 @@ class Notification < ApplicationRecord content: primary_actor.content&.truncate_words(10) ) when 'conversation_mention' - I18n.t('notifications.notification_title.conversation_mention', display_id: conversation.display_id, name: secondary_actor.name) + "[##{conversation.display_id}] #{transform_user_mention_content primary_actor.content}" else '' end diff --git a/lib/regex_helper.rb b/lib/regex_helper.rb index 88f2b2835..2bbd51809 100644 --- a/lib/regex_helper.rb +++ b/lib/regex_helper.rb @@ -5,4 +5,5 @@ module RegexHelper # valid unicode letter, unicode number, underscore, hyphen # shouldn't start with a underscore or hyphen UNICODE_CHARACTER_NUMBER_HYPHEN_UNDERSCORE = Regexp.new('\A[\p{L}\p{N}]+[\p{L}\p{N}_-]+\Z') + MENTION_REGEX = Regexp.new('\[(@[\w_. ]+)\]\(mention://(?:user|team)/\d+/(.*?)+\)') end diff --git a/spec/helpers/message_format_helper_spec.rb b/spec/helpers/message_format_helper_spec.rb new file mode 100644 index 000000000..a2edc38c0 --- /dev/null +++ b/spec/helpers/message_format_helper_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +describe MessageFormatHelper, type: :helper do + describe '#transform_user_mention_content' do + context 'when transform_user_mention_content called' do + it 'return transormed text correctly' do + expect(helper.transform_user_mention_content('[@john](mention://user/1/John%20K), check this ticket')).to eq '@john, check this ticket' + end + end + end +end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index be10fc411..8b37b7c75 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -50,11 +50,32 @@ RSpec.describe Notification do end it 'returns appropriate title suited for the notification type conversation_mention' do - message = create(:message, sender: create(:user)) + message = create(:message, sender: create(:user), content: 'Hey [@John](mention://user/1/john), can you check this ticket?') notification = create(:notification, notification_type: 'conversation_mention', primary_actor: message, secondary_actor: message.sender) - expect(notification.push_message_title).to eq "You have been mentioned in conversation [ID - #{message.conversation.display_id}] \ -by #{message.sender.name}" + expect(notification.push_message_title).to eq "[##{message.conversation.display_id}] Hey @John, can you check this ticket?" + end + + it 'returns appropriate title suited for the notification type conversation_mention having multiple mentions' do + message = create( + :message, sender: + create(:user), + content: 'Hey [@John](mention://user/1/john), [@Alisha Peter](mention://user/2/alisha) can you check this ticket?' + ) + notification = create(:notification, notification_type: 'conversation_mention', primary_actor: message, secondary_actor: message.sender) + + expect(notification.push_message_title).to eq "[##{message.conversation.display_id}] Hey @John, @Alisha Peter can you check this ticket?" + end + + it 'returns appropriate title suited for the notification type conversation_mention if username contains white space' do + message = create( + :message, sender: + create(:user), + content: 'Hey [@John Peter](mention://user/1/john%20K) please check this?' + ) + notification = create(:notification, notification_type: 'conversation_mention', primary_actor: message, secondary_actor: message.sender) + + expect(notification.push_message_title).to eq "[##{message.conversation.display_id}] Hey @John Peter please check this?" end end end