2019-08-29 03:33:58 +02:00
|
|
|
defmodule PhilomenaWeb.SearchController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-01 03:22:05 +01:00
|
|
|
alias PhilomenaWeb.ImageLoader
|
2020-08-23 21:47:42 +02:00
|
|
|
alias Philomena.Images.Image
|
2024-05-25 20:03:45 +02:00
|
|
|
alias PhilomenaQuery.Search
|
2019-11-17 03:20:33 +01:00
|
|
|
alias Philomena.Interactions
|
2020-08-23 21:47:42 +02:00
|
|
|
import Ecto.Query
|
2019-08-29 03:33:58 +02:00
|
|
|
|
|
|
|
def index(conn, params) do
|
2019-11-17 03:20:33 +01:00
|
|
|
user = conn.assigns.current_user
|
2019-08-29 03:33:58 +02:00
|
|
|
|
2020-08-23 21:47:42 +02:00
|
|
|
case ImageLoader.search_string(conn, params["q"]) do
|
2019-12-05 22:57:59 +01:00
|
|
|
{:ok, {images, tags}} ->
|
2023-05-29 13:06:41 +02:00
|
|
|
images =
|
|
|
|
search_function(custom_ordering?(conn)).(
|
|
|
|
images,
|
|
|
|
preload(Image, [:sources, tags: :aliases])
|
|
|
|
)
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
interactions = Interactions.user_interactions(images, user)
|
2019-08-29 03:33:58 +02:00
|
|
|
|
2019-12-01 03:22:05 +01:00
|
|
|
conn
|
2020-01-11 05:20:19 +01:00
|
|
|
|> render("index.html",
|
|
|
|
title: "Searching for #{params["q"]}",
|
|
|
|
images: images,
|
|
|
|
tags: tags,
|
|
|
|
search_query: params["q"],
|
|
|
|
interactions: interactions,
|
|
|
|
layout_class: "layout--wide"
|
|
|
|
)
|
2019-11-17 03:20:33 +01:00
|
|
|
|
2019-11-15 16:40:10 +01:00
|
|
|
{:error, msg} ->
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "index.html",
|
|
|
|
title: "Searching for #{params["q"]}",
|
|
|
|
images: [],
|
2024-04-08 19:03:20 +02:00
|
|
|
tags: [],
|
2020-01-11 05:20:19 +01:00
|
|
|
error: msg,
|
|
|
|
search_query: params["q"]
|
|
|
|
)
|
2019-08-29 03:35:01 +02:00
|
|
|
end
|
2019-08-29 03:33:58 +02:00
|
|
|
end
|
2020-08-13 17:32:35 +02:00
|
|
|
|
2024-05-25 20:03:45 +02:00
|
|
|
defp search_function(true), do: &Search.search_records_with_hits/2
|
|
|
|
defp search_function(_custom), do: &Search.search_records/2
|
2020-08-23 21:47:42 +02:00
|
|
|
|
2020-08-13 17:32:35 +02:00
|
|
|
defp custom_ordering?(%{params: %{"sf" => sf}}) when sf != "id", do: true
|
|
|
|
defp custom_ordering?(_conn), do: false
|
2019-08-29 03:33:58 +02:00
|
|
|
end
|