diff --git a/app/assets/javascripts/posts.js.erb b/app/assets/javascripts/posts.js.erb index 0adadf4d2..74c73164a 100644 --- a/app/assets/javascripts/posts.js.erb +++ b/app/assets/javascripts/posts.js.erb @@ -154,7 +154,7 @@ Danbooru.Post.initialize_similar = function() { $("#similar-button").click(function(e) { - $.get("/iqdb_queries/preview", {"url": $("#post_source").val()}).done(function(html) {$("#iqdb-similar").html(html).show()}); + $.get("/iqdb_queries", {"variant": "xhr", "url": $("#post_source").val()}).done(function(html) {$("#iqdb-similar").html(html).show()}); e.preventDefault(); }); } diff --git a/app/assets/javascripts/uploads.js b/app/assets/javascripts/uploads.js index fa74257ba..c18dabdd0 100644 --- a/app/assets/javascripts/uploads.js +++ b/app/assets/javascripts/uploads.js @@ -61,7 +61,7 @@ Danbooru.Upload.initialize_iqdb_source = function() { if (/^https?:\/\//.test($("#normalized_url").val())) { - $.get("/iqdb_queries/preview", {"url": $("#normalized_url").val()}).done(function(html) {$("#iqdb-similar").html(html)}); + $.get("/iqdb_queries", {"variant": "xhr", "url": $("#normalized_url").val()}).done(function(html) {$("#iqdb-similar").html(html)}); } } @@ -77,7 +77,7 @@ Danbooru.Upload.initialize_similar = function() { $("#similar-button").click(function(e) { - $.get("/iqdb_queries/preview", {"url": $("#upload_source").val()}).done(function(html) {$("#iqdb-similar").html(html).show()}); + $.get("/iqdb_queries", {"variant": "xhr", "url": $("#upload_source").val()}).done(function(html) {$("#iqdb-similar").html(html).show()}); e.preventDefault(); }); } diff --git a/app/controllers/iqdb_queries_controller.rb b/app/controllers/iqdb_queries_controller.rb index f5162330b..c076dd420 100644 --- a/app/controllers/iqdb_queries_controller.rb +++ b/app/controllers/iqdb_queries_controller.rb @@ -3,35 +3,27 @@ class IqdbQueriesController < ApplicationController def show if params[:url] - url = URI.parse(Danbooru.config.iqdbs_server) - url.path = "/similar" - url.query = {callback: iqdb_queries_url(format: params[:format]), url: params[:url]}.to_query - redirect_to url.to_s - return + @matches = IqdbProxy.query(params[:url]) end if params[:post_id] - post = Post.find(params[:post_id]) - url = URI.parse(Danbooru.config.iqdbs_server) - url.path = "/similar" - url.query = {callback: iqdb_queries_url(format: params[:format]), url: post.preview_file_url}.to_query - redirect_to url.to_s - return + @matches = IqdbProxy.query(Post.find(params[:post_id]).preview_file_url) end if params[:matches] - @matches = JSON.parse(params[:matches]) - @matches = @matches.map {|x| [Post.find(x[0]), x[1]]} + @matches = IqdbProxy.decorate_posts(JSON.parse(params[:matches])) end - respond_with(@matches) - end + respond_with(@matches) do |fmt| + fmt.html - def preview - url = URI.parse(Danbooru.config.iqdbs_server) - url.path = "/similar" - url.query = {url: params[:url]}.to_query - @results = HTTParty.get(url.to_s).parsed_response - render layout: false + fmt.html.xhr do + render layout: false + end + + fmt.json do + render json: @matches + end + end end end diff --git a/app/logical/iqdb_proxy.rb b/app/logical/iqdb_proxy.rb new file mode 100644 index 000000000..d5330f562 --- /dev/null +++ b/app/logical/iqdb_proxy.rb @@ -0,0 +1,18 @@ +class IqdbProxy + def self.query(url) + raise NotImplementedError unless Danbooru.config.iqdbs_server.present? + + url = URI.parse(Danbooru.config.iqdbs_server) + url.path = "/similar" + url.query = {url: url}.to_query + json = HTTParty.get(url.to_s, Danbooru.config.httparty_options).parsed_response + decorate_posts(json) + end + + def self.decorate_posts(json) + json.map do |x| + x["post"] = Post.find(x["id"]) + x + end + end +end diff --git a/app/views/iqdb_queries/_matches.html.erb b/app/views/iqdb_queries/_matches.html.erb new file mode 100644 index 000000000..2b6e5a413 --- /dev/null +++ b/app/views/iqdb_queries/_matches.html.erb @@ -0,0 +1,9 @@ +<% if @matches.present? %> +

Similar

+ <% @matches.each do |match| %> + <%= PostPresenter.preview(match["post"], :tags => "status:any", :similarity => match["score"], :size => true) %> + <% end %> +<% else %> +

Similar

+

No matches found

+<% end %> diff --git a/app/views/iqdb_queries/preview.html.erb b/app/views/iqdb_queries/preview.html.erb deleted file mode 100644 index 23b4ee5ce..000000000 --- a/app/views/iqdb_queries/preview.html.erb +++ /dev/null @@ -1,9 +0,0 @@ -<% if @results.any? %> -

Similar

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

Similar

-

No matches found

-<% end %> diff --git a/app/views/iqdb_queries/show+xhr.html.erb b/app/views/iqdb_queries/show+xhr.html.erb new file mode 100644 index 000000000..1e6f5999d --- /dev/null +++ b/app/views/iqdb_queries/show+xhr.html.erb @@ -0,0 +1 @@ +<%= render "iqdb_queries/matches" %> \ No newline at end of file diff --git a/app/views/iqdb_queries/show.html.erb b/app/views/iqdb_queries/show.html.erb index 67686e241..a66dd34b3 100644 --- a/app/views/iqdb_queries/show.html.erb +++ b/app/views/iqdb_queries/show.html.erb @@ -21,18 +21,7 @@ <% end %> - <% if @matches %> -
-

Similar results

- <% if @matches.any? %> - <% @matches.each do |post, score| %> - <%= PostPresenter.preview(post, :tags => "status:any", :size => true, :similarity => score) %> - <% end %> - <% else %> -

No matches found

- <% end %> -
- <% end %> + <%= render "iqdb_queries/matches" %> diff --git a/app/views/iqdb_queries/show.json.erb b/app/views/iqdb_queries/show.json.erb deleted file mode 100644 index 04451bf99..000000000 --- a/app/views/iqdb_queries/show.json.erb +++ /dev/null @@ -1,3 +0,0 @@ -<% if @matches %> - <%= raw @matches.to_json %> -<% end %> diff --git a/app/views/posts/partials/show/_options.html.erb b/app/views/posts/partials/show/_options.html.erb index e73da1cbc..80c227a4f 100644 --- a/app/views/posts/partials/show/_options.html.erb +++ b/app/views/posts/partials/show/_options.html.erb @@ -16,7 +16,7 @@ <% end %>
  • <%= link_to "Add commentary", "#", :id => "add-commentary" %>
  • <%= link_to "Add to favorite group", "#", :id => "open-favgroup-dialog-link" %>
  • -
  • <%= link_to "Find similar", iqdb_queries_path(:post_id => post.id), :method => :post, :remote => true %>
  • +
  • <%= link_to "Find similar", iqdb_queries_path(:post_id => post.id) %>
  • <% if post.is_status_locked? %>
  • Status locked
  • diff --git a/test/functional/iqdb_queries_controller_test.rb b/test/functional/iqdb_queries_controller_test.rb index 6fc24b7a6..596db5019 100644 --- a/test/functional/iqdb_queries_controller_test.rb +++ b/test/functional/iqdb_queries_controller_test.rb @@ -3,6 +3,7 @@ require 'test_helper' class IqdbQueriesControllerTest < ActionDispatch::IntegrationTest context "The iqdb controller" do setup do + Danbooru.config.stubs(:iqdbs_server).returns("https://karasuma.donmai.us") @user = create(:user) as_user do @posts = FactoryBot.create_list(:post, 2) @@ -10,10 +11,52 @@ class IqdbQueriesControllerTest < ActionDispatch::IntegrationTest end context "show action" do - should "render with matches" do - json = @posts.map {|x| [x.id, 1]}.to_json - get_auth iqdb_queries_path, @user, params: { matches: json } - assert_response :success + context "with a url parameter" do + setup do + @url = "https://google.com" + @params = { url: @url } + @mocked_response = [{ + "post" => @posts[0], + "id" => @posts[0].id, + "score" => 1 + }] + end + + should "render a response" do + IqdbProxy.expects(:query).with(@url).returns(@mocked_response) + get_auth iqdb_queries_path(variant: "xhr"), @user, params: @params + assert_select("#post_#{@posts[0].id}") + end + end + + context "with a post_id parameter" do + setup do + @params = { post_id: @posts[0].id } + @url = @posts[0].preview_file_url + @mocked_response = [{ + "post" => @posts[0], + "id" => @posts[0].id, + "score" => 1 + }] + end + + should "redirect to iqdbs" do + IqdbProxy.expects(:query).with(@posts[0].preview_file_url).returns(@mocked_response) + get_auth iqdb_queries_path, @user, params: @params + assert_select("#post_#{@posts[0].id}") + end + end + + context "with matches" do + setup do + json = @posts.map {|x| {"id" => x.id, "score" => 1}}.to_json + @params = { matches: json } + end + + should "render with matches" do + get_auth iqdb_queries_path, @user, params: @params + assert_response :success + end end end end