2019-10-25 17:43:37 +02:00
|
|
|
defmodule Mobilizon.Mention do
|
|
|
|
@moduledoc """
|
|
|
|
The Mentions context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
alias Mobilizon.Actors.Actor
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions.Comment
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Events.Event
|
2019-10-25 17:43:37 +02:00
|
|
|
alias Mobilizon.Storage.Repo
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{
|
|
|
|
silent: boolean,
|
|
|
|
actor: Actor.t(),
|
|
|
|
event: Event.t(),
|
|
|
|
comment: Comment.t()
|
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:actor_id]
|
|
|
|
@optional_attrs [:silent, :event_id, :comment_id]
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
|
|
|
|
|
|
|
schema "mentions" do
|
|
|
|
field(:silent, :boolean, default: false)
|
|
|
|
belongs_to(:actor, Actor)
|
|
|
|
belongs_to(:event, Event)
|
|
|
|
belongs_to(:comment, Comment)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec changeset(t | Ecto.Schema.t(), map) :: Ecto.Changeset.t()
|
2019-10-25 17:43:37 +02:00
|
|
|
def changeset(event, attrs) do
|
|
|
|
event
|
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
# TODO: Enforce having either event_id or comment_id
|
|
|
|
|> validate_required(@required_attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a new mention
|
|
|
|
"""
|
|
|
|
@spec create_mention(map()) :: {:ok, t} | {:error, Ecto.Changeset.t()}
|
|
|
|
def create_mention(args) do
|
|
|
|
with {:ok, %__MODULE__{} = mention} <-
|
|
|
|
%__MODULE__{}
|
|
|
|
|> changeset(args)
|
|
|
|
|> Repo.insert() do
|
|
|
|
{:ok, Repo.preload(mention, [:actor, :event, :comment])}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|