mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-01 03:46: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()
|
|> Repo.update()
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_topic(%Topic{} = topic, attrs) do
|
def move_topic(topic, new_forum_id) do
|
||||||
Topic.move_changeset(topic, attrs)
|
old_forum_id = topic.forum_id
|
||||||
|> Repo.update()
|
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
|
end
|
||||||
|
|
||||||
def clear_notification(nil, _user), do: nil
|
def clear_notification(nil, _user), do: nil
|
||||||
|
|
|
@ -91,9 +91,9 @@ defmodule Philomena.Topics.Topic do
|
||||||
|> put_change(:lock_reason, "")
|
|> put_change(:lock_reason, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_changeset(topic, attrs) do
|
def move_changeset(topic, new_forum_id) do
|
||||||
change(topic)
|
change(topic)
|
||||||
|> put_change(:forum_id, attrs["target_forum_id"] |> String.to_integer())
|
|> put_change(:forum_id, new_forum_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def put_slug(changeset) do
|
def put_slug(changeset) do
|
||||||
|
|
|
@ -19,11 +19,11 @@ defmodule PhilomenaWeb.Topic.LockController do
|
||||||
{:ok, topic} ->
|
{:ok, topic} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Topic successfully locked!")
|
|> 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} ->
|
{:error, _changeset} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to lock the topic!")
|
|> 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -34,17 +34,18 @@ defmodule PhilomenaWeb.Topic.LockController do
|
||||||
{:ok, topic} ->
|
{:ok, topic} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Topic successfully unlocked!")
|
|> 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} ->
|
{:error, _changeset} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to unlock the topic!")
|
|> 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
|
||||||
end
|
end
|
||||||
|
|
||||||
defp load_topic(conn, _opts) do
|
defp load_topic(conn, _opts) do
|
||||||
topic = Topic
|
topic = Topic
|
||||||
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|
||||||
|
|> preload([:forum])
|
||||||
|> Repo.one()
|
|> Repo.one()
|
||||||
|
|
||||||
Plug.Conn.assign(conn, :topic, topic)
|
Plug.Conn.assign(conn, :topic, topic)
|
||||||
|
|
|
@ -15,16 +15,21 @@ defmodule PhilomenaWeb.Topic.MoveController do
|
||||||
# todo: moving
|
# todo: moving
|
||||||
def create(conn, %{"topic" => topic_params}) do
|
def create(conn, %{"topic" => topic_params}) do
|
||||||
topic = conn.assigns.topic
|
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
|
conn
|
||||||
|> put_flash(:info, "Topic successfully moved!")
|
|> 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} ->
|
{:error, _changeset} ->
|
||||||
|
topic = Repo.preload(topic, :forum)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to move the topic!")
|
|> 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ defmodule PhilomenaWeb.Topic.Post.DeleteController do
|
||||||
alias Philomena.Posts
|
alias Philomena.Posts
|
||||||
|
|
||||||
plug PhilomenaWeb.CanaryMapPlug, create: :hide
|
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
|
def create(conn, _params) do
|
||||||
post = conn.assigns.post
|
post = conn.assigns.post
|
||||||
|
@ -16,12 +16,12 @@ defmodule PhilomenaWeb.Topic.Post.DeleteController do
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Post successfully destroyed!")
|
|> 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} ->
|
{:error, _changeset} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to destroy post!")
|
|> 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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,7 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
|
||||||
alias Philomena.Posts
|
alias Philomena.Posts
|
||||||
|
|
||||||
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
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
|
def create(conn, %{"post" => post_params}) do
|
||||||
post = conn.assigns.post
|
post = conn.assigns.post
|
||||||
|
@ -17,11 +17,11 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Post successfully hidden!")
|
|> 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} ->
|
{:error, _changeset} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to hide post!")
|
|> 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -34,11 +34,11 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Post successfully unhidden!")
|
|> 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} ->
|
{:error, _changeset} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to unhide post!")
|
|> 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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,11 +18,11 @@ defmodule PhilomenaWeb.Topic.StickController do
|
||||||
{:ok, topic} ->
|
{:ok, topic} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Topic successfully stickied!")
|
|> 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} ->
|
{:error, _changeset} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to stick the topic!")
|
|> 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -33,17 +33,18 @@ defmodule PhilomenaWeb.Topic.StickController do
|
||||||
{:ok, topic} ->
|
{:ok, topic} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Topic successfully unstickied!")
|
|> 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} ->
|
{:error, _changeset} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to unstick the topic!")
|
|> 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
|
||||||
end
|
end
|
||||||
|
|
||||||
defp load_topic(conn, _opts) do
|
defp load_topic(conn, _opts) do
|
||||||
topic = Topic
|
topic = Topic
|
||||||
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|
||||||
|
|> preload([:forum])
|
||||||
|> Repo.one()
|
|> Repo.one()
|
||||||
|
|
||||||
Plug.Conn.assign(conn, :topic, topic)
|
Plug.Conn.assign(conn, :topic, topic)
|
||||||
|
|
|
@ -118,13 +118,6 @@ h1 = @topic.title
|
||||||
/ .toggle-box-container
|
/ .toggle-box-container
|
||||||
/ .toggle-box-container__content
|
/ .toggle-box-container__content
|
||||||
/ p
|
/ 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
|
/ - if can?(:destroy, @topic) && !@topic.hidden_from_users
|
||||||
/ = form_tag forum_topic_hide_path(@forum, @topic), method: :post, class: 'hform' do
|
/ = form_tag forum_topic_hide_path(@forum, @topic), method: :post, class: 'hform' do
|
||||||
/ .field
|
/ .field
|
||||||
|
|
Loading…
Reference in a new issue