From cec8b605a841adc14f0afa80d3d45c83617de58e Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 15 Jun 2020 19:57:33 -0400 Subject: [PATCH] add additional live events --- lib/philomena/workers/thumbnail_worker.ex | 6 ++++++ .../controllers/api/json/image_controller.ex | 6 ++++++ .../controllers/image/comment_controller.ex | 6 ++++++ .../controllers/image/description_controller.ex | 14 +++++++++++++- .../controllers/image/source_controller.ex | 5 +++++ .../controllers/image/tag_controller.ex | 5 +++++ 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/philomena/workers/thumbnail_worker.ex b/lib/philomena/workers/thumbnail_worker.ex index b3bd632e..6e1cb3a7 100644 --- a/lib/philomena/workers/thumbnail_worker.ex +++ b/lib/philomena/workers/thumbnail_worker.ex @@ -3,5 +3,11 @@ defmodule Philomena.ThumbnailWorker do def perform(image_id) do Thumbnailer.generate_thumbnails(image_id) + + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "image:processed", + %{image_id: image_id} + ) end end diff --git a/lib/philomena_web/controllers/api/json/image_controller.ex b/lib/philomena_web/controllers/api/json/image_controller.ex index 0ddc1f51..4d115c47 100644 --- a/lib/philomena_web/controllers/api/json/image_controller.ex +++ b/lib/philomena_web/controllers/api/json/image_controller.ex @@ -41,6 +41,12 @@ defmodule PhilomenaWeb.Api.Json.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: []}) + ) + render(conn, "show.json", image: image, interactions: []) {:error, :image, changeset, _} -> diff --git a/lib/philomena_web/controllers/image/comment_controller.ex b/lib/philomena_web/controllers/image/comment_controller.ex index 5d443903..a5e3de95 100644 --- a/lib/philomena_web/controllers/image/comment_controller.ex +++ b/lib/philomena_web/controllers/image/comment_controller.ex @@ -103,6 +103,12 @@ defmodule PhilomenaWeb.Image.CommentController do def update(conn, %{"comment" => comment_params}) do case Comments.update_comment(conn.assigns.comment, conn.assigns.current_user, comment_params) do {:ok, %{comment: comment}} -> + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "comment:update", + PhilomenaWeb.Api.Json.CommentView.render("show.json", %{comment: comment}) + ) + Comments.reindex_comment(comment) conn diff --git a/lib/philomena_web/controllers/image/description_controller.ex b/lib/philomena_web/controllers/image/description_controller.ex index e5dd6e90..b249ba4c 100644 --- a/lib/philomena_web/controllers/image/description_controller.ex +++ b/lib/philomena_web/controllers/image/description_controller.ex @@ -7,13 +7,25 @@ defmodule PhilomenaWeb.Image.DescriptionController do plug PhilomenaWeb.FilterBannedUsersPlug plug PhilomenaWeb.CanaryMapPlug, update: :edit_description - plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true + plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true, preload: [:tags, :user] def update(conn, %{"image" => image_params}) do image = conn.assigns.image + old_description = image.description case Images.update_description(image, image_params) do {:ok, image} -> + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "image:description_update", + %{image_id: image.id, added: image.description, removed: old_description} + ) + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "image:update", + PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []}) + ) + Images.reindex_image(image) body = TextileRenderer.render_one(%{body: image.description}, conn) diff --git a/lib/philomena_web/controllers/image/source_controller.ex b/lib/philomena_web/controllers/image/source_controller.ex index 820fa509..729e7466 100644 --- a/lib/philomena_web/controllers/image/source_controller.ex +++ b/lib/philomena_web/controllers/image/source_controller.ex @@ -21,6 +21,11 @@ defmodule PhilomenaWeb.Image.SourceController do case Images.update_source(image, attributes, image_params) do {:ok, %{image: image}} -> + PhilomenaWeb.Endpoint.broadcast!( + "firehose", + "image:source_update", + %{image_id: image.id, added: [image.source_url], removed: [old_source]} + ) PhilomenaWeb.Endpoint.broadcast!( "firehose", "image:update", diff --git a/lib/philomena_web/controllers/image/tag_controller.ex b/lib/philomena_web/controllers/image/tag_controller.ex index 2fd92a42..f80acdc7 100644 --- a/lib/philomena_web/controllers/image/tag_controller.ex +++ b/lib/philomena_web/controllers/image/tag_controller.ex @@ -22,6 +22,11 @@ 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:tag_update", + %{image_id: image.id, added: Enum.map(added_tags, & &1.name), removed: Enum.map(removed_tags, & &1.name)} + ) PhilomenaWeb.Endpoint.broadcast!( "firehose", "image:update",