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__{
|
2021-09-10 11:36:05 +02:00
|
|
|
id: String.t(),
|
2019-09-09 00:52:49 +02:00
|
|
|
approved: boolean,
|
|
|
|
url: String.t(),
|
|
|
|
target_actor: Actor.t(),
|
2021-09-10 11:36:05 +02:00
|
|
|
actor: Actor.t(),
|
|
|
|
inserted_at: DateTime.t(),
|
|
|
|
updated_at: DateTime.t()
|
2019-09-09 00:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec changeset(follower :: t | Ecto.Schema.t(), attrs :: 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
|