chat_status_customer.rb 1.9 KB

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