search_controller.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class SearchController < ApplicationController
  3. prepend_before_action :authentication_check
  4. # GET|POST /api/v1/search
  5. # GET|POST /api/v1/search/:objects
  6. def search_generic
  7. # get params
  8. query = params[:query]
  9. if query.respond_to?(:permit!)
  10. query = query.permit!.to_h
  11. end
  12. limit = params[:limit] || 10
  13. # convert objects string into array of class names
  14. # e.g. user-ticket-another_object = %w( User Ticket AnotherObject )
  15. objects = if params[:objects]
  16. params[:objects].split('-').map(&:camelize)
  17. else
  18. Setting.get('models_searchable')
  19. end
  20. assets = {}
  21. result = []
  22. Service::Search.new(current_user: current_user).execute(
  23. term: query,
  24. objects: objects.map(&:constantize),
  25. options: { limit: limit, ids: params[:ids] },
  26. ).each do |item|
  27. assets = item.assets(assets)
  28. result << {
  29. type: item.class.to_app_model.to_s,
  30. id: item[:id],
  31. }
  32. end
  33. render json: {
  34. assets: assets,
  35. result: result,
  36. }
  37. end
  38. end