2015-11-11 13:10:26 +00:00
|
|
|
class Sessions::Event::ChatSessionInit < 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-25 09:33:39 +00:00
|
|
|
return if !check_chat_exists
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2016-01-05 08:49:11 +00:00
|
|
|
# geo ip lookup
|
|
|
|
geo_ip = nil
|
2016-01-05 12:46:43 +00:00
|
|
|
if @remote_ip
|
|
|
|
geo_ip = Service::GeoIp.location(@remote_ip)
|
2016-01-05 08:49:11 +00:00
|
|
|
end
|
|
|
|
|
2016-03-24 14:34:49 +00:00
|
|
|
# dns lookup
|
|
|
|
dns_name = nil
|
|
|
|
if @remote_ip
|
|
|
|
begin
|
|
|
|
dns = Resolv::DNS.new
|
|
|
|
dns.timeouts = 3
|
|
|
|
result = dns.getname @remote_ip
|
|
|
|
if result
|
|
|
|
dns_name = result.to_s
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
Rails.logger.error e.backtrace.inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
# create chat session
|
|
|
|
chat_session = Chat::Session.create(
|
2015-12-09 13:09:37 +00:00
|
|
|
chat_id: @payload['data']['chat_id'],
|
2015-11-10 14:01:04 +00:00
|
|
|
name: '',
|
|
|
|
state: 'waiting',
|
|
|
|
preferences: {
|
2015-11-11 13:10:26 +00:00
|
|
|
participants: [@client_id],
|
2016-01-05 12:46:43 +00:00
|
|
|
remote_ip: @remote_ip,
|
2016-01-05 08:49:11 +00:00
|
|
|
geo_ip: geo_ip,
|
2016-03-24 14:34:49 +00:00
|
|
|
dns_name: dns_name,
|
2015-11-10 14:01:04 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
# send broadcast to agents
|
2016-01-06 00:45:03 +00:00
|
|
|
Chat.broadcast_agent_state_update
|
2015-11-10 14:01:04 +00:00
|
|
|
|
|
|
|
# return new session
|
|
|
|
{
|
2015-11-12 13:19:41 +00:00
|
|
|
event: 'chat_session_queue',
|
2015-11-10 14:01:04 +00:00
|
|
|
data: {
|
|
|
|
state: 'queue',
|
|
|
|
position: Chat.waiting_chat_count,
|
|
|
|
session_id: chat_session.session_id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
end
|