chat_session_close.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. class Sessions::Event::ChatSessionClose < Sessions::Event::ChatBase
  2. =begin
  3. a agent or customer is closing the chat session
  4. payload
  5. {
  6. event: 'chat_session_close',
  7. data: {},
  8. }
  9. return is sent as message back to peer
  10. =end
  11. def run
  12. return super if super
  13. return if !check_chat_session_exists
  14. realname = 'Anonymous'
  15. # if it is a agent session, use the realname if the agent for close message
  16. chat_session = current_chat_session
  17. if @session && @session['id'] && chat_session.user_id
  18. agent_user = chat_session.agent_user
  19. if agent_user[:name]
  20. realname = agent_user[:name]
  21. end
  22. end
  23. # check count of participents
  24. participents_count = 0
  25. if chat_session.preferences[:participents]
  26. participents_count = chat_session.preferences[:participents].count
  27. end
  28. # notify about "closing"
  29. if participents_count < 2 || (@session && chat_session.user_id == @session['id'])
  30. message = {
  31. event: 'chat_session_closed',
  32. data: {
  33. session_id: chat_session.session_id,
  34. realname: realname,
  35. },
  36. }
  37. # close session if host is closing it
  38. chat_session.state = 'closed'
  39. chat_session.save
  40. # set state update to all agents
  41. Chat.broadcast_agent_state_update([chat_session.chat_id])
  42. # send position update to other waiting sessions
  43. Chat.broadcast_customer_state_update(chat_session.chat_id)
  44. # notify about "leaving"
  45. else
  46. message = {
  47. event: 'chat_session_left',
  48. data: {
  49. session_id: chat_session.session_id,
  50. realname: realname,
  51. },
  52. }
  53. end
  54. chat_session.send_to_recipients(message, @client_id)
  55. # notifiy participients
  56. {
  57. event: 'chat_status_close',
  58. data: {
  59. state: 'ok',
  60. session_id: chat_session.session_id,
  61. },
  62. }
  63. end
  64. end