feeds_controller.rb 2.8 KB

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