Files
chatwoot/app/controllers/api/v1/accounts/conversations_controller.rb
Sojan Jose dc6398ab56 Feature: Api to toggle typing status on a conversation (#807)
- api to toggle typing status on a conversation
- clients receive webhook events on the same

Addresses: #718 , #719 , #775
2020-05-03 12:17:27 +05:30

70 lines
1.8 KiB
Ruby

class Api::V1::Accounts::ConversationsController < Api::BaseController
include Events::Types
before_action :conversation, except: [:index]
before_action :contact_inbox, only: [:create]
def index
result = conversation_finder.perform
@conversations = result[:conversations]
@conversations_count = result[:count]
end
def meta
result = conversation_finder.perform
@conversations_count = result[:count]
end
def create
@conversation = ::Conversation.create!(conversation_params)
end
def show; end
def toggle_status
@status = @conversation.toggle_status
end
def toggle_typing_status
user = current_user.presence || @resource
if params[:typing_status] == 'on'
Rails.configuration.dispatcher.dispatch(CONVERSATION_TYPING_ON, Time.zone.now, conversation: @conversation, user: user)
elsif params[:typing_status] == 'off'
Rails.configuration.dispatcher.dispatch(CONVERSATION_TYPING_OFF, Time.zone.now, conversation: @conversation)
end
head :ok
end
def update_last_seen
@conversation.agent_last_seen_at = parsed_last_seen_at
@conversation.save!
head :ok
end
private
def parsed_last_seen_at
DateTime.strptime(params[:agent_last_seen_at].to_s, '%s')
end
def conversation
@conversation ||= current_account.conversations.find_by(display_id: params[:id])
end
def contact_inbox
@contact_inbox ||= ::ContactInbox.find_by!(source_id: params[:source_id])
end
def conversation_params
{
account_id: current_account.id,
inbox_id: @contact_inbox.inbox_id,
contact_id: @contact_inbox.contact_id,
contact_inbox_id: @contact_inbox.id
}
end
def conversation_finder
@conversation_finder ||= ConversationFinder.new(current_user, params)
end
end