chore: Enable an echo id for message create end points

This commit is contained in:
Sojan 2020-09-13 16:16:52 +05:30
parent afc062537c
commit 739c062676
3 changed files with 15 additions and 6 deletions

View file

@ -3,12 +3,11 @@ class Messages::MessageBuilder
attr_reader :message attr_reader :message
def initialize(user, conversation, params) def initialize(user, conversation, params)
@content = params[:content] @params = params
@private = params[:private] || false @private = params[:private] || false
@conversation = conversation @conversation = conversation
@user = user @user = user
@message_type = params[:message_type] || 'outgoing' @message_type = params[:message_type] || 'outgoing'
@content_type = params[:content_type]
@items = params.to_unsafe_h&.dig(:content_attributes, :items) @items = params.to_unsafe_h&.dig(:content_attributes, :items)
@attachments = params[:attachments] @attachments = params[:attachments]
@in_reply_to = params.to_unsafe_h&.dig(:content_attributes, :in_reply_to) @in_reply_to = params.to_unsafe_h&.dig(:content_attributes, :in_reply_to)
@ -48,12 +47,13 @@ class Messages::MessageBuilder
account_id: @conversation.account_id, account_id: @conversation.account_id,
inbox_id: @conversation.inbox_id, inbox_id: @conversation.inbox_id,
message_type: message_type, message_type: message_type,
content: @content, content: @params[:content],
private: @private, private: @private,
sender: sender, sender: sender,
content_type: @content_type, content_type: @params[:content_type],
items: @items, items: @items,
in_reply_to: @in_reply_to in_reply_to: @in_reply_to,
echo_id: @params[:echo_id]
} }
end end
end end

View file

@ -48,6 +48,7 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController
sender: @contact, sender: @contact,
content: permitted_params[:message][:content], content: permitted_params[:message][:content],
inbox_id: conversation.inbox_id, inbox_id: conversation.inbox_id,
echo_id: permitted_params[:message][:echo_id],
message_type: :incoming message_type: :incoming
} }
end end
@ -116,7 +117,7 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController
end end
def permitted_params def permitted_params
params.permit(:id, :before, :website_token, contact: [:email], message: [:content, :referer_url, :timestamp]) params.permit(:id, :before, :website_token, contact: [:email], message: [:content, :referer_url, :timestamp, :echo_id])
end end
def set_message def set_message

View file

@ -36,6 +36,9 @@ class Message < ApplicationRecord
validates :conversation_id, presence: true validates :conversation_id, presence: true
validates_with ContentAttributeValidator validates_with ContentAttributeValidator
# when you have a temperory id in your frontend and want it echoed back via action cable
attr_accessor :echo_id
enum message_type: { incoming: 0, outgoing: 1, activity: 2, template: 3 } enum message_type: { incoming: 0, outgoing: 1, activity: 2, template: 3 }
enum content_type: { enum content_type: {
text: 0, text: 0,
@ -89,7 +92,12 @@ class Message < ApplicationRecord
message_type: message_type_before_type_cast, message_type: message_type_before_type_cast,
conversation_id: conversation.display_id conversation_id: conversation.display_id
) )
data.merge!(echo_id: echo_id) if echo_id.present?
data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present? data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present?
merge_sender_attributes(data)
end
def merge_sender_attributes(data)
data.merge!(sender: sender.push_event_data) if sender && !sender.is_a?(AgentBot) data.merge!(sender: sender.push_event_data) if sender && !sender.is_a?(AgentBot)
data.merge!(sender: sender.push_event_data(inbox)) if sender&.is_a?(AgentBot) data.merge!(sender: sender.push_event_data(inbox)) if sender&.is_a?(AgentBot)
data data