2019-09-07 02:38:13 +02:00
|
|
|
defmodule Mobilizon.Reports do
|
|
|
|
@moduledoc """
|
|
|
|
The Reports context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query
|
|
|
|
import EctoEnum
|
|
|
|
|
2019-09-08 01:49:56 +02:00
|
|
|
import Mobilizon.Storage.Ecto
|
2019-09-07 02:38:13 +02:00
|
|
|
|
2022-01-10 10:17:50 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-07 02:38:13 +02:00
|
|
|
alias Mobilizon.Reports.{Note, Report}
|
2019-09-08 01:49:56 +02:00
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
2019-09-07 02:38:13 +02:00
|
|
|
|
|
|
|
defenum(ReportStatus, :report_status, [:open, :closed, :resolved])
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single report.
|
|
|
|
"""
|
|
|
|
@spec get_report(integer | String.t()) :: Report.t() | nil
|
|
|
|
def get_report(id) do
|
|
|
|
Report
|
|
|
|
|> Repo.get(id)
|
2023-06-05 18:32:29 +02:00
|
|
|
|> Repo.preload([:reported, :reporter, :manager, :events, :comments, :notes])
|
2019-09-07 02:38:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a report.
|
|
|
|
"""
|
|
|
|
@spec create_report(map) :: {:ok, Report.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def create_report(attrs \\ %{}) do
|
|
|
|
with {:ok, %Report{} = report} <-
|
|
|
|
%Report{}
|
|
|
|
|> Report.changeset(attrs)
|
|
|
|
|> Repo.insert() do
|
2023-06-05 18:32:29 +02:00
|
|
|
{:ok, Repo.preload(report, [:events, :reported, :reporter, :comments])}
|
2019-09-07 02:38:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Updates a report.
|
|
|
|
"""
|
|
|
|
@spec update_report(Report.t(), map) :: {:ok, Report.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def update_report(%Report{} = report, attrs) do
|
|
|
|
report
|
|
|
|
|> Report.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Returns the list of reports.
|
2019-09-07 02:38:13 +02:00
|
|
|
"""
|
2022-01-10 10:17:50 +01:00
|
|
|
@spec list_reports(Keyword.t()) :: Page.t(Report.t())
|
|
|
|
def list_reports(options) do
|
|
|
|
page = Keyword.get(options, :page)
|
|
|
|
limit = Keyword.get(options, :limit)
|
|
|
|
sort = Keyword.get(options, :sort, :updated_at)
|
|
|
|
direction = Keyword.get(options, :direction, :asc)
|
|
|
|
status = Keyword.get(options, :status, :open)
|
|
|
|
domain = Keyword.get(options, :domain)
|
|
|
|
|
2019-09-21 23:59:07 +02:00
|
|
|
status
|
|
|
|
|> list_reports_query()
|
2022-01-10 10:17:50 +01:00
|
|
|
|> filter_domain_name(domain)
|
2019-09-09 00:52:49 +02:00
|
|
|
|> sort(sort, direction)
|
2021-05-12 18:16:30 +02:00
|
|
|
|> Page.build_page(page, limit)
|
2019-09-07 02:38:13 +02:00
|
|
|
end
|
|
|
|
|
2019-09-21 23:59:07 +02:00
|
|
|
@doc """
|
|
|
|
Counts opened reports.
|
|
|
|
"""
|
|
|
|
@spec count_opened_reports :: integer
|
|
|
|
def count_opened_reports do
|
|
|
|
Repo.aggregate(count_reports_query(), :count, :id)
|
|
|
|
end
|
|
|
|
|
2019-09-07 02:38:13 +02:00
|
|
|
@doc """
|
|
|
|
Gets a single note.
|
|
|
|
"""
|
|
|
|
@spec get_note(integer | String.t()) :: Note.t() | nil
|
|
|
|
def get_note(id), do: Repo.get(Note, id)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a note.
|
|
|
|
"""
|
|
|
|
@spec create_note(map) :: {:ok, Note.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def create_note(attrs \\ %{}) do
|
|
|
|
with {:ok, %Note{} = note} <-
|
|
|
|
%Note{}
|
|
|
|
|> Note.changeset(attrs)
|
|
|
|
|> Repo.insert() do
|
|
|
|
{:ok, Repo.preload(note, [:report, :moderator])}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a note.
|
|
|
|
"""
|
|
|
|
@spec delete_note(Note.t()) :: {:ok, Note.t()} | {:error, Ecto.Changeset.t()}
|
2019-09-09 00:52:49 +02:00
|
|
|
def delete_note(%Note{} = note), do: Repo.delete(note)
|
|
|
|
|
2022-04-18 14:38:57 +02:00
|
|
|
@spec list_reports_query(atom()) :: Ecto.Query.t()
|
2019-09-21 23:59:07 +02:00
|
|
|
defp list_reports_query(status) do
|
2022-01-10 10:17:50 +01:00
|
|
|
Report
|
2023-06-05 18:32:29 +02:00
|
|
|
|> preload([:reported, :reporter, :manager, :events, :comments, :notes])
|
2022-01-10 10:17:50 +01:00
|
|
|
|> where([r], r.status == ^status)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec filter_domain_name(Ecto.Queryable.t(), String.t() | nil) :: Ecto.Queryable.t()
|
|
|
|
defp filter_domain_name(query, nil), do: query
|
|
|
|
defp filter_domain_name(query, ""), do: query
|
|
|
|
|
|
|
|
defp filter_domain_name(query, domain) do
|
|
|
|
query
|
|
|
|
|> join(:inner, [r], a in Actor, on: a.id == r.reported_id)
|
|
|
|
|> where([_r, a], like(a.domain, ^"%#{domain}%"))
|
2019-09-07 02:38:13 +02:00
|
|
|
end
|
|
|
|
|
2019-09-21 23:59:07 +02:00
|
|
|
@spec count_reports_query :: Ecto.Query.t()
|
|
|
|
defp count_reports_query do
|
|
|
|
from(r in Report, where: r.status == ^:open)
|
|
|
|
end
|
2019-09-07 02:38:13 +02:00
|
|
|
end
|