From 4cdc3622370969456b2facad6d9171d0c498cf7b Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 4 Jul 2020 13:13:05 -0400 Subject: [PATCH] philomena_web: hack in forum posts to the firehose (#2) * philomena_web: hack in forum posts to the firehose (cherry-picked from commit c5ff6f38f3c1274cd0c6160b798d6f74549735ef) --- lib/philomena_web/controllers/topic/post_controller.ex | 8 ++++++++ lib/philomena_web/controllers/topic_controller.ex | 8 ++++++++ lib/philomena_web/views/api/json/forum/topic/post_view.ex | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/lib/philomena_web/controllers/topic/post_controller.ex b/lib/philomena_web/controllers/topic/post_controller.ex index f1234dc3..df6b62c1 100644 --- a/lib/philomena_web/controllers/topic/post_controller.ex +++ b/lib/philomena_web/controllers/topic/post_controller.ex @@ -35,6 +35,14 @@ defmodule PhilomenaWeb.Topic.PostController do 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 + conn |> put_flash(:info, "Post created successfully.") |> redirect( diff --git a/lib/philomena_web/controllers/topic_controller.ex b/lib/philomena_web/controllers/topic_controller.ex index e96ab7ad..9d781676 100644 --- a/lib/philomena_web/controllers/topic_controller.ex +++ b/lib/philomena_web/controllers/topic_controller.ex @@ -108,6 +108,14 @@ defmodule PhilomenaWeb.TopicController do Posts.reindex_post(post) Topics.notify_topic(topic) + 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 + conn |> put_flash(:info, "Successfully posted topic.") |> redirect(to: Routes.forum_topic_path(conn, :show, forum, topic)) 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 ef69e3d6..4d6ecdcb 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 @@ -13,6 +13,14 @@ defmodule PhilomenaWeb.Api.Json.Forum.Topic.PostView do %{post: render_one(post, PhilomenaWeb.Api.Json.Forum.Topic.PostView, "post.json", assigns)} end + def render("firehose.json", %{post: post, topic: topic, forum: forum} = assigns) do + %{ + post: render_one(post, PhilomenaWeb.Api.Json.Forum.Topic.PostView, "post.json", assigns), + topic: render_one(topic, PhilomenaWeb.Api.Json.Forum.TopicView, "topic.json", assigns), + forum: render_one(forum, PhilomenaWeb.Api.Json.ForumView, "forum.json", assigns) + } + end + def render("post.json", %{post: %{topic: %{hidden_from_users: true}} = post}) do %{ id: post.id,