chat_session_init.rb 1.5 KB

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