2019-12-12 00:58:00 +01:00
|
|
|
defmodule PhilomenaWeb.Topic.Post.HideController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Posts.Post
|
|
|
|
alias Philomena.Posts
|
|
|
|
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :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
|
|
|
|
|
|
|
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
|
2020-09-07 20:50:34 +02:00
|
|
|
{:ok, post} ->
|
2019-12-12 00:58:00 +01:00
|
|
|
conn
|
2020-09-07 20:50:34 +02:00
|
|
|
|> put_flash(:info, "Post successfully hidden.")
|
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:
|
2024-04-29 02:55:27 +02:00
|
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
2020-01-11 05:20:19 +01:00
|
|
|
"#post_#{post.id}"
|
|
|
|
)
|
2019-12-14 20:46:50 +01:00
|
|
|
|
2019-12-12 00:58:00 +01:00
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to hide post!")
|
2020-01-11 05:20:19 +01:00
|
|
|
|> redirect(
|
|
|
|
to:
|
2024-04-29 02:55:27 +02:00
|
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
2020-01-11 05:20:19 +01:00
|
|
|
"#post_#{post.id}"
|
|
|
|
)
|
2019-12-12 00:58:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
post = conn.assigns.post
|
|
|
|
|
|
|
|
case Posts.unhide_post(post) do
|
|
|
|
{:ok, post} ->
|
|
|
|
conn
|
2020-09-07 20:50:34 +02:00
|
|
|
|> put_flash(:info, "Post successfully unhidden.")
|
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:
|
2024-04-29 02:55:27 +02:00
|
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
2020-01-11 05:20:19 +01:00
|
|
|
"#post_#{post.id}"
|
|
|
|
)
|
2019-12-14 20:46:50 +01:00
|
|
|
|
2019-12-12 00:58:00 +01:00
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to unhide post!")
|
2020-01-11 05:20:19 +01:00
|
|
|
|> redirect(
|
|
|
|
to:
|
2024-04-29 02:55:27 +02:00
|
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
2020-01-11 05:20:19 +01:00
|
|
|
"#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 =
|
|
|
|
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:
|
2024-04-29 02:55:27 +02:00
|
|
|
~p"/forums/#{post.topic.forum}/topics/#{post.topic}?#{[post_id: post.id]}" <>
|
2021-11-07 19:51:55 +01:00
|
|
|
"#post_#{post.id}"
|
|
|
|
}
|
|
|
|
end
|
2019-12-12 00:58:00 +01:00
|
|
|
end
|