From 8ef94e912b8fb8c6d6903871e7bbd68484c0a1e3 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 12 Jun 2020 12:56:11 -0400 Subject: [PATCH] add firehose API --- config/config.exs | 3 ++- docker/web/nginx.conf | 1 + lib/philomena_web/channels/firehose_channel.ex | 16 ++++++++++++++++ lib/philomena_web/channels/user_socket.ex | 4 ++-- .../controllers/image/comment_controller.ex | 6 ++++++ .../controllers/image/source_controller.ex | 8 +++++++- .../controllers/image/tag_controller.ex | 8 +++++++- .../controllers/image_controller.ex | 6 ++++++ lib/philomena_web/views/api/json/image_view.ex | 7 ++++++- 9 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 lib/philomena_web/channels/firehose_channel.ex diff --git a/config/config.exs b/config/config.exs index 04d0bea7..b84e4493 100644 --- a/config/config.exs +++ b/config/config.exs @@ -63,7 +63,8 @@ config :canary, config :philomena, PhilomenaWeb.Endpoint, url: [host: "localhost"], secret_key_base: "xZYTon09JNRrj8snd7KL31wya4x71jmo5aaSSRmw1dGjWLRmEwWMTccwxgsGFGjM", - render_errors: [view: PhilomenaWeb.ErrorView, accepts: ~w(html json)] + render_errors: [view: PhilomenaWeb.ErrorView, accepts: ~w(html json)], + pubsub_server: Philomena.PubSub config :phoenix, :template_engines, slim: PhoenixSlime.Engine, diff --git a/docker/web/nginx.conf b/docker/web/nginx.conf index 6d590d5b..9b3321ff 100644 --- a/docker/web/nginx.conf +++ b/docker/web/nginx.conf @@ -70,6 +70,7 @@ server { proxy_temp_file_write_size 64k; # Configuration for Phoenix WS + proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } diff --git a/lib/philomena_web/channels/firehose_channel.ex b/lib/philomena_web/channels/firehose_channel.ex new file mode 100644 index 00000000..7f34e955 --- /dev/null +++ b/lib/philomena_web/channels/firehose_channel.ex @@ -0,0 +1,16 @@ +defmodule PhilomenaWeb.FirehoseChannel do + use Phoenix.Channel + + def join("firehose", _params, socket) do + {:ok, socket} + end + + def join(_room, _params, _socket) do + {:error, %{reason: "not_found"}} + end + + # Don't allow the connected client to send any messages to the socket + def handle_in(message, _params, socket) do + {:stop, :shutdown, socket} + end +end diff --git a/lib/philomena_web/channels/user_socket.ex b/lib/philomena_web/channels/user_socket.ex index 5f6ca42b..041525ac 100644 --- a/lib/philomena_web/channels/user_socket.ex +++ b/lib/philomena_web/channels/user_socket.ex @@ -1,8 +1,8 @@ defmodule PhilomenaWeb.UserSocket do use Phoenix.Socket - ## Channels - # channel "room:*", PhilomenaWeb.RoomChannel + # Channels + channel "firehose", PhilomenaWeb.FirehoseChannel # Socket params are passed from the client and can # be used to verify and authenticate a user. After diff --git a/lib/philomena_web/controllers/image/comment_controller.ex b/lib/philomena_web/controllers/image/comment_controller.ex index 4e0399af..1348bbd6 100644 --- a/lib/philomena_web/controllers/image/comment_controller.ex +++ b/lib/philomena_web/controllers/image/comment_controller.ex @@ -63,6 +63,12 @@ defmodule PhilomenaWeb.Image.CommentController do case Comments.create_comment(image, attributes, comment_params) do {:ok, %{comment: comment}} -> + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "comment:create", + PhilomenaWeb.Api.Json.CommentView.render("show.json", %{comment: comment}) + ) + Comments.notify_comment(comment) Comments.reindex_comment(comment) Images.reindex_image(conn.assigns.image) diff --git a/lib/philomena_web/controllers/image/source_controller.ex b/lib/philomena_web/controllers/image/source_controller.ex index 561b4ca4..820fa509 100644 --- a/lib/philomena_web/controllers/image/source_controller.ex +++ b/lib/philomena_web/controllers/image/source_controller.ex @@ -12,7 +12,7 @@ defmodule PhilomenaWeb.Image.SourceController do plug PhilomenaWeb.CaptchaPlug plug PhilomenaWeb.UserAttributionPlug plug PhilomenaWeb.CanaryMapPlug, update: :edit_metadata - plug :load_and_authorize_resource, model: Image, id_name: "image_id" + plug :load_and_authorize_resource, model: Image, id_name: "image_id", preload: [:tags, :user] def update(conn, %{"image" => image_params}) do attributes = conn.assigns.attributes @@ -21,6 +21,12 @@ defmodule PhilomenaWeb.Image.SourceController do case Images.update_source(image, attributes, image_params) do {:ok, %{image: image}} -> + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "image:update", + PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []}) + ) + changeset = Images.change_image(image) source_change_count = diff --git a/lib/philomena_web/controllers/image/tag_controller.ex b/lib/philomena_web/controllers/image/tag_controller.ex index c7fdfb7d..2fd92a42 100644 --- a/lib/philomena_web/controllers/image/tag_controller.ex +++ b/lib/philomena_web/controllers/image/tag_controller.ex @@ -14,7 +14,7 @@ defmodule PhilomenaWeb.Image.TagController do plug PhilomenaWeb.CaptchaPlug plug PhilomenaWeb.UserAttributionPlug plug PhilomenaWeb.CanaryMapPlug, update: :edit_metadata - plug :load_and_authorize_resource, model: Image, id_name: "image_id" + plug :load_and_authorize_resource, model: Image, id_name: "image_id", preload: [:tags, :user] def update(conn, %{"image" => image_params}) do attributes = conn.assigns.attributes @@ -22,6 +22,12 @@ defmodule PhilomenaWeb.Image.TagController do case Images.update_tags(image, attributes, image_params) do {:ok, %{image: {image, added_tags, removed_tags}}} -> + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "image:update", + PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []}) + ) + Comments.reindex_comments(image) Images.reindex_image(image) Tags.reindex_tags(added_tags ++ removed_tags) diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 1e363e97..010dcf70 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -108,6 +108,12 @@ defmodule PhilomenaWeb.ImageController do case Images.create_image(attributes, image_params) do {:ok, %{image: image}} -> + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "image:create", + PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []}) + ) + conn |> put_flash(:info, "Image created successfully.") |> redirect(to: Routes.image_path(conn, :show, image)) diff --git a/lib/philomena_web/views/api/json/image_view.ex b/lib/philomena_web/views/api/json/image_view.ex index a722f7c9..d0985a85 100644 --- a/lib/philomena_web/views/api/json/image_view.ex +++ b/lib/philomena_web/views/api/json/image_view.ex @@ -45,6 +45,12 @@ defmodule PhilomenaWeb.Api.Json.ImageView do end def render("image.json", %{conn: conn, image: %{hidden_from_users: false} = image}) do + result = render_one(image, PhilomenaWeb.Api.Json.ImageView, "image.json", %{image: image}) + + Map.put(result, :spoilered, ImageView.filter_or_spoiler_hits?(conn, image)) + end + + def render("image.json", %{image: %{hidden_from_users: false} = image}) do %{ id: image.id, created_at: image.created_at, @@ -74,7 +80,6 @@ defmodule PhilomenaWeb.Api.Json.ImageView do source_url: image.source_url, view_url: ImageView.pretty_url(image, false, false), representations: ImageView.thumb_urls(image, false), - spoilered: ImageView.filter_or_spoiler_hits?(conn, image), thumbnails_generated: image.thumbnails_generated, processed: image.processed, deletion_reason: nil,