mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-21 21:44:52 +00:00
feat: Revamp the notification title and content (#8988)
This commit is contained in:
@@ -70,7 +70,8 @@ class Notification < ApplicationRecord
|
|||||||
}
|
}
|
||||||
if primary_actor.present?
|
if primary_actor.present?
|
||||||
payload[:primary_actor] = primary_actor&.push_event_data
|
payload[:primary_actor] = primary_actor&.push_event_data
|
||||||
payload[:push_message_title] = push_message_title
|
# TODO: Rename push_message_title to push_message_body
|
||||||
|
payload[:push_message_title] = push_message_body
|
||||||
end
|
end
|
||||||
payload
|
payload
|
||||||
end
|
end
|
||||||
@@ -93,13 +94,20 @@ class Notification < ApplicationRecord
|
|||||||
when 'conversation_assignment'
|
when 'conversation_assignment'
|
||||||
I18n.t('notifications.notification_title.conversation_assignment', display_id: conversation.display_id)
|
I18n.t('notifications.notification_title.conversation_assignment', display_id: conversation.display_id)
|
||||||
when 'assigned_conversation_new_message', 'participating_conversation_new_message'
|
when 'assigned_conversation_new_message', 'participating_conversation_new_message'
|
||||||
I18n.t(
|
I18n.t('notifications.notification_title.assigned_conversation_new_message', display_id: conversation.display_id)
|
||||||
'notifications.notification_title.assigned_conversation_new_message',
|
|
||||||
display_id: conversation.display_id,
|
|
||||||
content: content
|
|
||||||
)
|
|
||||||
when 'conversation_mention'
|
when 'conversation_mention'
|
||||||
"[##{conversation&.display_id}] #{transform_user_mention_content content}"
|
I18n.t('notifications.notification_title.conversation_mention', display_id: conversation.display_id)
|
||||||
|
else
|
||||||
|
''
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def push_message_body
|
||||||
|
case notification_type
|
||||||
|
when 'conversation_creation'
|
||||||
|
message_body(conversation.messages.first)
|
||||||
|
when 'assigned_conversation_new_message', 'participating_conversation_new_message', 'conversation_assignment', 'conversation_mention'
|
||||||
|
message_body(secondary_actor)
|
||||||
else
|
else
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
@@ -109,11 +117,28 @@ class Notification < ApplicationRecord
|
|||||||
primary_actor
|
primary_actor
|
||||||
end
|
end
|
||||||
|
|
||||||
def content
|
private
|
||||||
transform_user_mention_content(secondary_actor&.content&.truncate_words(10) || '')
|
|
||||||
|
def message_body(actor)
|
||||||
|
sender_name = sender_name(actor)
|
||||||
|
content = message_content(actor)
|
||||||
|
"#{sender_name}: #{content}"
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def sender_name(actor)
|
||||||
|
actor.try(:sender)&.name || ''
|
||||||
|
end
|
||||||
|
|
||||||
|
def message_content(actor)
|
||||||
|
content = actor.try(:content)
|
||||||
|
attachments = actor.try(:attachments)
|
||||||
|
|
||||||
|
if content.present?
|
||||||
|
transform_user_mention_content(content.truncate_words(10))
|
||||||
|
else
|
||||||
|
attachments.present? ? I18n.t('notifications.attachment') : I18n.t('notifications.no_content')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def process_notification_delivery
|
def process_notification_delivery
|
||||||
Notification::PushNotificationJob.perform_later(self) if user_subscribed_to_notification?('push')
|
Notification::PushNotificationJob.perform_later(self) if user_subscribed_to_notification?('push')
|
||||||
|
|||||||
@@ -93,8 +93,8 @@ class Notification::PushNotificationService
|
|||||||
def fcm_options
|
def fcm_options
|
||||||
{
|
{
|
||||||
notification: {
|
notification: {
|
||||||
title: notification.notification_type.titleize,
|
title: notification.push_message_title,
|
||||||
body: notification.push_message_title,
|
body: notification.push_message_body,
|
||||||
sound: 'default'
|
sound: 'default'
|
||||||
},
|
},
|
||||||
android: { priority: 'high' },
|
android: { priority: 'high' },
|
||||||
|
|||||||
@@ -118,10 +118,12 @@ en:
|
|||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
notification_title:
|
notification_title:
|
||||||
conversation_creation: "[New conversation] - #%{display_id} has been created in %{inbox_name}"
|
conversation_creation: "A conversation (#%{display_id}) has been created in %{inbox_name}"
|
||||||
conversation_assignment: "[Assigned to you] - #%{display_id} has been assigned to you"
|
conversation_assignment: "A conversation (#%{display_id}) has been assigned to you"
|
||||||
assigned_conversation_new_message: "[New message] - #%{display_id} %{content}"
|
assigned_conversation_new_message: "A new message is created in conversation (#%{display_id})"
|
||||||
conversation_mention: "You have been mentioned in conversation [ID - %{display_id}] by %{name}"
|
conversation_mention: "You have been mentioned in conversation (#%{display_id})"
|
||||||
|
attachment: "Attachment"
|
||||||
|
no_content: "No content"
|
||||||
conversations:
|
conversations:
|
||||||
messages:
|
messages:
|
||||||
instagram_story_content: "%{story_sender} mentioned you in the story: "
|
instagram_story_content: "%{story_sender} mentioned you in the story: "
|
||||||
|
|||||||
@@ -24,14 +24,14 @@ RSpec.describe Notification do
|
|||||||
context 'when push_title is called' do
|
context 'when push_title is called' do
|
||||||
it 'returns appropriate title suited for the notification type conversation_creation' do
|
it 'returns appropriate title suited for the notification type conversation_creation' do
|
||||||
notification = create(:notification, notification_type: 'conversation_creation')
|
notification = create(:notification, notification_type: 'conversation_creation')
|
||||||
expect(notification.push_message_title).to eq "[New conversation] - ##{notification.primary_actor.display_id} has\
|
expect(notification.push_message_title).to eq "A conversation (##{notification.primary_actor.display_id}) \
|
||||||
been created in #{notification.primary_actor.inbox.name}"
|
has been created in #{notification.primary_actor.inbox.name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type conversation_assignment' do
|
it 'returns appropriate title suited for the notification type conversation_assignment' do
|
||||||
notification = create(:notification, notification_type: 'conversation_assignment')
|
notification = create(:notification, notification_type: 'conversation_assignment')
|
||||||
|
expect(notification.push_message_title).to eq "A conversation (##{notification.primary_actor.display_id}) \
|
||||||
expect(notification.push_message_title).to eq "[Assigned to you] - ##{notification.primary_actor.display_id} has been assigned to you"
|
has been assigned to you"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type assigned_conversation_new_message' do
|
it 'returns appropriate title suited for the notification type assigned_conversation_new_message' do
|
||||||
@@ -39,8 +39,7 @@ RSpec.describe Notification do
|
|||||||
notification = create(:notification, notification_type: 'assigned_conversation_new_message', primary_actor: message.conversation,
|
notification = create(:notification, notification_type: 'assigned_conversation_new_message', primary_actor: message.conversation,
|
||||||
secondary_actor: message)
|
secondary_actor: message)
|
||||||
|
|
||||||
expect(notification.push_message_title).to eq "[New message] - ##{notification.conversation.display_id} \
|
expect(notification.push_message_title).to eq "A new message is created in conversation (##{notification.primary_actor.display_id})"
|
||||||
#{message.content.truncate_words(10)}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type assigned_conversation_new_message when attachment message' do
|
it 'returns appropriate title suited for the notification type assigned_conversation_new_message when attachment message' do
|
||||||
@@ -49,7 +48,7 @@ RSpec.describe Notification do
|
|||||||
notification = create(:notification, notification_type: 'assigned_conversation_new_message', primary_actor: message.conversation,
|
notification = create(:notification, notification_type: 'assigned_conversation_new_message', primary_actor: message.conversation,
|
||||||
secondary_actor: message)
|
secondary_actor: message)
|
||||||
|
|
||||||
expect(notification.push_message_title).to eq "[New message] - ##{notification.conversation.display_id} "
|
expect(notification.push_message_title).to eq "A new message is created in conversation (##{notification.primary_actor.display_id})"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type participating_conversation_new_message' do
|
it 'returns appropriate title suited for the notification type participating_conversation_new_message' do
|
||||||
@@ -57,61 +56,61 @@ RSpec.describe Notification do
|
|||||||
notification = create(:notification, notification_type: 'participating_conversation_new_message', primary_actor: message.conversation,
|
notification = create(:notification, notification_type: 'participating_conversation_new_message', primary_actor: message.conversation,
|
||||||
secondary_actor: message)
|
secondary_actor: message)
|
||||||
|
|
||||||
expect(notification.push_message_title).to eq "[New message] - ##{notification.conversation.display_id} \
|
expect(notification.push_message_title).to eq "A new message is created in conversation (##{notification.primary_actor.display_id})"
|
||||||
#{message.content.truncate_words(10)}"
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type participating_conversation_new_message having mention' do
|
|
||||||
message = create(:message, sender: create(:user), content: 'Hey [@John](mention://user/1/john), can you check this ticket?')
|
|
||||||
notification = create(:notification, notification_type: 'participating_conversation_new_message', primary_actor: message.conversation,
|
|
||||||
secondary_actor: message)
|
|
||||||
expect(notification.push_message_title).to eq "[New message] - ##{notification.conversation.display_id} Hey @John, can you check this ticket?"
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type participating_conversation_new_message having multple mention' 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: 'participating_conversation_new_message', primary_actor: message.conversation,
|
|
||||||
secondary_actor: message)
|
|
||||||
|
|
||||||
expect(notification.push_message_title).to eq "[New message] - ##{notification.conversation.display_id} \
|
|
||||||
Hey @John, @Alisha Peter can you check this ticket?"
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type participating_conversation_new_message 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: 'participating_conversation_new_message', primary_actor: message.conversation,
|
|
||||||
secondary_actor: message)
|
|
||||||
|
|
||||||
expect(notification.push_message_title).to eq "[New message] - ##{notification.conversation.display_id} Hey @John Peter please check this?"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type conversation_mention' do
|
it 'returns appropriate title suited for the notification type conversation_mention' do
|
||||||
message = create(:message, sender: create(:user), content: 'Hey [@John](mention://user/1/john), can you check this ticket?')
|
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.conversation, secondary_actor: message)
|
notification = create(:notification, notification_type: 'conversation_mention', primary_actor: message.conversation,
|
||||||
|
secondary_actor: message)
|
||||||
|
|
||||||
expect(notification.push_message_title).to eq "[##{message.conversation.display_id}] Hey @John, can you check this ticket?"
|
expect(notification.push_message_title).to eq "You have been mentioned in conversation (##{notification.primary_actor.display_id})"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type conversation_mention having multiple mentions' do
|
context 'when push_message_body is called' do
|
||||||
message = create(
|
it 'returns appropriate body suited for the notification type conversation_creation' do
|
||||||
:message, sender:
|
conversation = create(:conversation)
|
||||||
create(:user),
|
message = create(:message, sender: create(:user), content: Faker::Lorem.paragraphs(number: 2), conversation: conversation)
|
||||||
content: 'Hey [@John](mention://user/1/john), [@Alisha Peter](mention://user/2/alisha) can you check this ticket?'
|
notification = create(:notification, notification_type: 'conversation_creation', primary_actor: conversation, secondary_actor: message)
|
||||||
)
|
expect(notification.push_message_body).to eq "#{message.sender.name}: #{message.content.truncate_words(10)}"
|
||||||
notification = create(:notification, notification_type: 'conversation_mention', primary_actor: message.conversation, secondary_actor: message)
|
|
||||||
|
|
||||||
expect(notification.push_message_title).to eq "[##{message.conversation.display_id}] Hey @John, @Alisha Peter can you check this ticket?"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns appropriate title suited for the notification type conversation_mention if username contains white space' do
|
it 'returns appropriate body suited for the notification type assigned_conversation_new_message' do
|
||||||
message = create(
|
conversation = create(:conversation)
|
||||||
:message, sender:
|
message = create(:message, sender: create(:user), content: Faker::Lorem.paragraphs(number: 2), conversation: conversation)
|
||||||
create(:user),
|
notification = create(:notification, notification_type: 'assigned_conversation_new_message', primary_actor: conversation,
|
||||||
content: 'Hey [@John Peter](mention://user/1/john%20K) please check this?'
|
secondary_actor: message)
|
||||||
)
|
expect(notification.push_message_body).to eq "#{message.sender.name}: #{message.content.truncate_words(10)}"
|
||||||
notification = create(:notification, notification_type: 'conversation_mention', primary_actor: message.conversation, secondary_actor: message)
|
end
|
||||||
expect(notification.push_message_title).to eq "[##{message.conversation.display_id}] Hey @John Peter please check this?"
|
|
||||||
|
it 'returns appropriate body suited for the notification type assigned_conversation_new_message when attachment message' do
|
||||||
|
conversation = create(:conversation)
|
||||||
|
message = create(:message, sender: create(:user), content: nil, conversation: conversation)
|
||||||
|
attachment = message.attachments.new(file_type: :image, account_id: message.account_id)
|
||||||
|
attachment.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
|
||||||
|
message.save!
|
||||||
|
notification = create(:notification, notification_type: 'assigned_conversation_new_message', primary_actor: conversation,
|
||||||
|
secondary_actor: message)
|
||||||
|
expect(notification.push_message_body).to eq "#{message.sender.name}: Attachment"
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns appropriate body suited for the notification type participating_conversation_new_message having multple mention' do
|
||||||
|
conversation = create(:conversation)
|
||||||
|
message = create(:message, sender: create(:user),
|
||||||
|
content: 'Hey [@John](mention://user/1/john), [@Alisha Peter](mention://user/2/alisha) can you check this ticket?',
|
||||||
|
conversation: conversation)
|
||||||
|
notification = create(:notification, notification_type: 'participating_conversation_new_message', primary_actor: conversation,
|
||||||
|
secondary_actor: message)
|
||||||
|
expect(notification.push_message_body).to eq "#{message.sender.name}: Hey @John, @Alisha Peter can you check this ticket?"
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns appropriate body suited for the notification type conversation_mention if username contains white space' do
|
||||||
|
conversation = create(:conversation)
|
||||||
|
message = create(:message, sender: create(:user), content: 'Hey [@John Peter](mention://user/1/john%20K) please check this?',
|
||||||
|
conversation: conversation)
|
||||||
|
notification = create(:notification, notification_type: 'conversation_mention', primary_actor: conversation, secondary_actor: message)
|
||||||
|
expect(notification.push_message_body).to eq "#{message.sender.name}: Hey @John Peter please check this?"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'calls remove duplicate notification job' do
|
it 'calls remove duplicate notification job' do
|
||||||
|
|||||||
Reference in New Issue
Block a user