2019-11-17 03:20:33 +01:00
|
|
|
defmodule PhilomenaWeb.Image.FaveController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.{Images, Images.Image}
|
|
|
|
alias Philomena.{ImageFaves, ImageVotes}
|
|
|
|
alias Philomena.Repo
|
|
|
|
alias Ecto.Multi
|
|
|
|
|
2019-11-17 19:18:21 +01:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :vote, delete: :vote
|
2020-06-12 19:00:59 +02:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Image,
|
|
|
|
id_name: "image_id",
|
|
|
|
persisted: true,
|
2023-05-29 13:06:41 +02:00
|
|
|
preload: [:sources, tags: :aliases]
|
2020-06-12 19:00:59 +02:00
|
|
|
|
2020-06-07 03:03:17 +02:00
|
|
|
plug PhilomenaWeb.FilterForcedUsersPlug
|
2019-11-17 03:20:33 +01:00
|
|
|
|
|
|
|
def create(conn, _params) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
image = conn.assigns.image
|
|
|
|
|
|
|
|
Multi.append(
|
|
|
|
ImageFaves.delete_fave_transaction(image, user),
|
|
|
|
ImageFaves.create_fave_transaction(image, user)
|
|
|
|
)
|
|
|
|
|> Multi.append(ImageVotes.delete_vote_transaction(image, user))
|
|
|
|
|> Multi.append(ImageVotes.create_vote_transaction(image, user, true))
|
2020-09-06 07:30:53 +02:00
|
|
|
|> Repo.transaction()
|
2019-11-17 03:20:33 +01:00
|
|
|
|> case do
|
|
|
|
{:ok, _result} ->
|
|
|
|
image =
|
|
|
|
Images.get_image!(image.id)
|
|
|
|
|> Images.reindex_image()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> json(Image.interaction_data(image))
|
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> Plug.Conn.put_status(409)
|
|
|
|
|> json(%{})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
image = conn.assigns.image
|
|
|
|
|
|
|
|
ImageFaves.delete_fave_transaction(image, user)
|
2020-09-06 07:30:53 +02:00
|
|
|
|> Repo.transaction()
|
2019-11-17 03:20:33 +01:00
|
|
|
|> case do
|
|
|
|
{:ok, _result} ->
|
|
|
|
image =
|
|
|
|
Images.get_image!(image.id)
|
|
|
|
|> Images.reindex_image()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> json(Image.interaction_data(image))
|
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> Plug.Conn.put_status(409)
|
|
|
|
|> json(%{})
|
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|