internal_assets.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase
  3. class InternalAssets
  4. CategoriesCache = Struct.new(:editor, :reader, :public_reader, keyword_init: true) do
  5. def visible
  6. editor + reader + public_reader
  7. end
  8. def internally_visible
  9. editor + reader
  10. end
  11. end
  12. attr_reader :assets
  13. def initialize(user, categories_filter: [], answer_translation_content_ids: [])
  14. @user = user
  15. @assets = {}
  16. @categories_filter = categories_filter
  17. @answer_translation_content_ids = answer_translation_content_ids
  18. end
  19. def collect_assets
  20. collect_base_assets
  21. add_to_assets accessible_categories.visible, type: :essential
  22. add_to_assets KnowledgeBase::Category::Translation.where(category: accessible_categories.visible)
  23. collect_all_answer_assets
  24. @assets
  25. end
  26. def accessible_categories
  27. @accessible_categories ||= accessible_categories_calculate
  28. end
  29. def all_answer_ids
  30. all_answer_batches.each_with_object([]) do |elem, sum|
  31. sum.concat elem.pluck(:id)
  32. end
  33. end
  34. def all_category_ids
  35. accessible_categories.visible.pluck(:id)
  36. end
  37. def visible_ids
  38. {
  39. answer_ids: all_answer_ids,
  40. category_ids: all_category_ids
  41. }
  42. end
  43. private
  44. def accessible_categories_calculate
  45. struct = CategoriesCache.new editor: [], reader: [], public_reader: []
  46. accessible_categories_calculate_scope.each do |group|
  47. group.each do |cat|
  48. accessible_categories_calculate_group(struct, cat)
  49. end
  50. end
  51. struct
  52. end
  53. def accessible_categories_calculate_scope
  54. return KnowledgeBase::Category.find_in_batches if @categories_filter.blank?
  55. Array(@categories_filter)
  56. .map(&:self_with_children)
  57. .each
  58. end
  59. def accessible_categories_calculate_group(struct, category)
  60. case KnowledgeBase::EffectivePermission.new(@user, category).access_effective
  61. when 'editor'
  62. struct.editor << category
  63. when 'reader'
  64. struct.reader << category if category.internal_content?
  65. when 'public_reader'
  66. struct.public_reader << category if category.public_content?
  67. end
  68. end
  69. def add_to_assets(objects, type: nil)
  70. @assets = ApplicationModel::CanAssets.reduce(objects, @assets, type)
  71. end
  72. def collect_base_assets
  73. [KnowledgeBase, KnowledgeBase::Translation, KnowledgeBase::Locale]
  74. .each do |klass|
  75. klass.find_in_batches do |group|
  76. add_to_assets group, type: :essential
  77. end
  78. end
  79. end
  80. def all_answer_batches
  81. [
  82. KnowledgeBase::Answer.where(category: accessible_categories.editor),
  83. KnowledgeBase::Answer.internal.where(category: accessible_categories.reader),
  84. KnowledgeBase::Answer.published.where(category: accessible_categories.public_reader)
  85. ]
  86. end
  87. def collect_all_answer_assets
  88. all_answer_batches.each do |batch|
  89. collect_answers_assets batch
  90. end
  91. end
  92. def collect_answers_assets(scope)
  93. scope.find_in_batches do |group|
  94. add_to_assets group, type: :essential
  95. translations = KnowledgeBase::Answer::Translation.where(answer: group)
  96. add_to_assets translations, type: :essential
  97. if @answer_translation_content_ids.present?
  98. contents = KnowledgeBase::Answer::Translation::Content
  99. .joins(:translation)
  100. .where(
  101. id: @answer_translation_content_ids,
  102. knowledge_base_answer_translations: { answer_id: group }
  103. )
  104. add_to_assets contents
  105. end
  106. end
  107. end
  108. end
  109. end