2015-11-11 13:10:26 +00:00
|
|
|
class Sessions::Event::ChatSessionStart < Sessions::Event::ChatBase
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2019-11-21 07:47:05 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
a agent start`s a new chat session
|
|
|
|
|
|
|
|
payload
|
|
|
|
|
|
|
|
{
|
|
|
|
event: 'chat_session_start',
|
|
|
|
data: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
return is sent as message back to peer
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
def run
|
2015-12-09 13:09:37 +00:00
|
|
|
return super if super
|
2016-08-12 16:39:09 +00:00
|
|
|
return if !permission_check('chat.agent', 'chat')
|
2015-11-10 14:01:04 +00:00
|
|
|
|
|
|
|
# find first in waiting list
|
2019-11-12 16:06:42 +00:00
|
|
|
chat_user = User.lookup(id: @session['id'])
|
|
|
|
chat_ids = Chat.agent_active_chat_ids(chat_user)
|
|
|
|
chat_session = Chat::Session.where(state: 'waiting', chat_id: chat_ids).order(created_at: :asc).first
|
2015-11-10 14:01:04 +00:00
|
|
|
if !chat_session
|
|
|
|
return {
|
|
|
|
event: 'chat_session_start',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
|
|
|
state: 'failed',
|
2015-11-10 14:01:04 +00:00
|
|
|
message: 'No session available.',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2019-11-12 16:06:42 +00:00
|
|
|
chat_session.user_id = chat_user.id
|
2015-11-10 14:01:04 +00:00
|
|
|
chat_session.state = 'running'
|
2015-11-11 13:10:26 +00:00
|
|
|
chat_session.preferences[:participants] = chat_session.add_recipient(@client_id)
|
2015-11-10 14:01:04 +00:00
|
|
|
chat_session.save
|
|
|
|
|
|
|
|
# send chat_session_init to client
|
2019-11-21 07:47:05 +00:00
|
|
|
user = chat_session.agent_user
|
2015-11-10 14:01:04 +00:00
|
|
|
data = {
|
|
|
|
event: 'chat_session_start',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
|
|
|
state: 'ok',
|
|
|
|
agent: user,
|
2015-11-10 14:01:04 +00:00
|
|
|
session_id: chat_session.session_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
chat_id: chat_session.chat_id,
|
2015-11-10 14:01:04 +00:00
|
|
|
},
|
|
|
|
}
|
2016-01-05 13:47:41 +00:00
|
|
|
# send to customer
|
|
|
|
chat_session.send_to_recipients(data, @client_id)
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
# send to agent
|
|
|
|
data = {
|
|
|
|
event: 'chat_session_start',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2016-01-05 13:47:41 +00:00
|
|
|
session: chat_session.attributes,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Sessions.send(@client_id, data)
|
2015-11-12 13:19:41 +00:00
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
# send state update with sessions to agents
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat.broadcast_agent_state_update([chat_session.chat_id])
|
2015-11-12 22:56:56 +00:00
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
# send position update to other waiting sessions
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat.broadcast_customer_state_update(chat_session.chat_id)
|
2016-01-05 13:47:41 +00:00
|
|
|
|
2015-11-12 13:19:41 +00:00
|
|
|
nil
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|