2019-11-18 05:47:09 +01:00
|
|
|
defmodule PhilomenaWeb.GalleryController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-05 01:11:31 +01:00
|
|
|
alias PhilomenaWeb.ImageLoader
|
2019-12-05 05:12:49 +01:00
|
|
|
alias PhilomenaWeb.NotificationCountPlug
|
2019-12-24 22:14:42 +01:00
|
|
|
alias Philomena.Elasticsearch
|
2019-12-05 01:11:31 +01:00
|
|
|
alias Philomena.Interactions
|
2019-11-18 05:47:09 +01:00
|
|
|
alias Philomena.Galleries.Gallery
|
2019-12-05 01:11:31 +01:00
|
|
|
alias Philomena.Galleries
|
2019-11-18 05:47:09 +01:00
|
|
|
import Ecto.Query
|
|
|
|
|
2019-12-05 01:11:31 +01:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create, :edit, :update, :delete]
|
2020-08-01 18:42:29 +02:00
|
|
|
plug PhilomenaWeb.MapParameterPlug, [param: "gallery"] when action in [:index]
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Gallery,
|
|
|
|
except: [:index],
|
|
|
|
preload: [:creator, thumbnail: :tags]
|
2019-11-18 05:47:09 +01:00
|
|
|
|
|
|
|
def index(conn, params) do
|
|
|
|
galleries =
|
2019-12-24 22:14:42 +01:00
|
|
|
Elasticsearch.search_records(
|
|
|
|
Gallery,
|
2019-11-18 05:47:09 +01:00
|
|
|
%{
|
|
|
|
query: %{
|
|
|
|
bool: %{
|
|
|
|
must: parse_search(params)
|
|
|
|
}
|
|
|
|
},
|
2020-01-11 05:20:19 +01:00
|
|
|
sort: parse_sort(params)
|
2019-11-18 05:47:09 +01:00
|
|
|
},
|
|
|
|
conn.assigns.pagination,
|
2019-11-18 05:51:39 +01:00
|
|
|
Gallery |> preload([:creator, thumbnail: :tags])
|
2019-11-18 05:47:09 +01:00
|
|
|
)
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "index.html",
|
|
|
|
title: "Galleries",
|
|
|
|
galleries: galleries,
|
|
|
|
layout_class: "layout--wide"
|
|
|
|
)
|
2019-11-18 05:47:09 +01:00
|
|
|
end
|
|
|
|
|
2019-11-18 15:25:56 +01:00
|
|
|
def show(conn, _params) do
|
2019-12-05 01:11:31 +01:00
|
|
|
gallery = conn.assigns.gallery
|
2019-12-05 05:12:49 +01:00
|
|
|
user = conn.assigns.current_user
|
2019-12-05 01:11:31 +01:00
|
|
|
query = "gallery_id:#{gallery.id}"
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
params =
|
|
|
|
Map.merge(conn.params, %{
|
|
|
|
"q" => query,
|
|
|
|
"sf" => "gallery_id:#{gallery.id}",
|
|
|
|
"sd" => position_order(gallery)
|
|
|
|
})
|
|
|
|
|
2020-05-29 01:43:17 +02:00
|
|
|
conn = %{conn | params: params}
|
2019-12-05 01:11:31 +01:00
|
|
|
|
2020-08-13 17:32:35 +02:00
|
|
|
{:ok, {images, _tags}} = ImageLoader.search_string(conn, query, include_hits: true)
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2020-05-29 01:43:17 +02:00
|
|
|
{gallery_prev, gallery_next} = prev_next_page_images(conn, query)
|
2020-05-01 21:51:28 +02:00
|
|
|
|
|
|
|
interactions = Interactions.user_interactions([images, gallery_prev, gallery_next], user)
|
2019-12-05 05:12:49 +01:00
|
|
|
|
|
|
|
watching = Galleries.subscribed?(gallery, user)
|
2020-05-01 21:51:28 +02:00
|
|
|
|
|
|
|
prev_image = if gallery_prev, do: [gallery_prev], else: []
|
|
|
|
next_image = if gallery_next, do: [gallery_next], else: []
|
|
|
|
|
|
|
|
gallery_images = prev_image ++ Enum.to_list(images) ++ next_image
|
2020-08-13 17:32:35 +02:00
|
|
|
gallery_json = Jason.encode!(Enum.map(gallery_images, &elem(&1, 0).id))
|
2019-12-05 05:12:49 +01:00
|
|
|
|
|
|
|
Galleries.clear_notification(gallery, user)
|
2019-12-05 01:11:31 +01:00
|
|
|
|
|
|
|
conn
|
2019-12-05 05:12:49 +01:00
|
|
|
|> NotificationCountPlug.call([])
|
2019-12-05 01:11:31 +01:00
|
|
|
|> Map.put(:params, params)
|
2020-05-01 21:51:28 +02:00
|
|
|
|> assign(:clientside_data, gallery_images: gallery_json)
|
2020-01-11 05:20:19 +01:00
|
|
|
|> render("show.html",
|
|
|
|
title: "Showing Gallery",
|
|
|
|
layout_class: "layout--wide",
|
|
|
|
watching: watching,
|
|
|
|
gallery: gallery,
|
2020-05-01 21:51:28 +02:00
|
|
|
gallery_prev: gallery_prev,
|
|
|
|
gallery_next: gallery_next,
|
|
|
|
gallery_images: gallery_images,
|
2020-01-11 05:20:19 +01:00
|
|
|
images: images,
|
|
|
|
interactions: interactions
|
|
|
|
)
|
2019-12-05 01:11:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def new(conn, _params) do
|
|
|
|
changeset = Galleries.change_gallery(%Gallery{})
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "new.html", title: "New Gallery", changeset: changeset)
|
2019-12-05 01:11:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"gallery" => gallery_params}) do
|
|
|
|
user = conn.assigns.current_user
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-05 01:11:31 +01:00
|
|
|
case Galleries.create_gallery(user, gallery_params) do
|
|
|
|
{:ok, gallery} ->
|
|
|
|
Galleries.reindex_gallery(gallery)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Gallery successfully created.")
|
|
|
|
|> redirect(to: Routes.gallery_path(conn, :show, gallery))
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
conn
|
|
|
|
|> render("new.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
gallery = conn.assigns.gallery
|
|
|
|
changeset = Galleries.change_gallery(gallery)
|
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "edit.html", title: "Editing Gallery", gallery: gallery, changeset: changeset)
|
2019-12-05 01:11:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"gallery" => gallery_params}) do
|
|
|
|
gallery = conn.assigns.gallery
|
|
|
|
|
|
|
|
case Galleries.update_gallery(gallery, gallery_params) do
|
|
|
|
{:ok, gallery} ->
|
|
|
|
Galleries.reindex_gallery(gallery)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Gallery successfully updated.")
|
|
|
|
|> redirect(to: Routes.gallery_path(conn, :show, gallery))
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
conn
|
|
|
|
|> render("edit.html", gallery: gallery, changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
gallery = conn.assigns.gallery
|
|
|
|
|
|
|
|
{:ok, _gallery} = Galleries.delete_gallery(gallery)
|
|
|
|
Galleries.unindex_gallery(gallery)
|
|
|
|
|
2019-11-18 15:25:56 +01:00
|
|
|
conn
|
2019-12-05 01:11:31 +01:00
|
|
|
|> put_flash(:info, "Gallery successfully destroyed.")
|
|
|
|
|> redirect(to: Routes.gallery_path(conn, :index))
|
2019-11-18 05:47:09 +01:00
|
|
|
end
|
|
|
|
|
2020-05-29 01:43:17 +02:00
|
|
|
defp prev_next_page_images(conn, query) do
|
2020-05-01 21:51:28 +02:00
|
|
|
limit = conn.assigns.image_pagination.page_size
|
|
|
|
offset = (conn.assigns.image_pagination.page_number - 1) * limit
|
|
|
|
|
|
|
|
# Inconsistency: Elasticsearch doesn't allow requesting offsets which are less than 0,
|
|
|
|
# but it does allow requesting offsets which are beyond the total number of results.
|
|
|
|
|
2020-05-29 01:43:17 +02:00
|
|
|
prev_image = gallery_image(offset - 1, conn, query)
|
|
|
|
next_image = gallery_image(offset + limit, conn, query)
|
2020-05-01 21:51:28 +02:00
|
|
|
|
|
|
|
{prev_image, next_image}
|
|
|
|
end
|
|
|
|
|
2020-05-29 01:43:17 +02:00
|
|
|
defp gallery_image(offset, _conn, _query) when offset < 0, do: nil
|
2020-05-01 21:51:28 +02:00
|
|
|
|
2020-05-29 01:43:17 +02:00
|
|
|
defp gallery_image(offset, conn, query) do
|
2020-05-01 21:51:28 +02:00
|
|
|
pagination_params = %{page_number: offset + 1, page_size: 1}
|
|
|
|
|
2020-08-13 17:32:35 +02:00
|
|
|
{:ok, {image, _tags}} =
|
|
|
|
ImageLoader.search_string(conn, query, pagination: pagination_params, include_hits: true)
|
2020-05-01 21:51:28 +02:00
|
|
|
|
|
|
|
case Enum.to_list(image) do
|
|
|
|
[image] -> image
|
|
|
|
[] -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-18 05:47:09 +01:00
|
|
|
defp parse_search(%{"gallery" => gallery_params}) do
|
|
|
|
parse_title(gallery_params) ++
|
2020-01-11 05:20:19 +01:00
|
|
|
parse_creator(gallery_params) ++
|
|
|
|
parse_included_image(gallery_params) ++
|
|
|
|
parse_description(gallery_params)
|
2019-11-18 05:47:09 +01:00
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-18 05:47:09 +01:00
|
|
|
defp parse_search(_params), do: [%{match_all: %{}}]
|
|
|
|
|
|
|
|
defp parse_title(%{"title" => title}) when is_binary(title) and title not in [nil, ""],
|
|
|
|
do: [%{wildcard: %{title: "*" <> String.downcase(title) <> "*"}}]
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-18 05:47:09 +01:00
|
|
|
defp parse_title(_params), do: []
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
defp parse_creator(%{"creator" => creator})
|
|
|
|
when is_binary(creator) and creator not in [nil, ""],
|
|
|
|
do: [%{term: %{creator: String.downcase(creator)}}]
|
|
|
|
|
2019-11-18 05:47:09 +01:00
|
|
|
defp parse_creator(_params), do: []
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
defp parse_included_image(%{"include_image" => image_id})
|
|
|
|
when is_binary(image_id) and image_id not in [nil, ""] do
|
2019-11-18 05:47:09 +01:00
|
|
|
with {image_id, _rest} <- Integer.parse(image_id) do
|
2019-12-16 07:07:10 +01:00
|
|
|
[%{term: %{image_ids: image_id}}]
|
2019-11-18 05:47:09 +01:00
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-18 05:47:09 +01:00
|
|
|
defp parse_included_image(_params), do: []
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
defp parse_description(%{"description" => description})
|
|
|
|
when is_binary(description) and description not in [nil, ""],
|
|
|
|
do: [%{match_phrase: %{description: description}}]
|
|
|
|
|
2019-11-18 05:47:09 +01:00
|
|
|
defp parse_description(_params), do: []
|
|
|
|
|
|
|
|
defp parse_sort(%{"gallery" => %{"sf" => sf, "sd" => sd}})
|
2020-01-11 05:20:19 +01:00
|
|
|
when sf in ["created_at", "updated_at", "image_count", "_score"] and
|
|
|
|
sd in ["desc", "asc"] do
|
2019-11-18 05:47:09 +01:00
|
|
|
%{sf => sd}
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-18 05:47:09 +01:00
|
|
|
defp parse_sort(_params) do
|
|
|
|
%{created_at: :desc}
|
|
|
|
end
|
2019-12-05 01:11:31 +01:00
|
|
|
|
|
|
|
defp position_order(%{order_position_asc: true}), do: "asc"
|
|
|
|
defp position_order(_gallery), do: "desc"
|
2019-12-16 07:07:10 +01:00
|
|
|
end
|