2019-12-12 00:58:00 +01:00
|
|
|
defmodule PhilomenaWeb.Topic.Post.HideController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Posts.Post
|
2019-12-17 17:45:22 +01:00
|
|
|
alias Philomena.Reports
|
2019-12-12 00:58:00 +01:00
|
|
|
alias Philomena.Posts
|
|
|
|
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
2019-12-14 17:43:26 +01:00
|
|
|
plug :load_and_authorize_resource, model: Post, id_name: "post_id", persisted: true, preload: [:topic, topic: :forum]
|
2019-12-12 00:58:00 +01:00
|
|
|
|
|
|
|
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
|
2019-12-17 17:45:22 +01:00
|
|
|
{:ok, %{post: post, reports: {_count, reports}}} ->
|
2019-12-12 00:58:00 +01:00
|
|
|
Posts.reindex_post(post)
|
2019-12-17 17:45:22 +01:00
|
|
|
Reports.reindex_reports(reports)
|
2019-12-12 00:58:00 +01:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Post successfully hidden!")
|
2019-12-14 17:43:26 +01:00
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
2019-12-14 20:46:50 +01:00
|
|
|
|
2019-12-12 00:58:00 +01:00
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to hide post!")
|
2019-12-14 17:43:26 +01:00
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
2019-12-12 00:58:00 +01:00
|
|
|
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!")
|
2019-12-14 17:43:26 +01:00
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
2019-12-14 20:46:50 +01:00
|
|
|
|
2019-12-12 00:58:00 +01:00
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to unhide post!")
|
2019-12-14 17:43:26 +01:00
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic) <> "#post_#{post.id}")
|
2019-12-12 00:58:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|