Merge pull request #2963 from evazion/feat-iqdb-api
Add API support to /iqdb_queries
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
# todo: move this to iqdbs
|
# todo: move this to iqdbs
|
||||||
class IqdbQueriesController < ApplicationController
|
class IqdbQueriesController < ApplicationController
|
||||||
before_filter :member_only
|
before_filter :member_only
|
||||||
|
respond_to :html, :json, :xml
|
||||||
|
|
||||||
def create
|
def index
|
||||||
if !Danbooru.config.iqdbs_server
|
if !Danbooru.config.iqdbs_server
|
||||||
raise NotImplementedError.new("the IQDBs service isn't configured. Similarity searches are not available.")
|
raise NotImplementedError.new("the IQDBs service isn't configured. Similarity searches are not available.")
|
||||||
end
|
end
|
||||||
@@ -16,12 +17,17 @@ class IqdbQueriesController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Support both POST /iqdb_queries and GET /iqdb_queries.
|
||||||
|
alias_method :create, :index
|
||||||
|
|
||||||
protected
|
protected
|
||||||
def create_by_url
|
def create_by_url
|
||||||
@download = Iqdb::Download.new(params[:url])
|
@download = Iqdb::Download.new(params[:url])
|
||||||
@download.find_similar
|
@download.find_similar
|
||||||
@results = @download.matches
|
@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
|
end
|
||||||
|
|
||||||
def create_by_post
|
def create_by_post
|
||||||
@@ -29,6 +35,8 @@ protected
|
|||||||
@download = Iqdb::Download.new(@post.complete_preview_file_url)
|
@download = Iqdb::Download.new(@post.complete_preview_file_url)
|
||||||
@download.find_similar
|
@download.find_similar
|
||||||
@results = @download.matches
|
@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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -33,7 +33,13 @@ module Iqdb
|
|||||||
if resp.is_a?(Net::HTTPSuccess)
|
if resp.is_a?(Net::HTTPSuccess)
|
||||||
json = JSON.parse(resp.body)
|
json = JSON.parse(resp.body)
|
||||||
if json.is_a?(Array)
|
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
|
else
|
||||||
@matches = []
|
@matches = []
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
var html = '';
|
var html = '';
|
||||||
<% if @results.any? %>
|
<% if @results.any? %>
|
||||||
<% @results.each do |match| %>
|
<% @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 %>
|
<% end %>
|
||||||
<% else %>
|
<% else %>
|
||||||
html += '<p>No matches found</p>';
|
html += '<p>No matches found</p>';
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<% if @results.any? %>
|
<% if @results.any? %>
|
||||||
<h3>Similar</h3>
|
<h3>Similar</h3>
|
||||||
<% @results.each do |match| %>
|
<% @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 %>
|
<% end %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<h3>Similar</h3>
|
<h3>Similar</h3>
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ Rails.application.routes.draw do
|
|||||||
resource :visit, :controller => "forum_topic_visits"
|
resource :visit, :controller => "forum_topic_visits"
|
||||||
end
|
end
|
||||||
resources :ip_bans
|
resources :ip_bans
|
||||||
resources :iqdb_queries, :only => [:create]
|
resources :iqdb_queries, :only => [:create, :index]
|
||||||
resources :janitor_trials do
|
resources :janitor_trials do
|
||||||
collection do
|
collection do
|
||||||
get :test
|
get :test
|
||||||
@@ -206,6 +206,7 @@ Rails.application.routes.draw do
|
|||||||
get :show_seq
|
get :show_seq
|
||||||
put :mark_as_translated
|
put :mark_as_translated
|
||||||
end
|
end
|
||||||
|
get :similar, :to => "iqdb_queries#index"
|
||||||
end
|
end
|
||||||
resources :post_appeals
|
resources :post_appeals
|
||||||
resources :post_flags
|
resources :post_flags
|
||||||
|
|||||||
@@ -28,6 +28,13 @@ class IqdbQueriesControllerTest < ActionController::TestCase
|
|||||||
|
|
||||||
assert_response :success
|
assert_response :success
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user