fixes #97: add new route for changing anonymity status of upload

This commit is contained in:
byte[] 2020-04-30 00:14:14 -04:00
parent 4abd5acd53
commit 406c7dd74b
5 changed files with 53 additions and 4 deletions

View file

@ -324,6 +324,12 @@ defmodule Philomena.Images do
|> Repo.update() |> Repo.update()
end end
def update_anonymous(%Image{} = image, attrs) do
image
|> Image.anonymous_changeset(attrs)
|> Repo.update()
end
def hide_image(%Image{} = image, user, attrs) do def hide_image(%Image{} = image, user, attrs) do
DuplicateReport DuplicateReport
|> where(state: "open") |> where(state: "open")

View file

@ -260,12 +260,15 @@ defmodule Philomena.Images.Image do
end end
image image
|> cast(attrs, [:anonymous])
|> put_change(:user_id, user_id) |> put_change(:user_id, user_id)
|> put_change(:ip, %Postgrex.INET{address: {127, 0, 0, 1}, netmask: 32}) |> put_change(:ip, %Postgrex.INET{address: {127, 0, 0, 1}, netmask: 32})
|> put_change(:fingerprint, "ffff") |> put_change(:fingerprint, "ffff")
end end
def anonymous_changeset(image, attrs) do
cast(image, attrs, [:anonymous])
end
def cache_changeset(image) do def cache_changeset(image) do
changeset = change(image) changeset = change(image)
image = apply_changes(changeset) image = apply_changes(changeset)

View file

@ -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

View file

@ -188,6 +188,7 @@ defmodule PhilomenaWeb.Router do
resources "/file", Image.FileController, only: [:update], singleton: true resources "/file", Image.FileController, only: [:update], singleton: true
resources "/scratchpad", Image.ScratchpadController, only: [:edit, :update], singleton: true resources "/scratchpad", Image.ScratchpadController, only: [:edit, :update], singleton: true
resources "/uploader", Image.UploaderController, only: [: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, resources "/comment_lock", Image.CommentLockController,
only: [:create, :delete], only: [:create, :delete],

View file

@ -7,15 +7,20 @@ span.image_uploader
=> link_to_fingerprint(@conn, @image.fingerprint) => link_to_fingerprint(@conn, @image.fingerprint)
a#edit-uploader href="#" data-click-hide=".image_uploader" data-click-show="#uploader-form" a#edit-uploader href="#" data-click-hide=".image_uploader" data-click-show="#uploader-form"
i.fas.fa-edit i.fas.fa-edit
a#edit-anonymous href="#" data-click-toggle=".image-anonymous"
i.fas.fa-eye
= if can?(@conn, :show, :ip_address) do = 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 -> = 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" => label f, :username, "Uploader"
=> text_input f, :username, value: username(@image.user), class: "input input--short input--small" => 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…")] = 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. 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?"]