philomena/lib/philomena_web/controllers/topic/move_controller.ex

49 lines
1.4 KiB
Elixir
Raw Permalink Normal View History

2019-12-14 00:31:42 +01:00
defmodule PhilomenaWeb.Topic.MoveController do
import Plug.Conn
use PhilomenaWeb, :controller
2019-12-14 20:46:50 +01:00
alias Philomena.Forums.Forum
2019-12-14 00:31:42 +01:00
alias Philomena.Topics.Topic
alias Philomena.Topics
alias Philomena.Repo
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 00:31:42 +01:00
2019-12-14 20:46:50 +01:00
plug PhilomenaWeb.LoadTopicPlug
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
plug :authorize_resource, model: Topic, persisted: true
def create(conn, %{"topic" => %{"target_forum_id" => target_id}}) do
2019-12-14 17:10:33 +01:00
topic = conn.assigns.topic
2019-12-14 20:46:50 +01:00
target_forum_id = String.to_integer(target_id)
2019-12-14 17:43:26 +01:00
case Topics.move_topic(topic, target_forum_id) do
{:ok, %{topic: topic}} ->
2019-12-14 20:46:50 +01:00
topic = Repo.preload(topic, :forum, force: true)
2019-12-14 17:10:33 +01:00
conn
|> put_flash(:info, "Topic successfully moved!")
|> moderation_log(details: &log_details/2, data: topic)
|> redirect(to: ~p"/forums/#{topic.forum}/topics/#{topic}")
2019-12-14 17:43:26 +01:00
2019-12-14 20:46:50 +01:00
{:error, _changeset} ->
2019-12-14 17:10:33 +01:00
conn
|> put_flash(:error, "Unable to move the topic!")
|> redirect(to: ~p"/forums/#{conn.assigns.forum}/topics/#{topic}")
2019-12-14 17:10:33 +01:00
end
2019-12-14 00:31:42 +01:00
end
2021-11-07 19:51:55 +01:00
defp log_details(_action, topic) do
2021-11-07 19:51:55 +01:00
%{
body: "Topic '#{topic.title}' moved to #{topic.forum.name}",
subject_path: ~p"/forums/#{topic.forum}/topics/#{topic}"
2021-11-07 19:51:55 +01:00
}
end
2019-12-14 00:31:42 +01:00
end