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
|
|
|
|
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug
|
|
|
|
plug PhilomenaWeb.CaptchaPlug
|
|
|
|
plug PhilomenaWeb.UserAttributionPlug
|
2019-11-25 03:16:22 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, update: :edit_metadata
|
2019-11-24 19:36:21 +01:00
|
|
|
plug :load_and_authorize_resource, model: Image, id_name: "image_id"
|
|
|
|
|
|
|
|
def update(conn, %{"image" => image_params}) do
|
|
|
|
attributes = conn.assigns.attributes
|
|
|
|
image = conn.assigns.image
|
|
|
|
|
|
|
|
case Images.update_source(image, attributes, image_params) do
|
|
|
|
{:ok, %{image: image}} ->
|
|
|
|
changeset =
|
|
|
|
Images.change_image(image)
|
|
|
|
|
2019-12-08 00:41:31 +01:00
|
|
|
source_change_count =
|
|
|
|
SourceChange
|
|
|
|
|> where(image_id: ^image.id)
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
|
2019-12-05 20:23:26 +01:00
|
|
|
UserStatistics.inc_stat(conn.assigns.current_user, :metadata_updates)
|
|
|
|
|
2019-11-24 19:36:21 +01:00
|
|
|
conn
|
|
|
|
|> put_view(PhilomenaWeb.ImageView)
|
2019-12-08 00:41:31 +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)
|
2019-12-08 00:41:31 +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
|
|
|
|
end
|