2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
class Sessions::Event::ChatSessionInit < Sessions::Event::ChatBase
|
2015-11-10 14:01:04 +00:00
|
|
|
|
2019-11-21 07:47:05 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
a customer requests a new chat session
|
|
|
|
|
|
|
|
payload
|
|
|
|
|
|
|
|
{
|
|
|
|
event: 'chat_session_init',
|
|
|
|
data: {
|
|
|
|
chat_id: 'the id of chat',
|
|
|
|
url: 'the browser url',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
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
|
2020-02-13 08:27:36 +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
|
2020-02-13 08:27:36 +00:00
|
|
|
if remote_ip
|
2016-03-24 14:34:49 +00:00
|
|
|
begin
|
|
|
|
dns = Resolv::DNS.new
|
|
|
|
dns.timeouts = 3
|
2020-02-13 08:27:36 +00:00
|
|
|
result = dns.getname remote_ip
|
2016-03-24 14:34:49 +00:00
|
|
|
if result
|
|
|
|
dns_name = result.to_s
|
|
|
|
end
|
|
|
|
rescue => e
|
2017-04-19 10:09:54 +00:00
|
|
|
Rails.logger.error e
|
2016-03-24 14:34:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-10 14:01:04 +00:00
|
|
|
# create chat session
|
|
|
|
chat_session = Chat::Session.create(
|
2018-12-19 17:31:51 +00:00
|
|
|
chat_id: @payload['data']['chat_id'],
|
|
|
|
name: '',
|
|
|
|
state: 'waiting',
|
2015-11-10 14:01:04 +00:00
|
|
|
preferences: {
|
2018-12-19 17:31:51 +00:00
|
|
|
url: @payload['data']['url'],
|
2015-11-11 13:10:26 +00:00
|
|
|
participants: [@client_id],
|
2020-02-13 08:27:36 +00:00
|
|
|
remote_ip: remote_ip,
|
2018-12-19 17:31:51 +00:00
|
|
|
geo_ip: geo_ip,
|
|
|
|
dns_name: dns_name,
|
2015-11-10 14:01:04 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2015-11-11 13:10:26 +00:00
|
|
|
# send broadcast to agents
|
2019-11-12 16:06:42 +00:00
|
|
|
Chat.broadcast_agent_state_update([chat_session.chat_id])
|
2015-11-10 14:01:04 +00:00
|
|
|
|
|
|
|
# return new session
|
|
|
|
{
|
2015-11-12 13:19:41 +00:00
|
|
|
event: 'chat_session_queue',
|
2018-12-19 17:31:51 +00:00
|
|
|
data: {
|
|
|
|
state: 'queue',
|
2019-11-12 16:06:42 +00:00
|
|
|
position: Chat.waiting_chat_count([chat_session.chat_id]),
|
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
|