From 047e02c9ba1800b3080d0649a12910695ba5724f Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Wed, 28 Aug 2019 21:14:54 -0400 Subject: [PATCH] some filter incorporation --- lib/philomena/images/query.ex | 18 +++++++++++++ lib/philomena/search/lexer.ex | 2 +- lib/philomena/search/parser.ex | 2 ++ lib/philomena_web.ex | 1 + .../controllers/image_controller.ex | 17 ++++-------- lib/philomena_web/plugs/image_filter.ex | 27 +++++++++++++++++++ 6 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 lib/philomena_web/plugs/image_filter.ex diff --git a/lib/philomena/images/query.ex b/lib/philomena/images/query.ex index 8523d7db..d1abd431 100644 --- a/lib/philomena/images/query.ex +++ b/lib/philomena/images/query.ex @@ -170,4 +170,22 @@ defmodule Philomena.Images.Query do }, default: "namespaced_tags.name" ) + + def compile(user, query_string, watch \\ false) do + query_string = query_string || "" + + case user do + nil -> + anonymous_parser(%{user: nil, watch: watch}, query_string) + + %{role: role} when role in ~W(user assistant) -> + user_parser(%{user: user, watch: watch}, query_string) + + %{role: role} when role in ~W(moderator admin) -> + moderator_parser(%{user: user, watch: watch}, query_string) + + _ -> + raise ArgumentError, "Unknown user role." + end + end end diff --git a/lib/philomena/search/lexer.ex b/lib/philomena/search/lexer.ex index 83b21fe7..2d871640 100644 --- a/lib/philomena/search/lexer.ex +++ b/lib/philomena/search/lexer.ex @@ -459,7 +459,7 @@ defmodule Philomena.Search.Lexer do ]) search = - times(outer, min: 1) + repeat(outer) |> eos() defparsec(unquote(:"#{name}_lexer"), search) diff --git a/lib/philomena/search/parser.ex b/lib/philomena/search/parser.ex index 9467c6ab..271a8a33 100644 --- a/lib/philomena/search/parser.ex +++ b/lib/philomena/search/parser.ex @@ -35,6 +35,8 @@ defmodule Philomena.Search.Parser do # Predictive LL(k) parser for search grammar # + defp unquote(:"#{name}_top")(_ctx, []), do: {%{match_none: %{}}, []} + defp unquote(:"#{name}_top")(ctx, tokens), do: unquote(:"#{name}_or")(ctx, tokens) # diff --git a/lib/philomena_web.ex b/lib/philomena_web.ex index 8d668dff..1359c2e4 100644 --- a/lib/philomena_web.ex +++ b/lib/philomena_web.ex @@ -24,6 +24,7 @@ defmodule PhilomenaWeb do import Plug.Conn import PhilomenaWeb.Gettext alias PhilomenaWeb.Router.Helpers, as: Routes + alias PhilomenaWeb.Plugs.ImageFilter end end diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index d9503d54..9db64f60 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -4,22 +4,15 @@ defmodule PhilomenaWeb.ImageController do alias Philomena.{Images, Images.Image} import Ecto.Query + plug ImageFilter + def index(conn, _params) do + query = conn.assigns[:compiled_filter] images = Image.search_records( %{ - query: %{ - bool: %{ - must_not: %{ - terms: %{ - tag_ids: conn.assigns[:current_filter].hidden_tag_ids - } - } - } - }, - sort: %{ - created_at: :desc - } + query: %{bool: %{must_not: query}}, + sort: %{created_at: :desc} }, Image |> preload(:tags) ) diff --git a/lib/philomena_web/plugs/image_filter.ex b/lib/philomena_web/plugs/image_filter.ex new file mode 100644 index 00000000..2a6131ad --- /dev/null +++ b/lib/philomena_web/plugs/image_filter.ex @@ -0,0 +1,27 @@ +defmodule PhilomenaWeb.Plugs.ImageFilter do + import Plug.Conn + + alias Philomena.Images.Query + alias Pow.Plug + + # No options + def init([]), do: false + + # Assign current filter + def call(conn, _opts) do + user = conn |> Plug.current_user() + filter = conn.assigns[:current_filter] + + tag_exclusion = %{terms: %{tag_ids: filter.hidden_tag_ids}} + {:ok, query_exclusion} = Query.compile(user, filter.hidden_complex_str) + + query = %{ + bool: %{ + should: [tag_exclusion, query_exclusion] + } + } + + conn + |> assign(:compiled_filter, query) + end +end