2019-12-01 03:22:05 +01:00
|
|
|
defmodule PhilomenaWeb.ImageLoader do
|
2020-05-29 01:43:17 +02:00
|
|
|
alias PhilomenaWeb.ImageSorter
|
2024-05-25 20:03:45 +02:00
|
|
|
alias PhilomenaQuery.Search
|
2019-12-01 03:22:05 +01:00
|
|
|
alias Philomena.Images.{Image, Query}
|
2021-09-29 22:24:38 +02:00
|
|
|
alias PhilomenaWeb.MarkdownRenderer
|
2019-12-05 22:57:59 +01:00
|
|
|
alias Philomena.Tags.Tag
|
|
|
|
alias Philomena.Repo
|
2019-12-01 03:22:05 +01:00
|
|
|
import Ecto.Query
|
|
|
|
|
2021-04-01 18:49:41 +02:00
|
|
|
# sobelow_skip ["SQL.Query"]
|
2019-12-01 03:22:05 +01:00
|
|
|
def search_string(conn, search_string, options \\ []) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
2024-07-13 20:59:36 +02:00
|
|
|
with {:ok, tree} <- Query.compile(search_string, user: user) do
|
2019-12-01 03:22:05 +01:00
|
|
|
{:ok, query(conn, tree, options)}
|
|
|
|
else
|
|
|
|
error ->
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def query(conn, body, options \\ []) do
|
2020-01-11 05:20:19 +01:00
|
|
|
pagination = Keyword.get(options, :pagination, conn.assigns.image_pagination)
|
2020-05-29 01:43:17 +02:00
|
|
|
sorts = Keyword.get(options, :sorts, &ImageSorter.parse_sort(conn.params, &1))
|
2019-12-01 03:22:05 +01:00
|
|
|
|
2019-12-27 00:58:52 +01:00
|
|
|
tags =
|
|
|
|
body
|
|
|
|
|> search_tag_names()
|
|
|
|
|> load_tags()
|
|
|
|
|> render_bodies(conn)
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
user = conn.assigns.current_user
|
|
|
|
filter = conn.assigns.compiled_filter
|
2019-12-18 01:42:50 +01:00
|
|
|
filters = create_filters(conn, user, filter)
|
2020-05-29 01:43:17 +02:00
|
|
|
|
|
|
|
%{query: query, sorts: sort} = sorts.(body)
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2020-08-23 21:47:42 +02:00
|
|
|
definition =
|
2024-05-25 20:03:45 +02:00
|
|
|
Search.search_definition(
|
2019-12-24 22:14:42 +01:00
|
|
|
Image,
|
2019-12-05 22:57:59 +01:00
|
|
|
%{
|
|
|
|
query: %{
|
|
|
|
bool: %{
|
2020-05-29 01:43:17 +02:00
|
|
|
must: query,
|
2019-12-05 22:57:59 +01:00
|
|
|
must_not: filters
|
|
|
|
}
|
|
|
|
},
|
2020-05-29 01:43:17 +02:00
|
|
|
sort: sort
|
2019-12-01 03:22:05 +01:00
|
|
|
},
|
2020-08-23 21:47:42 +02:00
|
|
|
pagination
|
2019-12-05 22:57:59 +01:00
|
|
|
)
|
|
|
|
|
2020-08-23 21:47:42 +02:00
|
|
|
{definition, tags}
|
2019-12-01 03:22:05 +01:00
|
|
|
end
|
|
|
|
|
2019-12-18 01:42:50 +01:00
|
|
|
defp create_filters(conn, user, filter) do
|
|
|
|
show_hidden? = Canada.Can.can?(user, :hide, %Image{})
|
|
|
|
del = conn.params["del"]
|
|
|
|
hidden = conn.params["hidden"]
|
|
|
|
|
2019-12-01 03:22:05 +01:00
|
|
|
[
|
2019-12-18 01:42:50 +01:00
|
|
|
filter
|
2019-12-01 03:22:05 +01:00
|
|
|
]
|
2019-12-18 01:42:50 +01:00
|
|
|
|> maybe_show_deleted(show_hidden?, del)
|
|
|
|
|> maybe_custom_hide(user, hidden)
|
2022-03-24 17:31:57 +01:00
|
|
|
|> hide_non_approved()
|
2019-12-01 03:22:05 +01:00
|
|
|
end
|
|
|
|
|
2019-12-18 01:42:50 +01:00
|
|
|
# Allow moderators to index hidden images
|
2020-02-01 17:04:11 +01:00
|
|
|
|
2020-01-14 12:54:15 +01:00
|
|
|
defp maybe_show_deleted(filters, _show_hidden?, "1"),
|
|
|
|
do: filters
|
2019-12-18 01:42:50 +01:00
|
|
|
|
|
|
|
defp maybe_show_deleted(filters, false, _param),
|
|
|
|
do: [%{term: %{hidden_from_users: true}} | filters]
|
|
|
|
|
|
|
|
defp maybe_show_deleted(filters, true, "only"),
|
|
|
|
do: [%{term: %{hidden_from_users: false}} | filters]
|
|
|
|
|
2019-12-19 23:39:34 +01:00
|
|
|
defp maybe_show_deleted(filters, true, "deleted"),
|
2020-01-11 05:20:19 +01:00
|
|
|
do: [%{term: %{hidden_from_users: false}}, %{exists: %{field: :duplicate_id}} | filters]
|
2019-12-19 23:39:34 +01:00
|
|
|
|
2019-12-18 01:42:50 +01:00
|
|
|
defp maybe_show_deleted(filters, true, _param),
|
|
|
|
do: [%{term: %{hidden_from_users: true}} | filters]
|
|
|
|
|
|
|
|
# Allow users to reverse the effect of hiding images,
|
|
|
|
# if desired
|
|
|
|
|
2019-12-18 01:44:26 +01:00
|
|
|
defp maybe_custom_hide(filters, %{id: _id}, "1"),
|
2019-12-18 01:42:50 +01:00
|
|
|
do: filters
|
|
|
|
|
|
|
|
defp maybe_custom_hide(filters, %{id: id}, _param),
|
2019-12-01 03:22:05 +01:00
|
|
|
do: [%{term: %{hidden_by_user_ids: id}} | filters]
|
|
|
|
|
2019-12-18 01:42:50 +01:00
|
|
|
defp maybe_custom_hide(filters, _user, _param),
|
2019-12-01 03:22:05 +01:00
|
|
|
do: filters
|
2019-12-05 22:57:59 +01:00
|
|
|
|
2022-03-24 17:31:57 +01:00
|
|
|
# Hide all images that aren't approved from all search queries.
|
|
|
|
defp hide_non_approved(filters),
|
|
|
|
do: [%{term: %{approved: false}} | filters]
|
|
|
|
|
2019-12-05 22:57:59 +01:00
|
|
|
# TODO: the search parser should try to optimize queries
|
|
|
|
defp search_tag_name(%{term: %{"namespaced_tags.name" => tag_name}}), do: [tag_name]
|
|
|
|
defp search_tag_name(_other_query), do: []
|
|
|
|
|
|
|
|
defp search_tag_names(%{bool: %{must: musts}}), do: Enum.flat_map(musts, &search_tag_name(&1))
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
defp search_tag_names(%{bool: %{should: shoulds}}),
|
|
|
|
do: Enum.flat_map(shoulds, &search_tag_name(&1))
|
|
|
|
|
2019-12-05 22:57:59 +01:00
|
|
|
defp search_tag_names(%{term: %{"namespaced_tags.name" => tag_name}}), do: [tag_name]
|
|
|
|
defp search_tag_names(_other_query), do: []
|
|
|
|
|
|
|
|
defp load_tags([]), do: []
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-05 22:57:59 +01:00
|
|
|
defp load_tags(tags) do
|
|
|
|
Tag
|
|
|
|
|> join(:left, [t], at in Tag, on: t.id == at.aliased_tag_id)
|
|
|
|
|> where([t, at], t.name in ^tags or at.name in ^tags)
|
2020-01-11 05:20:19 +01:00
|
|
|
|> preload([
|
|
|
|
:aliases,
|
|
|
|
:aliased_tag,
|
|
|
|
:implied_tags,
|
|
|
|
:implied_by_tags,
|
|
|
|
:dnp_entries,
|
2020-05-13 20:17:04 +02:00
|
|
|
:channels,
|
2020-01-11 05:20:19 +01:00
|
|
|
public_links: :user,
|
|
|
|
hidden_links: :user
|
|
|
|
])
|
2019-12-05 22:57:59 +01:00
|
|
|
|> Repo.all()
|
2019-12-08 02:56:08 +01:00
|
|
|
|> Enum.uniq_by(& &1.id)
|
2019-12-08 03:00:36 +01:00
|
|
|
|> Enum.filter(&is_nil(&1.aliased_tag))
|
2019-12-08 19:04:06 +01:00
|
|
|
|> Tag.display_order()
|
2019-12-05 22:57:59 +01:00
|
|
|
end
|
|
|
|
|
2019-12-05 23:01:09 +01:00
|
|
|
defp render_bodies([], _conn), do: []
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-05 23:01:09 +01:00
|
|
|
defp render_bodies([tag], conn) do
|
2019-12-05 22:57:59 +01:00
|
|
|
dnp_bodies =
|
2021-09-29 22:24:38 +02:00
|
|
|
MarkdownRenderer.render_collection(
|
2021-09-29 22:05:16 +02:00
|
|
|
Enum.map(tag.dnp_entries, &%{body: &1.conditions || ""}),
|
2020-05-20 20:23:57 +02:00
|
|
|
conn
|
|
|
|
)
|
2019-12-05 22:57:59 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
dnp_entries = Enum.zip(dnp_bodies, tag.dnp_entries)
|
2019-12-05 22:57:59 +01:00
|
|
|
|
2021-09-30 04:09:21 +02:00
|
|
|
description = MarkdownRenderer.render_one(%{body: tag.description || ""}, conn)
|
2019-12-05 23:01:09 +01:00
|
|
|
|
|
|
|
[{tag, description, dnp_entries}]
|
2019-12-05 22:57:59 +01:00
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-05 23:01:09 +01:00
|
|
|
defp render_bodies(tags, _conn), do: tags
|
2019-12-18 01:42:50 +01:00
|
|
|
end
|