2019-11-12 03:38:51 +01:00
|
|
|
defmodule PhilomenaWeb.Image.CommentController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.{Images.Image, Comments.Comment, Textile.Renderer}
|
2019-11-17 05:59:24 +01:00
|
|
|
alias Philomena.Comments
|
|
|
|
alias Philomena.Images
|
2019-11-12 03:38:51 +01:00
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
2019-11-17 19:18:21 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :show, edit: :show, update: :show
|
2019-11-12 03:38:51 +01:00
|
|
|
plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
|
2019-11-17 05:59:24 +01:00
|
|
|
|
|
|
|
# Undo the previous private parameter screwery
|
2019-11-17 19:18:21 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :create, edit: :edit, update: :update
|
2019-11-12 03:53:19 +01:00
|
|
|
plug :load_and_authorize_resource, model: Comment, only: [:show], preload: [:image, user: [awards: :badge]]
|
2019-11-12 03:38:51 +01:00
|
|
|
|
2019-11-17 19:18:21 +01:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:create, :edit, :update]
|
2019-11-17 19:52:59 +01:00
|
|
|
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
|
2019-11-17 05:59:24 +01:00
|
|
|
|
|
|
|
def index(conn, %{"comment_id" => comment_id}) do
|
|
|
|
comment =
|
|
|
|
Comment
|
|
|
|
|> where(image_id: ^conn.assigns.image.id)
|
|
|
|
|> where(id: ^comment_id)
|
|
|
|
|> Repo.one!()
|
|
|
|
|
|
|
|
offset =
|
|
|
|
Comment
|
|
|
|
|> where(image_id: ^conn.assigns.image.id)
|
|
|
|
|> where([c], c.created_at > ^comment.created_at)
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
|
|
|
|
%{page_size: page_size} = conn.assigns.pagination
|
|
|
|
page = div(offset, page_size)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> redirect(to: Routes.image_comment_path(conn, :index, conn.assigns.image, page: page))
|
|
|
|
end
|
|
|
|
|
2019-11-12 03:38:51 +01:00
|
|
|
def index(conn, _params) do
|
|
|
|
comments =
|
|
|
|
Comment
|
|
|
|
|> where(image_id: ^conn.assigns.image.id)
|
|
|
|
|> order_by(desc: :created_at)
|
2019-11-12 03:53:19 +01:00
|
|
|
|> preload([:image, user: [awards: :badge]])
|
2019-11-12 03:38:51 +01:00
|
|
|
|> Repo.paginate(conn.assigns.scrivener)
|
|
|
|
|
|
|
|
rendered =
|
|
|
|
comments.entries
|
|
|
|
|> Renderer.render_collection()
|
|
|
|
|
|
|
|
comments =
|
|
|
|
%{comments | entries: Enum.zip(comments.entries, rendered)}
|
|
|
|
|
|
|
|
render(conn, "index.html", layout: false, image: conn.assigns.image, comments: comments)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, _params) do
|
|
|
|
rendered = Renderer.render_one(conn.assigns.comment)
|
|
|
|
render(conn, "show.html", layout: false, image: conn.assigns.image, comment: conn.assigns.comment, body: rendered)
|
|
|
|
end
|
2019-11-17 05:59:24 +01:00
|
|
|
|
|
|
|
def create(conn, %{"comment" => comment_params}) do
|
2019-11-17 19:52:59 +01:00
|
|
|
attributes = conn.assigns.attributes
|
2019-11-17 05:59:24 +01:00
|
|
|
image = conn.assigns.image
|
2019-11-18 02:53:08 +01:00
|
|
|
user = conn.assigns.current_user
|
2019-11-17 05:59:24 +01:00
|
|
|
|
2019-11-18 02:53:08 +01:00
|
|
|
case Comments.create_comment(image, user, attributes, comment_params) do
|
2019-11-17 05:59:24 +01:00
|
|
|
{:ok, %{comment: comment}} ->
|
|
|
|
Comments.notify_comment(comment)
|
|
|
|
Comments.reindex_comment(comment)
|
|
|
|
Images.reindex_image(conn.assigns.image)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Comment created successfully.")
|
|
|
|
|> redirect(to: Routes.image_path(conn, :show, image) <> "#comment_#{comment.id}")
|
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "There was an error posting your comment")
|
|
|
|
|> redirect(to: Routes.image_path(conn, :show, image))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
changeset =
|
|
|
|
conn.assigns.comment
|
|
|
|
|> Comments.change_comment()
|
|
|
|
|
|
|
|
render(conn, "edit.html", comment: conn.assigns.comment, changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"comment" => comment_params}) do
|
|
|
|
case Comments.update_comment(conn.assigns.comment, comment_params) do
|
|
|
|
{:ok, _comment} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Comment updated successfully.")
|
|
|
|
|> redirect(to: Routes.image_path(conn, :show, conn.assigns.image) <> "#comment_#{conn.assigns.comment.id}")
|
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "There was an error editing your comment")
|
|
|
|
|> redirect(to: Routes.image_path(conn, :show, conn.assigns.image))
|
|
|
|
end
|
|
|
|
end
|
2019-11-12 03:38:51 +01:00
|
|
|
end
|