collection_update_job.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class CollectionUpdateJob < ApplicationJob
  3. include HasActiveJobLock
  4. def lock_key
  5. # "CollectionUpdateJob/:model"
  6. "#{self.class.name}/#{arguments[0]}"
  7. end
  8. def perform(model)
  9. model = model.safe_constantize
  10. return if model.blank?
  11. assets = {}
  12. all = []
  13. model.reorder(id: :asc).find_each do |record|
  14. assets = record.assets(assets)
  15. all.push record.attributes_with_association_ids
  16. end
  17. return if all.blank?
  18. Sessions.list.each do |client_id, data|
  19. next if client_id.blank?
  20. user_id = data&.dig(:user, 'id')
  21. next if user_id.blank?
  22. # check permission based access
  23. if model.collection_push_permission_value.present?
  24. user = User.lookup(id: user_id)
  25. next if !user&.permissions?(model.collection_push_permission_value)
  26. end
  27. Rails.logger.debug { "push assets for push_collection #{model} for user #{user_id}" }
  28. Sessions.send(client_id, {
  29. data: assets,
  30. event: 'loadAssets',
  31. })
  32. Rails.logger.debug { "push push_collection #{model} for user #{user_id}" }
  33. Sessions.send(client_id, {
  34. event: 'resetCollection',
  35. data: {
  36. model.to_app_model => all,
  37. },
  38. })
  39. end
  40. end
  41. end