refactor iqdb endpoints

This commit is contained in:
r888888888
2018-06-23 10:32:39 -07:00
parent 27baa08e59
commit 3ba5c6fb51
11 changed files with 93 additions and 53 deletions

View File

@@ -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();
});
}

View File

@@ -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();
});
}

View File

@@ -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

18
app/logical/iqdb_proxy.rb Normal file
View File

@@ -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

View File

@@ -0,0 +1,9 @@
<% if @matches.present? %>
<h3>Similar</h3>
<% @matches.each do |match| %>
<%= PostPresenter.preview(match["post"], :tags => "status:any", :similarity => match["score"], :size => true) %>
<% end %>
<% else %>
<h3>Similar</h3>
<p>No matches found</p>
<% end %>

View File

@@ -1,9 +0,0 @@
<% if @results.any? %>
<h3>Similar</h3>
<% @results.each do |match| %>
<%= PostPresenter.preview(Post.find(match["post_id"]), :tags => "status:any", :similarity => match["score"], :size => true) %>
<% end %>
<% else %>
<h3>Similar</h3>
<p>No matches found</p>
<% end %>

View File

@@ -0,0 +1 @@
<%= render "iqdb_queries/matches" %>

View File

@@ -21,18 +21,7 @@
<% end %>
</section>
<% if @matches %>
<section>
<h2>Similar results</h2>
<% if @matches.any? %>
<% @matches.each do |post, score| %>
<%= PostPresenter.preview(post, :tags => "status:any", :size => true, :similarity => score) %>
<% end %>
<% else %>
<p>No matches found</p>
<% end %>
</section>
<% end %>
<%= render "iqdb_queries/matches" %>
</div>
</div>

View File

@@ -1,3 +0,0 @@
<% if @matches %>
<%= raw @matches.to_json %>
<% end %>

View File

@@ -16,7 +16,7 @@
<% end %>
<li id="add-artist-commentary-list"><%= link_to "Add commentary", "#", :id => "add-commentary" %></li>
<li><%= link_to "Add to favorite group", "#", :id => "open-favgroup-dialog-link" %></li>
<li><%= link_to "Find similar", iqdb_queries_path(:post_id => post.id), :method => :post, :remote => true %></li>
<li><%= link_to "Find similar", iqdb_queries_path(:post_id => post.id) %></li>
<% if post.is_status_locked? %>
<li><span id="status-locked-notice">Status locked</span></li>

View File

@@ -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