ticket_live_user_updates.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Subscriptions
  3. class TicketLiveUserUpdates < BaseSubscription
  4. description 'Updates to ticket live users (for agents).'
  5. argument :user_id, GraphQL::Types::ID, loads: Gql::Types::UserType, description: 'ID of the user to receive updates for'
  6. argument :key, String, description: 'Taskbar key to filter for.'
  7. argument :app, Gql::Types::Enum::TaskbarAppType, description: 'Taskbar app to filter for.'
  8. field :live_users, [Gql::Types::Ticket::LiveUserType], description: 'Current live users from the ticket.'
  9. def authorized?(user:, key:, app:)
  10. context.current_user.permissions?('ticket.agent') && context.current_user == user
  11. end
  12. def subscribe(user:, key:, app:)
  13. response(Taskbar.find_by(key: key, user_id: context.current_user.id, app: app))
  14. end
  15. def update(user:, key:, app:)
  16. response(object)
  17. end
  18. private
  19. def response(taskbar_item)
  20. { live_users: transform_tasks(taskbar_item) }
  21. end
  22. def transform_tasks(taskbar_item)
  23. return [] if taskbar_item.blank?
  24. taskbar_item
  25. .preferences
  26. .fetch(:tasks, [])
  27. .map { |task| transform_single_task(task) }
  28. end
  29. def transform_single_task(task)
  30. {
  31. user: ::User.find_by(id: task[:user_id]),
  32. apps: transform_task_apps(task[:apps]),
  33. }
  34. end
  35. def transform_task_apps(apps)
  36. apps.map do |app, data|
  37. {
  38. name: app,
  39. editing: data[:changed],
  40. last_interaction: data[:last_contact],
  41. }
  42. end
  43. end
  44. end
  45. end