2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseController
|
2020-05-03 06:47:27 +00:00
|
|
|
include Events::Types
|
2020-08-25 17:34:02 +00:00
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
before_action :conversation, except: [:index]
|
2020-04-10 11:12:37 +00:00
|
|
|
before_action :contact_inbox, only: [:create]
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
result = conversation_finder.perform
|
|
|
|
@conversations = result[:conversations]
|
|
|
|
@conversations_count = result[:count]
|
|
|
|
end
|
|
|
|
|
2020-05-03 06:45:10 +00:00
|
|
|
def meta
|
|
|
|
result = conversation_finder.perform
|
|
|
|
@conversations_count = result[:count]
|
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def create
|
|
|
|
@conversation = ::Conversation.create!(conversation_params)
|
|
|
|
end
|
|
|
|
|
2020-03-08 16:38:25 +00:00
|
|
|
def show; end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
def mute
|
|
|
|
@conversation.mute!
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2020-08-17 05:55:13 +00:00
|
|
|
def transcript
|
|
|
|
ConversationReplyMailer.conversation_transcript(@conversation, params[:email])&.deliver_later if params[:email].present?
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def toggle_status
|
2020-06-02 18:20:39 +00:00
|
|
|
if params[:status]
|
|
|
|
@conversation.status = params[:status]
|
|
|
|
@status = @conversation.save
|
|
|
|
else
|
|
|
|
@status = @conversation.toggle_status
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-05-03 06:47:27 +00:00
|
|
|
def toggle_typing_status
|
|
|
|
if params[:typing_status] == 'on'
|
2020-05-04 17:37:56 +00:00
|
|
|
trigger_typing_event(CONVERSATION_TYPING_ON)
|
2020-05-03 06:47:27 +00:00
|
|
|
elsif params[:typing_status] == 'off'
|
2020-05-04 17:37:56 +00:00
|
|
|
trigger_typing_event(CONVERSATION_TYPING_OFF)
|
2020-05-03 06:47:27 +00:00
|
|
|
end
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def update_last_seen
|
2020-08-03 08:10:20 +00:00
|
|
|
@conversation.agent_last_seen_at = DateTime.now.utc
|
2019-08-14 09:48:44 +00:00
|
|
|
@conversation.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-04 17:37:56 +00:00
|
|
|
def trigger_typing_event(event)
|
|
|
|
user = current_user.presence || @resource
|
|
|
|
Rails.configuration.dispatcher.dispatch(event, Time.zone.now, conversation: @conversation, user: user)
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
def conversation
|
2020-06-07 08:28:05 +00:00
|
|
|
@conversation ||= Current.account.conversations.find_by(display_id: params[:id])
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def contact_inbox
|
|
|
|
@contact_inbox ||= ::ContactInbox.find_by!(source_id: params[:source_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def conversation_params
|
|
|
|
{
|
2020-06-07 08:28:05 +00:00
|
|
|
account_id: Current.account.id,
|
2020-04-10 11:12:37 +00:00
|
|
|
inbox_id: @contact_inbox.inbox_id,
|
|
|
|
contact_id: @contact_inbox.contact_id,
|
|
|
|
contact_inbox_id: @contact_inbox.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def conversation_finder
|
|
|
|
@conversation_finder ||= ConversationFinder.new(current_user, params)
|
|
|
|
end
|
|
|
|
end
|