background_job.rb 877 B

1234567891011121314151617181920212223242526272829303132333435
  1. class Observer::Chat::Leave::BackgroundJob
  2. def initialize(chat_session_id, client_id, session)
  3. @chat_session_id = chat_session_id
  4. @client_id = client_id
  5. @session = session
  6. end
  7. def perform
  8. # check if customer has permanently left the conversation
  9. chat_session = Chat::Session.find_by(id: @chat_session_id)
  10. return if !chat_session
  11. return if chat_session.recipients_active?
  12. chat_session.state = 'closed'
  13. chat_session.save
  14. realname = 'Anonymous'
  15. if @session && @session['id']
  16. realname = User.lookup(id: @session['id']).fullname
  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
  28. end
  29. end