2019-11-29 06:39:15 +01:00
|
|
|
defmodule PhilomenaWeb.Image.DescriptionController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2021-09-29 22:24:38 +02:00
|
|
|
alias PhilomenaWeb.MarkdownRenderer
|
2019-11-29 06:39:15 +01:00
|
|
|
alias Philomena.Images.Image
|
|
|
|
alias Philomena.Images
|
|
|
|
|
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, update: :edit_description
|
2020-07-06 20:12:18 +02:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Image,
|
|
|
|
id_name: "image_id",
|
|
|
|
persisted: true,
|
2020-12-03 20:23:32 +01:00
|
|
|
preload: [:user, tags: :aliases]
|
2019-11-29 06:39:15 +01:00
|
|
|
|
|
|
|
def update(conn, %{"image" => image_params}) do
|
|
|
|
image = conn.assigns.image
|
2020-06-16 01:57:33 +02:00
|
|
|
old_description = image.description
|
2019-11-29 06:39:15 +01:00
|
|
|
|
|
|
|
case Images.update_description(image, image_params) do
|
|
|
|
{:ok, image} ->
|
2020-06-16 01:57:33 +02:00
|
|
|
PhilomenaWeb.Endpoint.broadcast!(
|
|
|
|
"firehose",
|
|
|
|
"image:description_update",
|
|
|
|
%{image_id: image.id, added: image.description, removed: old_description}
|
|
|
|
)
|
2020-07-06 20:12:18 +02:00
|
|
|
|
2020-06-16 01:57:33 +02:00
|
|
|
PhilomenaWeb.Endpoint.broadcast!(
|
|
|
|
"firehose",
|
|
|
|
"image:update",
|
|
|
|
PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []})
|
|
|
|
)
|
|
|
|
|
2019-11-29 06:39:15 +01:00
|
|
|
Images.reindex_image(image)
|
|
|
|
|
2021-09-19 23:22:48 +02:00
|
|
|
body =
|
2021-09-29 22:24:38 +02:00
|
|
|
MarkdownRenderer.render_one(%{body: image.description}, conn)
|
2019-11-29 06:39:15 +01:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_view(PhilomenaWeb.ImageView)
|
|
|
|
|> render("_description.html", layout: false, image: image, body: body)
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
conn
|
|
|
|
|> render("_form.html", layout: false, image: image, changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|