philomena/lib/philomena_web/controllers/duplicate_report_controller.ex

86 lines
2.5 KiB
Elixir
Raw Normal View History

2019-12-01 05:51:44 +01:00
defmodule PhilomenaWeb.DuplicateReportController do
use PhilomenaWeb, :controller
alias Philomena.DuplicateReports
alias Philomena.DuplicateReports.DuplicateReport
alias Philomena.SpoilerExecutor
2019-12-01 05:51:44 +01:00
alias Philomena.Images.Image
alias Philomena.Repo
import Ecto.Query
@valid_states ~W(open rejected accepted claimed)
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:create]
2019-12-07 01:11:25 +01:00
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
2020-01-11 05:20:19 +01:00
plug :load_resource,
model: DuplicateReport,
only: [:show],
preload: [:image, :duplicate_of_image]
2019-12-01 05:51:44 +01:00
def index(conn, params) do
states =
(presence(params["states"]) || ~W(open claimed))
2019-12-01 05:51:44 +01:00
|> wrap()
|> Enum.filter(&Enum.member?(@valid_states, &1))
duplicate_reports =
DuplicateReport
|> where([d], d.state in ^states)
2020-08-16 12:03:33 +02:00
|> preload([:user, :modifier, image: [:user], duplicate_of_image: [:user]])
2019-12-01 05:51:44 +01:00
|> order_by(desc: :created_at)
2020-02-11 04:26:45 +01:00
|> Repo.paginate(conn.assigns.scrivener)
2019-12-01 05:51:44 +01:00
spoilers =
SpoilerExecutor.execute_spoiler(
conn.assigns.compiled_spoiler,
Enum.map(duplicate_reports, &[&1.image, &1.duplicate_of_image])
)
2020-01-11 05:20:19 +01:00
render(conn, "index.html",
title: "Duplicate Reports",
duplicate_reports: duplicate_reports,
spoilers: spoilers,
2020-01-11 05:20:19 +01:00
layout_class: "layout--wide"
)
2019-12-01 05:51:44 +01:00
end
def create(conn, %{"duplicate_report" => duplicate_report_params}) do
2019-12-07 01:11:25 +01:00
attributes = conn.assigns.attributes
2019-12-01 05:51:44 +01:00
source = Repo.get!(Image, duplicate_report_params["image_id"])
target = Repo.get!(Image, duplicate_report_params["duplicate_of_image_id"])
2020-01-11 05:20:19 +01:00
case DuplicateReports.create_duplicate_report(
source,
target,
attributes,
duplicate_report_params
) do
2019-12-01 05:51:44 +01:00
{:ok, duplicate_report} ->
conn
|> put_flash(:info, "Duplicate report created successfully.")
|> redirect(to: Routes.image_path(conn, :show, duplicate_report.image_id))
{:error, _changeset} ->
conn
|> put_flash(:error, "Failed to submit duplicate report")
|> redirect(external: conn.assigns.referrer)
end
end
def show(conn, _params) do
dr = conn.assigns.duplicate_report
2020-01-11 05:20:19 +01:00
render(conn, "show.html",
title: "Showing Duplicate Report",
duplicate_report: dr,
layout_class: "layout--wide"
)
2019-12-01 05:51:44 +01:00
end
defp wrap(list) when is_list(list), do: list
defp wrap(not_a_list), do: [not_a_list]
defp presence(""), do: nil
defp presence(x), do: x
2019-12-01 05:51:44 +01:00
end