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