diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index 795020ed..7dd9c87b 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -334,6 +334,12 @@ defmodule Philomena.Images do |> internal_hide_image(image) end + def update_hide_reason(%Image{} = image, attrs) do + image + |> Image.hide_reason_changeset(attrs) + |> Repo.update() + end + def merge_image(%Image{} = image, duplicate_of_image) do result = Image.merge_changeset(image, duplicate_of_image) diff --git a/lib/philomena/images/image.ex b/lib/philomena/images/image.ex index f138a73f..cd104150 100644 --- a/lib/philomena/images/image.ex +++ b/lib/philomena/images/image.ex @@ -202,6 +202,12 @@ defmodule Philomena.Images.Image do |> validate_required([:deletion_reason, :deleter_id]) end + def hide_reason_changeset(image, attrs) do + image + |> cast(attrs, [:deletion_reason]) + |> validate_required([:deletion_reason]) + end + def merge_changeset(image, duplicate_of_image) do change(image) |> put_change(:duplicate_id, duplicate_of_image.id) diff --git a/lib/philomena_web/controllers/image/delete_controller.ex b/lib/philomena_web/controllers/image/delete_controller.ex index 1531cc51..4cf041c4 100644 --- a/lib/philomena_web/controllers/image/delete_controller.ex +++ b/lib/philomena_web/controllers/image/delete_controller.ex @@ -11,6 +11,7 @@ defmodule PhilomenaWeb.Image.DeleteController do plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true + plug :verify_deleted when action in [:update] def create(conn, %{"image" => image_params}) do image = conn.assigns.image @@ -31,6 +32,35 @@ defmodule PhilomenaWeb.Image.DeleteController do end end + def update(conn, %{"image" => image_params}) do + image = conn.assigns.image + + case Images.update_hide_reason(image, image_params) do + {:ok, image} -> + Images.reindex_image(image) + + conn + |> put_flash(:info, "Hide reason updated.") + |> redirect(to: Routes.image_path(conn, :show, image)) + + {:error, _changeset} -> + conn + |> put_flash(:error, "Couldn't update hide reason.") + |> redirect(to: Routes.image_path(conn, :show, image)) + end + end + + defp verify_deleted(conn, _opts) do + case conn.assigns.image.hidden_from_users do + true -> + conn + _false -> + conn + |> put_flash(:error, "Cannot change hide reason on a non-hidden image!") + |> redirect(to: Routes.image_path(conn, :show, conn.assigns.image)) + end + end + def delete(conn, _params) do image = conn.assigns.image diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index d82efc39..06cc509e 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -158,7 +158,7 @@ defmodule PhilomenaWeb.Router do resources "/delete", Image.Comment.DeleteController, only: [:create], singleton: true end - resources "/delete", Image.DeleteController, only: [:create, :delete], singleton: true + resources "/delete", Image.DeleteController, only: [:create, :delete, :update], singleton: true resources "/tamper", Image.TamperController, only: [:create], singleton: true resources "/hash", Image.HashController, only: [:delete], singleton: true diff --git a/lib/philomena_web/templates/image/_options.html.slime b/lib/philomena_web/templates/image/_options.html.slime index 1b26136c..90ab2546 100644 --- a/lib/philomena_web/templates/image/_options.html.slime +++ b/lib/philomena_web/templates/image/_options.html.slime @@ -90,6 +90,12 @@ .field.field--inline = text_input f, :deletion_reason, class: "input input--wide", placeholder: "Rule violation", required: true = submit "Delete", class: "button button--state-danger button--separate-left" + - else + = form_for @changeset, Routes.image_delete_path(@conn, :update, @image), [method: "put"], fn f -> + = label f, :deletion_reason, "Deletion reason (cannot be empty)" + .field.field--inline + = text_input f, :deletion_reason, class: "input input--wide", placeholder: "Rule violation", required: true + = submit "Change hide reason", class: "button button--state-danger button--separate-left" .flex.flex--spaced-out.flex--wrap = if not @image.hidden_from_users do