philomena/lib/philomena_web/controllers/image/description_controller.ex

50 lines
1.4 KiB
Elixir
Raw Normal View History

2019-11-29 06:39:15 +01:00
defmodule PhilomenaWeb.Image.DescriptionController do
use PhilomenaWeb, :controller
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,
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 =
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