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

58 lines
1.8 KiB
Elixir
Raw 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
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
2020-09-07 20:50:34 +02:00
{:ok, comment} ->
2019-12-10 21:29:48 +01:00
conn
|> put_flash(:info, "Comment successfully hidden!")
|> moderation_log(details: &log_details/3, data: comment)
|> redirect(to: ~p"/images/#{comment.image_id}" <> "#comment_#{comment.id}")
_error ->
2019-12-10 21:29:48 +01:00
conn
|> put_flash(:error, "Unable to hide comment!")
|> redirect(to: ~p"/images/#{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} ->
conn
|> put_flash(:info, "Comment successfully unhidden!")
|> moderation_log(details: &log_details/3, data: comment)
|> redirect(to: ~p"/images/#{comment.image_id}" <> "#comment_#{comment.id}")
2019-12-10 21:29:48 +01:00
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to unhide comment!")
|> redirect(to: ~p"/images/#{comment.image_id}" <> "#comment_#{comment.id}")
2019-12-10 21:29:48 +01:00
end
end
2021-11-07 19:51:55 +01:00
defp log_details(_conn, action, comment) do
2021-11-07 19:51:55 +01:00
body =
case action do
:create -> "Hidden comment on image >>#{comment.image_id} (#{comment.deletion_reason})"
:delete -> "Restored comment on image >>#{comment.image_id}"
end
%{
body: body,
subject_path: ~p"/images/#{comment.image_id}" <> "#comment_#{comment.id}"
2021-11-07 19:51:55 +01:00
}
end
2019-12-10 21:29:48 +01:00
end