From 406c7dd74ba0e8d5b1bad3fb6dc791bdbe0e7752 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Thu, 30 Apr 2020 00:14:14 -0400 Subject: [PATCH] fixes #97: add new route for changing anonymity status of upload --- lib/philomena/images.ex | 6 ++++ lib/philomena/images/image.ex | 5 ++- .../controllers/image/anonymous_controller.ex | 34 +++++++++++++++++++ lib/philomena_web/router.ex | 1 + .../templates/image/_uploader.html.slime | 11 ++++-- 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 lib/philomena_web/controllers/image/anonymous_controller.ex diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index 7dd9c87b..01d929a5 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -324,6 +324,12 @@ defmodule Philomena.Images do |> Repo.update() end + def update_anonymous(%Image{} = image, attrs) do + image + |> Image.anonymous_changeset(attrs) + |> Repo.update() + end + def hide_image(%Image{} = image, user, attrs) do DuplicateReport |> where(state: "open") diff --git a/lib/philomena/images/image.ex b/lib/philomena/images/image.ex index 8204c45f..d785e5aa 100644 --- a/lib/philomena/images/image.ex +++ b/lib/philomena/images/image.ex @@ -260,12 +260,15 @@ defmodule Philomena.Images.Image do end image - |> cast(attrs, [:anonymous]) |> put_change(:user_id, user_id) |> put_change(:ip, %Postgrex.INET{address: {127, 0, 0, 1}, netmask: 32}) |> put_change(:fingerprint, "ffff") end + def anonymous_changeset(image, attrs) do + cast(image, attrs, [:anonymous]) + end + def cache_changeset(image) do changeset = change(image) image = apply_changes(changeset) diff --git a/lib/philomena_web/controllers/image/anonymous_controller.ex b/lib/philomena_web/controllers/image/anonymous_controller.ex new file mode 100644 index 00000000..57e4ea92 --- /dev/null +++ b/lib/philomena_web/controllers/image/anonymous_controller.ex @@ -0,0 +1,34 @@ +defmodule PhilomenaWeb.Image.AnonymousController do + use PhilomenaWeb, :controller + + alias Philomena.Images.Image + alias Philomena.Images + + plug :verify_authorized + plug :load_resource, model: Image, id_name: "image_id", persisted: true + + def create(conn, _params) do + Images.update_anonymous(conn.assigns.image, %{"anonymous" => true}) + |> process_request(conn) + end + + def delete(conn, _params) do + Images.update_anonymous(conn.assigns.image, %{"anonymous" => false}) + |> process_request(conn) + end + + defp process_request({:ok, image}, conn) do + Images.reindex_image(image) + + conn + |> put_flash(:info, "Successfully updated anonymity.") + |> redirect(to: Routes.image_path(conn, :show, image)) + end + + defp verify_authorized(conn, _opts) do + case Canada.Can.can?(conn.assigns.current_user, :show, :ip_address) do + true -> conn + _false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) + end + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index fc5f8894..5accded2 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -188,6 +188,7 @@ defmodule PhilomenaWeb.Router do resources "/file", Image.FileController, only: [:update], singleton: true resources "/scratchpad", Image.ScratchpadController, only: [:edit, :update], singleton: true resources "/uploader", Image.UploaderController, only: [:update], singleton: true + resources "/anonymous", Image.AnonymousController, only: [:create, :delete], singleton: true resources "/comment_lock", Image.CommentLockController, only: [:create, :delete], diff --git a/lib/philomena_web/templates/image/_uploader.html.slime b/lib/philomena_web/templates/image/_uploader.html.slime index 0711fa7c..81d69284 100644 --- a/lib/philomena_web/templates/image/_uploader.html.slime +++ b/lib/philomena_web/templates/image/_uploader.html.slime @@ -7,15 +7,20 @@ span.image_uploader => link_to_fingerprint(@conn, @image.fingerprint) a#edit-uploader href="#" data-click-hide=".image_uploader" data-click-show="#uploader-form" i.fas.fa-edit + a#edit-anonymous href="#" data-click-toggle=".image-anonymous" + i.fas.fa-eye = if can?(@conn, :show, :ip_address) do = form_for @changeset, Routes.image_uploader_path(@conn, :update, @image), [class: "block__content hidden", id: "uploader-form", data: [remote: "true", method: "put"]], fn f -> => label f, :username, "Uploader" => text_input f, :username, value: username(@image.user), class: "input input--short input--small" - => label f, :anonymous - = checkbox f, :anonymous, class: "checkbox" - = submit "Save Changes", class: "button button--small", data: [disable_with: raw("Saving…")] div Changes IP to '127.0.0.1' and FP to 'ffff'. Empty for anonymous. + + .image-anonymous.hidden + = if @image.anonymous do + = button_to "Reveal author", Routes.image_anonymous_path(@conn, :delete, @image), class: "button button--small", method: "delete", data: [confirm: "Are you really, really sure?"] + - else + = button_to "Hide author", Routes.image_anonymous_path(@conn, :create, @image), class: "button button--small", method: "create", data: [confirm: "Are you really, really sure?"]