triggers_subscriptions.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. return if !saved_change_to_dirty? &&
  39. saved_changes.keys.without('active', 'preferences', 'prio', 'last_contact', 'updated_at').none?
  40. Gql::Subscriptions::User::Current::TaskbarItemUpdates.trigger_after_update(self)
  41. end
  42. def trigger_taskbar_item_destroy_subscriptions
  43. return if app != 'desktop'
  44. Gql::Subscriptions::User::Current::TaskbarItemUpdates.trigger_after_destroy(self)
  45. end
  46. def trigger_taskbar_item_state_update_subscriptions
  47. return if !saved_change_to_attribute?('state')
  48. return if app != 'desktop'
  49. Gql::Subscriptions::User::Current::TaskbarItemStateUpdates.trigger(
  50. nil,
  51. arguments: {
  52. taskbar_item_id: Gql::ZammadSchema.id_from_internal_id('Taskbar', id),
  53. }
  54. )
  55. end
  56. end