2019-12-30 13:50:02 +01:00
|
|
|
defmodule PhilomenaWeb.Image.TamperController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Users.User
|
|
|
|
alias Philomena.Images.Image
|
|
|
|
alias Philomena.Images
|
|
|
|
|
|
|
|
alias Philomena.ImageVotes
|
|
|
|
alias Philomena.Repo
|
|
|
|
|
2020-09-09 01:03:18 +02:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :tamper
|
2019-12-30 13:50:02 +01:00
|
|
|
plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
|
|
|
|
plug :load_resource, model: User, id_name: "user_id", persisted: true
|
|
|
|
|
|
|
|
def create(conn, _params) do
|
|
|
|
image = conn.assigns.image
|
|
|
|
user = conn.assigns.user
|
|
|
|
|
2021-11-07 19:51:55 +01:00
|
|
|
{:ok, result} =
|
2019-12-30 13:50:02 +01:00
|
|
|
ImageVotes.delete_vote_transaction(image, user)
|
2020-09-06 07:30:53 +02:00
|
|
|
|> Repo.transaction()
|
2019-12-30 13:50:02 +01:00
|
|
|
|
|
|
|
Images.reindex_image(image)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Vote removed.")
|
2021-11-08 14:45:19 +01:00
|
|
|
|> moderation_log(
|
2024-06-23 16:42:29 +02:00
|
|
|
details: &log_details/2,
|
2024-06-23 16:38:56 +02:00
|
|
|
data: %{vote: result, image: image, username: conn.assigns.user.name}
|
2021-11-07 19:51:55 +01:00
|
|
|
)
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/images/#{conn.assigns.image}")
|
2019-12-30 13:50:02 +01:00
|
|
|
end
|
2021-11-07 19:51:55 +01:00
|
|
|
|
2024-06-23 16:42:29 +02:00
|
|
|
defp log_details(_action, data) do
|
2021-11-07 19:51:55 +01:00
|
|
|
image = data.image
|
|
|
|
|
|
|
|
vote_type =
|
|
|
|
case data.vote do
|
|
|
|
%{undownvote: {1, _}} -> "downvote"
|
|
|
|
%{unupvote: {1, _}} -> "upvote"
|
|
|
|
_ -> "vote"
|
|
|
|
end
|
|
|
|
|
|
|
|
%{
|
2024-06-23 16:38:56 +02:00
|
|
|
body: "Deleted #{vote_type} by #{data.username} on image >>#{data.image.id}",
|
2024-04-29 02:55:27 +02:00
|
|
|
subject_path: ~p"/images/#{image}"
|
2021-11-07 19:51:55 +01:00
|
|
|
}
|
|
|
|
end
|
2019-12-30 13:50:02 +01:00
|
|
|
end
|