From c402e15ccae585fbf34384896de69bd11631460a Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 23 Dec 2016 01:33:40 -0600 Subject: [PATCH 1/4] /iqdb_queries: support both GET and POST; add GET /posts/1/similar. --- app/controllers/iqdb_queries_controller.rb | 5 ++++- config/routes.rb | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/iqdb_queries_controller.rb b/app/controllers/iqdb_queries_controller.rb index 8f1393ed2..19e8fa536 100644 --- a/app/controllers/iqdb_queries_controller.rb +++ b/app/controllers/iqdb_queries_controller.rb @@ -2,7 +2,7 @@ class IqdbQueriesController < ApplicationController before_filter :member_only - 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,6 +16,9 @@ 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]) diff --git a/config/routes.rb b/config/routes.rb index c8877a524..45cddb0fc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -147,7 +147,7 @@ Rails.application.routes.draw do resource :visit, :controller => "forum_topic_visits" end resources :ip_bans - resources :iqdb_queries, :only => [:create] + resources :iqdb_queries, :only => [:create, :index] resources :janitor_trials do collection do get :test @@ -206,6 +206,7 @@ Rails.application.routes.draw do get :show_seq put :mark_as_translated end + get :similar, :to => "iqdb_queries#index" end resources :post_appeals resources :post_flags From e15f955677a6443d370fde57808830e6087fcb0e Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 23 Dec 2016 18:44:43 -0600 Subject: [PATCH 2/4] /iqdb_queries: support json/xml api responses. --- app/controllers/iqdb_queries_controller.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/iqdb_queries_controller.rb b/app/controllers/iqdb_queries_controller.rb index 19e8fa536..e5b88b425 100644 --- a/app/controllers/iqdb_queries_controller.rb +++ b/app/controllers/iqdb_queries_controller.rb @@ -1,6 +1,7 @@ # todo: move this to iqdbs class IqdbQueriesController < ApplicationController before_filter :member_only + respond_to :html, :json, :xml def index if !Danbooru.config.iqdbs_server @@ -24,7 +25,9 @@ protected @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 @@ -32,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 From 423dd0b848e9d2b260c3a68d4ce0bbcb4fe909a1 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 5 Apr 2017 17:26:15 -0500 Subject: [PATCH 3/4] /iqdb_queries: include full data for each post in api response. --- app/logical/iqdb/download.rb | 8 +++++++- app/views/iqdb_queries/create_by_post.js.erb | 2 +- app/views/iqdb_queries/create_by_url.html.erb | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) 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? %>

Similar

<% @results.each do |match| %> - <%= PostPresenter.preview(Post.find(match["post_id"]), :tags => "status:any", :size => true) %> + <%= PostPresenter.preview(match[:post], :tags => "status:any", :size => true) %> <% end %> <% else %>

Similar

From 2d2efbddfe643a0370f1d93bdb2db51ed1970ffb Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 5 Apr 2017 18:48:57 -0500 Subject: [PATCH 4/4] /iqdb_queries: add api test. --- test/functional/iqdb_queries_controller_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/functional/iqdb_queries_controller_test.rb b/test/functional/iqdb_queries_controller_test.rb index a9dd869f1..2e6cba454 100644 --- a/test/functional/iqdb_queries_controller_test.rb +++ b/test/functional/iqdb_queries_controller_test.rb @@ -28,6 +28,13 @@ class IqdbQueriesControllerTest < ActionController::TestCase assert_response :success end + + should "render for a json response" do + mock_iqdb_matches!(@posts[0].source, @posts) + get :index, { url: @posts[0].source, format: "json" }, { user_id: @user.id } + + assert_response :success + end end end end