philomena/lib/philomena_web/controllers/topic/post/delete_controller.ex

49 lines
1.3 KiB
Elixir
Raw Normal View History

defmodule PhilomenaWeb.Topic.Post.DeleteController do
use PhilomenaWeb, :controller
alias Philomena.Posts.Post
alias Philomena.Posts
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]
def create(conn, _params) do
post = conn.assigns.post
case Posts.destroy_post(post) do
{:ok, post} ->
conn
|> put_flash(:info, "Post successfully destroyed!")
|> moderation_log(details: &log_details/2, data: post)
2020-01-11 05:20:19 +01:00
|> redirect(
to:
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
2020-01-11 05:20:19 +01:00
"#post_#{post.id}"
)
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to destroy post!")
2020-01-11 05:20:19 +01:00
|> redirect(
to:
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
2020-01-11 05:20:19 +01:00
"#post_#{post.id}"
)
end
end
2021-11-07 19:51:55 +01:00
defp log_details(_action, post) do
2021-11-07 19:51:55 +01:00
%{
body: "Destroyed forum post ##{post.id} in topic '#{post.topic.title}'",
subject_path:
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
2021-11-07 19:51:55 +01:00
"#post_#{post.id}"
}
end
end