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

100 lines
3 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
2020-07-21 16:50:33 +02:00
plug PhilomenaWeb.LimitPlug,
2021-09-10 19:34:21 +02:00
[time: 15, error: "You may only make a post once every 15 seconds."]
2020-07-21 16:50:33 +02:00
when action in [:create]
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}} ->
2022-03-22 22:23:30 +01:00
if post.approved do
2022-03-24 17:31:57 +01:00
Posts.notify_post(post)
2022-03-22 22:23:30 +01:00
UserStatistics.inc_stat(conn.assigns.current_user, :forum_posts)
else
Posts.report_non_approved(post)
end
2019-11-18 04:31:28 +01:00
if forum.access_level == "normal" do
PhilomenaWeb.Endpoint.broadcast!(
"firehose",
"post:create",
2020-07-06 20:20:50 +02:00
PhilomenaWeb.Api.Json.Forum.Topic.PostView.render("firehose.json", %{
post: post,
topic: topic,
forum: forum
})
)
end
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")
2020-09-07 06:28:24 +02:00
|> redirect(to: Routes.forum_topic_path(conn, :show, forum, topic))
2019-11-18 04:31:28 +01:00
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
2022-03-24 19:19:50 +01:00
{:ok, post} ->
if not post.approved do
Posts.report_non_approved(post)
end
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