From 0a087c95fddba70b9c071ab2f7cad280d4c298f0 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 8 Jun 2021 01:11:06 +0530 Subject: [PATCH] chore: Message API improvements (#2396) Co-authored-by: Pranav Raj S --- app/builders/messages/message_builder.rb | 14 ++++++++++++-- .../components/widgets/conversation/Message.vue | 7 +++++-- app/models/message.rb | 5 ++++- .../api/v1/models/_agent_bot.json.jbuilder | 2 +- .../conversations/messages_controller_spec.rb | 17 +++++++++++++++++ 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb index 6fc3bab5d..ba78d76d7 100644 --- a/app/builders/messages/message_builder.rb +++ b/app/builders/messages/message_builder.rb @@ -39,7 +39,17 @@ class Messages::MessageBuilder end def sender - message_type == 'outgoing' ? @user : @conversation.contact + message_type == 'outgoing' ? (message_sender || @user) : @conversation.contact + end + + def external_created_at + @params[:external_created_at].present? ? { external_created_at: @params[:external_created_at] } : {} + end + + def message_sender + return if @params[:sender_type] != 'AgentBot' + + AgentBot.where(account_id: [nil, @conversation.account.id]).find_by(id: @params[:sender_id]) end def message_params @@ -54,6 +64,6 @@ class Messages::MessageBuilder items: @items, in_reply_to: @in_reply_to, echo_id: @params[:echo_id] - } + }.merge(external_created_at) end end diff --git a/app/javascript/dashboard/components/widgets/conversation/Message.vue b/app/javascript/dashboard/components/widgets/conversation/Message.vue index 78b1c148f..eb6644c91 100644 --- a/app/javascript/dashboard/components/widgets/conversation/Message.vue +++ b/app/javascript/dashboard/components/widgets/conversation/Message.vue @@ -152,7 +152,10 @@ export default { return !messageType ? 'left' : 'right'; }, readableTime() { - return this.messageStamp(this.data.created_at, 'LLL d, h:mm a'); + return this.messageStamp( + this.contentAttributes.external_created_at || this.data.created_at, + 'LLL d, h:mm a' + ); }, isBubble() { return [0, 1, 3].includes(this.data.message_type); @@ -205,7 +208,7 @@ export default { }, isSentByBot() { if (this.isPending) return false; - return !this.sender.type || this.sender.type === 'bot'; + return !this.sender.type || this.sender.type === 'agent_bot'; }, }, }; diff --git a/app/models/message.rb b/app/models/message.rb index ea41b15fa..aaaa34db5 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -55,7 +55,10 @@ class Message < ApplicationRecord # [:submitted_email, :items, :submitted_values] : Used for bot message types # [:email] : Used by conversation_continuity incoming email messages # [:in_reply_to] : Used to reply to a particular tweet in threads - store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email, :in_reply_to, :deleted], coder: JSON + # [:deleted] : Used to denote whether the message was deleted by the agent + # [:external_created_at] : Can specify if the message was created at a different timestamp externally + store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email, :in_reply_to, :deleted, + :external_created_at], coder: JSON store :external_source_ids, accessors: [:slack], coder: JSON, prefix: :external_source_id diff --git a/app/views/api/v1/models/_agent_bot.json.jbuilder b/app/views/api/v1/models/_agent_bot.json.jbuilder index 1aa152186..d9d1eaa17 100644 --- a/app/views/api/v1/models/_agent_bot.json.jbuilder +++ b/app/views/api/v1/models/_agent_bot.json.jbuilder @@ -1,5 +1,5 @@ json.id resource.id json.name resource.name json.description resource.description -json.outgoing_url resource.name +json.outgoing_url resource.outgoing_url json.account_id resource.account_id diff --git a/spec/controllers/api/v1/accounts/conversations/messages_controller_spec.rb b/spec/controllers/api/v1/accounts/conversations/messages_controller_spec.rb index a8521a439..93283dbb5 100644 --- a/spec/controllers/api/v1/accounts/conversations/messages_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/conversations/messages_controller_spec.rb @@ -31,6 +31,23 @@ RSpec.describe 'Conversation Messages API', type: :request do expect(conversation.messages.first.content).to eq(params[:content]) end + it 'creates an outgoing message with a specific bot sender' do + agent_bot = create(:agent_bot) + time_stamp = Time.now.utc.to_s + params = { content: 'test-message', external_created_at: time_stamp, sender_type: 'AgentBot', sender_id: agent_bot.id } + + post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id), + params: params, + headers: agent.create_new_auth_token, + as: :json + + expect(response).to have_http_status(:success) + response_data = JSON.parse(response.body) + expect(response_data['content_attributes']['external_created_at']).to eq time_stamp + expect(conversation.messages.count).to eq(1) + expect(conversation.messages.last.sender_id).to eq(agent_bot.id) + end + it 'creates a new outgoing message with attachment' do file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') params = { content: 'test-message', attachments: [file] }