chore: Message API improvements (#2396)
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
parent
1ebab21cfa
commit
0a087c95fd
5 changed files with 39 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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';
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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] }
|
||||
|
|
Loading…
Reference in a new issue