2019-07-23 13:49:22 +02:00
|
|
|
defmodule Mobilizon.Reports.Report do
|
|
|
|
@moduledoc """
|
2019-09-07 02:38:13 +02:00
|
|
|
Represents a report entity.
|
2019-07-23 13:49:22 +02:00
|
|
|
"""
|
2019-09-07 02:38:13 +02:00
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
use Ecto.Schema
|
2019-09-07 02:38:13 +02:00
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
import Ecto.Changeset
|
2019-09-07 02:38:13 +02:00
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-07 02:38:13 +02:00
|
|
|
alias Mobilizon.Events.{Comment, Event}
|
|
|
|
alias Mobilizon.Reports.{Note, ReportStatus}
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{
|
|
|
|
content: String.t(),
|
|
|
|
status: ReportStatus.t(),
|
|
|
|
uri: String.t(),
|
|
|
|
reported: Actor.t(),
|
|
|
|
reporter: Actor.t(),
|
|
|
|
manager: Actor.t(),
|
|
|
|
event: Event.t(),
|
|
|
|
comments: [Comment.t()],
|
|
|
|
notes: [Note.t()]
|
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:content, :uri, :reported_id, :reporter_id]
|
|
|
|
@optional_attrs [:status, :manager_id, :event_id]
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
2019-07-23 13:49:22 +02:00
|
|
|
|
|
|
|
@derive {Jason.Encoder, only: [:status, :uri]}
|
|
|
|
schema "reports" do
|
|
|
|
field(:content, :string)
|
2019-09-07 02:38:13 +02:00
|
|
|
field(:status, ReportStatus, default: :open)
|
2019-07-23 13:49:22 +02:00
|
|
|
field(:uri, :string)
|
|
|
|
|
|
|
|
# The reported actor
|
|
|
|
belongs_to(:reported, Actor)
|
|
|
|
# The actor who reported
|
|
|
|
belongs_to(:reporter, Actor)
|
|
|
|
# The actor who last acted on this report
|
|
|
|
belongs_to(:manager, Actor)
|
|
|
|
# The eventual Event inside the report
|
|
|
|
belongs_to(:event, Event)
|
|
|
|
# The eventual Comments inside the report
|
|
|
|
many_to_many(:comments, Comment, join_through: "reports_comments", on_replace: :delete)
|
|
|
|
# The notes associated to the report
|
|
|
|
has_many(:notes, Note, foreign_key: :report_id)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def changeset(%__MODULE__{} = report, attrs) do
|
2019-07-23 13:49:22 +02:00
|
|
|
report
|
2019-09-07 02:38:13 +02:00
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
|> validate_required(@required_attrs)
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
2019-09-07 02:38:13 +02:00
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec creation_changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def creation_changeset(%__MODULE__{} = report, attrs) do
|
2019-07-23 13:49:22 +02:00
|
|
|
report
|
|
|
|
|> changeset(attrs)
|
|
|
|
|> put_assoc(:comments, attrs["comments"])
|
|
|
|
end
|
|
|
|
end
|