2020-05-04 17:37:56 +00:00
|
|
|
class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController
|
|
|
|
include Events::Types
|
|
|
|
|
2020-05-09 16:32:43 +00:00
|
|
|
def index
|
|
|
|
@conversation = conversation
|
|
|
|
end
|
|
|
|
|
2021-02-15 18:44:13 +00:00
|
|
|
def create
|
|
|
|
ActiveRecord::Base.transaction do
|
2022-04-08 09:23:37 +00:00
|
|
|
process_update_contact
|
2021-02-15 18:44:13 +00:00
|
|
|
@conversation = create_conversation
|
2022-09-13 12:10:06 +00:00
|
|
|
conversation.messages.create!(message_params)
|
2021-02-15 18:44:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-08 09:23:37 +00:00
|
|
|
def process_update_contact
|
2022-06-23 10:18:56 +00:00
|
|
|
@contact = ContactIdentifyAction.new(
|
|
|
|
contact: @contact,
|
|
|
|
params: { email: contact_email, phone_number: contact_phone_number, name: contact_name },
|
2022-10-18 20:16:29 +00:00
|
|
|
retain_original_contact_name: true,
|
|
|
|
discard_invalid_attrs: true
|
2022-06-23 10:18:56 +00:00
|
|
|
).perform
|
2022-04-08 09:23:37 +00:00
|
|
|
end
|
|
|
|
|
2020-07-07 18:34:44 +00:00
|
|
|
def update_last_seen
|
|
|
|
head :ok && return if conversation.nil?
|
|
|
|
|
2020-09-10 13:49:15 +00:00
|
|
|
conversation.contact_last_seen_at = DateTime.now.utc
|
2020-07-07 18:34:44 +00:00
|
|
|
conversation.save!
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2020-08-17 05:55:13 +00:00
|
|
|
def transcript
|
|
|
|
if permitted_params[:email].present? && conversation.present?
|
2021-06-08 17:15:01 +00:00
|
|
|
ConversationReplyMailer.with(account: conversation.account).conversation_transcript(
|
2020-08-17 05:55:13 +00:00
|
|
|
conversation,
|
|
|
|
permitted_params[:email]
|
|
|
|
)&.deliver_later
|
|
|
|
end
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2020-05-04 17:37:56 +00:00
|
|
|
def toggle_typing
|
2020-05-05 18:40:56 +00:00
|
|
|
head :ok && return if conversation.nil?
|
2020-05-04 17:37:56 +00:00
|
|
|
|
2020-09-08 05:54:08 +00:00
|
|
|
case permitted_params[:typing_status]
|
|
|
|
when 'on'
|
2020-05-04 17:37:56 +00:00
|
|
|
trigger_typing_event(CONVERSATION_TYPING_ON)
|
2020-09-08 05:54:08 +00:00
|
|
|
when 'off'
|
2020-05-04 17:37:56 +00:00
|
|
|
trigger_typing_event(CONVERSATION_TYPING_OFF)
|
|
|
|
end
|
|
|
|
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2022-03-15 16:37:30 +00:00
|
|
|
def toggle_status
|
2022-04-06 08:24:55 +00:00
|
|
|
return head :not_found if conversation.nil?
|
|
|
|
|
|
|
|
return head :forbidden unless @web_widget.end_conversation?
|
|
|
|
|
2022-03-15 16:37:30 +00:00
|
|
|
unless conversation.resolved?
|
2022-03-18 08:13:02 +00:00
|
|
|
conversation.status = :resolved
|
2022-09-13 12:10:06 +00:00
|
|
|
conversation.save!
|
2022-03-15 16:37:30 +00:00
|
|
|
end
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2020-05-04 17:37:56 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def trigger_typing_event(event)
|
|
|
|
Rails.configuration.dispatcher.dispatch(event, Time.zone.now, conversation: conversation, user: @contact)
|
|
|
|
end
|
|
|
|
|
|
|
|
def permitted_params
|
2022-04-21 15:09:45 +00:00
|
|
|
params.permit(:id, :typing_status, :website_token, :email, contact: [:name, :email, :phone_number],
|
|
|
|
message: [:content, :referer_url, :timestamp, :echo_id],
|
2022-04-08 09:23:37 +00:00
|
|
|
custom_attributes: {})
|
2020-05-04 17:37:56 +00:00
|
|
|
end
|
|
|
|
end
|