philomena/lib/philomena_web/controllers/image_controller.ex

165 lines
4.6 KiB
Elixir
Raw Normal View History

2019-08-18 18:17:05 +02:00
defmodule PhilomenaWeb.ImageController do
use PhilomenaWeb, :controller
2019-12-01 03:22:05 +01:00
alias PhilomenaWeb.ImageLoader
2019-12-02 16:02:48 +01:00
alias PhilomenaWeb.NotificationCountPlug
2019-12-05 05:12:49 +01:00
alias Philomena.{Images, Images.Image, Comments.Comment, Galleries.Gallery, Galleries.Interaction, Textile.Renderer}
2019-11-27 02:45:57 +01:00
alias Philomena.Servers.ImageProcessor
alias Philomena.UserStatistics
2019-11-17 03:20:33 +01:00
alias Philomena.Interactions
alias Philomena.Comments
2019-11-27 02:45:57 +01:00
alias Philomena.Tags
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-12-07 17:48:39 +01:00
plug :load_image
2019-08-29 03:14:54 +02:00
2019-11-26 05:51:17 +01:00
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
plug PhilomenaWeb.CaptchaPlug when action in [:create]
2019-11-29 00:19:47 +01:00
plug PhilomenaWeb.ScraperPlug, [params_name: "image", params_key: "image"] when action in [:create]
2019-11-29 07:26:05 +01:00
plug PhilomenaWeb.AdvertPlug when action in [:show]
2019-11-26 05:51:17 +01:00
2019-08-18 18:17:05 +02:00
def index(conn, _params) do
2019-12-05 22:57:59 +01:00
{images, _tags} = ImageLoader.query(conn, %{match_all: %{}})
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)
2019-11-17 23:51:14 +01:00
render(conn, "index.html", layout_class: "layout--wide", 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-12-01 06:03:45 +01:00
user = conn.assigns.current_user
Images.clear_notification(image, user)
2019-11-17 03:20:33 +01:00
2019-12-02 15:55:48 +01:00
# Update the notification ticker in the header
conn = NotificationCountPlug.call(conn)
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-30 23:40:53 +01:00
|> Repo.paginate(conn.assigns.comment_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-12-01 18:11:00 +01:00
|> Renderer.render_collection(conn)
2019-11-11 00:35:52 +01:00
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-12-01 18:11:00 +01:00
|> Renderer.render_one(conn)
2019-11-11 23:57:29 +01:00
2019-11-17 03:20:33 +01:00
interactions =
Interactions.user_interactions([image], conn.assigns.current_user)
comment_changeset =
%Comment{}
|> Comments.change_comment()
image_changeset =
image
|> Images.change_image()
2019-11-17 18:50:42 +01:00
watching =
Images.subscribed?(image, conn.assigns.current_user)
2019-12-05 05:12:49 +01:00
{user_galleries, image_galleries} = image_and_user_galleries(image, conn.assigns.current_user)
render(
conn,
"show.html",
image: image,
comments: comments,
image_changeset: image_changeset,
comment_changeset: comment_changeset,
2019-12-05 05:12:49 +01:00
image_galleries: image_galleries,
user_galleries: user_galleries,
description: description,
2019-11-17 18:50:42 +01:00
interactions: interactions,
2019-11-17 23:51:14 +01:00
watching: watching,
layout_class: "layout--wide"
)
2019-08-18 18:17:05 +02:00
end
2019-11-26 05:51:17 +01:00
def new(conn, _params) do
changeset =
%Image{}
|> Images.change_image()
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"image" => image_params}) do
attributes = conn.assigns.attributes
case Images.create_image(attributes, image_params) do
{:ok, %{image: image}} ->
2019-11-27 02:45:57 +01:00
ImageProcessor.cast(image.id)
2019-11-26 05:51:17 +01:00
Images.reindex_image(image)
2019-11-27 02:45:57 +01:00
Tags.reindex_tags(image.added_tags)
UserStatistics.inc_stat(conn.assigns.current_user, :uploads)
2019-11-26 05:51:17 +01:00
conn
|> put_flash(:info, "Image created successfully.")
|> redirect(to: Routes.image_path(conn, :show, image))
{:error, :image, changeset, _} ->
conn
|> render("new.html", changeset: changeset)
end
end
2019-12-05 05:12:49 +01:00
defp image_and_user_galleries(_image, nil), do: {[], []}
defp image_and_user_galleries(image, user) do
image_galleries =
Gallery
|> where(creator_id: ^user.id)
|> join(:inner, [g], gi in Interaction, on: g.id == gi.gallery_id and gi.image_id == ^image.id)
|> Repo.all()
image_gallery_ids = Enum.map(image_galleries, & &1.id)
user_galleries =
Gallery
|> where(creator_id: ^user.id)
|> where([g], g.id not in ^image_gallery_ids)
|> Repo.all()
{user_galleries, image_galleries}
end
2019-12-07 17:48:39 +01:00
defp load_image(conn, _opts) do
id = conn.params["id"]
image =
Image
|> where(id: ^id)
|> preload([:tags, :deleter, user: [awards: :badge]])
|> Repo.one()
cond do
is_nil(image) ->
PhilomenaWeb.NotFoundPlug.call(conn)
2019-12-07 17:52:27 +01:00
not is_nil(image.duplicate_id) and not Canada.Can.can?(conn.assigns.current_user, :show, image) ->
2019-12-07 17:48:39 +01:00
conn
|> put_flash(:info, "The image you were looking for has been marked a duplicate of the image below")
|> redirect(to: Routes.image_path(conn, :show, image.duplicate_id))
image.hidden_from_users ->
render(conn, "deleted.html", image: image)
true ->
assign(conn, :image, image)
end
end
2019-08-18 18:17:05 +02:00
end