2020-01-09 07:36:40 +00:00
|
|
|
class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController
|
2019-10-29 07:20:54 +00:00
|
|
|
before_action :set_conversation, only: [:create]
|
2020-01-09 07:36:40 +00:00
|
|
|
before_action :set_message, only: [:update]
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def index
|
|
|
|
@messages = conversation.nil? ? [] : message_finder.perform
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def create
|
|
|
|
@message = conversation.messages.new(message_params)
|
2020-04-17 15:45:20 +00:00
|
|
|
@message.save
|
2020-04-02 06:58:38 +00:00
|
|
|
build_attachment
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def update
|
2020-04-10 11:12:37 +00:00
|
|
|
if @message.content_type == 'input_email'
|
|
|
|
@message.update!(submitted_email: contact_email)
|
|
|
|
update_contact(contact_email)
|
|
|
|
else
|
|
|
|
@message.update!(message_update_params[:message])
|
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
rescue StandardError => e
|
|
|
|
render json: { error: @contact.errors, message: e.message }.to_json, status: 500
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
private
|
|
|
|
|
2020-04-02 06:58:38 +00:00
|
|
|
def build_attachment
|
2020-04-17 15:45:20 +00:00
|
|
|
return if params[:message][:attachments].blank?
|
|
|
|
|
|
|
|
params[:message][:attachments].each do |uploaded_attachment|
|
|
|
|
attachment = @message.attachments.new(
|
|
|
|
account_id: @message.account_id,
|
|
|
|
file_type: helpers.file_type(uploaded_attachment&.content_type)
|
|
|
|
)
|
|
|
|
attachment.file.attach(uploaded_attachment)
|
|
|
|
end
|
|
|
|
@message.save!
|
2020-04-02 06:58:38 +00:00
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def set_conversation
|
|
|
|
@conversation = ::Conversation.create!(conversation_params) if conversation.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_params
|
|
|
|
{
|
|
|
|
account_id: conversation.account_id,
|
2020-06-27 16:04:53 +00:00
|
|
|
sender: @contact,
|
2020-02-29 12:13:49 +00:00
|
|
|
content: permitted_params[:message][:content],
|
2019-10-29 07:20:54 +00:00
|
|
|
inbox_id: conversation.inbox_id,
|
2020-02-29 12:13:49 +00:00
|
|
|
message_type: :incoming
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def conversation_params
|
|
|
|
{
|
|
|
|
account_id: inbox.account_id,
|
|
|
|
inbox_id: inbox.id,
|
2020-01-09 07:36:40 +00:00
|
|
|
contact_id: @contact.id,
|
|
|
|
contact_inbox_id: @contact_inbox.id,
|
2019-11-16 09:18:38 +00:00
|
|
|
additional_attributes: {
|
2020-01-01 17:00:43 +00:00
|
|
|
browser: browser_params,
|
|
|
|
referer: permitted_params[:message][:referer_url],
|
|
|
|
initiated_at: timestamp_params
|
2019-11-16 09:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-01-01 17:00:43 +00:00
|
|
|
def timestamp_params
|
|
|
|
{
|
|
|
|
timestamp: permitted_params[:message][:timestamp]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def inbox
|
2020-01-09 07:36:40 +00:00
|
|
|
@inbox ||= ::Inbox.find_by(id: auth_token_params[:inbox_id])
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def message_finder_params
|
|
|
|
{
|
2019-12-11 15:27:06 +00:00
|
|
|
filter_internal_messages: true,
|
|
|
|
before: permitted_params[:before]
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_finder
|
|
|
|
@message_finder ||= MessageFinder.new(conversation, message_finder_params)
|
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def update_contact(email)
|
|
|
|
contact_with_email = @account.contacts.find_by(email: email)
|
|
|
|
if contact_with_email
|
2020-04-03 07:34:58 +00:00
|
|
|
@contact = ::ContactMergeAction.new(
|
|
|
|
account: @account,
|
|
|
|
base_contact: contact_with_email,
|
|
|
|
mergee_contact: @contact
|
|
|
|
).perform
|
2020-01-09 07:36:40 +00:00
|
|
|
else
|
|
|
|
@contact.update!(
|
2020-01-13 08:55:23 +00:00
|
|
|
email: email,
|
2020-01-09 07:36:40 +00:00
|
|
|
name: contact_name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-13 08:55:23 +00:00
|
|
|
def contact_email
|
|
|
|
permitted_params[:contact][:email].downcase
|
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def contact_name
|
2020-01-13 08:55:23 +00:00
|
|
|
contact_email.split('@')[0]
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def message_update_params
|
|
|
|
params.permit(message: [submitted_values: [:name, :title, :value]])
|
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def permitted_params
|
2020-01-09 07:36:40 +00:00
|
|
|
params.permit(:id, :before, :website_token, contact: [:email], message: [:content, :referer_url, :timestamp])
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2019-10-30 05:13:11 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def set_message
|
|
|
|
@message = @web_widget.inbox.messages.find(permitted_params[:id])
|
2019-10-30 05:13:11 +00:00
|
|
|
end
|
2019-10-20 08:47:26 +00:00
|
|
|
end
|