mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-23 01:18:00 +01:00
dfeefb1651
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'
83 lines
2.2 KiB
Elixir
83 lines
2.2 KiB
Elixir
defmodule PhilomenaWeb.Topic.Post.HideController do
|
|
use PhilomenaWeb, :controller
|
|
|
|
alias Philomena.Posts.Post
|
|
alias Philomena.Posts
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
|
|
|
plug :load_and_authorize_resource,
|
|
model: Post,
|
|
id_name: "post_id",
|
|
persisted: true,
|
|
preload: [:topic, topic: :forum]
|
|
|
|
def create(conn, %{"post" => post_params}) do
|
|
post = conn.assigns.post
|
|
user = conn.assigns.current_user
|
|
|
|
case Posts.hide_post(post, post_params, user) do
|
|
{:ok, post} ->
|
|
conn
|
|
|> put_flash(:info, "Post successfully hidden.")
|
|
|> 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 hide post!")
|
|
|> redirect(
|
|
to:
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
|
"#post_#{post.id}"
|
|
)
|
|
end
|
|
end
|
|
|
|
def delete(conn, _params) do
|
|
post = conn.assigns.post
|
|
|
|
case Posts.unhide_post(post) do
|
|
{:ok, post} ->
|
|
conn
|
|
|> put_flash(:info, "Post successfully unhidden.")
|
|
|> 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 unhide 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 =
|
|
case action do
|
|
:create ->
|
|
"Hidden forum post ##{post.id} in topic '#{post.topic.title}' (#{post.deletion_reason})"
|
|
|
|
:delete ->
|
|
"Restored forum post ##{post.id} in topic '#{post.topic.title}'"
|
|
end
|
|
|
|
%{
|
|
body: body,
|
|
subject_path:
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
|
"#post_#{post.id}"
|
|
}
|
|
end
|
|
end
|