2019-12-08 18:45:37 +01:00
|
|
|
defmodule PhilomenaWeb.Admin.ReportController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2024-05-25 20:03:45 +02:00
|
|
|
alias PhilomenaQuery.Search
|
2021-09-29 22:24:38 +02:00
|
|
|
alias PhilomenaWeb.MarkdownRenderer
|
2019-12-08 18:45:37 +01:00
|
|
|
alias Philomena.Reports.Report
|
|
|
|
alias Philomena.Reports.Query
|
|
|
|
alias Philomena.Polymorphic
|
2024-07-16 03:19:32 +02:00
|
|
|
alias Philomena.ModNotes
|
2019-12-08 18:45:37 +01:00
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
2019-12-08 18:50:44 +01:00
|
|
|
plug :verify_authorized
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Report,
|
|
|
|
only: [:show],
|
|
|
|
preload: [:admin, user: [:linked_tags, awards: :badge]]
|
|
|
|
|
2019-12-17 18:29:18 +01:00
|
|
|
plug :set_mod_notes when action in [:show]
|
2019-12-08 18:45:37 +01:00
|
|
|
|
|
|
|
def index(conn, %{"rq" => query_string}) do
|
|
|
|
{:ok, query} = Query.compile(query_string)
|
|
|
|
|
|
|
|
reports = load_reports(conn, query)
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "index.html",
|
|
|
|
title: "Admin - Reports",
|
|
|
|
layout_class: "layout--wide",
|
|
|
|
reports: reports,
|
2022-03-26 13:38:25 +01:00
|
|
|
my_reports: [],
|
|
|
|
system_reports: []
|
2020-01-11 05:20:19 +01:00
|
|
|
)
|
2019-12-08 18:45:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
query = %{
|
|
|
|
bool: %{
|
|
|
|
should: [
|
|
|
|
%{term: %{open: false}},
|
|
|
|
%{
|
|
|
|
bool: %{
|
|
|
|
must: %{term: %{open: true}},
|
2022-03-24 17:31:57 +01:00
|
|
|
must_not: [
|
|
|
|
%{term: %{admin_id: user.id}},
|
|
|
|
%{term: %{system: true}}
|
|
|
|
]
|
2019-12-08 18:45:37 +01:00
|
|
|
}
|
2020-01-11 05:20:19 +01:00
|
|
|
}
|
|
|
|
]
|
2019-12-08 18:45:37 +01:00
|
|
|
}
|
2020-01-11 05:20:19 +01:00
|
|
|
}
|
2019-12-08 18:45:37 +01:00
|
|
|
|
|
|
|
reports = load_reports(conn, query)
|
|
|
|
|
|
|
|
my_reports =
|
|
|
|
Report
|
|
|
|
|> where(open: true, admin_id: ^user.id)
|
|
|
|
|> preload([:admin, user: :linked_tags])
|
|
|
|
|> order_by(desc: :created_at)
|
|
|
|
|> Repo.all()
|
|
|
|
|> Polymorphic.load_polymorphic(reportable: [reportable_id: :reportable_type])
|
|
|
|
|
2022-03-24 17:31:57 +01:00
|
|
|
system_reports =
|
|
|
|
Report
|
|
|
|
|> where(open: true, system: true)
|
|
|
|
|> preload([:admin, user: :linked_tags])
|
|
|
|
|> order_by(desc: :created_at)
|
|
|
|
|> Repo.all()
|
|
|
|
|> Polymorphic.load_polymorphic(reportable: [reportable_id: :reportable_type])
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "index.html",
|
|
|
|
title: "Admin - Reports",
|
|
|
|
layout_class: "layout--wide",
|
|
|
|
reports: reports,
|
2022-03-24 17:31:57 +01:00
|
|
|
my_reports: my_reports,
|
|
|
|
system_reports: system_reports
|
2020-01-11 05:20:19 +01:00
|
|
|
)
|
2019-12-08 18:45:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, _params) do
|
2020-01-11 05:20:19 +01:00
|
|
|
[report] =
|
|
|
|
Polymorphic.load_polymorphic([conn.assigns.report],
|
|
|
|
reportable: [reportable_id: :reportable_type]
|
|
|
|
)
|
|
|
|
|
2021-09-29 22:24:38 +02:00
|
|
|
body = MarkdownRenderer.render_one(%{body: report.reason}, conn)
|
2019-12-08 18:45:37 +01:00
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "show.html", title: "Showing Report", report: report, body: body)
|
2019-12-08 18:45:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp load_reports(conn, query) do
|
|
|
|
reports =
|
2020-08-23 21:47:42 +02:00
|
|
|
Report
|
2024-05-25 20:03:45 +02:00
|
|
|
|> Search.search_definition(
|
2019-12-08 18:45:37 +01:00
|
|
|
%{
|
|
|
|
query: query,
|
|
|
|
sort: sorts()
|
|
|
|
},
|
2020-08-23 21:47:42 +02:00
|
|
|
conn.assigns.pagination
|
2019-12-08 18:45:37 +01:00
|
|
|
)
|
2024-05-25 20:03:45 +02:00
|
|
|
|> Search.search_records(preload(Report, [:admin, user: :linked_tags]))
|
2019-12-08 18:45:37 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
entries = Polymorphic.load_polymorphic(reports, reportable: [reportable_id: :reportable_type])
|
2019-12-08 18:45:37 +01:00
|
|
|
|
|
|
|
%{reports | entries: entries}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp sorts do
|
|
|
|
[
|
|
|
|
%{open: :desc},
|
|
|
|
%{state: :desc},
|
|
|
|
%{created_at: :desc}
|
|
|
|
]
|
|
|
|
end
|
2019-12-08 18:50:44 +01:00
|
|
|
|
|
|
|
defp verify_authorized(conn, _opts) do
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :index, Report) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> conn
|
2019-12-08 18:50:44 +01:00
|
|
|
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
end
|
|
|
|
end
|
2019-12-17 18:29:18 +01:00
|
|
|
|
|
|
|
defp set_mod_notes(conn, _opts) do
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :index, ModNote) do
|
|
|
|
true ->
|
|
|
|
report = conn.assigns.report
|
|
|
|
|
2024-07-16 03:19:32 +02:00
|
|
|
renderer = &MarkdownRenderer.render_collection(&1, conn)
|
|
|
|
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("Report", report.id, renderer)
|
2019-12-17 18:29:18 +01:00
|
|
|
assign(conn, :mod_notes, mod_notes)
|
|
|
|
|
|
|
|
_false ->
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
2019-12-16 20:24:38 +01:00
|
|
|
end
|