diff --git a/app/controllers/iqdb_queries_controller.rb b/app/controllers/iqdb_queries_controller.rb index 8f1393ed2..e5b88b425 100644 --- a/app/controllers/iqdb_queries_controller.rb +++ b/app/controllers/iqdb_queries_controller.rb @@ -1,8 +1,9 @@ # todo: move this to iqdbs class IqdbQueriesController < ApplicationController before_filter :member_only + respond_to :html, :json, :xml - def create + def index if !Danbooru.config.iqdbs_server raise NotImplementedError.new("the IQDBs service isn't configured. Similarity searches are not available.") end @@ -16,12 +17,17 @@ class IqdbQueriesController < ApplicationController end end + # Support both POST /iqdb_queries and GET /iqdb_queries. + alias_method :create, :index + protected def create_by_url @download = Iqdb::Download.new(params[:url]) @download.find_similar @results = @download.matches - render :layout => false, :action => "create_by_url" + respond_with(@results) do |fmt| + fmt.html { render :layout => false, :action => "create_by_url" } + end end def create_by_post @@ -29,6 +35,8 @@ protected @download = Iqdb::Download.new(@post.complete_preview_file_url) @download.find_similar @results = @download.matches - render :layout => false, :action => "create_by_post" + respond_with(@results) do |fmt| + fmt.js { render :layout => false, :action => "create_by_post" } + end end end diff --git a/app/logical/iqdb/download.rb b/app/logical/iqdb/download.rb index f7ace2006..512cb2ce5 100644 --- a/app/logical/iqdb/download.rb +++ b/app/logical/iqdb/download.rb @@ -33,7 +33,13 @@ module Iqdb if resp.is_a?(Net::HTTPSuccess) json = JSON.parse(resp.body) if json.is_a?(Array) - @matches = json + post_ids = json.map { |match| match["post_id"] } + posts = Post.find(post_ids) + + @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 diff --git a/app/views/iqdb_queries/create_by_post.js.erb b/app/views/iqdb_queries/create_by_post.js.erb index e49812c6f..1877d32a4 100644 --- a/app/views/iqdb_queries/create_by_post.js.erb +++ b/app/views/iqdb_queries/create_by_post.js.erb @@ -1,7 +1,7 @@ var html = ''; <% if @results.any? %> <% @results.each do |match| %> - html += '<%= j PostPresenter.preview(Post.find(match["post_id"]), :tags => "status:any") %>'; + html += '<%= j PostPresenter.preview(match[:post], :tags => "status:any") %>'; <% end %> <% else %> html += '
No matches found
'; diff --git a/app/views/iqdb_queries/create_by_url.html.erb b/app/views/iqdb_queries/create_by_url.html.erb index b33993d41..87846f0b6 100644 --- a/app/views/iqdb_queries/create_by_url.html.erb +++ b/app/views/iqdb_queries/create_by_url.html.erb @@ -1,7 +1,7 @@ <% if @results.any? %>