From 2f182e906875384361bbbba3dd245b56fc577a7e Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 13 Jul 2024 14:59:36 -0400 Subject: [PATCH] Use compile with opts for image queries --- lib/philomena/images/query.ex | 4 +++- lib/philomena/schema/search.ex | 2 +- .../controllers/image/navigate_controller.ex | 5 ++++- lib/philomena_web/controllers/tag_controller.ex | 2 +- lib/philomena_web/image_loader.ex | 2 +- lib/philomena_web/plugs/filter_forced_users_plug.ex | 7 +++++-- lib/philomena_web/plugs/image_filter_plug.ex | 8 +++++--- 7 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/philomena/images/query.ex b/lib/philomena/images/query.ex index 9eedcd74..2c987d1b 100644 --- a/lib/philomena/images/query.ex +++ b/lib/philomena/images/query.ex @@ -144,7 +144,9 @@ defmodule Philomena.Images.Query do |> Parser.parse(query_string, context) end - def compile(user, query_string, watch \\ false) do + def compile(query_string, opts \\ []) do + user = Keyword.get(opts, :user) + watch = Keyword.get(opts, :watch, false) query_string = query_string || "" case user do diff --git a/lib/philomena/schema/search.ex b/lib/philomena/schema/search.ex index 9b4e7e08..819823f2 100644 --- a/lib/philomena/schema/search.ex +++ b/lib/philomena/schema/search.ex @@ -5,7 +5,7 @@ defmodule Philomena.Schema.Search do def validate_search(changeset, field, user, watched \\ false) do query = changeset |> get_field(field) |> String.normalize() - output = Query.compile(user, query, watched) + output = Query.compile(query, user: user, watch: watched) case output do {:ok, _} -> diff --git a/lib/philomena_web/controllers/image/navigate_controller.ex b/lib/philomena_web/controllers/image/navigate_controller.ex index 9cb61d48..13facfae 100644 --- a/lib/philomena_web/controllers/image/navigate_controller.ex +++ b/lib/philomena_web/controllers/image/navigate_controller.ex @@ -54,7 +54,10 @@ defmodule PhilomenaWeb.Image.NavigateController do defp compile_query(conn) do user = conn.assigns.current_user - {:ok, query} = Query.compile(user, match_all_if_blank(conn.params["q"])) + {:ok, query} = + conn.params["q"] + |> match_all_if_blank() + |> Query.compile(user: user) query end diff --git a/lib/philomena_web/controllers/tag_controller.ex b/lib/philomena_web/controllers/tag_controller.ex index 82ea790a..4900ddd3 100644 --- a/lib/philomena_web/controllers/tag_controller.ex +++ b/lib/philomena_web/controllers/tag_controller.ex @@ -121,7 +121,7 @@ defmodule PhilomenaWeb.TagController do |> String.trim() |> String.downcase() - case Images.Query.compile(nil, name) do + case Images.Query.compile(name) do {:ok, %{term: %{"namespaced_tags.name" => ^name}}} -> name diff --git a/lib/philomena_web/image_loader.ex b/lib/philomena_web/image_loader.ex index d2bc80ea..81271e05 100644 --- a/lib/philomena_web/image_loader.ex +++ b/lib/philomena_web/image_loader.ex @@ -11,7 +11,7 @@ defmodule PhilomenaWeb.ImageLoader do def search_string(conn, search_string, options \\ []) do user = conn.assigns.current_user - with {:ok, tree} <- Query.compile(user, search_string) do + with {:ok, tree} <- Query.compile(search_string, user: user) do {:ok, query(conn, tree, options)} else error -> diff --git a/lib/philomena_web/plugs/filter_forced_users_plug.ex b/lib/philomena_web/plugs/filter_forced_users_plug.ex index e28de969..d9881f96 100644 --- a/lib/philomena_web/plugs/filter_forced_users_plug.ex +++ b/lib/philomena_web/plugs/filter_forced_users_plug.ex @@ -6,7 +6,7 @@ defmodule PhilomenaWeb.FilterForcedUsersPlug do import Phoenix.Controller import Plug.Conn - alias PhilomenaQuery.Parse.String, as: SearchString + alias PhilomenaQuery.Parse.String alias PhilomenaQuery.Parse.Evaluator alias Philomena.Images.Query alias PhilomenaWeb.ImageView @@ -53,7 +53,10 @@ defmodule PhilomenaWeb.FilterForcedUsersPlug do end defp compile_filter(user, search_string) do - case Query.compile(user, SearchString.normalize(search_string)) do + search_string + |> String.normalize() + |> Query.compile(user: user) + |> case do {:ok, query} -> query _error -> %{match_all: %{}} end diff --git a/lib/philomena_web/plugs/image_filter_plug.ex b/lib/philomena_web/plugs/image_filter_plug.ex index c8138d68..a6d46187 100644 --- a/lib/philomena_web/plugs/image_filter_plug.ex +++ b/lib/philomena_web/plugs/image_filter_plug.ex @@ -1,7 +1,6 @@ defmodule PhilomenaWeb.ImageFilterPlug do import Plug.Conn - import PhilomenaQuery.Parse.String - + alias PhilomenaQuery.Parse.String alias Philomena.Images.Query # No options @@ -50,7 +49,10 @@ defmodule PhilomenaWeb.ImageFilterPlug do end defp invalid_filter_guard(user, search_string) do - case Query.compile(user, normalize(search_string)) do + search_string + |> String.normalize() + |> Query.compile(user: user) + |> case do {:ok, query} -> query _error -> %{match_all: %{}} end