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
|
2021-07-23 09:54:07 +00:00
|
|
|
include DateRangeHelper
|
2020-08-25 17:34:02 +00:00
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
before_action :conversation, except: [:index, :meta, :search, :create]
|
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-09-23 09:57:41 +00:00
|
|
|
def search
|
|
|
|
result = conversation_finder.perform
|
|
|
|
@conversations = result[:conversations]
|
|
|
|
@conversations_count = result[:count]
|
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def create
|
2021-04-15 09:43:01 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@conversation = ::Conversation.create!(conversation_params)
|
|
|
|
Messages::MessageBuilder.new(Current.user, @conversation, params[:message]).perform if params[:message].present?
|
|
|
|
end
|
2020-04-10 11:12:37 +00:00
|
|
|
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-10-08 06:32:08 +00:00
|
|
|
def unmute
|
|
|
|
@conversation.unmute!
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2020-08-17 05:55:13 +00:00
|
|
|
def transcript
|
2021-06-08 17:15:01 +00:00
|
|
|
render json: { error: 'email param missing' }, status: :unprocessable_entity and return if params[:email].blank?
|
|
|
|
|
|
|
|
ConversationReplyMailer.with(account: @conversation.account).conversation_transcript(@conversation, params[:email])&.deliver_later
|
2020-08-17 05:55:13 +00:00
|
|
|
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]
|
2021-07-23 09:54:07 +00:00
|
|
|
set_conversation_status
|
|
|
|
@status = @conversation.save!
|
2020-06-02 18:20:39 +00:00
|
|
|
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
|
2020-09-08 05:54:08 +00:00
|
|
|
case 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)
|
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
|
|
|
|
|
2021-07-23 09:54:07 +00:00
|
|
|
def set_conversation_status
|
|
|
|
status = params[:status] == 'bot' ? 'pending' : params[:status]
|
|
|
|
@conversation.status = status
|
|
|
|
@conversation.snoozed_until = parse_date_time(params[:snoozed_until].to_s) if params[:snoozed_until]
|
|
|
|
end
|
|
|
|
|
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
|
2021-06-11 06:14:31 +00:00
|
|
|
@conversation ||= Current.account.conversations.find_by!(display_id: params[:id])
|
|
|
|
authorize @conversation.inbox, :show?
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def contact_inbox
|
2021-04-15 09:43:01 +00:00
|
|
|
@contact_inbox = build_contact_inbox
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
@contact_inbox ||= ::ContactInbox.find_by!(source_id: params[:source_id])
|
2021-06-11 06:14:31 +00:00
|
|
|
authorize @contact_inbox.inbox, :show?
|
2020-04-10 11:12:37 +00:00
|
|
|
end
|
|
|
|
|
2021-04-15 09:43:01 +00:00
|
|
|
def build_contact_inbox
|
|
|
|
return if params[:contact_id].blank? || params[:inbox_id].blank?
|
|
|
|
|
2021-06-11 06:14:31 +00:00
|
|
|
inbox = Current.account.inboxes.find(params[:inbox_id])
|
|
|
|
authorize inbox, :show?
|
|
|
|
|
2021-04-15 09:43:01 +00:00
|
|
|
ContactInboxBuilder.new(
|
|
|
|
contact_id: params[:contact_id],
|
2021-06-11 06:14:31 +00:00
|
|
|
inbox_id: inbox.id,
|
2021-04-15 09:43:01 +00:00
|
|
|
source_id: params[:source_id]
|
|
|
|
).perform
|
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def conversation_params
|
2021-04-15 09:43:01 +00:00
|
|
|
additional_attributes = params[:additional_attributes]&.permit! || {}
|
2021-06-07 14:34:31 +00:00
|
|
|
status = params[:status].present? ? { status: params[:status] } : {}
|
2021-07-21 16:32:43 +00:00
|
|
|
|
|
|
|
# TODO: temporary fallback for the old bot status in conversation, we will remove after couple of releases
|
|
|
|
status = { status: 'pending' } if status[:status] == 'bot'
|
2020-04-10 11:12:37 +00:00
|
|
|
{
|
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,
|
2021-01-07 08:17:38 +00:00
|
|
|
contact_inbox_id: @contact_inbox.id,
|
2021-07-23 09:54:07 +00:00
|
|
|
additional_attributes: additional_attributes,
|
|
|
|
snoozed_until: params[:snoozed_until]
|
2021-06-07 14:34:31 +00:00
|
|
|
}.merge(status)
|
2020-04-10 11:12:37 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def conversation_finder
|
|
|
|
@conversation_finder ||= ConversationFinder.new(current_user, params)
|
|
|
|
end
|
|
|
|
end
|