triggers_subscriptions.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Trigger GraphQL subscriptions on ticket changes.
  3. module Taskbar::TriggersSubscriptions
  4. extend ActiveSupport::Concern
  5. included do
  6. attr_accessor :skip_live_user_trigger, :skip_item_trigger
  7. # Sends latest live users list to both mobile and desktop apps.
  8. after_save_commit :trigger_live_user_subscriptions, unless: :skip_live_user_trigger
  9. # Sends changes in taskbars list to desktop app
  10. after_create_commit :trigger_taskbar_item_create_subscriptions, unless: :skip_item_trigger
  11. after_update_commit :trigger_taskbar_item_update_subscriptions, unless: :skip_item_trigger
  12. after_destroy_commit :trigger_taskbar_item_destroy_subscriptions, unless: :skip_item_trigger
  13. # Tells dekstop app that changes are available.
  14. after_update_commit :trigger_taskbar_item_state_update_subscriptions
  15. end
  16. private
  17. def trigger_live_user_subscriptions
  18. return if !saved_change_to_attribute?('preferences')
  19. Gql::Subscriptions::TicketLiveUserUpdates.trigger(
  20. self,
  21. arguments: {
  22. user_id: Gql::ZammadSchema.id_from_internal_id('User', user_id),
  23. key: key,
  24. app: app,
  25. }
  26. )
  27. end
  28. def trigger_taskbar_item_create_subscriptions
  29. return if app != 'desktop'
  30. Gql::Subscriptions::User::Current::TaskbarItemUpdates.trigger_after_create(self)
  31. end
  32. def trigger_taskbar_item_update_subscriptions
  33. return if app != 'desktop'
  34. # See specific subscription for prio changes / list sorting.
  35. # Active attribute is not sent by this subscription.
  36. # Nor are live users, which are most of preferences content.
  37. # However, there is dirty flag in preferences and it is checked separately.
  38. without_saved_changes_keys = %w[active preferences prio last_contact updated_at]
  39. if self.class.taskbar_ignore_state_updates_entities.include?(callback)
  40. without_saved_changes_keys << 'state'
  41. end
  42. return if !saved_change_to_dirty? &&
  43. saved_changes.keys.without(*without_saved_changes_keys).none?
  44. Gql::Subscriptions::User::Current::TaskbarItemUpdates.trigger_after_update(self)
  45. end
  46. def trigger_taskbar_item_destroy_subscriptions
  47. return if app != 'desktop'
  48. Gql::Subscriptions::User::Current::TaskbarItemUpdates.trigger_after_destroy(self)
  49. end
  50. def trigger_taskbar_item_state_update_subscriptions
  51. return if !saved_change_to_attribute?('state')
  52. return if app != 'desktop'
  53. Gql::Subscriptions::User::Current::TaskbarItemStateUpdates.trigger(
  54. nil,
  55. arguments: {
  56. taskbar_item_id: Gql::ZammadSchema.id_from_internal_id('Taskbar', id),
  57. }
  58. )
  59. end
  60. end