chat_session_init.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. class Sessions::Event::ChatSessionInit < Sessions::Event::ChatBase
  2. def run
  3. return super if super
  4. return if !check_chat_exists
  5. # geo ip lookup
  6. geo_ip = nil
  7. if @remote_ip
  8. geo_ip = Service::GeoIp.location(@remote_ip)
  9. end
  10. # dns lookup
  11. dns_name = nil
  12. if @remote_ip
  13. begin
  14. dns = Resolv::DNS.new
  15. dns.timeouts = 3
  16. result = dns.getname @remote_ip
  17. if result
  18. dns_name = result.to_s
  19. end
  20. rescue => e
  21. Rails.logger.error e
  22. end
  23. end
  24. # create chat session
  25. chat_session = Chat::Session.create(
  26. chat_id: @payload['data']['chat_id'],
  27. name: '',
  28. state: 'waiting',
  29. preferences: {
  30. url: @payload['data']['url'],
  31. participants: [@client_id],
  32. remote_ip: @remote_ip,
  33. geo_ip: geo_ip,
  34. dns_name: dns_name,
  35. },
  36. )
  37. # send broadcast to agents
  38. Chat.broadcast_agent_state_update
  39. # return new session
  40. {
  41. event: 'chat_session_queue',
  42. data: {
  43. state: 'queue',
  44. position: Chat.waiting_chat_count,
  45. session_id: chat_session.session_id,
  46. },
  47. }
  48. end
  49. end