internal_assets.rb 3.6 KB

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