checks_kb_client_notification_job.rb 1.2 KB

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