2019-11-30 03:33:15 +01:00
|
|
|
defmodule PhilomenaWeb.Image.NavigateController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-22 22:02:27 +01:00
|
|
|
alias PhilomenaWeb.ImageLoader
|
2020-05-08 04:10:54 +02:00
|
|
|
alias PhilomenaWeb.ImageNavigator
|
|
|
|
alias PhilomenaWeb.ImageScope
|
2024-05-25 20:03:45 +02:00
|
|
|
alias PhilomenaQuery.Search
|
2019-11-30 03:33:15 +01:00
|
|
|
alias Philomena.Images.Image
|
|
|
|
alias Philomena.Images.Query
|
|
|
|
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, index: :show
|
|
|
|
plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
|
|
|
|
|
2020-08-13 17:32:35 +02:00
|
|
|
def index(conn, %{"rel" => rel}) when rel in ~W(prev next) do
|
2019-11-30 03:33:15 +01:00
|
|
|
image = conn.assigns.image
|
|
|
|
filter = conn.assigns.compiled_filter
|
2020-05-08 04:10:54 +02:00
|
|
|
scope = ImageScope.scope(conn)
|
2019-11-30 03:33:15 +01:00
|
|
|
|
|
|
|
conn
|
2020-08-13 17:32:35 +02:00
|
|
|
|> ImageNavigator.find_consecutive(image, compile_query(conn), filter)
|
|
|
|
|> case do
|
|
|
|
{next_image, hit} ->
|
|
|
|
redirect(conn,
|
2024-04-29 02:55:27 +02:00
|
|
|
to: ~p"/images/#{next_image}?#{Keyword.put(scope, :sort, hit["sort"])}"
|
2020-08-13 17:32:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
nil ->
|
2024-04-29 02:55:27 +02:00
|
|
|
redirect(conn, to: ~p"/images/#{image}?#{scope}")
|
2020-08-13 17:32:35 +02:00
|
|
|
end
|
2019-11-30 03:33:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def index(conn, %{"rel" => "find"}) do
|
2019-12-22 22:02:27 +01:00
|
|
|
pagination = %{conn.assigns.image_pagination | page_number: 1}
|
2019-11-30 03:33:15 +01:00
|
|
|
|
2019-12-22 22:02:27 +01:00
|
|
|
# Find does not use the current search scope
|
|
|
|
# (although it probably should).
|
|
|
|
body = %{range: %{id: %{gt: conn.assigns.image.id}}}
|
2019-11-30 03:33:15 +01:00
|
|
|
|
2020-08-23 21:47:42 +02:00
|
|
|
{images, _tags} = ImageLoader.query(conn, body, pagination: pagination)
|
2024-05-25 20:03:45 +02:00
|
|
|
images = Search.search_records(images, Image)
|
2019-11-30 03:33:15 +01:00
|
|
|
|
2019-12-22 22:02:27 +01:00
|
|
|
page_num = page_for_offset(pagination.page_size, images.total_entries)
|
|
|
|
|
2024-04-29 02:55:27 +02:00
|
|
|
redirect(conn, to: ~p"/search?#{[q: "*", page: page_num]}")
|
2019-11-30 03:33:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp page_for_offset(per_page, offset) do
|
2019-12-22 22:05:41 +01:00
|
|
|
offset
|
|
|
|
|> div(per_page)
|
|
|
|
|> Kernel.+(1)
|
2019-11-30 03:33:15 +01:00
|
|
|
|> to_string()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp compile_query(conn) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
2019-12-08 16:00:54 +01:00
|
|
|
{:ok, query} = Query.compile(user, match_all_if_blank(conn.params["q"]))
|
2019-11-30 03:33:15 +01:00
|
|
|
|
|
|
|
query
|
|
|
|
end
|
2019-12-08 16:00:54 +01:00
|
|
|
|
|
|
|
defp match_all_if_blank(nil), do: "*"
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-08 16:00:54 +01:00
|
|
|
defp match_all_if_blank(input) do
|
|
|
|
case String.trim(input) == "" do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> "*"
|
2019-12-08 16:00:54 +01:00
|
|
|
false -> input
|
|
|
|
end
|
|
|
|
end
|
2019-12-22 22:02:27 +01:00
|
|
|
end
|