checks_client_notification.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module ChecksClientNotification
  3. extend ActiveSupport::Concern
  4. included do
  5. after_create :notify_clients_after_create
  6. after_update :notify_clients_after_update
  7. after_touch :notify_clients_after_touch
  8. after_destroy :notify_clients_after_destroy
  9. end
  10. def notify_clients_data(event)
  11. class_name = self.class.name.gsub(%r{::}, '')
  12. {
  13. message: {
  14. event: "#{class_name}:#{event}",
  15. data: notify_clients_data_attributes
  16. },
  17. type: client_notification_send_type,
  18. }
  19. end
  20. def notify_clients_data_attributes
  21. {
  22. id: id,
  23. updated_at: updated_at
  24. }
  25. end
  26. def notify_clients_send(data)
  27. return notify_clients_send_to(data[:message]) if client_notification_send_to.present?
  28. PushMessages.send(data)
  29. end
  30. def notify_clients_send_to(data)
  31. client_notification_send_to.each do |user_id|
  32. PushMessages.send_to(send(user_id), data)
  33. end
  34. end
  35. def notify_clients_after_create
  36. # return if we run import mode
  37. return if Setting.get('import_mode')
  38. # skip if ignored
  39. return if client_notification_events_ignored.include?(:create)
  40. logger.debug { "#{self.class.name}.find(#{id}) notify created #{created_at}" }
  41. data = notify_clients_data(:create)
  42. notify_clients_send(data)
  43. end
  44. def notify_clients_after_update
  45. # return if we run import mode
  46. return if Setting.get('import_mode')
  47. # skip if ignored
  48. return if client_notification_events_ignored.include?(:update)
  49. logger.debug { "#{self.class.name}.find(#{id}) notify UPDATED #{updated_at}" }
  50. data = notify_clients_data(:update)
  51. notify_clients_send(data)
  52. end
  53. def notify_clients_after_touch
  54. # return if we run import mode
  55. return if Setting.get('import_mode')
  56. # skip if ignored
  57. return if client_notification_events_ignored.include?(:touch)
  58. logger.debug { "#{self.class.name}.find(#{id}) notify TOUCH #{updated_at}" }
  59. data = notify_clients_data(:touch)
  60. notify_clients_send(data)
  61. end
  62. def notify_clients_after_destroy
  63. # return if we run import mode
  64. return if Setting.get('import_mode')
  65. # skip if ignored
  66. return if client_notification_events_ignored.include?(:destroy)
  67. logger.debug { "#{self.class.name}.find(#{id}) notify DESTOY #{updated_at}" }
  68. data = notify_clients_data(:destroy)
  69. notify_clients_send(data)
  70. end
  71. private
  72. def client_notification_events_ignored
  73. @client_notification_events_ignored ||= self.class.instance_variable_get(:@client_notification_events_ignored) || []
  74. end
  75. def client_notification_send_to
  76. @client_notification_send_to ||= self.class.instance_variable_get(:@client_notification_send_to) || []
  77. end
  78. def client_notification_send_type
  79. @client_notification_send_type ||= self.class.instance_variable_get(:@client_notification_send_type) || 'authenticated'
  80. end
  81. # methods defined here are going to extend the class, not the instance of it
  82. class_methods do
  83. # serve method to ignore events
  84. #
  85. # @example
  86. # class Model < ApplicationModel
  87. # include ChecksClientNotification
  88. # client_notification_events_ignored :create, :update, :touch
  89. # end
  90. def client_notification_events_ignored(*attributes)
  91. @client_notification_events_ignored ||= []
  92. @client_notification_events_ignored |= attributes
  93. end
  94. # serve method to define recipient user ids
  95. #
  96. # @example
  97. # class Model < ApplicationModel
  98. # include ChecksClientNotification
  99. # client_notification_send_to :user_id
  100. # end
  101. def client_notification_send_to(*attributes)
  102. @client_notification_send_to ||= []
  103. @client_notification_send_to |= attributes
  104. end
  105. def client_notification_send_type(type)
  106. @client_notification_send_type = type
  107. end
  108. end
  109. end