chat_agent_state.rb 959 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sessions::Event::ChatAgentState < Sessions::Event::ChatBase
  3. =begin
  4. a agent triggers its own chat availability state
  5. payload
  6. {
  7. event: 'chat_agent_state',
  8. data: {
  9. active: true, # true|false
  10. },
  11. }
  12. return is sent as message back to peer
  13. =end
  14. def run
  15. return super if super
  16. # check if user has permissions
  17. return if !permission_check('chat.agent', 'chat')
  18. update_state
  19. {
  20. event: 'chat_agent_state',
  21. data: {
  22. state: 'ok',
  23. active: @payload['data']['active'],
  24. },
  25. }
  26. end
  27. private
  28. def update_state
  29. chat_user = User.lookup(id: @session['id'])
  30. return if !Chat::Agent.state(@session['id'], @payload['data']['active'])
  31. chat_ids = Chat.agent_active_chat_ids(chat_user)
  32. # broadcast new state to agents
  33. Chat.broadcast_agent_state_update(chat_ids, @session['id'])
  34. end
  35. end