From d8142a6c21ceb1223107fdc28f945974836f3a73 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 7 Apr 2018 11:39:43 -0500 Subject: [PATCH] iqdb_queries_controller.rb: simplify show / check actions. * Combine create_by_post + create_by_url. Rename to find_similar. * Move iqdb enabled check to find_similar. --- app/controllers/iqdb_queries_controller.rb | 47 +++++----------------- app/logical/iqdb/download.rb | 16 ++++---- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/app/controllers/iqdb_queries_controller.rb b/app/controllers/iqdb_queries_controller.rb index 62a9b93a5..ee865c974 100644 --- a/app/controllers/iqdb_queries_controller.rb +++ b/app/controllers/iqdb_queries_controller.rb @@ -3,31 +3,16 @@ class IqdbQueriesController < ApplicationController respond_to :html, :json, :xml def show - server_check - if params[:url] - create_by_url - respond_with(@results) do |fmt| - fmt.html { render :layout => false, :action => "create_by_url" } - end - elsif params[:post_id] - create_by_post - respond_with(@results) do |fmt| - fmt.js { render :layout => false, :action => "create_by_post" } - end - else - render :nothing => true, :status => 422 + @results = find_similar + + respond_with(@results) do |fmt| + fmt.html { render :layout => false, :action => "create_by_url" } + fmt.js { render :layout => false, :action => "create_by_post" } end end def check - server_check - if params[:url].present? - create_by_url - elsif params[:post_id].present? - create_by_post - else - @results = [] - end + @results = find_similar respond_with(@results) end @@ -35,22 +20,10 @@ class IqdbQueriesController < ApplicationController alias_method :create, :show protected - def server_check - if !Danbooru.config.iqdbs_server - raise NotImplementedError.new("the IQDBs service isn't configured. Similarity searches are not available.") - end - end + def find_similar + return [] if params[:url].blank? && params[:post_id].blank? - def create_by_url - @download = Iqdb::Download.new(params[:url]) - @download.find_similar - @results = @download.matches - end - - def create_by_post - @post = Post.find(params[:post_id]) - @download = Iqdb::Download.new(@post.preview_file_url) - @download.find_similar - @results = @download.matches + params[:url] = Post.find(params[:post_id]).preview_file_url if params[:post_id].present? + Iqdb::Download.find_similar(params[:url]) end end diff --git a/app/logical/iqdb/download.rb b/app/logical/iqdb/download.rb index e77f4d1eb..b9c52e66a 100644 --- a/app/logical/iqdb/download.rb +++ b/app/logical/iqdb/download.rb @@ -1,12 +1,10 @@ module Iqdb class Download - attr_reader :source, :download, :matches - - def initialize(source) - @source = source + def self.enabled? + Danbooru.config.iqdbs_server.present? && Danbooru.config.iqdbs_auth_key.present? end - def get_referer(url) + def self.get_referer(url) headers = {} datums = {} @@ -17,7 +15,7 @@ module Iqdb [url, headers["Referer"]] end - def find_similar + def self.find_similar(source) if Danbooru.config.iqdbs_server url, ref = get_referer(source) params = { @@ -35,18 +33,18 @@ module Iqdb post_ids = json.map { |match| match["post_id"] } posts = Post.find(post_ids) - @matches = json.map do |match| + json.map do |match| post = posts.find { |post| post.id == match["post_id"] } match.with_indifferent_access.merge({ post: post }) end else - @matches = [] + [] end else raise "HTTP error code: #{resp.code} #{resp.message}" end else - raise NotImplementedError + raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled? end end end