From bfcde9b02257f1ba4a355ebf152204446678a53d Mon Sep 17 00:00:00 2001 From: Shivam Chahar Date: Sat, 27 Nov 2021 15:38:27 +0530 Subject: [PATCH] chore: Conversation history in email notifications (#3414) Display recent messages in the notification email when a new conversation is created. Fixes: #2041 --- app/drops/conversation_drop.rb | 15 +++++++++- app/drops/inbox_drop.rb | 3 ++ .../conversation_creation.liquid | 30 ++++++++++++++++--- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/app/drops/conversation_drop.rb b/app/drops/conversation_drop.rb index 1758c29c5..b24586c30 100644 --- a/app/drops/conversation_drop.rb +++ b/app/drops/conversation_drop.rb @@ -5,13 +5,26 @@ class ConversationDrop < BaseDrop @obj.try(:display_id) end + def contact_name + @obj.try(:contact).name.capitalize || 'Customer' + end + def recent_messages @obj.try(:recent_messages).map do |message| { - 'sender' => message.sender&.available_name || message.sender&.name, + 'sender' => message_sender_name(message.sender), 'content' => transform_user_mention_content(message.content), 'attachments' => message.attachments.map(&:file_url) } end end + + private + + def message_sender_name(sender) + return 'Bot' if sender.blank? + return contact_name if sender.instance_of?(Contact) + + sender&.available_name || sender&.name + end end diff --git a/app/drops/inbox_drop.rb b/app/drops/inbox_drop.rb index 7972e7325..563916d5f 100644 --- a/app/drops/inbox_drop.rb +++ b/app/drops/inbox_drop.rb @@ -1,2 +1,5 @@ class InboxDrop < BaseDrop + def name + @obj.try(:name) + end end diff --git a/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_creation.liquid b/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_creation.liquid index e324dd9e6..095e1bd7b 100644 --- a/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_creation.liquid +++ b/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_creation.liquid @@ -1,8 +1,30 @@

Hi {{user.available_name}}

- -

Time to save the world. A new conversation has been created in {{ inbox.name }}

-

-Click here to get cracking. + A new conversation (#{{conversation.display_id}}) has been created in {{ inbox.name }}. + {{ conversation.contact_name }} wrote:

+ +{% for chat_message in conversation.recent_messages %} +
+ {% if chat_message.sender == user.available_name %} +

You

+ {% else %} +

{{chat_message.sender}}

+ {% endif %} +
+ +
+

+ {% if chat_message.content %} + {{chat_message.content}} + {% endif %} + + {% if chat_message.attachments %} + {% for attachment in chat_message.attachments %} + Attachment [Click here to view] + {% endfor %} + {% endif %} +

+
+{% endfor %}