chat_session_close.rb 1.9 KB

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