background_job.rb 896 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # encoding: utf-8
  2. class Observer::Chat::Leave::BackgroundJob
  3. def initialize(chat_session_id, client_id, session)
  4. @chat_session_id = chat_session_id
  5. @client_id = client_id
  6. @session = session
  7. end
  8. def perform
  9. # check if customer has permanently left the conversation
  10. chat_session = Chat::Session.find_by(id: @chat_session_id)
  11. return if !chat_session
  12. return if chat_session.recipients_active?
  13. chat_session.state = 'closed'
  14. chat_session.save
  15. realname = 'Anonymous'
  16. if @session && @session['id']
  17. realname = User.lookup(id: @session['id']).fullname
  18. end
  19. # notify participants
  20. message = {
  21. event: 'chat_session_left',
  22. data: {
  23. realname: realname,
  24. session_id: chat_session.session_id,
  25. },
  26. }
  27. chat_session.send_to_recipients(message, @client_id)
  28. Chat.broadcast_agent_state_update
  29. end
  30. end