refactor iqdb endpoints
This commit is contained in:
@@ -154,7 +154,7 @@
|
|||||||
|
|
||||||
Danbooru.Post.initialize_similar = function() {
|
Danbooru.Post.initialize_similar = function() {
|
||||||
$("#similar-button").click(function(e) {
|
$("#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();
|
e.preventDefault();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@
|
|||||||
|
|
||||||
Danbooru.Upload.initialize_iqdb_source = function() {
|
Danbooru.Upload.initialize_iqdb_source = function() {
|
||||||
if (/^https?:\/\//.test($("#normalized_url").val())) {
|
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() {
|
Danbooru.Upload.initialize_similar = function() {
|
||||||
$("#similar-button").click(function(e) {
|
$("#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();
|
e.preventDefault();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,35 +3,27 @@ class IqdbQueriesController < ApplicationController
|
|||||||
|
|
||||||
def show
|
def show
|
||||||
if params[:url]
|
if params[:url]
|
||||||
url = URI.parse(Danbooru.config.iqdbs_server)
|
@matches = IqdbProxy.query(params[:url])
|
||||||
url.path = "/similar"
|
|
||||||
url.query = {callback: iqdb_queries_url(format: params[:format]), url: params[:url]}.to_query
|
|
||||||
redirect_to url.to_s
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:post_id]
|
if params[:post_id]
|
||||||
post = Post.find(params[:post_id])
|
@matches = IqdbProxy.query(Post.find(params[:post_id]).preview_file_url)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if params[:matches]
|
if params[:matches]
|
||||||
@matches = JSON.parse(params[:matches])
|
@matches = IqdbProxy.decorate_posts(JSON.parse(params[:matches]))
|
||||||
@matches = @matches.map {|x| [Post.find(x[0]), x[1]]}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
respond_with(@matches)
|
respond_with(@matches) do |fmt|
|
||||||
end
|
fmt.html
|
||||||
|
|
||||||
def preview
|
fmt.html.xhr do
|
||||||
url = URI.parse(Danbooru.config.iqdbs_server)
|
render layout: false
|
||||||
url.path = "/similar"
|
end
|
||||||
url.query = {url: params[:url]}.to_query
|
|
||||||
@results = HTTParty.get(url.to_s).parsed_response
|
fmt.json do
|
||||||
render layout: false
|
render json: @matches
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
18
app/logical/iqdb_proxy.rb
Normal file
18
app/logical/iqdb_proxy.rb
Normal 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
|
||||||
9
app/views/iqdb_queries/_matches.html.erb
Normal file
9
app/views/iqdb_queries/_matches.html.erb
Normal 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 %>
|
||||||
@@ -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 %>
|
|
||||||
1
app/views/iqdb_queries/show+xhr.html.erb
Normal file
1
app/views/iqdb_queries/show+xhr.html.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<%= render "iqdb_queries/matches" %>
|
||||||
@@ -21,18 +21,7 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<% if @matches %>
|
<%= render "iqdb_queries/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 %>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
<% if @matches %>
|
|
||||||
<%= raw @matches.to_json %>
|
|
||||||
<% end %>
|
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
<li id="add-artist-commentary-list"><%= link_to "Add commentary", "#", :id => "add-commentary" %></li>
|
<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 "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? %>
|
<% if post.is_status_locked? %>
|
||||||
<li><span id="status-locked-notice">Status locked</span></li>
|
<li><span id="status-locked-notice">Status locked</span></li>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ require 'test_helper'
|
|||||||
class IqdbQueriesControllerTest < ActionDispatch::IntegrationTest
|
class IqdbQueriesControllerTest < ActionDispatch::IntegrationTest
|
||||||
context "The iqdb controller" do
|
context "The iqdb controller" do
|
||||||
setup do
|
setup do
|
||||||
|
Danbooru.config.stubs(:iqdbs_server).returns("https://karasuma.donmai.us")
|
||||||
@user = create(:user)
|
@user = create(:user)
|
||||||
as_user do
|
as_user do
|
||||||
@posts = FactoryBot.create_list(:post, 2)
|
@posts = FactoryBot.create_list(:post, 2)
|
||||||
@@ -10,10 +11,52 @@ class IqdbQueriesControllerTest < ActionDispatch::IntegrationTest
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "show action" do
|
context "show action" do
|
||||||
should "render with matches" do
|
context "with a url parameter" do
|
||||||
json = @posts.map {|x| [x.id, 1]}.to_json
|
setup do
|
||||||
get_auth iqdb_queries_path, @user, params: { matches: json }
|
@url = "https://google.com"
|
||||||
assert_response :success
|
@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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user