iqdb: delete leftover unused code.

* Delete unused Iqdb::Download module.
* Fix N+1 query in decorate_posts.
* /iqdb_queries: Raise max value of `limit` param to 1000.
This commit is contained in:
evazion
2019-10-16 23:52:39 -05:00
parent 57de1d8ef3
commit 958ba0f8b7
2 changed files with 21 additions and 70 deletions

View File

@@ -1,19 +1,24 @@
class IqdbProxy
def self.search(params)
raise NotImplementedError unless Danbooru.config.iqdbs_server.present?
class Error < StandardError; end
limit = params[:limit].presence || 20
limit = limit.to_i.clamp(1, 100)
def self.enabled?
Danbooru.config.iqdbs_server.present?
end
def self.search(params)
raise NotImplementedError, "the IQDBs service isn't configured" unless enabled?
limit = params[:limit]&.to_i&.clamp(1, 1000) || 20
similarity = params[:similarity].to_f.clamp(0.0, 100.0)
if params[:file].present?
results = query_file(params[:file], limit)
results = query(file: params[:file], limit: limit)
elsif params[:url].present?
url = Sources::Strategies.find(params[:url]).image_url
results = query_url(url, limit)
results = query(url: url, limit: limit)
elsif params[:post_id].present?
url = Post.find(params[:post_id]).preview_file_url
results = query_url(url, limit)
results = query(url: url, limit: limit)
else
results = []
end
@@ -22,26 +27,19 @@ class IqdbProxy
decorate_posts(results)
end
def self.query_url(url, limit)
query = { url: url, limit: limit }
response = HTTParty.get("#{Danbooru.config.iqdbs_server}/similar", query: query, **Danbooru.config.httparty_options)
response.parsed_response
end
def self.query_file(file, limit)
body = { file: file, limit: limit }
response = HTTParty.post("#{Danbooru.config.iqdbs_server}/similar", body: body, **Danbooru.config.httparty_options)
def self.query(params)
response = HTTParty.post("#{Danbooru.config.iqdbs_server}/similar", body: params, **Danbooru.config.httparty_options)
raise Error, "HTTP error: #{response.code} #{response.message}" unless response.success?
response.parsed_response
end
def self.decorate_posts(json)
json.map do |x|
begin
x["post"] = Post.find(x["post_id"])
x
rescue ActiveRecord::RecordNotFound
nil
end
post_ids = json.map { |match| match["post_id"] }
posts = Post.where(id: post_ids).group_by(&:id).transform_values(&:first)
json.map do |match|
post = posts.fetch(match["post_id"], nil)
match.with_indifferent_access.merge(post: post) if post
end.compact
end
end