philomena/lib/philomena_web/controllers/image/comment/hide_controller.ex

57 lines
1.7 KiB
Elixir
Raw Permalink Normal View History

2019-12-10 21:29:48 +01:00
defmodule PhilomenaWeb.Image.Comment.HideController do
use PhilomenaWeb, :controller
alias Philomena.Comments.Comment
alias Philomena.Comments
alias Philomena.Reports
2019-12-10 21:29:48 +01:00
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
plug :load_and_authorize_resource, model: Comment, id_name: "comment_id", persisted: true
def create(conn, %{"comment" => comment_params}) do
comment = conn.assigns.comment
user = conn.assigns.current_user
case Comments.hide_comment(comment, comment_params, user) do
{:ok, %{comment: comment, reports: {_count, reports}}} ->
2019-12-10 21:29:48 +01:00
Comments.reindex_comment(comment)
Reports.reindex_reports(reports)
2019-12-10 21:29:48 +01:00
conn
|> put_flash(:info, "Comment successfully hidden!")
2020-01-11 05:20:19 +01:00
|> redirect(
to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}"
)
_error ->
2019-12-10 21:29:48 +01:00
conn
|> put_flash(:error, "Unable to hide comment!")
2020-01-11 05:20:19 +01:00
|> redirect(
to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}"
)
2019-12-10 21:29:48 +01:00
end
end
def delete(conn, _params) do
comment = conn.assigns.comment
case Comments.unhide_comment(comment) do
{:ok, comment} ->
Comments.reindex_comment(comment)
conn
|> put_flash(:info, "Comment successfully unhidden!")
2020-01-11 05:20:19 +01:00
|> redirect(
to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}"
)
2019-12-10 21:29:48 +01:00
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to unhide comment!")
2020-01-11 05:20:19 +01:00
|> redirect(
to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}"
)
2019-12-10 21:29:48 +01:00
end
end
end