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

78 lines
2.2 KiB
Elixir
Raw Normal View History

2019-11-24 13:36:21 -05:00
defmodule PhilomenaWeb.Image.SourceController do
use PhilomenaWeb, :controller
alias Philomena.SourceChanges.SourceChange
alias Philomena.UserStatistics
2019-11-24 13:36:21 -05:00
alias Philomena.Images.Image
alias Philomena.Images
alias Philomena.Repo
import Ecto.Query
2019-11-24 13:36:21 -05:00
2020-07-21 10:50:33 -04:00
plug PhilomenaWeb.LimitPlug,
[time: 5, error: "You may only update metadata once every 5 seconds."]
when action in [:update]
2019-11-24 13:36:21 -05:00
plug PhilomenaWeb.FilterBannedUsersPlug
plug PhilomenaWeb.CaptchaPlug
2020-09-12 13:43:16 -04:00
plug PhilomenaWeb.CheckCaptchaPlug
2019-11-24 13:36:21 -05:00
plug PhilomenaWeb.UserAttributionPlug
plug PhilomenaWeb.CanaryMapPlug, update: :edit_metadata
2020-12-06 10:08:13 -05:00
plug :load_and_authorize_resource,
model: Image,
id_name: "image_id",
2021-10-08 21:33:16 -04:00
preload: [:user, :sources, tags: :aliases]
2019-11-24 13:36:21 -05:00
def update(conn, %{"image" => image_params}) do
attributes = conn.assigns.attributes
image = conn.assigns.image
2021-10-08 21:33:16 -04:00
case Images.update_sources(image, attributes, image_params) do
{:ok, %{image: {image, added_sources, removed_sources}}} ->
2020-06-12 12:56:11 -04:00
PhilomenaWeb.Endpoint.broadcast!(
2020-06-15 19:57:33 -04:00
"firehose",
"image:source_update",
2021-10-08 21:33:16 -04:00
%{image_id: image.id, added: [added_sources], removed: [removed_sources]}
2020-06-15 19:57:33 -04:00
)
2020-07-06 14:12:18 -04:00
2020-06-15 19:57:33 -04:00
PhilomenaWeb.Endpoint.broadcast!(
2020-06-12 12:56:11 -04:00
"firehose",
"image:update",
PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []})
)
2020-01-10 23:20:19 -05:00
changeset = Images.change_image(image)
2019-11-24 13:36:21 -05:00
source_change_count =
SourceChange
|> where(image_id: ^image.id)
|> Repo.aggregate(:count, :id)
2021-10-08 21:33:16 -04:00
if Enum.any?(added_sources) or Enum.any?(removed_sources) do
UserStatistics.inc_stat(conn.assigns.current_user, :metadata_updates)
end
2020-04-19 15:05:42 -04:00
Images.reindex_image(image)
2019-11-24 13:36:21 -05:00
conn
|> put_view(PhilomenaWeb.ImageView)
2020-01-10 23:20:19 -05:00
|> render("_source.html",
layout: false,
source_change_count: source_change_count,
image: image,
changeset: changeset
)
2019-11-24 13:36:21 -05:00
{:error, :image, changeset, _} ->
conn
|> put_view(PhilomenaWeb.ImageView)
2020-01-10 23:20:19 -05:00
|> render("_source.html",
layout: false,
source_change_count: 0,
image: image,
changeset: changeset
)
2019-11-24 13:36:21 -05:00
end
end
2020-01-10 23:20:19 -05:00
end