triggers_subscriptions.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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_trigger
  7. after_commit :trigger_live_user_subscriptions
  8. after_create_commit :trigger_taskbar_item_create_subscriptions, unless: :skip_trigger
  9. after_update_commit :trigger_taskbar_item_update_subscriptions, unless: :skip_trigger
  10. after_destroy_commit :trigger_taskbar_item_destroy_subscriptions, unless: :skip_trigger
  11. after_update_commit :trigger_taskbar_item_state_update_subscriptions
  12. end
  13. private
  14. def trigger_live_user_subscriptions
  15. return true if !saved_change_to_attribute?('preferences')
  16. # For now it's only needed in the mobile view context.
  17. return true if !app.eql?('mobile') && !persisted?
  18. Gql::Subscriptions::TicketLiveUserUpdates.trigger(
  19. self,
  20. arguments: {
  21. user_id: Gql::ZammadSchema.id_from_internal_id('User', user_id),
  22. key: key,
  23. app: app,
  24. }
  25. )
  26. end
  27. def trigger_taskbar_item_create_subscriptions
  28. Gql::Subscriptions::User::Current::TaskbarItemUpdates.trigger_after_create(self)
  29. end
  30. def trigger_taskbar_item_update_subscriptions
  31. # See specific subscription for prio changes / list sorting.
  32. return true if saved_change_to_attribute?('prio')
  33. Gql::Subscriptions::User::Current::TaskbarItemUpdates.trigger_after_update(self)
  34. end
  35. def trigger_taskbar_item_destroy_subscriptions
  36. Gql::Subscriptions::User::Current::TaskbarItemUpdates.trigger_after_destroy(self)
  37. end
  38. def trigger_taskbar_item_state_update_subscriptions
  39. return true if !saved_change_to_attribute?('state')
  40. return true if !app.eql?('desktop')
  41. return true if destroyed?
  42. Gql::Subscriptions::User::Current::TaskbarItemStateUpdates.trigger(
  43. nil,
  44. arguments: {
  45. taskbar_item_id: Gql::ZammadSchema.id_from_internal_id('Taskbar', id),
  46. }
  47. )
  48. end
  49. end