2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
class ChecksKbClientNotificationJob < ApplicationJob
|
2019-11-13 07:03:47 +00:00
|
|
|
include HasActiveJobLock
|
|
|
|
|
|
|
|
def lock_key
|
|
|
|
# "ChecksKbClientNotificationJob/KnowledgeBase::Answer/42/destroy"
|
|
|
|
"#{self.class.name}/#{arguments[0]}/#{arguments[1]}/#{arguments[2]}"
|
|
|
|
end
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
def perform(klass_name, id, event)
|
|
|
|
object = klass_name.constantize.find_by(id: id)
|
|
|
|
return if object.blank?
|
|
|
|
|
|
|
|
level = needs_editor?(object) ? 'editor' : '*'
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
event: 'kb_data_changed',
|
|
|
|
data: build_data(object, event)
|
|
|
|
}
|
|
|
|
|
|
|
|
users_for(level).each { |user| notify(user, payload) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_data(object, event)
|
|
|
|
timestamp = event == :destroy ? Time.zone.now : object.updated_at
|
|
|
|
url = event == :destroy ? nil : object.api_url
|
|
|
|
|
|
|
|
{
|
|
|
|
class: object.class.to_s,
|
|
|
|
event: event,
|
|
|
|
id: object.id,
|
|
|
|
timestamp: timestamp,
|
|
|
|
url: url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def needs_editor?(object)
|
|
|
|
case object
|
|
|
|
when KnowledgeBase::Answer
|
|
|
|
object.can_be_published_aasm.draft?
|
|
|
|
when KnowledgeBase::Category
|
|
|
|
!object.internal_content?
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify(user, payload)
|
|
|
|
PushMessages.send_to(user.id, payload)
|
|
|
|
end
|
|
|
|
|
|
|
|
def users_for(permission_suffix)
|
|
|
|
Sessions
|
|
|
|
.sessions
|
|
|
|
.map { |client_id| Sessions.get(client_id)&.dig(:user, 'id') }
|
|
|
|
.compact
|
2020-06-22 09:57:45 +00:00
|
|
|
.map { |user_id| User.find_by(id: user_id) }
|
2019-06-04 03:40:48 +00:00
|
|
|
.compact
|
2020-06-22 09:57:45 +00:00
|
|
|
.select { |user| user.permissions? "knowledge_base.#{permission_suffix}" }
|
2019-06-04 03:40:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.notify_later(object, event)
|
|
|
|
perform_later(object.class.to_s, object.id, event.to_s)
|
|
|
|
end
|
|
|
|
end
|