ticket_live_user_updates.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2023 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. taskbar_item
  24. .preferences
  25. .fetch(:tasks, [])
  26. .map { |task| transform_single_task(task) }
  27. end
  28. def transform_single_task(task)
  29. {
  30. user: ::User.find_by(id: task[:user_id]),
  31. apps: transform_task_apps(task[:apps]),
  32. }
  33. end
  34. def transform_task_apps(apps)
  35. apps.map do |app, data|
  36. {
  37. name: app,
  38. editing: data[:changed],
  39. last_interaction: data[:last_contact],
  40. }
  41. end
  42. end
  43. end
  44. end