philomena/lib/philomena_web/controllers/image_controller.ex
2019-12-02 09:55:48 -05:00

112 lines
3.1 KiB
Elixir

defmodule PhilomenaWeb.ImageController do
use PhilomenaWeb, :controller
alias PhilomenaWeb.ImageLoader
alias Philomena.{Images, Images.Image, Comments.Comment, Textile.Renderer}
alias Philomena.Servers.ImageProcessor
alias Philomena.Interactions
alias Philomena.Comments
alias Philomena.Tags
alias Philomena.Repo
import Ecto.Query
plug :load_and_authorize_resource, model: Image, only: :show, preload: [:tags, user: [awards: :badge]]
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
plug PhilomenaWeb.CaptchaPlug when action in [:create]
plug PhilomenaWeb.ScraperPlug, [params_name: "image", params_key: "image"] when action in [:create]
plug PhilomenaWeb.AdvertPlug when action in [:show]
def index(conn, _params) do
images = ImageLoader.query(conn, %{match_all: %{}})
interactions =
Interactions.user_interactions(images, conn.assigns.current_user)
render(conn, "index.html", layout_class: "layout--wide", images: images, interactions: interactions)
end
def show(conn, %{"id" => _id}) do
image = conn.assigns.image
user = conn.assigns.current_user
Images.clear_notification(image, user)
# Update the notification ticker in the header
conn = NotificationCountPlug.call(conn)
comments =
Comment
|> where(image_id: ^image.id)
|> preload([:image, user: [awards: :badge]])
|> order_by(desc: :created_at)
|> limit(25)
|> Repo.paginate(conn.assigns.comment_scrivener)
rendered =
comments.entries
|> Renderer.render_collection(conn)
comments =
%{comments | entries: Enum.zip(comments.entries, rendered)}
description =
%{body: image.description}
|> Renderer.render_one(conn)
interactions =
Interactions.user_interactions([image], conn.assigns.current_user)
comment_changeset =
%Comment{}
|> Comments.change_comment()
image_changeset =
image
|> Images.change_image()
watching =
Images.subscribed?(image, conn.assigns.current_user)
render(
conn,
"show.html",
image: image,
comments: comments,
image_changeset: image_changeset,
comment_changeset: comment_changeset,
description: description,
interactions: interactions,
watching: watching,
layout_class: "layout--wide"
)
end
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}} ->
ImageProcessor.cast(image.id)
Images.reindex_image(image)
Tags.reindex_tags(image.added_tags)
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
end