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
|
2019-12-05 20:23:26 +01:00
|
|
|
alias Philomena.UserStatistics
|
2019-11-18 04:31:28 +01:00
|
|
|
alias Philomena.Repo
|
2019-12-06 16:14:25 +01:00
|
|
|
import Ecto.Query
|
2019-11-18 02:33:50 +01:00
|
|
|
|
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug
|
2019-11-18 04:31:28 +01:00
|
|
|
plug PhilomenaWeb.UserAttributionPlug
|
2019-11-18 02:33:50 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :show, edit: :show, update: :show
|
|
|
|
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-11-18 04:31:28 +01:00
|
|
|
plug :load_topic
|
2019-12-07 00:25:05 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :create_post, edit: :create_post, update: :create_post
|
2019-12-06 16:14:25 +01:00
|
|
|
plug :authorize_resource, model: Topic, id_field: "slug", id_name: "topic_id", persisted: true
|
|
|
|
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, edit: :edit, update: :edit
|
|
|
|
plug :load_and_authorize_resource, model: Post, only: [:edit, :update], preload: [topic: :forum]
|
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
|
|
|
|
|
2019-11-19 01:33:27 +01:00
|
|
|
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)
|
2019-12-05 20:23:26 +01:00
|
|
|
UserStatistics.inc_stat(conn.assigns.current_user, :forum_posts)
|
2019-11-18 04:31:28 +01:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Post created successfully.")
|
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, forum, topic, post_id: post.id) <> "#post_#{post.id}")
|
|
|
|
|
|
|
|
_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)
|
|
|
|
render(conn, "edit.html", changeset: changeset)
|
|
|
|
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.")
|
|
|
|
|> redirect(to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <> "#post_#{post.id}")
|
|
|
|
|
|
|
|
{:error, :post, changeset, _changes} ->
|
|
|
|
render(conn, "edit.html", post: conn.assigns.post, changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp load_topic(conn, _opts) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
forum = conn.assigns.forum
|
|
|
|
topic =
|
|
|
|
Topic
|
|
|
|
|> where(forum_id: ^forum.id, slug: ^conn.params["topic_id"])
|
|
|
|
|> preload(:forum)
|
|
|
|
|> Repo.one()
|
|
|
|
|
|
|
|
cond do
|
|
|
|
is_nil(topic) ->
|
|
|
|
PhilomenaWeb.NotFoundPlug.call(conn)
|
|
|
|
|
|
|
|
not Canada.Can.can?(user, :show, topic) ->
|
|
|
|
PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
|
|
|
|
true ->
|
|
|
|
Plug.Conn.assign(conn, :topic, topic)
|
2019-11-18 04:31:28 +01:00
|
|
|
end
|
|
|
|
end
|
2019-12-14 00:31:42 +01:00
|
|
|
end
|