2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Actors.Follower do
|
2018-05-18 09:56:21 +02:00
|
|
|
@moduledoc """
|
2019-09-09 00:52:49 +02:00
|
|
|
Represents the following of an actor to another actor.
|
2018-05-18 09:56:21 +02:00
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
use Ecto.Schema
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
import Ecto.Changeset
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.Endpoint
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
approved: boolean,
|
|
|
|
url: String.t(),
|
|
|
|
target_actor: Actor.t(),
|
|
|
|
actor: Actor.t()
|
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:url, :approved, :target_actor_id, :actor_id]
|
|
|
|
@attrs @required_attrs
|
2019-07-30 16:40:59 +02:00
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@timestamps_opts [type: :utc_datetime]
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
2018-05-18 09:56:21 +02:00
|
|
|
schema "followers" do
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:approved, :boolean, default: false)
|
2019-07-30 16:40:59 +02:00
|
|
|
field(:url, :string)
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
timestamps()
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
belongs_to(:target_actor, Actor)
|
|
|
|
belongs_to(:actor, Actor)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec changeset(t, map) :: Ecto.Changeset.t()
|
2019-09-09 00:52:49 +02:00
|
|
|
def changeset(follower, attrs) do
|
|
|
|
follower
|
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
|> ensure_url()
|
|
|
|
|> validate_required(@required_attrs)
|
|
|
|
|> unique_constraint(:target_actor_id,
|
|
|
|
name: :followers_actor_target_actor_unique_index
|
|
|
|
)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
2019-07-30 16:40:59 +02:00
|
|
|
|
|
|
|
# If there's a blank URL that's because we're doing the first insert
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec ensure_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
|
|
|
defp ensure_url(%Ecto.Changeset{data: %__MODULE__{url: nil}} = changeset) do
|
2019-07-30 16:40:59 +02:00
|
|
|
case fetch_change(changeset, :url) do
|
2019-09-11 03:16:37 +02:00
|
|
|
{:ok, _url} ->
|
|
|
|
changeset
|
|
|
|
|
|
|
|
:error ->
|
|
|
|
generate_url(changeset)
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Most time just go with the given URL
|
2019-09-09 00:52:49 +02:00
|
|
|
defp ensure_url(%Ecto.Changeset{} = changeset), do: changeset
|
2019-07-30 16:40:59 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@spec generate_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
|
|
|
defp generate_url(%Ecto.Changeset{} = changeset) do
|
2019-07-30 16:40:59 +02:00
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
|
|
|
changeset
|
2019-09-09 00:52:49 +02:00
|
|
|
|> put_change(:id, uuid)
|
2020-01-28 19:18:33 +01:00
|
|
|
|> put_change(:url, "#{Endpoint.url()}/follow/#{uuid}")
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|