From 9a035825131cb73425b20ec48e806a2a9e75c3ac Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 4 Jul 2020 17:46:58 -0500 Subject: [PATCH] Add OpenSearch suggestion support. Add autocomplete support when searching Danbooru from the Chrome address bar. If you type "danb" in the address bar then search for a tag, then autocomplete results from Danbooru will appear as search suggestions in Chrome. Note that the "Autocomplete searches and URLs" Chrome setting must be enabled for this to work. Ref: * http://dev.chromium.org/tab-to-search * https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Supporting_search_suggestions_in_search_plugins * https://github.com/dewitt/opensearch/blob/master/mediawiki/Specifications/OpenSearch/Extensions/Suggestions/1.1/Draft%201.wiki --- app/controllers/autocomplete_controller.rb | 16 ++++++++++++++++ app/views/static/opensearch.xml.erb | 1 + test/functional/autocomplete_controller_test.rb | 17 +++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 app/controllers/autocomplete_controller.rb create mode 100644 test/functional/autocomplete_controller_test.rb diff --git a/app/controllers/autocomplete_controller.rb b/app/controllers/autocomplete_controller.rb new file mode 100644 index 000000000..37fa3565b --- /dev/null +++ b/app/controllers/autocomplete_controller.rb @@ -0,0 +1,16 @@ +class AutocompleteController < ApplicationController + respond_to :xml, :json + + def index + @tags = Tag.names_matches_with_aliases(params[:query], params.fetch(:limit, 10).to_i) + + if request.variant.opensearch? + expires_in 1.hour + results = [params[:query], @tags.map(&:pretty_name)] + respond_with(results) + else + # XXX + respond_with(@tags.map(&:attributes)) + end + end +end diff --git a/app/views/static/opensearch.xml.erb b/app/views/static/opensearch.xml.erb index 3c67241f8..8652a2f9c 100644 --- a/app/views/static/opensearch.xml.erb +++ b/app/views/static/opensearch.xml.erb @@ -4,4 +4,5 @@ <%= Danbooru.config.app_name %> search <%= root_url %>favicon.ico + diff --git a/test/functional/autocomplete_controller_test.rb b/test/functional/autocomplete_controller_test.rb new file mode 100644 index 000000000..51c064029 --- /dev/null +++ b/test/functional/autocomplete_controller_test.rb @@ -0,0 +1,17 @@ +require "test_helper" + +class AutocompleteControllerTest < ActionDispatch::IntegrationTest + context "Autocomplete controller" do + context "index action" do + setup do + create(:tag, name: "azur_lane") + end + + should "work for opensearch queries" do + get autocomplete_index_path(query: "azur", variant: "opensearch"), as: :json + assert_response :success + assert_equal(["azur", ["azur lane"]], response.parsed_body) + end + end + end +end