chat_status_customer.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. class Sessions::Event::ChatStatusCustomer < Sessions::Event::ChatBase
  2. =begin
  3. a customer requests the current state of a chat
  4. payload
  5. {
  6. event: 'chat_status_agent',
  7. data: {
  8. session_id: 'the id of the current chat session',
  9. url: 'optional url', # will trigger a chat_session_notice to agent
  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. return if blocked_ip?
  18. return if blocked_country?
  19. return if blocked_origin?
  20. # check if it's a chat sessin reconnect
  21. session_id = nil
  22. if @payload['data']['session_id']
  23. session_id = @payload['data']['session_id']
  24. # update recipients of existing sessions
  25. chat_session = Chat::Session.find_by(session_id: session_id)
  26. if chat_session
  27. chat_session.add_recipient(@client_id, true)
  28. # sent url update to agent
  29. if @payload['data']['url']
  30. message = {
  31. event: 'chat_session_notice',
  32. data: {
  33. session_id: chat_session.session_id,
  34. message: @payload['data']['url'],
  35. },
  36. }
  37. chat_session.send_to_recipients(message, @client_id)
  38. end
  39. end
  40. end
  41. {
  42. event: 'chat_status_customer',
  43. data: current_chat.customer_state(session_id),
  44. }
  45. end
  46. def blocked_ip?
  47. return false if !current_chat.blocked_ip?(remote_ip)
  48. send_unavailable
  49. true
  50. end
  51. def blocked_country?
  52. return false if !current_chat.blocked_country?(remote_ip)
  53. send_unavailable
  54. true
  55. end
  56. def blocked_origin?
  57. return false if current_chat.website_whitelisted?(origin)
  58. send_unavailable
  59. true
  60. end
  61. def send_unavailable
  62. error = {
  63. event: 'chat_error',
  64. data: {
  65. state: 'chat_unavailable',
  66. },
  67. }
  68. Sessions.send(@client_id, error)
  69. end
  70. end