2019-11-12 03:38:51 +01:00
|
|
|
defmodule PhilomenaWeb.Image.CommentController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-19 21:11:44 +01:00
|
|
|
alias PhilomenaWeb.CommentLoader
|
2019-11-12 03:38:51 +01:00
|
|
|
alias Philomena.{Images.Image, Comments.Comment, Textile.Renderer}
|
2019-12-05 20:23:26 +01:00
|
|
|
alias Philomena.UserStatistics
|
2019-11-17 05:59:24 +01:00
|
|
|
alias Philomena.Comments
|
|
|
|
alias Philomena.Images
|
2019-11-12 03:38:51 +01:00
|
|
|
|
2019-12-14 20:46:50 +01:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:create, :edit, :update]
|
|
|
|
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
|
|
|
|
|
2019-11-25 03:16:22 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :create_comment, edit: :create_comment, update: :create_comment
|
2019-12-30 14:58:56 +01:00
|
|
|
plug :load_resource, model: Image, id_name: "image_id", persisted: true
|
|
|
|
plug :verify_authorized when action in [:show]
|
2019-11-17 05:59:24 +01:00
|
|
|
|
|
|
|
# Undo the previous private parameter screwery
|
2019-12-30 14:58:56 +01:00
|
|
|
plug PhilomenaWeb.LoadCommentPlug, [param: "id", show_hidden: true] when action in [:show]
|
|
|
|
plug PhilomenaWeb.LoadCommentPlug, [param: "id"] when action in [:edit, :update]
|
2019-12-06 15:43:01 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :create, edit: :edit, update: :edit
|
2019-12-30 14:58:56 +01:00
|
|
|
plug :authorize_resource, model: Comment, only: [:edit, :update], preload: [:image, user: [awards: :badge]]
|
2019-11-17 05:59:24 +01:00
|
|
|
|
|
|
|
def index(conn, %{"comment_id" => comment_id}) do
|
2019-12-19 21:11:44 +01:00
|
|
|
page = CommentLoader.find_page(conn, conn.assigns.image, comment_id)
|
2019-11-17 05:59:24 +01:00
|
|
|
|
2019-12-19 21:11:44 +01:00
|
|
|
redirect(conn, to: Routes.image_comment_path(conn, :index, conn.assigns.image, page: page))
|
2019-11-17 05:59:24 +01:00
|
|
|
end
|
|
|
|
|
2019-11-12 03:38:51 +01:00
|
|
|
def index(conn, _params) do
|
2019-12-19 21:11:44 +01:00
|
|
|
comments = CommentLoader.load_comments(conn, conn.assigns.image)
|
2019-11-12 03:38:51 +01:00
|
|
|
|
2019-12-19 21:11:44 +01:00
|
|
|
rendered = Renderer.render_collection(comments.entries, conn)
|
2019-11-12 03:38:51 +01:00
|
|
|
|
|
|
|
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
|
2019-12-01 18:11:00 +01:00
|
|
|
rendered = Renderer.render_one(conn.assigns.comment, conn)
|
2019-11-12 03:38:51 +01:00
|
|
|
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-19 01:33:27 +01:00
|
|
|
case Comments.create_comment(image, 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)
|
2019-12-05 20:23:26 +01:00
|
|
|
UserStatistics.inc_stat(conn.assigns.current_user, :comments_posted)
|
2019-11-17 05:59:24 +01:00
|
|
|
|
2020-01-01 18:27:20 +01:00
|
|
|
index(conn, %{"comment_id" => comment.id})
|
2019-11-17 05:59:24 +01:00
|
|
|
|
|
|
|
_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()
|
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "edit.html", title: "Editing Comment", comment: conn.assigns.comment, changeset: changeset)
|
2019-11-17 05:59:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"comment" => comment_params}) do
|
2019-12-06 15:43:01 +01:00
|
|
|
case Comments.update_comment(conn.assigns.comment, conn.assigns.current_user, comment_params) do
|
|
|
|
{:ok, %{comment: comment}} ->
|
|
|
|
Comments.reindex_comment(comment)
|
|
|
|
|
2019-11-17 05:59:24 +01:00
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Comment updated successfully.")
|
2019-12-06 15:43:01 +01:00
|
|
|
|> redirect(to: Routes.image_path(conn, :show, conn.assigns.image) <> "#comment_#{comment.id}")
|
2019-11-17 05:59:24 +01:00
|
|
|
|
2019-12-06 16:14:25 +01:00
|
|
|
{:error, :comment, changeset, _changes} ->
|
|
|
|
render(conn, "edit.html", comment: conn.assigns.comment, changeset: changeset)
|
2019-11-17 05:59:24 +01:00
|
|
|
end
|
|
|
|
end
|
2019-12-30 14:58:56 +01:00
|
|
|
|
|
|
|
defp verify_authorized(conn, _params) do
|
|
|
|
image = conn.assigns.image
|
|
|
|
image =
|
|
|
|
case is_nil(image.duplicate_id) do
|
|
|
|
true -> image
|
|
|
|
_false -> Images.get_image!(image.duplicate_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
conn = assign(conn, :image, image)
|
|
|
|
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :show, image) do
|
|
|
|
true -> conn
|
|
|
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
end
|
|
|
|
end
|
2019-11-12 03:38:51 +01:00
|
|
|
end
|