philomena/lib/philomena_web/controllers/topic/post/approve_controller.ex
Luna D dfeefb1651 Commands:
git grep -l 'defp log_details(_conn' | xargs sed -i -e 's/defp log_details(_conn, /defp log_details(/g'
git grep -l '&log_details/3' | xargs sed -i -e 's|\&log_details/3|\&log_details/2|g'
2024-06-23 10:45:31 -04:00

49 lines
1.3 KiB
Elixir

defmodule PhilomenaWeb.Topic.Post.ApproveController do
use PhilomenaWeb, :controller
alias Philomena.Posts.Post
alias Philomena.Posts
plug PhilomenaWeb.CanaryMapPlug, create: :approve
plug :load_and_authorize_resource,
model: Post,
id_name: "post_id",
persisted: true,
preload: [:topic, :user, topic: :forum]
def create(conn, _params) do
post = conn.assigns.post
user = conn.assigns.current_user
case Posts.approve_post(post, user) do
{:ok, post} ->
conn
|> put_flash(:info, "Post successfully approved.")
|> moderation_log(details: &log_details/2, data: post)
|> redirect(
to:
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
"#post_#{post.id}"
)
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to approve post!")
|> redirect(
to:
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
"#post_#{post.id}"
)
end
end
defp log_details(_action, post) do
%{
body: "Approved forum post ##{post.id} in topic '#{post.topic.title}'",
subject_path:
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
"#post_#{post.id}"
}
end
end