mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
fixes #97: add new route for changing anonymity status of upload
This commit is contained in:
parent
4abd5acd53
commit
406c7dd74b
5 changed files with 53 additions and 4 deletions
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
|
|
34
lib/philomena_web/controllers/image/anonymous_controller.ex
Normal file
34
lib/philomena_web/controllers/image/anonymous_controller.ex
Normal 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
|
|
@ -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],
|
||||
|
|
|
@ -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?"]
|
||||
|
|
Loading…
Reference in a new issue