chat_session_close.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. class Sessions::Event::ChatSessionClose < Sessions::Event::ChatBase
  2. def run
  3. return super if super
  4. return if !check_chat_session_exists
  5. realname = 'Anonymous'
  6. if @session && @session['id']
  7. realname = User.lookup(id: @session['id']).fullname
  8. end
  9. # check count of participents
  10. participents_count = 0
  11. chat_session = current_chat_session
  12. if chat_session.preferences[:participents]
  13. participents_count = chat_session.preferences[:participents].count
  14. end
  15. # notify about "closing"
  16. if participents_count < 2 || (@session && chat_session.user_id == @session['id'])
  17. message = {
  18. event: 'chat_session_closed',
  19. data: {
  20. session_id: chat_session.session_id,
  21. realname: realname,
  22. },
  23. }
  24. # close session if host is closing it
  25. chat_session.state = 'closed'
  26. chat_session.save
  27. # set state update to all agents
  28. Chat.broadcast_agent_state_update
  29. # send position update to other waiting sessions
  30. Chat.broadcast_customer_state_update
  31. # notify about "leaving"
  32. else
  33. message = {
  34. event: 'chat_session_left',
  35. data: {
  36. session_id: chat_session.session_id,
  37. realname: realname,
  38. },
  39. }
  40. end
  41. chat_session.send_to_recipients(message, @client_id)
  42. # notifiy participients
  43. {
  44. event: 'chat_status_close',
  45. data: {
  46. state: 'ok',
  47. session_id: chat_session.session_id,
  48. },
  49. }
  50. end
  51. end