philomena/lib/philomena_web/controllers/topic/post_controller.ex

79 lines
2.4 KiB
Elixir
Raw Normal View History

2019-11-18 02:33:50 +01:00
defmodule PhilomenaWeb.Topic.PostController do
use PhilomenaWeb, :controller
2019-12-06 16:14:25 +01:00
alias Philomena.{Forums.Forum, Topics.Topic, Posts.Post}
alias Philomena.Posts
alias Philomena.UserStatistics
2019-11-18 02:33:50 +01:00
plug PhilomenaWeb.FilterBannedUsersPlug
2019-11-18 04:31:28 +01:00
plug PhilomenaWeb.UserAttributionPlug
2019-12-14 20:46:50 +01:00
2019-11-18 02:33:50 +01:00
plug PhilomenaWeb.CanaryMapPlug, create: :show, edit: :show, update: :show
2020-01-11 05:20:19 +01:00
plug :load_and_authorize_resource,
model: Forum,
id_field: "short_name",
id_name: "forum_id",
persisted: true
2019-12-06 16:14:25 +01:00
2019-12-14 20:46:50 +01:00
plug PhilomenaWeb.LoadTopicPlug
2019-12-07 00:25:05 +01:00
plug PhilomenaWeb.CanaryMapPlug, create: :create_post, edit: :create_post, update: :create_post
2019-12-14 20:46:50 +01:00
plug :authorize_resource, model: Topic, persisted: true
2019-12-06 16:14:25 +01:00
2019-12-14 20:46:50 +01:00
plug PhilomenaWeb.LoadPostPlug, [param: "id"] when action in [:edit, :update]
2019-12-06 16:14:25 +01:00
plug PhilomenaWeb.CanaryMapPlug, edit: :edit, update: :edit
2019-12-14 20:46:50 +01:00
plug :authorize_resource, model: Post, only: [:edit, :update]
2019-11-18 04:31:28 +01:00
def create(conn, %{"post" => post_params}) do
attributes = conn.assigns.attributes
forum = conn.assigns.forum
topic = conn.assigns.topic
case Posts.create_post(topic, attributes, post_params) do
2019-11-18 04:31:28 +01:00
{:ok, %{post: post}} ->
Posts.notify_post(post)
Posts.reindex_post(post)
UserStatistics.inc_stat(conn.assigns.current_user, :forum_posts)
2019-11-18 04:31:28 +01:00
conn
|> put_flash(:info, "Post created successfully.")
2020-01-11 05:20:19 +01:00
|> redirect(
to:
Routes.forum_topic_path(conn, :show, forum, topic, post_id: post.id) <>
"#post_#{post.id}"
)
2019-11-18 04:31:28 +01:00
_error ->
conn
|> put_flash(:error, "There was an error creating the post")
|> redirect(external: conn.assigns.referrer)
end
end
2019-12-06 16:14:25 +01:00
def edit(conn, _params) do
changeset = Posts.change_post(conn.assigns.post)
2019-12-16 20:24:38 +01:00
render(conn, "edit.html", title: "Editing Post", changeset: changeset)
2019-12-06 16:14:25 +01:00
end
def update(conn, %{"post" => post_params}) do
post = conn.assigns.post
2019-11-18 04:31:28 +01:00
user = conn.assigns.current_user
2019-12-06 16:14:25 +01:00
case Posts.update_post(post, user, post_params) do
{:ok, _post} ->
Posts.reindex_post(post)
2019-11-18 04:31:28 +01:00
conn
2019-12-06 16:14:25 +01:00
|> put_flash(:info, "Post successfully edited.")
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-06 16:14:25 +01:00
{:error, :post, changeset, _changes} ->
render(conn, "edit.html", post: conn.assigns.post, changeset: changeset)
end
end
2019-12-14 00:31:42 +01:00
end