invoke query compiler to determine if name should be escaped

This commit is contained in:
byte[] 2020-09-10 11:25:00 -04:00
parent ecfe60bcaa
commit 24994d2fa2

View file

@ -4,7 +4,7 @@ defmodule PhilomenaWeb.TagController do
alias PhilomenaWeb.ImageLoader
alias Philomena.Elasticsearch
alias Philomena.{Tags, Tags.Tag}
alias Philomena.Images.Image
alias Philomena.{Images, Images.Image}
alias PhilomenaWeb.TextileRenderer
alias Philomena.Interactions
import Ecto.Query
@ -71,7 +71,7 @@ defmodule PhilomenaWeb.TagController do
dnp_entries = Enum.zip(dnp_bodies, tag.dnp_entries)
search_query = escape_name(tag)
search_query = maybe_escape_name(tag)
params = Map.put(conn.params, "q", search_query)
conn = Map.put(conn, :params, params)
@ -116,13 +116,23 @@ defmodule PhilomenaWeb.TagController do
|> redirect(to: "/")
end
def escape_name(%{name: name}) do
def maybe_escape_name(%{name: name}) do
name =
name
|> String.replace(~r/\s+/, " ")
|> String.trim()
|> String.downcase()
case Images.Query.compile(nil, name) do
{:ok, %{term: %{"namespaced_tags.name" => ^name}}} ->
name
_error ->
escape_name(name)
end
end
defp escape_name(name) do
cond do
String.contains?(name, "(") or String.contains?(name, ")") ->
# \ * ? " should be escaped, wrap in quotes so parser doesn't