2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
class ChecksKbClientNotificationJob < ApplicationJob
|
2019-11-13 07:03:47 +00:00
|
|
|
include HasActiveJobLock
|
|
|
|
|
|
|
|
def lock_key
|
2022-02-24 11:15:19 +00:00
|
|
|
# "ChecksKbClientNotificationJob/KnowledgeBase::Answer/42"
|
|
|
|
"#{self.class.name}/#{arguments[0]}/#{arguments[1]}"
|
2019-11-13 07:03:47 +00:00
|
|
|
end
|
|
|
|
|
2022-02-24 11:15:19 +00:00
|
|
|
def perform(klass_name, object_id)
|
|
|
|
object = klass_name.constantize.find_by(id: object_id)
|
2019-06-04 03:40:48 +00:00
|
|
|
return if object.blank?
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
event: 'kb_data_changed',
|
2022-04-01 09:41:19 +00:00
|
|
|
data: build_data(object)
|
2019-06-04 03:40:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 09:41:19 +00:00
|
|
|
active_users.each do |user|
|
|
|
|
notify(user, object, payload)
|
|
|
|
end
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
|
2022-04-01 09:41:19 +00:00
|
|
|
def build_data(object)
|
2019-06-04 03:40:48 +00:00
|
|
|
{
|
2022-04-01 09:41:19 +00:00
|
|
|
class: object.class.name,
|
2019-06-04 03:40:48 +00:00
|
|
|
id: object.id,
|
2022-04-01 09:41:19 +00:00
|
|
|
timestamp: object.updated_at,
|
|
|
|
url: object.try(:api_url)
|
2019-06-04 03:40:48 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-04-01 09:41:19 +00:00
|
|
|
def notify(user, object, payload)
|
|
|
|
return if !user.permissions? 'knowledge_base.*'
|
|
|
|
|
|
|
|
Pundit.authorize user, object, :show?
|
2019-06-04 03:40:48 +00:00
|
|
|
|
|
|
|
PushMessages.send_to(user.id, payload)
|
2022-04-01 09:41:19 +00:00
|
|
|
rescue Pundit::NotAuthorizedError
|
|
|
|
# do nothing if user is not authorized to access
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
|
2022-04-01 09:41:19 +00:00
|
|
|
def active_users
|
2019-06-04 03:40:48 +00:00
|
|
|
Sessions
|
|
|
|
.sessions
|
2021-06-24 07:05:39 +00:00
|
|
|
.filter_map { |client_id| Sessions.get(client_id)&.dig(:user, 'id') }
|
|
|
|
.filter_map { |user_id| User.find_by(id: user_id) }
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
end
|