2015-11-11 13:10:26 +00:00
|
|
|
class Sessions::Event::ChatSessionClose < 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
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2015-11-25 09:33:39 +00:00
|
|
|
return if !check_chat_session_exists
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2015-11-13 15:20:07 +00:00
|
|
|
realname = 'Anonymous'
|
2015-11-11 20:44:54 +00:00
|
|
|
if @session && @session['id']
|
2015-11-30 10:50:02 +00:00
|
|
|
realname = User.lookup(id: @session['id']).fullname
|
2015-11-11 20:44:54 +00:00
|
|
|
end
|
|
|
|
|
2015-11-12 22:56:56 +00:00
|
|
|
# check count of participents
|
|
|
|
participents_count = 0
|
2015-11-25 09:33:39 +00:00
|
|
|
chat_session = current_chat_session
|
2015-11-12 22:56:56 +00:00
|
|
|
if chat_session.preferences[:participents]
|
|
|
|
participents_count = chat_session.preferences[:participents].count
|
|
|
|
end
|
|
|
|
|
|
|
|
# notify about "closing"
|
|
|
|
if participents_count < 2 || (@session && chat_session.user_id == @session['id'])
|
2015-11-11 20:44:54 +00:00
|
|
|
message = {
|
|
|
|
event: 'chat_session_closed',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2015-11-11 20:44:54 +00:00
|
|
|
session_id: chat_session.session_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
realname: realname,
|
2015-11-11 20:44:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
# close session if host is closing it
|
|
|
|
chat_session.state = 'closed'
|
|
|
|
chat_session.save
|
2015-11-12 22:56:56 +00:00
|
|
|
|
|
|
|
# set state update to all agents
|
2016-01-06 00:45:03 +00:00
|
|
|
Chat.broadcast_agent_state_update
|
2015-11-12 22:56:56 +00:00
|
|
|
|
2015-11-13 16:03:58 +00:00
|
|
|
# send position update to other waiting sessions
|
2016-01-06 00:45:03 +00:00
|
|
|
Chat.broadcast_customer_state_update
|
2015-11-13 16:03:58 +00:00
|
|
|
|
2015-11-12 22:56:56 +00:00
|
|
|
# notify about "leaving"
|
2015-11-11 20:44:54 +00:00
|
|
|
else
|
|
|
|
message = {
|
|
|
|
event: 'chat_session_left',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
2015-11-11 20:44:54 +00:00
|
|
|
session_id: chat_session.session_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
realname: realname,
|
2015-11-11 20:44:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
chat_session.send_to_recipients(message, @client_id)
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2015-11-11 20:44:54 +00:00
|
|
|
# notifiy participients
|
2015-11-10 14:01:04 +00:00
|
|
|
{
|
|
|
|
event: 'chat_status_close',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
|
|
|
state: 'ok',
|
2015-11-10 14:01:04 +00:00
|
|
|
session_id: chat_session.session_id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|