mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-31 19:36:44 +01:00
finish topic moving
This commit is contained in:
parent
ffed45269b
commit
37be32f2b2
8 changed files with 52 additions and 32 deletions
|
@ -210,9 +210,29 @@ defmodule Philomena.Topics do
|
|||
|> Repo.update()
|
||||
end
|
||||
|
||||
def move_topic(%Topic{} = topic, attrs) do
|
||||
Topic.move_changeset(topic, attrs)
|
||||
|> Repo.update()
|
||||
def move_topic(topic, new_forum_id) do
|
||||
old_forum_id = topic.forum_id
|
||||
topic_changes = Topic.move_changeset(topic, new_forum_id)
|
||||
|
||||
Multi.new
|
||||
|> Multi.update(:topic, topic_changes)
|
||||
|> Multi.run(:update_old_forum, fn repo, %{topic: topic} ->
|
||||
{count, nil} =
|
||||
Forum
|
||||
|> where(id: ^old_forum_id)
|
||||
|> repo.update_all(inc: [post_count: -topic.post_count, topic_count: -1])
|
||||
|
||||
{:ok, count}
|
||||
end)
|
||||
|> Multi.run(:update_new_forum, fn repo, %{topic: topic} ->
|
||||
{count, nil} =
|
||||
Forum
|
||||
|> where(id: ^topic.forum_id)
|
||||
|> repo.update_all(inc: [post_count: topic.post_count, topic_count: 1])
|
||||
|
||||
{:ok, count}
|
||||
end)
|
||||
|> Repo.isolated_transaction(:serializable)
|
||||
end
|
||||
|
||||
def clear_notification(nil, _user), do: nil
|
||||
|
|
|
@ -91,9 +91,9 @@ defmodule Philomena.Topics.Topic do
|
|||
|> put_change(:lock_reason, "")
|
||||
end
|
||||
|
||||
def move_changeset(topic, attrs) do
|
||||
def move_changeset(topic, new_forum_id) do
|
||||
change(topic)
|
||||
|> put_change(:forum_id, attrs["target_forum_id"] |> String.to_integer())
|
||||
|> put_change(:forum_id, new_forum_id)
|
||||
end
|
||||
|
||||
def put_slug(changeset) do
|
||||
|
|
|
@ -19,11 +19,11 @@ defmodule PhilomenaWeb.Topic.LockController do
|
|||
{:ok, topic} ->
|
||||
conn
|
||||
|> put_flash(:info, "Topic successfully locked!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
{:error, _changeset} ->
|
||||
conn
|
||||
|> put_flash(:error, "Unable to lock the topic!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -34,17 +34,18 @@ defmodule PhilomenaWeb.Topic.LockController do
|
|||
{:ok, topic} ->
|
||||
conn
|
||||
|> put_flash(:info, "Topic successfully unlocked!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
{:error, _changeset} ->
|
||||
conn
|
||||
|> put_flash(:error, "Unable to unlock the topic!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
end
|
||||
end
|
||||
|
||||
defp load_topic(conn, _opts) do
|
||||
topic = Topic
|
||||
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|
||||
|> preload([:forum])
|
||||
|> Repo.one()
|
||||
|
||||
Plug.Conn.assign(conn, :topic, topic)
|
||||
|
|
|
@ -15,16 +15,21 @@ defmodule PhilomenaWeb.Topic.MoveController do
|
|||
# todo: moving
|
||||
def create(conn, %{"topic" => topic_params}) do
|
||||
topic = conn.assigns.topic
|
||||
target_forum_id = String.to_integer(topic_params["target_forum_id"])
|
||||
|
||||
case Topics.move_topic(topic, target_forum_id) do
|
||||
{:ok, %{topic: topic}} ->
|
||||
topic = Repo.preload(topic, :forum)
|
||||
|
||||
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))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
{:error, _changeset} ->
|
||||
topic = Repo.preload(topic, :forum)
|
||||
|
||||
conn
|
||||
|> put_flash(:error, "Unable to move the topic!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ defmodule PhilomenaWeb.Topic.Post.DeleteController do
|
|||
alias Philomena.Posts
|
||||
|
||||
plug PhilomenaWeb.CanaryMapPlug, create: :hide
|
||||
plug :load_and_authorize_resource, model: Post, id_name: "post_id", persisted: true, preload: [:topic]
|
||||
plug :load_and_authorize_resource, model: Post, id_name: "post_id", persisted: true, preload: [:topic, topic: :forum]
|
||||
|
||||
def create(conn, _params) do
|
||||
post = conn.assigns.post
|
||||
|
@ -16,12 +16,12 @@ defmodule PhilomenaWeb.Topic.Post.DeleteController do
|
|||
|
||||
conn
|
||||
|> put_flash(:info, "Post successfully destroyed!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
||||
|
||||
{:error, _changeset} ->
|
||||
conn
|
||||
|> put_flash(:error, "Unable to destroy post!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
|
|||
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]
|
||||
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
|
||||
|
@ -17,11 +17,11 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
|
|||
|
||||
conn
|
||||
|> put_flash(:info, "Post successfully hidden!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
||||
{:error, _changeset} ->
|
||||
conn
|
||||
|> put_flash(:error, "Unable to hide post!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -34,11 +34,11 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
|
|||
|
||||
conn
|
||||
|> put_flash(:info, "Post successfully unhidden!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
||||
{:error, _changeset} ->
|
||||
conn
|
||||
|> put_flash(:error, "Unable to unhide post!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum_id, post.topic_id) <> "#post_#{post.id}")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,11 +18,11 @@ defmodule PhilomenaWeb.Topic.StickController do
|
|||
{:ok, topic} ->
|
||||
conn
|
||||
|> put_flash(:info, "Topic successfully stickied!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
{:error, _changeset} ->
|
||||
conn
|
||||
|> put_flash(:error, "Unable to stick the topic!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -33,17 +33,18 @@ defmodule PhilomenaWeb.Topic.StickController do
|
|||
{:ok, topic} ->
|
||||
conn
|
||||
|> put_flash(:info, "Topic successfully unstickied!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
{:error, _changeset} ->
|
||||
conn
|
||||
|> put_flash(:error, "Unable to unstick the topic!")
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
|
||||
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
|
||||
end
|
||||
end
|
||||
|
||||
defp load_topic(conn, _opts) do
|
||||
topic = Topic
|
||||
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|
||||
|> preload([:forum])
|
||||
|> Repo.one()
|
||||
|
||||
Plug.Conn.assign(conn, :topic, topic)
|
||||
|
|
|
@ -118,13 +118,6 @@ h1 = @topic.title
|
|||
/ .toggle-box-container
|
||||
/ .toggle-box-container__content
|
||||
/ p
|
||||
/ - if can? :move, @topic
|
||||
/ = form_tag forum_topic_move_path(@forum, @topic), method: :post, class: 'hform' do
|
||||
/ .field
|
||||
/ => select_tag :target_forum_id, options_from_collection_for_select(Forum.where.not(id: @forum.id).all, :short_name, :name), class: 'input hform__text'
|
||||
/ => button_tag class: 'hform__button button' do
|
||||
/ i.fa.fa-truck>
|
||||
/ | Move
|
||||
/ - if can?(:destroy, @topic) && !@topic.hidden_from_users
|
||||
/ = form_tag forum_topic_hide_path(@forum, @topic), method: :post, class: 'hform' do
|
||||
/ .field
|
||||
|
|
Loading…
Reference in a new issue