2019-12-12 00:58:00 +01:00
|
|
|
defmodule PhilomenaWeb.Topic.Post.DeleteController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Posts.Post
|
|
|
|
alias Philomena.Posts
|
|
|
|
|
2019-12-12 05:41:33 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Post,
|
|
|
|
id_name: "post_id",
|
|
|
|
persisted: true,
|
|
|
|
preload: [:topic, topic: :forum]
|
2019-12-12 00:58:00 +01:00
|
|
|
|
2019-12-12 05:41:33 +01:00
|
|
|
def create(conn, _params) do
|
2019-12-12 00:58:00 +01:00
|
|
|
post = conn.assigns.post
|
|
|
|
|
|
|
|
case Posts.destroy_post(post) do
|
|
|
|
{:ok, post} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Post successfully destroyed!")
|
2021-11-08 14:45:19 +01:00
|
|
|
|> moderation_log(details: &log_details/3, data: post)
|
2020-01-11 05:20:19 +01:00
|
|
|
|> redirect(
|
|
|
|
to:
|
|
|
|
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
|
|
|
|
"#post_#{post.id}"
|
|
|
|
)
|
2019-12-12 05:41:33 +01:00
|
|
|
|
2019-12-12 00:58:00 +01:00
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to destroy post!")
|
2020-01-11 05:20:19 +01:00
|
|
|
|> redirect(
|
|
|
|
to:
|
|
|
|
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
|
|
|
|
"#post_#{post.id}"
|
|
|
|
)
|
2019-12-12 00:58:00 +01:00
|
|
|
end
|
|
|
|
end
|
2021-11-07 19:51:55 +01:00
|
|
|
|
|
|
|
defp log_details(conn, _action, post) do
|
|
|
|
%{
|
|
|
|
body: "Destroyed forum post ##{post.id} in topic '#{post.topic.title}'",
|
|
|
|
subject_path:
|
|
|
|
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
|
|
|
|
"#post_#{post.id}"
|
|
|
|
}
|
|
|
|
end
|
2019-12-12 00:58:00 +01:00
|
|
|
end
|