From 02b1eda21e732773442900c2e33aa5cd7e547da7 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 19 Jul 2020 23:10:01 -0400 Subject: [PATCH] add post creation via the API --- .../api/json/forum/topic/post_controller.ex | 54 +++++++++++++++++++ lib/philomena_web/router.ex | 2 +- .../views/api/json/forum/topic/post_view.ex | 6 +++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/philomena_web/controllers/api/json/forum/topic/post_controller.ex b/lib/philomena_web/controllers/api/json/forum/topic/post_controller.ex index e33f08d8d..49a6b94d8 100644 --- a/lib/philomena_web/controllers/api/json/forum/topic/post_controller.ex +++ b/lib/philomena_web/controllers/api/json/forum/topic/post_controller.ex @@ -1,10 +1,17 @@ defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostController do use PhilomenaWeb, :controller + alias Philomena.Forums.Forum alias Philomena.Posts.Post + alias Philomena.Posts + alias Philomena.Topics.Topic alias Philomena.Repo + alias Philomena.UserStatistics import Ecto.Query + plug PhilomenaWeb.ApiRequireAuthorizationPlug when action in [:create] + plug PhilomenaWeb.UserAttributionPlug when action in [:create] + def index(conn, %{"forum_id" => forum_id, "topic_id" => topic_id}) do page = conn.assigns.pagination.page_number @@ -46,4 +53,51 @@ defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostController do render(conn, "show.json", post: post) end end + + def create(conn, %{"forum_id" => forum_id, "topic_id" => topic_id, "post" => post_params}) do + attributes = conn.assigns.attributes + + topic = + Topic + |> join(:inner, [t], _ in assoc(t, :forum)) + |> where(slug: ^topic_id) + |> where(hidden_from_users: false) + |> where([_t, f], f.access_level == "normal" and f.short_name == ^forum_id) + |> order_by(desc: :sticky, desc: :last_replied_to_at) + |> preload([:user]) + |> Repo.one() + + forum = + Forum + |> where(short_name: ^forum_id) + |> where(access_level: "normal") + |> Repo.one() + + case Posts.create_post(topic, attributes, post_params) do + {:ok, %{post: post}} -> + Posts.notify_post(post) + Posts.reindex_post(post) + UserStatistics.inc_stat(conn.assigns.current_user, :forum_posts) + + if forum.access_level == "normal" do + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "post:create", + PhilomenaWeb.Api.Json.Forum.Topic.PostView.render("firehose.json", %{ + post: post, + topic: topic, + forum: forum + }) + ) + end + + render(conn, "show.json", post: post) + + {:error, :post, changeset, _} -> + conn + |> put_status(:bad_request) + |> put_view(PhilomenaWeb.Api.Json.Forum.Topic.PostView) + |> render("error.json", changeset: changeset) + end + end end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 129f3ab69..5cac7b61d 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -140,7 +140,7 @@ defmodule PhilomenaWeb.Router do resources "/forums", ForumController, only: [:show, :index] do resources "/topics", Forum.TopicController, only: [:show, :index] do - resources "/posts", Forum.Topic.PostController, only: [:show, :index] + resources "/posts", Forum.Topic.PostController, only: [:show, :index, :create] end end end diff --git a/lib/philomena_web/views/api/json/forum/topic/post_view.ex b/lib/philomena_web/views/api/json/forum/topic/post_view.ex index 4d6ecdcb0..8b0e93e40 100644 --- a/lib/philomena_web/views/api/json/forum/topic/post_view.ex +++ b/lib/philomena_web/views/api/json/forum/topic/post_view.ex @@ -61,4 +61,10 @@ defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostView do edit_reason: post.edit_reason } end + + def render("error.json", %{changeset: changeset}) do + %{ + errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1) + } + end end