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-10-20 12:44:56 +00:00
|
|
|
before_action :conversation, except: [:index, :meta, :search, :create, :filter]
|
2022-10-13 22:12:04 +00:00
|
|
|
before_action :inbox, :contact, :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
|
2022-11-25 10:01:04 +00:00
|
|
|
@conversation = ConversationBuilder.new(params: params, contact_inbox: @contact_inbox).perform
|
2021-04-15 09:43:01 +00:00
|
|
|
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
|
|
|
|
2021-10-20 12:44:56 +00:00
|
|
|
def filter
|
2021-11-01 08:27:04 +00:00
|
|
|
result = ::Conversations::FilterService.new(params.permit!, current_user).perform
|
|
|
|
@conversations = result[:conversations]
|
|
|
|
@conversations_count = result[:count]
|
2021-10-20 12:44:56 +00:00
|
|
|
end
|
|
|
|
|
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
|
2022-02-08 11:09:19 +00:00
|
|
|
assign_conversation if @conversation.status == 'open' && Current.user.is_a?(User) && Current.user&.agent?
|
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'
|
2021-11-01 08:20:07 +00:00
|
|
|
trigger_typing_event(CONVERSATION_TYPING_ON, params[:is_private])
|
2020-09-08 05:54:08 +00:00
|
|
|
when 'off'
|
2021-11-01 08:20:07 +00:00
|
|
|
trigger_typing_event(CONVERSATION_TYPING_OFF, params[:is_private])
|
2020-05-03 06:47:27 +00:00
|
|
|
end
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def update_last_seen
|
2022-11-24 07:55:45 +00:00
|
|
|
update_last_seen_on_conversation(DateTime.now.utc, assignee?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread
|
|
|
|
last_incoming_message = @conversation.messages.incoming.last
|
|
|
|
last_seen_at = last_incoming_message.created_at - 1.second if last_incoming_message.present?
|
|
|
|
update_last_seen_on_conversation(last_seen_at, true)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2021-09-22 05:16:48 +00:00
|
|
|
def custom_attributes
|
|
|
|
@conversation.custom_attributes = params.permit(custom_attributes: {})[:custom_attributes]
|
|
|
|
@conversation.save!
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
private
|
|
|
|
|
2022-11-24 07:55:45 +00:00
|
|
|
def update_last_seen_on_conversation(last_seen_at, update_assignee)
|
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
|
|
|
@conversation.update_column(:agent_last_seen_at, last_seen_at)
|
|
|
|
@conversation.update_column(:assignee_last_seen_at, last_seen_at) if update_assignee.present?
|
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
|
|
|
end
|
|
|
|
|
2021-07-23 09:54:07 +00:00
|
|
|
def set_conversation_status
|
2022-11-25 10:01:04 +00:00
|
|
|
# TODO: temporary fallback for the old bot status in conversation, we will remove after couple of releases
|
|
|
|
# commenting this out to see if there are any errors, if not we can remove this in subsequent releases
|
|
|
|
# status = params[:status] == 'bot' ? 'pending' : params[:status]
|
|
|
|
@conversation.status = params[:status]
|
2021-07-23 09:54:07 +00:00
|
|
|
@conversation.snoozed_until = parse_date_time(params[:snoozed_until].to_s) if params[:snoozed_until]
|
|
|
|
end
|
|
|
|
|
2022-02-08 11:09:19 +00:00
|
|
|
def assign_conversation
|
|
|
|
@agent = Current.account.users.find(current_user.id)
|
|
|
|
@conversation.update_assignee(@agent)
|
|
|
|
end
|
|
|
|
|
2021-11-01 08:20:07 +00:00
|
|
|
def trigger_typing_event(event, is_private)
|
2020-05-04 17:37:56 +00:00
|
|
|
user = current_user.presence || @resource
|
2021-11-01 08:20:07 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(event, Time.zone.now, conversation: @conversation, user: user, is_private: is_private)
|
2020-05-04 17:37:56 +00:00
|
|
|
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
|
|
|
|
|
2022-10-13 22:12:04 +00:00
|
|
|
def inbox
|
|
|
|
return if params[:inbox_id].blank?
|
|
|
|
|
|
|
|
@inbox = Current.account.inboxes.find(params[:inbox_id])
|
|
|
|
authorize @inbox, :show?
|
|
|
|
end
|
|
|
|
|
|
|
|
def contact
|
|
|
|
return if params[:contact_id].blank?
|
|
|
|
|
|
|
|
@contact = Current.account.contacts.find(params[:contact_id])
|
|
|
|
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
|
|
|
|
|
2022-10-13 22:12:04 +00:00
|
|
|
# fallback for the old case where we do look up only using source id
|
|
|
|
# In future we need to change this and make sure we do look up on combination of inbox_id and source_id
|
|
|
|
# and deprecate the support of passing only source_id as the param
|
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
|
2022-10-13 22:12:04 +00:00
|
|
|
return if @inbox.blank? || @contact.blank?
|
2021-06-11 06:14:31 +00:00
|
|
|
|
2021-04-15 09:43:01 +00:00
|
|
|
ContactInboxBuilder.new(
|
2022-10-13 22:12:04 +00:00
|
|
|
contact: @contact,
|
|
|
|
inbox: @inbox,
|
2021-04-15 09:43:01 +00:00
|
|
|
source_id: params[:source_id]
|
|
|
|
).perform
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def conversation_finder
|
2022-11-24 07:55:45 +00:00
|
|
|
@conversation_finder ||= ConversationFinder.new(Current.user, params)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2021-09-23 15:29:10 +00:00
|
|
|
|
|
|
|
def assignee?
|
2022-11-24 07:55:45 +00:00
|
|
|
@conversation.assignee_id? && Current.user == @conversation.assignee
|
2021-09-23 15:29:10 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|