2022-03-22 22:23:30 +01:00
|
|
|
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/3, data: post)
|
|
|
|
|> redirect(
|
|
|
|
to:
|
2024-04-29 02:55:27 +02:00
|
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
2022-03-22 22:23:30 +01:00
|
|
|
"#post_#{post.id}"
|
|
|
|
)
|
|
|
|
|
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to approve post!")
|
|
|
|
|> redirect(
|
|
|
|
to:
|
2024-04-29 02:55:27 +02:00
|
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
2022-03-22 22:23:30 +01:00
|
|
|
"#post_#{post.id}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-24 18:36:49 +01:00
|
|
|
defp log_details(conn, _action, post) do
|
2022-03-22 22:23:30 +01:00
|
|
|
%{
|
|
|
|
body: "Approved forum post ##{post.id} in topic '#{post.topic.title}'",
|
|
|
|
subject_path:
|
2024-04-29 02:55:27 +02:00
|
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
2022-03-22 22:23:30 +01:00
|
|
|
"#post_#{post.id}"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|