philomena/lib/philomena_web/controllers/duplicate_report_controller.ex

83 lines
2.3 KiB
Elixir
Raw Permalink 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.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-12-06 16:08:13 +01:00
|> preload([
:user,
:modifier,
2021-11-13 18:53:35 +01:00
image: [:user, :sources, tags: :aliases],
duplicate_of_image: [:user, :sources, tags: :aliases]
2020-12-06 16:08:13 +01:00
])
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
2020-01-11 05:20:19 +01:00
render(conn, "index.html",
title: "Duplicate Reports",
duplicate_reports: duplicate_reports,
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: ~p"/images/#{duplicate_report.image_id}")
2019-12-01 05:51:44 +01:00
{:error, _changeset} ->
conn
|> put_flash(:error, "Failed to submit duplicate report")
|> redirect(to: ~p"/images/#{source}")
2019-12-01 05:51:44 +01:00
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