2019-12-04 05:14:56 +01:00
|
|
|
defmodule PhilomenaWeb.ReportController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Polymorphic
|
|
|
|
alias Philomena.Reports.Report
|
|
|
|
alias Philomena.Reports
|
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
user = conn.assigns.current_user
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-04 05:14:56 +01:00
|
|
|
reports =
|
|
|
|
Report
|
|
|
|
|> where(user_id: ^user.id)
|
2020-02-11 04:26:45 +01:00
|
|
|
|> order_by(desc: :created_at)
|
2019-12-04 05:14:56 +01:00
|
|
|
|> Repo.paginate(conn.assigns.scrivener)
|
|
|
|
|
|
|
|
polymorphic =
|
|
|
|
reports
|
|
|
|
|> Polymorphic.load_polymorphic(reportable: [reportable_id: :reportable_type])
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
reports = %{reports | entries: polymorphic}
|
2019-12-04 05:14:56 +01:00
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "index.html", title: "My Reports", reports: reports)
|
2019-12-04 05:14:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Make sure that you load the resource in your controller:
|
|
|
|
#
|
|
|
|
# plug PhilomenaWeb.FilterBannedUsersPlug
|
|
|
|
# plug PhilomenaWeb.UserAttributionPlug
|
2020-09-12 19:43:16 +02:00
|
|
|
# plug PhilomenaWeb.CaptchaPlug
|
|
|
|
# plug PhilomenaWeb.CheckCaptchaPlug when action in [:create]
|
2019-12-04 05:14:56 +01:00
|
|
|
# plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
|
|
|
|
|
|
|
|
def create(conn, action, reportable, reportable_type, %{"report" => report_params}) do
|
|
|
|
attribution = conn.assigns.attributes
|
|
|
|
|
|
|
|
case too_many_reports?(conn) do
|
|
|
|
true ->
|
|
|
|
conn
|
2020-01-11 05:20:19 +01:00
|
|
|
|> put_flash(
|
2023-11-23 17:07:49 +01:00
|
|
|
:warning,
|
2020-09-10 04:33:57 +02:00
|
|
|
"You may not have more than #{max_reports()} open reports at a time. Did you read the reporting tips?"
|
2020-01-11 05:20:19 +01:00
|
|
|
)
|
2019-12-04 05:14:56 +01:00
|
|
|
|> redirect(to: "/")
|
|
|
|
|
|
|
|
_falsy ->
|
|
|
|
case Reports.create_report(reportable.id, reportable_type, attribution, report_params) do
|
2020-12-16 15:27:44 +01:00
|
|
|
{:ok, _report} ->
|
2019-12-04 05:14:56 +01:00
|
|
|
conn
|
2020-01-11 05:20:19 +01:00
|
|
|
|> put_flash(
|
2023-11-23 17:07:49 +01:00
|
|
|
:alert,
|
2020-01-11 05:20:19 +01:00
|
|
|
"Your report has been received and will be checked by staff shortly."
|
|
|
|
)
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: redirect_path(conn.assigns.current_user))
|
2019-12-04 05:14:56 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
# Note that we are depending on the controller that called
|
|
|
|
# us to have set up the view already (Phoenix does this)
|
|
|
|
conn
|
|
|
|
|> render("new.html", reportable: reportable, changeset: changeset, action: action)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp too_many_reports?(conn) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
2020-09-10 04:33:57 +02:00
|
|
|
case user do
|
|
|
|
%{role: role} when role != "user" ->
|
|
|
|
false
|
|
|
|
|
|
|
|
_user ->
|
|
|
|
too_many_reports_user?(user) or too_many_reports_ip?(conn)
|
|
|
|
end
|
2019-12-04 05:14:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp too_many_reports_user?(nil), do: false
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-04 05:14:56 +01:00
|
|
|
defp too_many_reports_user?(user) do
|
|
|
|
reports_open =
|
|
|
|
Report
|
|
|
|
|> where(user_id: ^user.id)
|
|
|
|
|> where([r], r.state in ["open", "in_progress"])
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
|
2020-09-10 04:33:57 +02:00
|
|
|
reports_open >= max_reports()
|
2019-12-04 05:14:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp too_many_reports_ip?(conn) do
|
|
|
|
attribution = conn.assigns.attributes
|
|
|
|
|
|
|
|
reports_open =
|
|
|
|
Report
|
|
|
|
|> where(ip: ^attribution[:ip])
|
|
|
|
|> where([r], r.state in ["open", "in_progress"])
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
|
2020-09-10 04:33:57 +02:00
|
|
|
reports_open >= max_reports()
|
2019-12-04 05:14:56 +01:00
|
|
|
end
|
2019-12-07 17:26:45 +01:00
|
|
|
|
2024-06-06 22:28:35 +02:00
|
|
|
defp redirect_path(nil), do: "/"
|
|
|
|
defp redirect_path(_user), do: ~p"/reports"
|
2020-09-10 04:33:57 +02:00
|
|
|
|
|
|
|
defp max_reports do
|
2023-04-20 20:37:58 +02:00
|
|
|
5
|
2020-09-10 04:33:57 +02:00
|
|
|
end
|
2019-12-16 20:24:38 +01:00
|
|
|
end
|