philomena/lib/philomena_web/controllers/topic/post/hide_controller.ex

70 lines
1.9 KiB
Elixir
Raw Normal View History

defmodule PhilomenaWeb.Topic.Post.HideController do
use PhilomenaWeb, :controller
alias Philomena.Posts.Post
alias Philomena.Reports
alias Philomena.Posts
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
2020-01-11 05:20:19 +01:00
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
user = conn.assigns.current_user
case Posts.hide_post(post, post_params, user) do
{:ok, %{post: post, reports: {_count, reports}}} ->
Posts.reindex_post(post)
Reports.reindex_reports(reports)
conn
|> put_flash(:info, "Post successfully hidden!")
2020-01-11 05:20:19 +01:00
|> redirect(
to:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
"#post_#{post.id}"
)
2019-12-14 20:46:50 +01:00
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to hide post!")
2020-01-11 05:20:19 +01:00
|> redirect(
to:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.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!")
2020-01-11 05:20:19 +01:00
|> redirect(
to:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
"#post_#{post.id}"
)
2019-12-14 20:46:50 +01:00
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to unhide post!")
2020-01-11 05:20:19 +01:00
|> redirect(
to:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
"#post_#{post.id}"
)
end
end
end