some filter incorporation

This commit is contained in:
Liam P. White 2019-08-28 21:14:54 -04:00
parent 5085d3c9d1
commit 047e02c9ba
6 changed files with 54 additions and 13 deletions

View file

@ -170,4 +170,22 @@ defmodule Philomena.Images.Query do
}, },
default: "namespaced_tags.name" 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 end

View file

@ -459,7 +459,7 @@ defmodule Philomena.Search.Lexer do
]) ])
search = search =
times(outer, min: 1) repeat(outer)
|> eos() |> eos()
defparsec(unquote(:"#{name}_lexer"), search) defparsec(unquote(:"#{name}_lexer"), search)

View file

@ -35,6 +35,8 @@ defmodule Philomena.Search.Parser do
# Predictive LL(k) parser for search grammar # 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) defp unquote(:"#{name}_top")(ctx, tokens), do: unquote(:"#{name}_or")(ctx, tokens)
# #

View file

@ -24,6 +24,7 @@ defmodule PhilomenaWeb do
import Plug.Conn import Plug.Conn
import PhilomenaWeb.Gettext import PhilomenaWeb.Gettext
alias PhilomenaWeb.Router.Helpers, as: Routes alias PhilomenaWeb.Router.Helpers, as: Routes
alias PhilomenaWeb.Plugs.ImageFilter
end end
end end

View file

@ -4,22 +4,15 @@ defmodule PhilomenaWeb.ImageController do
alias Philomena.{Images, Images.Image} alias Philomena.{Images, Images.Image}
import Ecto.Query import Ecto.Query
plug ImageFilter
def index(conn, _params) do def index(conn, _params) do
query = conn.assigns[:compiled_filter]
images = images =
Image.search_records( Image.search_records(
%{ %{
query: %{ query: %{bool: %{must_not: query}},
bool: %{ sort: %{created_at: :desc}
must_not: %{
terms: %{
tag_ids: conn.assigns[:current_filter].hidden_tag_ids
}
}
}
},
sort: %{
created_at: :desc
}
}, },
Image |> preload(:tags) Image |> preload(:tags)
) )

View file

@ -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