2017-01-31 17:13:45 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2020-09-08 15:06:23 +00:00
|
|
|
|
2017-05-02 15:21:13 +00:00
|
|
|
module ChecksClientNotification
|
2017-01-31 17:13:45 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
after_create :notify_clients_after_create
|
|
|
|
after_update :notify_clients_after_update
|
|
|
|
after_touch :notify_clients_after_touch
|
|
|
|
after_destroy :notify_clients_after_destroy
|
|
|
|
end
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
def notify_clients_data(event)
|
2017-01-31 17:13:45 +00:00
|
|
|
class_name = self.class.name
|
|
|
|
class_name.gsub!(/::/, '')
|
2020-09-08 15:06:23 +00:00
|
|
|
|
|
|
|
{
|
2017-01-31 17:13:45 +00:00
|
|
|
message: {
|
2020-09-08 15:06:23 +00:00
|
|
|
event: "#{class_name}:#{event}",
|
2018-12-19 17:31:51 +00:00
|
|
|
data: { id: id, updated_at: updated_at }
|
2017-01-31 17:13:45 +00:00
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
type: 'authenticated',
|
2020-09-08 15:06:23 +00:00
|
|
|
}
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
def notify_clients_send(data)
|
|
|
|
return notify_clients_send_to(data[:message]) if client_notification_send_to.present?
|
|
|
|
|
|
|
|
PushMessages.send(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_clients_send_to(data)
|
|
|
|
client_notification_send_to.each do |user_id|
|
|
|
|
PushMessages.send_to(send(user_id), data)
|
|
|
|
end
|
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
def notify_clients_after_create
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
# skip if ignored
|
|
|
|
return if client_notification_events_ignored.include?(:create)
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
logger.debug { "#{self.class.name}.find(#{id}) notify created #{created_at}" }
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
data = notify_clients_data(:create)
|
|
|
|
notify_clients_send(data)
|
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
def notify_clients_after_update
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
# skip if ignored
|
|
|
|
return if client_notification_events_ignored.include?(:update)
|
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
logger.debug { "#{self.class.name}.find(#{id}) notify UPDATED #{updated_at}" }
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
data = notify_clients_data(:update)
|
|
|
|
notify_clients_send(data)
|
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
def notify_clients_after_touch
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
# skip if ignored
|
|
|
|
return if client_notification_events_ignored.include?(:touch)
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
logger.debug { "#{self.class.name}.find(#{id}) notify TOUCH #{updated_at}" }
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
data = notify_clients_data(:touch)
|
|
|
|
notify_clients_send(data)
|
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
def notify_clients_after_destroy
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
# skip if ignored
|
|
|
|
return if client_notification_events_ignored.include?(:destroy)
|
|
|
|
|
|
|
|
logger.debug { "#{self.class.name}.find(#{id}) notify DESTOY #{updated_at}" }
|
|
|
|
|
|
|
|
data = notify_clients_data(:destroy)
|
|
|
|
notify_clients_send(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def client_notification_events_ignored
|
|
|
|
@client_notification_events_ignored ||= self.class.instance_variable_get(:@client_notification_events_ignored) || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_notification_send_to
|
|
|
|
@client_notification_send_to ||= self.class.instance_variable_get(:@client_notification_send_to) || []
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
# methods defined here are going to extend the class, not the instance of it
|
|
|
|
class_methods do
|
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
=begin
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
serve method to ignore events
|
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
include ChecksClientNotification
|
|
|
|
client_notification_events_ignored :create, :update, :touch
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def client_notification_events_ignored(*attributes)
|
|
|
|
@client_notification_events_ignored ||= []
|
|
|
|
@client_notification_events_ignored |= attributes
|
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
=begin
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
serve method to define recipient user ids
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
class Model < ApplicationModel
|
|
|
|
include ChecksClientNotification
|
|
|
|
client_notification_send_to :user_id
|
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
def client_notification_send_to(*attributes)
|
|
|
|
@client_notification_send_to ||= []
|
|
|
|
@client_notification_send_to |= attributes
|
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
2020-09-08 15:06:23 +00:00
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|