philomena/lib/philomena_web/controllers/image_controller.ex

71 lines
1.7 KiB
Elixir
Raw Normal View History

2019-08-18 18:17:05 +02:00
defmodule PhilomenaWeb.ImageController do
use PhilomenaWeb, :controller
2019-11-13 04:12:46 +01:00
alias Philomena.{Images.Image, Comments.Comment, Textile.Renderer}
2019-11-17 03:20:33 +01:00
alias Philomena.Interactions
alias Philomena.Comments
2019-10-05 02:51:56 +02:00
alias Philomena.Repo
2019-08-19 04:03:12 +02:00
import Ecto.Query
2019-08-18 18:17:05 +02:00
2019-11-12 03:53:19 +01:00
plug :load_and_authorize_resource, model: Image, only: :show, preload: [:tags, user: [awards: :badge]]
2019-08-29 03:14:54 +02:00
2019-08-18 18:17:05 +02:00
def index(conn, _params) do
2019-10-01 03:21:48 +02:00
query = conn.assigns.compiled_filter
2019-08-29 03:35:01 +02:00
2019-08-19 04:03:12 +02:00
images =
Image.search_records(
%{
2019-10-06 23:45:26 +02:00
query: %{bool: %{must_not: [query, %{term: %{hidden_from_users: true}}]}},
2019-08-29 03:14:54 +02:00
sort: %{created_at: :desc}
2019-08-19 04:03:12 +02:00
},
2019-10-09 01:19:57 +02:00
conn.assigns.pagination,
2019-10-04 03:03:33 +02:00
Image |> preload([:tags, :user])
2019-08-19 04:03:12 +02:00
)
2019-11-17 03:20:33 +01:00
interactions =
Interactions.user_interactions(images, conn.assigns.current_user)
render(conn, "index.html", images: images, interactions: interactions)
2019-08-18 18:17:05 +02:00
end
2019-10-04 01:58:54 +02:00
def show(conn, %{"id" => _id}) do
2019-11-17 03:20:33 +01:00
image = conn.assigns.image
2019-10-05 02:51:56 +02:00
comments =
Comment
2019-11-17 03:20:33 +01:00
|> where(image_id: ^image.id)
2019-11-12 03:53:19 +01:00
|> preload([:image, user: [awards: :badge]])
2019-10-05 02:51:56 +02:00
|> order_by(desc: :created_at)
|> limit(25)
2019-11-12 03:38:51 +01:00
|> Repo.paginate(conn.assigns.scrivener)
2019-10-05 02:51:56 +02:00
2019-11-11 00:35:52 +01:00
rendered =
2019-11-12 03:38:51 +01:00
comments.entries
2019-11-11 00:35:52 +01:00
|> Renderer.render_collection()
comments =
2019-11-12 03:38:51 +01:00
%{comments | entries: Enum.zip(comments.entries, rendered)}
2019-11-11 00:35:52 +01:00
2019-11-11 23:57:29 +01:00
description =
2019-11-17 03:20:33 +01:00
%{body: image.description}
2019-11-11 23:57:29 +01:00
|> Renderer.render_one()
2019-11-17 03:20:33 +01:00
interactions =
Interactions.user_interactions([image], conn.assigns.current_user)
comment_changeset =
%Comment{}
|> Comments.change_comment()
render(
conn,
"show.html",
image: image,
comments: comments,
comment_changeset: comment_changeset,
description: description,
interactions: interactions
)
2019-08-18 18:17:05 +02:00
end
end