chat_session_init.rb 1.5 KB

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