search_controller.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::SearchController < ApplicationController
  3. skip_before_action :verify_csrf_token
  4. prepend_before_action :authentication_check_only
  5. include KnowledgeBaseHelper
  6. include ActionView::Helpers::SanitizeHelper
  7. include CanPaginate
  8. # POST /api/v1/knowledge_bases/search
  9. # knowledge_base_id, locale, flavor, index, page, per_page, limit, include_locale
  10. def search
  11. knowledge_base = KnowledgeBase
  12. .active
  13. .find_by id: params[:knowledge_base_id]
  14. kb_locale = knowledge_base
  15. &.kb_locales
  16. &.joins(:system_locale)
  17. &.find_by(locales: { locale: params[:locale] })
  18. scope = knowledge_base
  19. &.categories
  20. &.find_by(id: params[:scope_id])
  21. search_backend = SearchKnowledgeBaseBackend.new(
  22. knowledge_base: knowledge_base,
  23. locale: kb_locale,
  24. scope: scope,
  25. flavor: params[:flavor],
  26. index: params[:index],
  27. limit: params[:limit],
  28. highlight_enabled: params[:highlight_enabled]
  29. )
  30. include_locale = params[:include_locale] && KnowledgeBase.with_multiple_locales_exists?
  31. result = search_backend.search params[:query], user: current_user, pagination: pagination
  32. if (exclude_ids = params[:exclude_ids]&.map(&:to_i))
  33. result.reject! { |meta| meta[:type] == params[:index] && exclude_ids.include?(meta[:id]) }
  34. end
  35. details = result.map { |item| public_item_details(item, include_locale) }
  36. render json: {
  37. result: result,
  38. details: details,
  39. }
  40. end
  41. private
  42. def item_assets(meta)
  43. object = meta[:type].constantize.find(meta[:id])
  44. object.assets
  45. end
  46. def public_item_details(meta, include_locale)
  47. object = meta[:type].constantize.find(meta[:id])
  48. output = case object
  49. when KnowledgeBase::Answer::Translation
  50. public_item_details_answer(meta, object)
  51. when KnowledgeBase::Category::Translation
  52. public_item_details_category(meta, object)
  53. when KnowledgeBase::Translation
  54. public_item_details_base(meta, object)
  55. end
  56. if include_locale && (system_locale = object.kb_locale.system_locale)
  57. output[:title] += " (#{system_locale.locale.upcase})"
  58. end
  59. output
  60. end
  61. def public_item_details_answer(meta, object)
  62. category_translation = object.answer.category.translation_preferred(object.kb_locale)
  63. path = help_answer_path(category_translation, object, locale: object.kb_locale.system_locale.locale)
  64. subtitle = object.answer.category.self_with_parents.map { |c| strip_tags(c.translation_preferred(object.kb_locale).title) }.reverse
  65. subtitle = if subtitle.count <= 2
  66. subtitle.join(' > ')
  67. else
  68. subtitle.values_at(0, -1).join(' > .. > ')
  69. end
  70. url = case url_type
  71. when :public
  72. custom_path_if_needed(path, object.answer.category.knowledge_base)
  73. when :agent
  74. knowledge_base_answer_path(object.answer.category.knowledge_base, object.answer) + "?include_contents=#{object.id}"
  75. end
  76. {
  77. id: object.id,
  78. type: object.class.name,
  79. icon: 'knowledge-base-answer',
  80. date: object.updated_at,
  81. url: url,
  82. title: meta.dig(:highlight, 'title')&.first || object.title,
  83. subtitle: subtitle,
  84. body: meta.dig(:highlight, 'content.body')&.first || strip_tags(object.content.body).truncate(100),
  85. tags: object.answer.tag_list
  86. }
  87. end
  88. def public_item_details_category(meta, object)
  89. parent_category_translation = object.category.parent&.translation_preferred(object.kb_locale)
  90. path = help_category_path(object, locale: object.kb_locale.system_locale.locale)
  91. url = case url_type
  92. when :public
  93. custom_path_if_needed(path, object.category.knowledge_base)
  94. when :agent
  95. knowledge_base_category_path(object.category.knowledge_base, object.category)
  96. end
  97. {
  98. id: object.id,
  99. type: object.class.name,
  100. fontName: object.category.knowledge_base.iconset,
  101. date: object.updated_at,
  102. url: url,
  103. icon: object.category.category_icon,
  104. subtitle: strip_tags(parent_category_translation&.title.presence),
  105. title: meta.dig(:highlight, 'title')&.first || strip_tags(object.title)
  106. }
  107. end
  108. def public_item_details_base(meta, object)
  109. path = help_root_path(object.kb_locale.system_locale.locale)
  110. url = case url_type
  111. when :public
  112. custom_path_if_needed(path, object.knowledge_base)
  113. when :agent
  114. knowledge_base_path(object.knowledge_base)
  115. end
  116. {
  117. id: object.id,
  118. type: object.class.name,
  119. icon: 'knowledge-base',
  120. date: object.updated_at,
  121. url: url,
  122. title: meta.dig(:highlight, 'title')&.first || strip_tags(object.title)
  123. }
  124. end
  125. def url_type
  126. params[:url_type]&.to_sym || :public
  127. end
  128. end