forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks_kb_client_notification_job.rb
50 lines (40 loc) · 1.21 KB
/
checks_kb_client_notification_job.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
class ChecksKbClientNotificationJob < ApplicationJob
include HasActiveJobLock
def lock_key
# "ChecksKbClientNotificationJob/KnowledgeBase::Answer/42"
"#{self.class.name}/#{arguments[0]}/#{arguments[1]}"
end
def perform(klass_name, object_id)
object = klass_name.constantize.find_by(id: object_id)
return if object.blank?
payload = {
event: 'kb_data_changed',
data: build_data(object)
}
active_users.each do |user|
notify(user, object, payload)
end
end
def build_data(object)
{
class: object.class.name,
id: object.id,
timestamp: object.updated_at,
url: object.try(:api_url)
}
end
def notify(user, object, payload)
return if !user.permissions? 'knowledge_base.*'
Pundit.authorize user, object, :show?
PushMessages.send_to(user.id, payload)
rescue Pundit::NotAuthorizedError
# do nothing if user is not authorized to access
end
def active_users
Sessions
.sessions
.filter_map { |client_id| Sessions.get(client_id)&.dig(:user, 'id') }
.filter_map { |user_id| User.find_by(id: user_id) }
end
end