Fix #4422: commentary metasearch: allow search for "true", "false"etc when in double quotes.
Treat the following searches as literal text searches instead of as special keywords: * source:none * commentary:true * commentary:false * commentary:translated * commentary:untranslated
This commit is contained in:
@@ -117,13 +117,13 @@ class PostQueryBuilder
|
|||||||
|
|
||||||
def metatags_match(metatags, relation)
|
def metatags_match(metatags, relation)
|
||||||
metatags.each do |metatag|
|
metatags.each do |metatag|
|
||||||
relation = relation.merge(metatag_matches(metatag.name, metatag.value))
|
relation = relation.merge(metatag_matches(metatag.name, metatag.value, quoted: metatag.quoted))
|
||||||
end
|
end
|
||||||
|
|
||||||
relation
|
relation
|
||||||
end
|
end
|
||||||
|
|
||||||
def metatag_matches(name, value)
|
def metatag_matches(name, value, quoted: false)
|
||||||
case name
|
case name
|
||||||
when "id"
|
when "id"
|
||||||
attribute_matches(value, :id)
|
attribute_matches(value, :id)
|
||||||
@@ -182,17 +182,17 @@ class PostQueryBuilder
|
|||||||
when "-embedded"
|
when "-embedded"
|
||||||
embedded_matches(value).negate
|
embedded_matches(value).negate
|
||||||
when "source"
|
when "source"
|
||||||
source_matches(value)
|
source_matches(value, quoted)
|
||||||
when "-source"
|
when "-source"
|
||||||
source_matches(value).negate
|
source_matches(value, quoted).negate
|
||||||
when "disapproved"
|
when "disapproved"
|
||||||
disapproved_matches(value)
|
disapproved_matches(value)
|
||||||
when "-disapproved"
|
when "-disapproved"
|
||||||
disapproved_matches(value).negate
|
disapproved_matches(value).negate
|
||||||
when "commentary"
|
when "commentary"
|
||||||
commentary_matches(value)
|
commentary_matches(value, quoted)
|
||||||
when "-commentary"
|
when "-commentary"
|
||||||
commentary_matches(value).negate
|
commentary_matches(value, quoted).negate
|
||||||
when "search"
|
when "search"
|
||||||
saved_search_matches(value)
|
saved_search_matches(value)
|
||||||
when "-search"
|
when "-search"
|
||||||
@@ -387,9 +387,9 @@ class PostQueryBuilder
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def source_matches(source)
|
def source_matches(source, quoted = false)
|
||||||
case source.downcase
|
case source.downcase
|
||||||
when "none"
|
in "none" unless quoted
|
||||||
Post.where_like(:source, "")
|
Post.where_like(:source, "")
|
||||||
else
|
else
|
||||||
Post.where_ilike(:source, source + "*")
|
Post.where_ilike(:source, source + "*")
|
||||||
@@ -459,15 +459,15 @@ class PostQueryBuilder
|
|||||||
favorites_include(username).joins(:favorites).merge(Favorite.for_user(user.id)).order("favorites.id DESC")
|
favorites_include(username).joins(:favorites).merge(Favorite.for_user(user.id)).order("favorites.id DESC")
|
||||||
end
|
end
|
||||||
|
|
||||||
def commentary_matches(query)
|
def commentary_matches(query, quoted = false)
|
||||||
case query.downcase
|
case query.downcase
|
||||||
when "none", "false"
|
in "none" | "false" unless quoted
|
||||||
Post.where.not(artist_commentary: ArtistCommentary.all).or(Post.where(artist_commentary: ArtistCommentary.deleted))
|
Post.where.not(artist_commentary: ArtistCommentary.all).or(Post.where(artist_commentary: ArtistCommentary.deleted))
|
||||||
when "any", "true"
|
in "any" | "true" unless quoted
|
||||||
Post.where(artist_commentary: ArtistCommentary.undeleted)
|
Post.where(artist_commentary: ArtistCommentary.undeleted)
|
||||||
when "translated"
|
in "translated" unless quoted
|
||||||
Post.where(artist_commentary: ArtistCommentary.translated)
|
Post.where(artist_commentary: ArtistCommentary.translated)
|
||||||
when "untranslated"
|
in "untranslated" unless quoted
|
||||||
Post.where(artist_commentary: ArtistCommentary.untranslated)
|
Post.where(artist_commentary: ArtistCommentary.untranslated)
|
||||||
else
|
else
|
||||||
Post.where(artist_commentary: ArtistCommentary.text_matches(query))
|
Post.where(artist_commentary: ArtistCommentary.text_matches(query))
|
||||||
@@ -691,12 +691,12 @@ class PostQueryBuilder
|
|||||||
if scanner.scan(/(#{METATAGS.join("|")}):/io)
|
if scanner.scan(/(#{METATAGS.join("|")}):/io)
|
||||||
metatag = scanner.captures.first.downcase
|
metatag = scanner.captures.first.downcase
|
||||||
|
|
||||||
if scanner.scan(/"(.+)"/)
|
if scanner.scan(/"(.+)"/) || scanner.scan(/'(.+)'/)
|
||||||
value = scanner.captures.first
|
|
||||||
elsif scanner.scan(/'(.+)'/)
|
|
||||||
value = scanner.captures.first
|
value = scanner.captures.first
|
||||||
|
quoted = true
|
||||||
else
|
else
|
||||||
value = scanner.scan(/[^ ]*/)
|
value = scanner.scan(/[^ ]*/)
|
||||||
|
quoted = false
|
||||||
end
|
end
|
||||||
|
|
||||||
if metatag.in?(COUNT_METATAG_SYNONYMS)
|
if metatag.in?(COUNT_METATAG_SYNONYMS)
|
||||||
@@ -708,7 +708,7 @@ class PostQueryBuilder
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
terms << OpenStruct.new({ type: :metatag, name: metatag, value: value })
|
terms << OpenStruct.new({ type: :metatag, name: metatag, value: value, quoted: quoted })
|
||||||
elsif scanner.scan(/([-~])?([^ ]+)/)
|
elsif scanner.scan(/([-~])?([^ ]+)/)
|
||||||
operator = scanner.captures.first
|
operator = scanner.captures.first
|
||||||
tag = scanner.captures.second
|
tag = scanner.captures.second
|
||||||
|
|||||||
@@ -422,6 +422,11 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
assert_tag_match([post1], 'commentary:"azur lane"')
|
assert_tag_match([post1], 'commentary:"azur lane"')
|
||||||
assert_tag_match([post4, post3, post2], '-commentary:"azur lane"')
|
assert_tag_match([post4, post3, post2], '-commentary:"azur lane"')
|
||||||
|
|
||||||
|
assert_tag_match([], "commentary:'true'")
|
||||||
|
assert_tag_match([], "commentary:'false'")
|
||||||
|
assert_tag_match([], "commentary:'translated'")
|
||||||
|
assert_tag_match([], "commentary:'untranslated'")
|
||||||
end
|
end
|
||||||
|
|
||||||
should "return posts for the date:<d> metatag" do
|
should "return posts for the date:<d> metatag" do
|
||||||
@@ -579,18 +584,14 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
|
|||||||
assert_tag_match([post1], 'source:"abc def"')
|
assert_tag_match([post1], 'source:"abc def"')
|
||||||
assert_tag_match([post1], "source:'abc def'")
|
assert_tag_match([post1], "source:'abc def'")
|
||||||
assert_tag_match([post2], "source:abcde")
|
assert_tag_match([post2], "source:abcde")
|
||||||
|
assert_tag_match([post2], "source:ABCDE")
|
||||||
assert_tag_match([post3, post1], "-source:abcde")
|
assert_tag_match([post3, post1], "-source:abcde")
|
||||||
|
|
||||||
assert_tag_match([post3], "source:none")
|
assert_tag_match([post3], "source:none")
|
||||||
assert_tag_match([post3], "source:NONE")
|
assert_tag_match([post3], "source:NONE")
|
||||||
assert_tag_match([post2, post1], "-source:none")
|
assert_tag_match([post2, post1], "-source:none")
|
||||||
end
|
|
||||||
|
|
||||||
should "return posts for a case insensitive source search" do
|
assert_tag_match([], "source:'none'")
|
||||||
post1 = create(:post, source: "ABCD")
|
|
||||||
post2 = create(:post, source: "1234")
|
|
||||||
|
|
||||||
assert_tag_match([post1], "source:abcd")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
should "return posts for a pixiv source search" do
|
should "return posts for a pixiv source search" do
|
||||||
|
|||||||
Reference in New Issue
Block a user