2019-12-14 19:03:11 +01:00
|
|
|
defmodule PhilomenaWeb.Topic.HideController do
|
|
|
|
import Plug.Conn
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-14 20:46:50 +01:00
|
|
|
alias Philomena.Forums.Forum
|
2019-12-14 19:03:11 +01:00
|
|
|
alias Philomena.Topics.Topic
|
|
|
|
alias Philomena.Topics
|
|
|
|
|
2019-12-14 20:46:50 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :show, delete: :show
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Forum,
|
|
|
|
id_name: "forum_id",
|
|
|
|
id_field: "short_name",
|
|
|
|
persisted: true
|
2019-12-14 20:46:50 +01:00
|
|
|
|
|
|
|
plug PhilomenaWeb.LoadTopicPlug
|
2019-12-14 19:03:11 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
2019-12-14 20:46:50 +01:00
|
|
|
plug :authorize_resource, model: Topic, persisted: true
|
2019-12-14 19:03:11 +01:00
|
|
|
|
|
|
|
def create(conn, %{"topic" => topic_params}) do
|
|
|
|
topic = conn.assigns.topic
|
|
|
|
deletion_reason = topic_params["deletion_reason"]
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Topics.hide_topic(topic, deletion_reason, user) do
|
|
|
|
{:ok, topic} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Topic successfully hidden!")
|
2021-11-08 14:45:19 +01:00
|
|
|
|> moderation_log(details: &log_details/3, data: topic)
|
2019-12-14 19:03:11 +01:00
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
2019-12-14 20:46:50 +01:00
|
|
|
|
2019-12-14 19:03:11 +01:00
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to hide the topic!")
|
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _opts) do
|
|
|
|
topic = conn.assigns.topic
|
|
|
|
|
|
|
|
case Topics.unhide_topic(topic) do
|
|
|
|
{:ok, topic} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Topic successfully restored!")
|
2021-11-08 14:45:19 +01:00
|
|
|
|> moderation_log(details: &log_details/3, data: topic)
|
2019-12-14 19:03:11 +01:00
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
2019-12-14 20:46:50 +01:00
|
|
|
|
2019-12-14 19:03:11 +01:00
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to restore the topic!")
|
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
|
|
|
end
|
|
|
|
end
|
2021-11-07 19:51:55 +01:00
|
|
|
|
|
|
|
defp log_details(conn, action, topic) do
|
|
|
|
body =
|
|
|
|
case action do
|
|
|
|
:create ->
|
|
|
|
"Hidden topic '#{topic.title}' (#{topic.deletion_reason}) in #{topic.forum.name}"
|
|
|
|
|
|
|
|
:delete ->
|
|
|
|
"Restored topic '#{topic.title}' in #{topic.forum.name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
%{
|
|
|
|
body: body,
|
|
|
|
subject_path: Routes.forum_topic_path(conn, :show, topic.forum, topic)
|
|
|
|
}
|
|
|
|
end
|
2019-12-14 19:03:11 +01:00
|
|
|
end
|