2015-11-11 13:10:26 +00:00
|
|
|
class Sessions::Event::ChatSessionStart < Sessions::Event::ChatBase
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
def run
|
2015-12-09 13:09:37 +00:00
|
|
|
return super if super
|
2016-05-25 07:19:45 +00:00
|
|
|
return if !role_permission_check('Agent', 'chat')
|
2015-11-10 14:01:04 +00:00
|
|
|
|
|
|
|
# find first in waiting list
|
|
|
|
chat_session = Chat::Session.where(state: 'waiting').order('created_at ASC').first
|
|
|
|
if !chat_session
|
|
|
|
return {
|
|
|
|
event: 'chat_session_start',
|
|
|
|
data: {
|
|
|
|
state: 'failed',
|
|
|
|
message: 'No session available.',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2015-11-11 13:10:26 +00:00
|
|
|
chat_session.user_id = @session['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
|
2015-11-30 10:50:02 +00:00
|
|
|
chat_user = User.lookup(id: chat_session.user_id)
|
2015-11-11 20:44:54 +00:00
|
|
|
url = nil
|
|
|
|
if chat_user.image && chat_user.image != 'none'
|
2015-11-25 12:18:56 +00:00
|
|
|
url = "#{Setting.get('http_type')}://#{Setting.get('fqdn')}/api/v1/users/image/#{chat_user.image}"
|
2015-11-11 20:44:54 +00:00
|
|
|
end
|
2015-11-10 14:01:04 +00:00
|
|
|
user = {
|
|
|
|
name: chat_user.fullname,
|
2015-11-11 20:44:54 +00:00
|
|
|
avatar: url,
|
2015-11-10 14:01:04 +00:00
|
|
|
}
|
|
|
|
data = {
|
|
|
|
event: 'chat_session_start',
|
|
|
|
data: {
|
|
|
|
state: 'ok',
|
|
|
|
agent: user,
|
|
|
|
session_id: chat_session.session_id,
|
2015-11-25 09:33:39 +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',
|
|
|
|
data: {
|
|
|
|
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
|
2016-01-06 00:45:03 +00:00
|
|
|
Chat.broadcast_agent_state_update
|
2015-11-12 22:56:56 +00:00
|
|
|
|
2016-01-05 13:47:41 +00:00
|
|
|
# send position update to other waiting sessions
|
2016-01-06 00:45:03 +00:00
|
|
|
Chat.broadcast_customer_state_update
|
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
|