2019-12-08 05:53:18 +01:00
|
|
|
defmodule PhilomenaWeb.Image.DeleteController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
# N.B.: this would be Image.Hide, because it hides the image, but that is
|
|
|
|
# taken by the user action
|
|
|
|
|
|
|
|
alias Philomena.Images.Image
|
|
|
|
alias Philomena.Images
|
|
|
|
|
2020-09-07 05:59:50 +02:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide, update: :hide, delete: :hide
|
2019-12-08 05:53:18 +01:00
|
|
|
plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
|
2020-02-01 00:50:50 +01:00
|
|
|
plug :verify_deleted when action in [:update]
|
2019-12-08 05:53:18 +01:00
|
|
|
|
|
|
|
def create(conn, %{"image" => image_params}) do
|
|
|
|
image = conn.assigns.image
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Images.hide_image(image, user, image_params) do
|
2021-11-07 19:51:55 +01:00
|
|
|
{:ok, result} ->
|
2019-12-08 05:53:18 +01:00
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Image successfully hidden.")
|
2021-11-08 14:45:19 +01:00
|
|
|
|> moderation_log(details: &log_details/3, data: result.image)
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/images/#{image}")
|
2019-12-08 05:53:18 +01:00
|
|
|
|
2020-09-10 18:02:01 +02:00
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Failed to hide image.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/images/#{image}")
|
2019-12-08 05:53:18 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-01 00:50:50 +01:00
|
|
|
def update(conn, %{"image" => image_params}) do
|
|
|
|
image = conn.assigns.image
|
|
|
|
|
|
|
|
case Images.update_hide_reason(image, image_params) do
|
|
|
|
{:ok, image} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Hide reason updated.")
|
2021-11-08 14:45:19 +01:00
|
|
|
|> moderation_log(details: &log_details/3, data: image)
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/images/#{image}")
|
2020-02-01 00:50:50 +01:00
|
|
|
|
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Couldn't update hide reason.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/images/#{image}")
|
2020-02-01 00:50:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_deleted(conn, _opts) do
|
|
|
|
case conn.assigns.image.hidden_from_users do
|
|
|
|
true ->
|
|
|
|
conn
|
2020-02-01 17:04:11 +01:00
|
|
|
|
2020-02-01 00:50:50 +01:00
|
|
|
_false ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Cannot change hide reason on a non-hidden image!")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/images/#{conn.assigns.image}")
|
2020-05-04 09:18:36 +02:00
|
|
|
|> halt()
|
2020-02-01 00:50:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-08 05:53:18 +01:00
|
|
|
def delete(conn, _params) do
|
|
|
|
image = conn.assigns.image
|
|
|
|
|
2021-11-07 19:51:55 +01:00
|
|
|
{:ok, image} = Images.unhide_image(image)
|
2019-12-08 05:53:18 +01:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Image successfully unhidden.")
|
2021-11-08 14:45:19 +01:00
|
|
|
|> moderation_log(details: &log_details/3, data: image)
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/images/#{image}")
|
2019-12-08 05:53:18 +01:00
|
|
|
end
|
2021-11-07 19:51:55 +01:00
|
|
|
|
2024-04-29 03:03:37 +02:00
|
|
|
defp log_details(_conn, action, image) do
|
2021-11-07 19:51:55 +01:00
|
|
|
body =
|
|
|
|
case action do
|
|
|
|
:create -> "Hidden image >>#{image.id} (#{image.deletion_reason})"
|
|
|
|
:update -> "Changed hide reason of >>#{image.id} (#{image.deletion_reason})"
|
|
|
|
:delete -> "Restored image >>#{image.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
%{
|
|
|
|
body: body,
|
2024-04-29 02:55:27 +02:00
|
|
|
subject_path: ~p"/images/#{image}"
|
2021-11-07 19:51:55 +01:00
|
|
|
}
|
|
|
|
end
|
2019-12-17 17:45:22 +01:00
|
|
|
end
|