diff --git a/app/assets/javascripts/common.js b/app/assets/javascripts/common.js index cbec48d0b..2b345a26d 100644 --- a/app/assets/javascripts/common.js +++ b/app/assets/javascripts/common.js @@ -39,6 +39,11 @@ $(function() { location.reload(); }); }); + + // triggered by rails when a `link_to ..., remote: true` call fails. + $(document).on("ajax:error", function (event, xhr, status, error) { + Danbooru.error("Error: " + xhr.status + " " + xhr.statusText); + }); }); var Danbooru = {}; 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..bdb74ae55 100644 --- a/app/logical/iqdb/download.rb +++ b/app/logical/iqdb/download.rb @@ -1,12 +1,12 @@ module Iqdb class Download - attr_reader :source, :download, :matches + class Error < StandardError; end - 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,37 +17,33 @@ module Iqdb [url, headers["Referer"]] end - def find_similar - if Danbooru.config.iqdbs_server - url, ref = get_referer(source) - params = { - "key" => Danbooru.config.iqdbs_auth_key, - "url" => url, - "ref" => ref - } - uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar") - uri.query = URI.encode_www_form(params) + def self.find_similar(source) + raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled? - resp = HTTParty.get(uri, Danbooru.config.httparty_options) - if resp.success? - json = JSON.parse(resp.body) - if json.is_a?(Array) - post_ids = json.map { |match| match["post_id"] } - posts = Post.find(post_ids) + url, ref = get_referer(source) + params = { + "key" => Danbooru.config.iqdbs_auth_key, + "url" => url, + "ref" => ref + } + uri = URI.parse("#{Danbooru.config.iqdbs_server}/similar") + uri.query = URI.encode_www_form(params) - @matches = 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 + resp = HTTParty.get(uri, Danbooru.config.httparty_options) + raise "HTTP error code: #{resp.code} #{resp.message}" unless resp.success? + + json = JSON.parse(resp.body) + raise "IQDB error: #{json["error"]}" unless json.is_a?(Array) + + post_ids = json.map { |match| match["post_id"] } + posts = Post.find(post_ids) + + json.map do |match| + post = posts.find { |post| post.id == match["post_id"] } + match.with_indifferent_access.merge({ post: post }) end + rescue => e + raise Error, { message: e.message, iqdb_response: json } end end end