philomena/lib/philomena_web/controllers/topic/post/hide_controller.ex
2019-12-11 18:58:00 -05:00

44 lines
1.5 KiB
Elixir

defmodule PhilomenaWeb.Topic.Post.HideController do
use PhilomenaWeb, :controller
alias Philomena.Posts.Post
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]
def create(conn, %{"post" => post_params}) do
post = conn.assigns.post
user = conn.assigns.current_user
case Posts.hide_post(post, post_params, user) do
{:ok, post} ->
Posts.reindex_post(post)
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}")
{: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}")
end
end
def delete(conn, _params) do
post = conn.assigns.post
case Posts.unhide_post(post) do
{:ok, post} ->
Posts.reindex_post(post)
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}")
{: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}")
end
end
end