chat_leave_job.rb 944 B

12345678910111213141516171819202122232425262728293031323334
  1. class ChatLeaveJob < ApplicationJob
  2. def perform(chat_session_id, client_id, session)
  3. # check if customer has permanently left the conversation
  4. chat_session = Chat::Session.find_by(id: chat_session_id)
  5. return if !chat_session
  6. return if chat_session.recipients_active?
  7. chat_session.state = 'closed'
  8. chat_session.save
  9. realname = 'Anonymous'
  10. # if it is a agent session, use the realname if the agent for close message
  11. if session && session['id'] && chat_session.user_id
  12. agent_user = chat_session.agent_user
  13. if agent_user[:name]
  14. realname = agent_user[:name]
  15. end
  16. end
  17. # notify participants
  18. message = {
  19. event: 'chat_session_left',
  20. data: {
  21. realname: realname,
  22. session_id: chat_session.session_id,
  23. },
  24. }
  25. chat_session.send_to_recipients(message, client_id)
  26. Chat.broadcast_agent_state_update([chat_session.chat_id])
  27. end
  28. end