2023-07-27 16:11:17 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::Peers::SearchController < Api::BaseController
|
|
|
|
before_action :require_enabled_api!
|
|
|
|
before_action :set_domains
|
|
|
|
|
2023-08-02 19:32:48 +02:00
|
|
|
skip_before_action :require_authenticated_user!, unless: :limited_federation_mode?
|
2023-07-27 16:11:17 +02:00
|
|
|
skip_around_action :set_locale
|
|
|
|
|
|
|
|
vary_by ''
|
|
|
|
|
|
|
|
def index
|
|
|
|
cache_even_if_authenticated!
|
|
|
|
render json: @domains
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def require_enabled_api!
|
2023-08-02 19:32:48 +02:00
|
|
|
head 404 unless Setting.peers_api_enabled && !limited_federation_mode?
|
2023-07-27 16:11:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_domains
|
|
|
|
return if params[:q].blank?
|
|
|
|
|
|
|
|
if Chewy.enabled?
|
|
|
|
@domains = InstancesIndex.query(function_score: {
|
|
|
|
query: {
|
|
|
|
prefix: {
|
2024-01-18 16:57:10 +01:00
|
|
|
domain: normalized_domain,
|
2023-07-27 16:11:17 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
field_value_factor: {
|
|
|
|
field: 'accounts_count',
|
|
|
|
modifier: 'log2p',
|
|
|
|
},
|
|
|
|
}).limit(10).pluck(:domain)
|
|
|
|
else
|
2024-01-18 16:57:10 +01:00
|
|
|
domain = normalized_domain
|
|
|
|
@domains = Instance.searchable.domain_starts_with(domain).limit(10).pluck(:domain)
|
2023-07-27 16:11:17 +02:00
|
|
|
end
|
2023-09-05 23:49:48 +02:00
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
@domains = []
|
2023-07-27 16:11:17 +02:00
|
|
|
end
|
2024-01-18 16:57:10 +01:00
|
|
|
|
|
|
|
def normalized_domain
|
|
|
|
TagManager.instance.normalize_domain(query_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def query_value
|
|
|
|
params[:q].strip
|
|
|
|
end
|
2023-07-27 16:11:17 +02:00
|
|
|
end
|