123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- class KnowledgeBase
- class InternalAssets
- CategoriesCache = Struct.new(:editor, :reader, :public_reader, keyword_init: true) do
- def visible
- editor + reader + public_reader
- end
- def internally_visible
- editor + reader
- end
- end
- attr_reader :assets
- def initialize(user, categories_filter: [], answer_translation_content_ids: [])
- @user = user
- @assets = {}
- @categories_filter = categories_filter
- @answer_translation_content_ids = answer_translation_content_ids
- end
- def collect_assets
- collect_base_assets
- add_to_assets accessible_categories.visible, type: :essential
- add_to_assets KnowledgeBase::Category::Translation.where(category: accessible_categories.visible)
- collect_all_answer_assets
- @assets
- end
- def accessible_categories
- @accessible_categories ||= accessible_categories_calculate
- end
- def all_answer_ids
- all_answer_batches.each_with_object([]) do |elem, sum|
- sum.concat elem.pluck(:id)
- end
- end
- def all_category_ids
- accessible_categories.visible.pluck(:id)
- end
- def visible_ids
- {
- answer_ids: all_answer_ids,
- category_ids: all_category_ids
- }
- end
- private
- def accessible_categories_calculate
- struct = CategoriesCache.new editor: [], reader: [], public_reader: []
- accessible_categories_calculate_scope.each do |group|
- group.each do |cat|
- accessible_categories_calculate_group(struct, cat)
- end
- end
- struct
- end
- def accessible_categories_calculate_scope
- return KnowledgeBase::Category.find_in_batches if @categories_filter.blank?
- Array(@categories_filter)
- .map(&:self_with_children)
- .each
- end
- def accessible_categories_calculate_group(struct, category)
- case KnowledgeBase::EffectivePermission.new(@user, category).access_effective
- when 'editor'
- struct.editor << category
- when 'reader'
- struct.reader << category if category.internal_content?
- when 'public_reader'
- struct.public_reader << category if category.public_content?
- end
- end
- def add_to_assets(objects, type: nil)
- @assets = ApplicationModel::CanAssets.reduce(objects, @assets, type)
- end
- def collect_base_assets
- [KnowledgeBase, KnowledgeBase::Translation, KnowledgeBase::Locale]
- .each do |klass|
- klass.find_in_batches do |group|
- add_to_assets group, type: :essential
- end
- end
- end
- def all_answer_batches
- [
- KnowledgeBase::Answer.where(category: accessible_categories.editor),
- KnowledgeBase::Answer.internal.where(category: accessible_categories.reader),
- KnowledgeBase::Answer.published.where(category: accessible_categories.public_reader)
- ]
- end
- def collect_all_answer_assets
- all_answer_batches.each do |batch|
- collect_answers_assets batch
- end
- end
- def collect_answers_assets(scope)
- scope.find_in_batches do |group|
- add_to_assets group, type: :essential
- translations = KnowledgeBase::Answer::Translation.where(answer: group)
- add_to_assets translations, type: :essential
- if @answer_translation_content_ids.present?
- contents = KnowledgeBase::Answer::Translation::Content
- .joins(:translation)
- .where(
- id: @answer_translation_content_ids,
- knowledge_base_answer_translations: { answer_id: group }
- )
- add_to_assets contents
- end
- end
- end
- end
- end
|