diff --git a/lib/philomena_web/controllers/image/favorite_controller.ex b/lib/philomena_web/controllers/image/favorite_controller.ex index 8d216cf1..6ce481b6 100644 --- a/lib/philomena_web/controllers/image/favorite_controller.ex +++ b/lib/philomena_web/controllers/image/favorite_controller.ex @@ -14,7 +14,7 @@ defmodule PhilomenaWeb.Image.FavoriteController do defp load_votes_if_authorized(conn, _opts) do case Canada.Can.can?(conn.assigns.current_user, :hide, conn.assigns.image) do true -> - image = Repo.preload(conn.assigns.image, upvotes: :user, downvotes: :user) + image = Repo.preload(conn.assigns.image, upvotes: :user, downvotes: :user, hides: :user) conn |> assign(:image, image) diff --git a/lib/philomena_web/controllers/image/tamper_controller.ex b/lib/philomena_web/controllers/image/tamper_controller.ex new file mode 100644 index 00000000..cb115862 --- /dev/null +++ b/lib/philomena_web/controllers/image/tamper_controller.ex @@ -0,0 +1,29 @@ +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 + + plug PhilomenaWeb.CanaryMapPlug, create: :hide + 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 + + {:ok, _result} = + ImageVotes.delete_vote_transaction(image, user) + |> Repo.isolated_transaction(:serializable) + + Images.reindex_image(image) + + conn + |> put_flash(:info, "Vote removed.") + |> redirect(to: Routes.image_path(conn, :show, conn.assigns.image)) + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 76da0c35..ce5c899a 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -138,6 +138,7 @@ defmodule PhilomenaWeb.Router do end resources "/delete", Image.DeleteController, only: [:create, :delete], singleton: true + resources "/tamper", Image.TamperController, only: [:create], singleton: true resources "/hash", Image.HashController, only: [:delete], singleton: true resources "/source_history", Image.SourceHistoryController, only: [:delete], singleton: true resources "/repair", Image.RepairController, only: [:create], singleton: true diff --git a/lib/philomena_web/templates/image/favorite/index.html.slime b/lib/philomena_web/templates/image/favorite/index.html.slime index 5bbdf7ea..3f8611d9 100644 --- a/lib/philomena_web/templates/image/favorite/index.html.slime +++ b/lib/philomena_web/templates/image/favorite/index.html.slime @@ -10,10 +10,12 @@ h5 h5 ' Upvoted by => @image.upvotes_count - = pluralize("user", "users", @image.faves_count) + = pluralize("user", "users", @image.upvotes_count) = for upvote <- Enum.sort_by(@image.upvotes, & &1.user.name) do - => link upvote.user.name, to: Routes.profile_path(@conn, :show, upvote.user), class: "interaction-user-list-item" + span.interaction-user-list-item + => link upvote.user.name, to: Routes.profile_path(@conn, :show, upvote.user) + => link "(x)", to: Routes.image_tamper_path(@conn, :create, @image, user_id: upvote.user_id), method: "post" h5 ' Downvoted by @@ -21,4 +23,14 @@ h5 = pluralize("user", "users", @image.downvotes_count) = for downvote <- Enum.sort_by(@image.downvotes, & &1.user.name) do - => link downvote.user.name, to: Routes.profile_path(@conn, :show, downvote.user), class: "interaction-user-list-item" + span.interaction-user-list-item + => link downvote.user.name, to: Routes.profile_path(@conn, :show, downvote.user) + => link "(x)", to: Routes.image_tamper_path(@conn, :create, @image, user_id: downvote.user_id), method: "post" + + h5 + ' Hidden by + => @image.hides_count + = pluralize("user", "users", @image.hides_count) + + = for hide <- Enum.sort_by(@image.hides, & &1.user.name) do + => link hide.user.name, to: Routes.profile_path(@conn, :show, hide.user), class: "interaction-user-list-item"