Merge pull request #2963 from evazion/feat-iqdb-api

Add API support to /iqdb_queries
This commit is contained in:
Albert Yi
2017-04-05 17:33:32 -07:00
committed by GitHub
6 changed files with 29 additions and 7 deletions

View File

@@ -1,8 +1,9 @@
# todo: move this to iqdbs
class IqdbQueriesController < ApplicationController
before_filter :member_only
respond_to :html, :json, :xml
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,12 +17,17 @@ 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])
@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
@@ -29,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

View File

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

View File

@@ -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 += '<p>No matches found</p>';

View File

@@ -1,7 +1,7 @@
<% if @results.any? %>
<h3>Similar</h3>
<% @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 %>
<h3>Similar</h3>

View File

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

View File

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