feeds_controller.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::FeedsController < ApplicationController
  3. ITEMS_IN_FEED = 10
  4. prepend_before_action { authorize! }
  5. prepend_before_action -> { authentication_check_only }, only: %i[root category]
  6. before_action :ensure_response_format
  7. before_action :fetch_knowledge_base
  8. helper_method :build_original_url, :publishing_date, :updating_date
  9. def root
  10. scope = if KnowledgeBase.granular_permissions?
  11. granular_scope
  12. else
  13. @knowledge_base.answers
  14. end
  15. @answers = scope
  16. .localed(@locale)
  17. .sorted_by_internally_published
  18. .limit(ITEMS_IN_FEED)
  19. render :feed
  20. end
  21. def category
  22. @category = KnowledgeBase::Category.where(knowledge_base_id: @knowledge_base.id, id: params[:id]).first
  23. scope = if KnowledgeBase.granular_permissions?
  24. granular_scope [@category]
  25. else
  26. @category.self_with_children_answers
  27. end
  28. @answers = scope
  29. .localed(@locale)
  30. .sorted_by_internally_published
  31. .limit(ITEMS_IN_FEED)
  32. render :feed
  33. end
  34. private
  35. def granular_scope(category_filter = [])
  36. categories = KnowledgeBase::InternalAssets
  37. .new(effective_user, categories_filter: category_filter)
  38. .accessible_categories
  39. if categories.public_reader.none?
  40. return @knowledge_base.answers.where(category_id: categories.visible)
  41. end
  42. answer_ids = granular_public_answers_ids(categories) + granular_internal_answers_ids(categories)
  43. @knowledge_base.answers.where(id: answer_ids)
  44. end
  45. def granular_public_answers_ids(categories)
  46. @knowledge_base.answers
  47. .where(category_id: categories.public_reader)
  48. .sorted_by_published
  49. .limit(ITEMS_IN_FEED)
  50. .pluck(:id)
  51. end
  52. def granular_internal_answers_ids(categories)
  53. @knowledge_base.answers
  54. .where(category_id: categories.internally_visible)
  55. .sorted_by_internally_published
  56. .limit(ITEMS_IN_FEED)
  57. .pluck(:id)
  58. end
  59. def effective_user
  60. return current_user if current_user.present?
  61. Token.check(action: 'KnowledgeBaseFeed', token: params[:token])
  62. end
  63. def ensure_response_format
  64. request.format = :atom
  65. end
  66. def fetch_knowledge_base
  67. @knowledge_base = KnowledgeBase.find params[:knowledge_base_id] || params[:id]
  68. @locale = Locale.find_by(locale: params[:locale])
  69. end
  70. def build_original_url(answer)
  71. translation = answer.translations.first
  72. help_answer_url(answer.category, translation, locale: translation.kb_locale.system_locale.locale)
  73. end
  74. def publishing_date(answer)
  75. [answer.published_at, answer.internal_at].compact.min
  76. end
  77. def updating_date(answer)
  78. [[answer.published_at, answer.internal_at].compact.min, answer.updated_at].compact.max
  79. end
  80. end