guard against invalid complex filters

This commit is contained in:
byte[] 2019-12-20 11:58:41 -05:00
parent 08e95e0a01
commit ffdddb3f68

View file

@ -14,8 +14,8 @@ defmodule PhilomenaWeb.ImageFilterPlug do
filter = conn.assigns[:current_filter] filter = conn.assigns[:current_filter]
tag_exclusion = %{terms: %{tag_ids: filter.hidden_tag_ids}} tag_exclusion = %{terms: %{tag_ids: filter.hidden_tag_ids}}
{:ok, query_exclusion} = Query.compile(user, normalize(filter.hidden_complex_str)) query_exclusion = invalid_filter_guard(user, filter.hidden_complex_str)
{:ok, query_spoiler} = Query.compile(user, normalize(filter.spoilered_complex_str)) query_spoiler = invalid_filter_guard(user, filter.spoilered_complex_str)
query = %{ query = %{
bool: %{ bool: %{
@ -28,4 +28,11 @@ defmodule PhilomenaWeb.ImageFilterPlug do
|> assign(:compiled_complex_spoiler, query_spoiler) |> assign(:compiled_complex_spoiler, query_spoiler)
|> assign(:compiled_filter, query) |> assign(:compiled_filter, query)
end end
defp invalid_filter_guard(user, search_string) do
case Query.compile(user, normalize(search_string)) do
{:ok, query} -> query
_error -> %{match_all: %{}}
end
end
end end