From 13ad9e964d3e2c7ad53f7f745a180707372382e5 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 7 Dec 2019 11:48:39 -0500 Subject: [PATCH] add duplicate and deleted handling --- .../controllers/image_controller.ex | 28 ++++++++++++++++++- .../templates/image/deleted.html.slime | 23 +++++++++++++++ lib/philomena_web/views/image_view.ex | 3 ++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 lib/philomena_web/templates/image/deleted.html.slime diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index b64b29d7..95f4ffae 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -12,7 +12,7 @@ defmodule PhilomenaWeb.ImageController do alias Philomena.Repo import Ecto.Query - plug :load_and_authorize_resource, model: Image, only: :show, preload: [:tags, user: [awards: :badge]] + plug :load_image plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create] plug PhilomenaWeb.UserAttributionPlug when action in [:create] @@ -135,4 +135,30 @@ defmodule PhilomenaWeb.ImageController do {user_galleries, image_galleries} end + + defp load_image(conn, _opts) do + id = conn.params["id"] + + image = + Image + |> where(id: ^id) + |> preload([:tags, :deleter, user: [awards: :badge]]) + |> Repo.one() + + cond do + is_nil(image) -> + PhilomenaWeb.NotFoundPlug.call(conn) + + not is_nil(image.duplicate_id) and not Canada.Can.can?(conn, :show, image) -> + conn + |> put_flash(:info, "The image you were looking for has been marked a duplicate of the image below") + |> redirect(to: Routes.image_path(conn, :show, image.duplicate_id)) + + image.hidden_from_users -> + render(conn, "deleted.html", image: image) + + true -> + assign(conn, :image, image) + end + end end diff --git a/lib/philomena_web/templates/image/deleted.html.slime b/lib/philomena_web/templates/image/deleted.html.slime new file mode 100644 index 00000000..389edc0a --- /dev/null +++ b/lib/philomena_web/templates/image/deleted.html.slime @@ -0,0 +1,23 @@ +.walloftext + .block.block--fixed.block--warning + h1 This image has been deleted + p + ' Reason: + strong + = @image.deletion_reason || "Unknown (likely deleted in error). Please contact a moderator." + + = if can?(@conn, :undelete, @image) do + p + strong> Spoilers! + ' Done by: + strong = deleter(@image) + p + ' If you originally uploaded the file previously located here, please don't re-upload it - + => link "contact us", to: "/pages/contact" + ' if you feel this was in error and we'll talk! We're only human, and mistakes happen. + p + ' Here's the + => link "tagging guidelines", to: "/pages/tags" + ' and + = link "rules of the site", to: "/pages/rules" + ' . Other useful links can be found at the bottom of the page. \ No newline at end of file diff --git a/lib/philomena_web/views/image_view.ex b/lib/philomena_web/views/image_view.ex index dc2595d8..2faafb80 100644 --- a/lib/philomena_web/views/image_view.ex +++ b/lib/philomena_web/views/image_view.ex @@ -144,6 +144,9 @@ defmodule PhilomenaWeb.ImageView do render PhilomenaWeb.TagView, "_tags_row.html", conn: conn, tags: tags end + def deleter(%{deleter: %{name: name}}), do: name + def deleter(_image), do: "System" + defp thumb_format("svg", _name), do: "png" defp thumb_format(_, :rendered), do: "png" defp thumb_format(format, _name), do: format