Use compile with opts for image queries

This commit is contained in:
Liam 2024-07-13 14:59:36 -04:00
parent 476363d4f7
commit 2f182e9068
7 changed files with 20 additions and 10 deletions

View file

@ -144,7 +144,9 @@ defmodule Philomena.Images.Query do
|> Parser.parse(query_string, context) |> Parser.parse(query_string, context)
end 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 || "" query_string = query_string || ""
case user do case user do

View file

@ -5,7 +5,7 @@ defmodule Philomena.Schema.Search do
def validate_search(changeset, field, user, watched \\ false) do def validate_search(changeset, field, user, watched \\ false) do
query = changeset |> get_field(field) |> String.normalize() query = changeset |> get_field(field) |> String.normalize()
output = Query.compile(user, query, watched) output = Query.compile(query, user: user, watch: watched)
case output do case output do
{:ok, _} -> {:ok, _} ->

View file

@ -54,7 +54,10 @@ defmodule PhilomenaWeb.Image.NavigateController do
defp compile_query(conn) do defp compile_query(conn) do
user = conn.assigns.current_user 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 query
end end

View file

@ -121,7 +121,7 @@ defmodule PhilomenaWeb.TagController do
|> String.trim() |> String.trim()
|> String.downcase() |> String.downcase()
case Images.Query.compile(nil, name) do case Images.Query.compile(name) do
{:ok, %{term: %{"namespaced_tags.name" => ^name}}} -> {:ok, %{term: %{"namespaced_tags.name" => ^name}}} ->
name name

View file

@ -11,7 +11,7 @@ defmodule PhilomenaWeb.ImageLoader do
def search_string(conn, search_string, options \\ []) do def search_string(conn, search_string, options \\ []) do
user = conn.assigns.current_user 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)} {:ok, query(conn, tree, options)}
else else
error -> error ->

View file

@ -6,7 +6,7 @@ defmodule PhilomenaWeb.FilterForcedUsersPlug do
import Phoenix.Controller import Phoenix.Controller
import Plug.Conn import Plug.Conn
alias PhilomenaQuery.Parse.String, as: SearchString alias PhilomenaQuery.Parse.String
alias PhilomenaQuery.Parse.Evaluator alias PhilomenaQuery.Parse.Evaluator
alias Philomena.Images.Query alias Philomena.Images.Query
alias PhilomenaWeb.ImageView alias PhilomenaWeb.ImageView
@ -53,7 +53,10 @@ defmodule PhilomenaWeb.FilterForcedUsersPlug do
end end
defp compile_filter(user, search_string) do 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 {:ok, query} -> query
_error -> %{match_all: %{}} _error -> %{match_all: %{}}
end end

View file

@ -1,7 +1,6 @@
defmodule PhilomenaWeb.ImageFilterPlug do defmodule PhilomenaWeb.ImageFilterPlug do
import Plug.Conn import Plug.Conn
import PhilomenaQuery.Parse.String alias PhilomenaQuery.Parse.String
alias Philomena.Images.Query alias Philomena.Images.Query
# No options # No options
@ -50,7 +49,10 @@ defmodule PhilomenaWeb.ImageFilterPlug do
end end
defp invalid_filter_guard(user, search_string) do 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 {:ok, query} -> query
_error -> %{match_all: %{}} _error -> %{match_all: %{}}
end end