search.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::Answer::Translation
  3. module Search
  4. extend ActiveSupport::Concern
  5. include CanSelector
  6. include CanSearch
  7. included do
  8. scope :search_sql_extension, lambda { |params|
  9. return if params[:current_user]&.permissions?('knowledge_base.editor')
  10. answer_ids = KnowledgeBase::Answer.internal.pluck(:id)
  11. where(answer_id: answer_ids)
  12. }
  13. scope :search_sql_query_extension, lambda { |params|
  14. return if params[:query].blank?
  15. search_sql_text_fallback(params[:query])
  16. }
  17. end
  18. class_methods do
  19. def search_preferences(current_user)
  20. return false if !KnowledgeBase.exists? || !current_user.permissions?('knowledge_base.*')
  21. {
  22. prio: 1209,
  23. direct_search_index: false,
  24. }
  25. end
  26. def search_query_extension(params)
  27. kb_locales = KnowledgeBase.active.map { |elem| KnowledgeBase::Locale.preferred(params[:current_user], elem) }
  28. output = { bool: { filter: { terms: { kb_locale_id: kb_locales.map(&:id) } } } }
  29. return output if params[:current_user]&.permissions?('knowledge_base.editor')
  30. output[:bool][:must] = [ { terms: {
  31. answer_id: KnowledgeBase::Answer.internal.pluck(:id)
  32. } } ]
  33. output
  34. end
  35. end
  36. end
  37. end