chat_leave_job.rb 1021 B

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