mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
invoke query compiler to determine if name should be escaped
This commit is contained in:
parent
ecfe60bcaa
commit
24994d2fa2
1 changed files with 13 additions and 3 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue