ticket_live_user_updates.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.'
  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 == 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) # rubocop:disable Metrics/AbcSize
  23. tasks = taskbar_item.preferences[:tasks]
  24. return [] if tasks.blank?
  25. tasks = tasks.reject { |task| task[:user_id].eql?(context.current_user.id) }
  26. return [] if tasks.blank?
  27. tasks.map do |task|
  28. app_item = task[:apps].values.min_by { |app| app[:last_contact] }
  29. {
  30. user: ::User.find_by(id: task[:user_id]),
  31. editing: app_item[:changed],
  32. last_interaction: app_item[:last_contact],
  33. apps: task[:apps].keys,
  34. }
  35. end
  36. end
  37. end
  38. end