From 8ef72d59c1436a8c00871e997481fd355c1955dc Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 3 Apr 2022 02:54:30 -0500 Subject: [PATCH] artists: allow `url_matches` param to take multiple urls. Pass as an array or space-separated string: * https://danbooru.donmai.us/artists?search[url_matches]=https://www.pixiv.net/en/users/32777+https://www.pixiv.net/en/users/3584828 * https://danbooru.donmai.us/artists?search[url_matches][]=https://www.pixiv.net/en/users/32777&search[url_matches][]=https://www.pixiv.net/en/users/3584828 --- app/models/artist.rb | 19 ++++++++++++------- app/models/artist_url.rb | 17 ++++++++++++++--- test/unit/artist_url_test.rb | 3 +++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app/models/artist.rb b/app/models/artist.rb index 33f83192b..adabc3295 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -244,18 +244,23 @@ class Artist < ApplicationRecord end end + def urls_match(urls) + urls = Array.wrap(urls).flat_map(&:split) + return all if urls.empty? + + urls.map do |url| + url_matches(url) + end.reduce(&:or) + end + def url_matches(query) query = query.strip - if query =~ %r{\A/(.*)/\z} - where(id: ArtistURL.where_regex(:url, $1).select(:artist_id)) - elsif query.include?("*") - where(id: ArtistURL.where_like(:url, query).select(:artist_id)) - elsif query =~ %r{\Ahttps?://}i + if query =~ %r{\Ahttps?://}i url = Source::Extractor.find(query).profile_url || query ArtistFinder.find_artists(url) else - where(id: ArtistURL.where_like(:url, "*#{query}*").select(:artist_id)) + where(id: ArtistURL.url_matches(query).select(:artist_id)) end end @@ -285,7 +290,7 @@ class Artist < ApplicationRecord end if params[:url_matches].present? - q = q.url_matches(params[:url_matches]) + q = q.urls_match(params[:url_matches]) end case params[:order] diff --git a/app/models/artist_url.rb b/app/models/artist_url.rb index 16545fb6d..a89fbba89 100644 --- a/app/models/artist_url.rb +++ b/app/models/artist_url.rb @@ -21,7 +21,7 @@ class ArtistURL < ApplicationRecord def self.search(params = {}) q = search_attributes(params, :id, :created_at, :updated_at, :url, :is_active, :artist) - q = q.url_matches(params[:url_matches]) + q = q.urls_match(params[:url_matches]) case params[:order] when /\A(id|artist_id|url|is_active|created_at|updated_at)(?:_(asc|desc))?\z/i @@ -34,6 +34,15 @@ class ArtistURL < ApplicationRecord q end + def self.urls_match(urls) + urls = Array.wrap(urls).flat_map(&:split) + return all if urls.empty? + + urls.map do |url| + url_matches(url) + end.reduce(&:or) + end + def self.url_matches(url) if url.blank? all @@ -41,9 +50,11 @@ class ArtistURL < ApplicationRecord where_regex(:url, $1) elsif url.include?("*") where_ilike(:url, url) - else - profile_url = Source::Extractor.find(url).profile_url || normalize_url(url) + elsif url =~ %r{\Ahttps?://}i + profile_url = Source::URL.profile_url(url) || Source::Extractor.find(url).profile_url || normalize_url(url) where(url: profile_url) + else + where_ilike(:url, "*#{url}*") end end diff --git a/test/unit/artist_url_test.rb b/test/unit/artist_url_test.rb index b44ac4723..25bd55641 100644 --- a/test/unit/artist_url_test.rb +++ b/test/unit/artist_url_test.rb @@ -188,6 +188,9 @@ class ArtistURLTest < ActiveSupport::TestCase assert_search_equals([@bkub_url], url_matches: "*bkub*") assert_search_equals([@bkub_url], url_matches: "/^https?://bkub\.com$/") + assert_search_equals([@bkub_url], url_matches: "https://bkub.com") + assert_search_equals([@masao_url, @bkub_url], url_matches: "https://bkub.com https://masao.com") + assert_search_equals([@masao_url, @bkub_url], url_matches: ["https://bkub.com", "https://masao.com"]) assert_search_equals([@bkub_url], url: "https://bkub.com") assert_search_equals([@bkub_url], url_eq: "https://bkub.com")