diff --git a/app/drops/conversation_drop.rb b/app/drops/conversation_drop.rb index 03bab2f58..1758c29c5 100644 --- a/app/drops/conversation_drop.rb +++ b/app/drops/conversation_drop.rb @@ -1,5 +1,17 @@ class ConversationDrop < BaseDrop + include MessageFormatHelper + def display_id @obj.try(:display_id) end + + def recent_messages + @obj.try(:recent_messages).map do |message| + { + 'sender' => message.sender&.available_name || message.sender&.name, + 'content' => transform_user_mention_content(message.content), + 'attachments' => message.attachments.map(&:file_url) + } + end + end end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index fdfd5002a..a7a5b5b6d 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -147,6 +147,10 @@ class Conversation < ApplicationRecord inbox.inbox_type == 'Twitter' && additional_attributes['type'] == 'tweet' end + def recent_messages + messages.chat.last(5) + end + private def execute_after_update_commit_callbacks diff --git a/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_assignment.liquid b/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_assignment.liquid index 63e22f093..e138fd11a 100644 --- a/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_assignment.liquid +++ b/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_assignment.liquid @@ -2,6 +2,30 @@

Time to save the world. A new conversation has been assigned to you

+{% 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 %} +

Click here to get cracking.

diff --git a/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_mention.liquid b/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_mention.liquid index 4783b1dde..9a30e6e09 100644 --- a/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_mention.liquid +++ b/app/views/mailers/agent_notifications/conversation_notifications_mailer/conversation_mention.liquid @@ -5,4 +5,28 @@ {{message.text_content}} +{% 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 %} +

View Message

diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 9d7fa2c8c..71adf6970 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -340,6 +340,30 @@ RSpec.describe Conversation, type: :model do end end + describe 'recent_messages' do + subject(:recent_messages) { conversation.recent_messages } + + let(:conversation) { create(:conversation, agent_last_seen_at: 1.hour.ago) } + let(:message_params) do + { + conversation: conversation, + account: conversation.account, + inbox: conversation.inbox, + sender: conversation.assignee + } + end + let!(:messages) do + create_list(:message, 10, **message_params) do |message, i| + message.created_at = i.minute.ago + end + end + + it 'returns upto 5 recent messages' do + expect(recent_messages.length).to be < 6 + expect(recent_messages).to eq messages.last(5) + end + end + describe 'unread_incoming_messages' do subject(:unread_incoming_messages) { conversation.unread_incoming_messages }