diff --git a/lib/philomena/topics.ex b/lib/philomena/topics.ex index 67c38bcb..c18cf37c 100644 --- a/lib/philomena/topics.ex +++ b/lib/philomena/topics.ex @@ -209,7 +209,13 @@ defmodule Philomena.Topics do Topic.unlock_changeset(topic) |> Repo.update() end + + def move_topic(%Topic{} = topic, attrs) do + Topic.move_changeset(topic, attrs) + |> Repo.update() + end + def clear_notification(nil, _user), do: nil def clear_notification(_topic, nil), do: nil def clear_notification(topic, user) do Notifications.delete_unread_notification("Topic", topic.id, user) diff --git a/lib/philomena/topics/topic.ex b/lib/philomena/topics/topic.ex index fea9af3f..6ad54980 100644 --- a/lib/philomena/topics/topic.ex +++ b/lib/philomena/topics/topic.ex @@ -91,6 +91,11 @@ defmodule Philomena.Topics.Topic do |> put_change(:lock_reason, "") end + def move_changeset(topic, attrs) do + change(topic) + |> put_change(:forum_id, attrs["target_forum_id"] |> String.to_integer()) + end + def put_slug(changeset) do slug = changeset diff --git a/lib/philomena_web/controllers/topic/move_controller.ex b/lib/philomena_web/controllers/topic/move_controller.ex index b8a8e83c..759c8f68 100644 --- a/lib/philomena_web/controllers/topic/move_controller.ex +++ b/lib/philomena_web/controllers/topic/move_controller.ex @@ -8,12 +8,24 @@ defmodule PhilomenaWeb.Topic.MoveController do import Ecto.Query plug :load_topic - plug PhilomenaWeb.CanaryMapPlug, create: :stick, delete: :stick + plug PhilomenaWeb.CanaryMapPlug, create: :move plug :authorize_resource, model: Topic, id_name: "topic_id", persisted: true # intentionally blank # todo: moving - def create(conn, _opts) do + def create(conn, %{"topic" => topic_params}) do + topic = conn.assigns.topic + + case Topics.move_topic(topic, topic_params) do + {:ok, topic} -> + conn + |> put_flash(:info, "Topic successfully moved!") + |> redirect(to: Routes.forum_topic_path(conn, :show, topic_params["target_forum_id"] |> String.to_integer(), topic.id)) + {:error, _changeset} -> + conn + |> put_flash(:error, "Unable to move the topic!") + |> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id)) + end end defp load_topic(conn, _opts) do diff --git a/lib/philomena_web/templates/topic/show.html.slime b/lib/philomena_web/templates/topic/show.html.slime index 7b6f4d27..cf905ab3 100644 --- a/lib/philomena_web/templates/topic/show.html.slime +++ b/lib/philomena_web/templates/topic/show.html.slime @@ -103,6 +103,13 @@ h1 = @topic.title i.fas.fa-thumbtack> ' Stick - true -> + = if can?(@conn, :move, @topic) do + = form_for :topic, Routes.forum_topic_move_path(@conn, :create, @topic.forum_id, @topic), [method: :post, class: "hform"], fn f -> + .field + = select f, :target_forum_id, @conn.assigns.forums |> Enum.map(fn forum -> {forum.name, forum.id} end), class: "input hform__text" + = submit class: "hform__button button" do + i.fas.fa-truck> + ' Move / Mod tools /- if can? :assist, Topic / .block__content @@ -111,25 +118,6 @@ h1 = @topic.title / .toggle-box-container / .toggle-box-container__content / p - / - if can?(:unlock, @topic) && !@topic.locked_at.nil? - / => button_to forum_topic_lock_path(@forum, @topic), method: :delete, class: 'button' do - / i.fa.fa-lock> - / | Unlock - / - if can?(:stick, @topic) && !@topic.sticky - / => button_to forum_topic_stick_path(@forum, @topic), method: :post, class: 'button' do - / i.fa.fa-thumbtack> - / | Stick - / - if can?(:stick, @topic) && @topic.sticky - / => button_to forum_topic_stick_path(@forum, @topic), method: :delete, class: 'button' do - / i.fa.fa-thumbtack> - / | Unstick - / - if can?(:lock, @topic) && @topic.locked_at.nil? - / = form_tag forum_topic_lock_path(@forum, @topic), method: :post, class: 'hform' do - / .field - / => text_field_tag :lock_reason, '', placeholder: 'Lock reason', class: 'input hform__text' - / => button_tag class: 'hform__button button' do - / i.fa.fa-unlock> - / | Lock / - if can? :move, @topic / = form_tag forum_topic_move_path(@forum, @topic), method: :post, class: 'hform' do / .field