From d8142a6c21ceb1223107fdc28f945974836f3a73 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 7 Apr 2018 11:39:43 -0500 Subject: [PATCH 1/4] 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 From b88a66dd306c24c07da1efd7ab0232ed51afbf21 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 7 Apr 2018 09:19:32 -0500 Subject: [PATCH 2/4] Iqdb::Download.find_similar: simplify error handling. Raise errors immediately to avoid deeply nested if-else statements. --- app/logical/iqdb/download.rb | 46 ++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/app/logical/iqdb/download.rb b/app/logical/iqdb/download.rb index b9c52e66a..081187ebb 100644 --- a/app/logical/iqdb/download.rb +++ b/app/logical/iqdb/download.rb @@ -16,35 +16,31 @@ module Iqdb end def self.find_similar(source) - 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) + 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) - json.map do |match| - post = posts.find { |post| post.id == match["post_id"] } - match.with_indifferent_access.merge({ post: post }) - end - else - [] - end - else - raise "HTTP error code: #{resp.code} #{resp.message}" + resp = HTTParty.get(uri, Danbooru.config.httparty_options) + raise "HTTP error code: #{resp.code} #{resp.message}" unless 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) + + json.map do |match| + post = posts.find { |post| post.id == match["post_id"] } + match.with_indifferent_access.merge({ post: post }) end else - raise NotImplementedError, "the IQDBs service isn't configured. Similarity searches are not available." unless enabled? + [] end end end From 8de20d2b36eb6961167a84ff2bde4eae08ab3f48 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 7 Apr 2018 11:42:06 -0500 Subject: [PATCH 3/4] Iqdb::Download.find_similar: raise on iqdb errors. * Raise an error on iqdb errors instead of returning no matches. * Include the iqdb api response in the error response. --- app/logical/iqdb/download.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/logical/iqdb/download.rb b/app/logical/iqdb/download.rb index 081187ebb..bdb74ae55 100644 --- a/app/logical/iqdb/download.rb +++ b/app/logical/iqdb/download.rb @@ -1,5 +1,7 @@ module Iqdb class Download + class Error < StandardError; end + def self.enabled? Danbooru.config.iqdbs_server.present? && Danbooru.config.iqdbs_auth_key.present? end @@ -31,17 +33,17 @@ module Iqdb raise "HTTP error code: #{resp.code} #{resp.message}" unless 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) + raise "IQDB error: #{json["error"]}" unless json.is_a?(Array) - json.map do |match| - post = posts.find { |post| post.id == match["post_id"] } - match.with_indifferent_access.merge({ post: post }) - end - else - [] + 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 From ad66b8abc923c374e59363a1a542b89354a548d7 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 7 Apr 2018 11:37:23 -0500 Subject: [PATCH 4/4] common.js: show error on ajax failures. Make `link_to ..., remote: true` ajax requests display an error notice on failure. --- app/assets/javascripts/common.js | 5 +++++ 1 file changed, 5 insertions(+) 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 = {};