mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
fix random results unintentionally hitting hidden images
This commit is contained in:
parent
21dcd0c9df
commit
351cb282bf
1 changed files with 21 additions and 36 deletions
|
@ -3,57 +3,42 @@ defmodule PhilomenaWeb.Image.RandomController do
|
||||||
|
|
||||||
alias PhilomenaWeb.ImageSorter
|
alias PhilomenaWeb.ImageSorter
|
||||||
alias PhilomenaWeb.ImageScope
|
alias PhilomenaWeb.ImageScope
|
||||||
|
alias PhilomenaWeb.ImageLoader
|
||||||
alias Philomena.Elasticsearch
|
alias Philomena.Elasticsearch
|
||||||
alias Philomena.Images.Query
|
|
||||||
alias Philomena.Images.Image
|
alias Philomena.Images.Image
|
||||||
|
|
||||||
def index(conn, params) do
|
def index(conn, params) do
|
||||||
user = conn.assigns.current_user
|
|
||||||
filter = conn.assigns.compiled_filter
|
|
||||||
|
|
||||||
scope = ImageScope.scope(conn)
|
scope = ImageScope.scope(conn)
|
||||||
query = query(user, params)
|
|
||||||
random_id = random_image_id(query, filter)
|
|
||||||
|
|
||||||
if random_id do
|
search_definition =
|
||||||
redirect(conn, to: Routes.image_path(conn, :show, random_id, scope))
|
ImageLoader.search_string(
|
||||||
else
|
conn,
|
||||||
redirect(conn, to: Routes.image_path(conn, :index))
|
query_string(params),
|
||||||
end
|
pagination: %{page_size: 1},
|
||||||
end
|
sorts: &ImageSorter.parse_sort(%{"sf" => "random"}, &1)
|
||||||
|
|
||||||
defp query(user, %{"q" => q}) do
|
|
||||||
{:ok, query} = Query.compile(user, q)
|
|
||||||
|
|
||||||
query
|
|
||||||
end
|
|
||||||
|
|
||||||
defp query(_user, _), do: %{match_all: %{}}
|
|
||||||
|
|
||||||
defp random_image_id(query, filter) do
|
|
||||||
%{query: query, sorts: sort} = ImageSorter.parse_sort(%{"sf" => "random"}, query)
|
|
||||||
|
|
||||||
Image
|
|
||||||
|> Elasticsearch.search_definition(
|
|
||||||
%{
|
|
||||||
query: %{
|
|
||||||
bool: %{
|
|
||||||
must: query,
|
|
||||||
must_not: [
|
|
||||||
filter,
|
|
||||||
%{term: %{hidden_from_users: true}}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
sort: sort
|
|
||||||
},
|
|
||||||
%{page_size: 1}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
case unwrap_random_result(search_definition) do
|
||||||
|
nil ->
|
||||||
|
redirect(conn, to: Routes.image_path(conn, :index))
|
||||||
|
|
||||||
|
random_id ->
|
||||||
|
redirect(conn, to: Routes.image_path(conn, :show, random_id, scope))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp query_string(%{"q" => query}), do: query
|
||||||
|
defp query_string(_params), do: "*"
|
||||||
|
|
||||||
|
defp unwrap_random_result({:ok, {definition, _tags}}) do
|
||||||
|
definition
|
||||||
|> Elasticsearch.search_records(Image)
|
|> Elasticsearch.search_records(Image)
|
||||||
|> Enum.to_list()
|
|> Enum.to_list()
|
||||||
|> unwrap()
|
|> unwrap()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp unwrap_random_result(_definition), do: nil
|
||||||
|
|
||||||
defp unwrap([image]), do: image.id
|
defp unwrap([image]), do: image.id
|
||||||
defp unwrap([]), do: nil
|
defp unwrap([]), do: nil
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue