Validate username on backend side and refactor actor changeset

Closes #316

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2019-11-18 17:54:42 +01:00
parent 3cc2e125ee
commit 4dc4524e71
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773

View File

@ -203,14 +203,9 @@ defmodule Mobilizon.Actors.Actor do
actor actor
|> cast(attrs, @attrs) |> cast(attrs, @attrs)
|> build_urls() |> build_urls()
|> cast_embed(:avatar) |> common_changeset()
|> cast_embed(:banner)
|> unique_username_validator() |> unique_username_validator()
|> validate_required(@required_attrs) |> validate_required(@required_attrs)
|> unique_constraint(:preferred_username,
name: :actors_preferred_username_domain_type_index
)
|> unique_constraint(:url, name: :actors_url_index)
end end
@doc false @doc false
@ -218,13 +213,8 @@ defmodule Mobilizon.Actors.Actor do
def update_changeset(%__MODULE__{} = actor, attrs) do def update_changeset(%__MODULE__{} = actor, attrs) do
actor actor
|> cast(attrs, @update_attrs) |> cast(attrs, @update_attrs)
|> cast_embed(:avatar) |> common_changeset()
|> cast_embed(:banner)
|> validate_required(@update_required_attrs) |> validate_required(@update_required_attrs)
|> unique_constraint(:preferred_username,
name: :actors_preferred_username_domain_type_index
)
|> unique_constraint(:url, name: :actors_url_index)
end end
@doc """ @doc """
@ -235,13 +225,8 @@ defmodule Mobilizon.Actors.Actor do
actor actor
|> cast(attrs, @registration_attrs) |> cast(attrs, @registration_attrs)
|> build_urls() |> build_urls()
|> cast_embed(:avatar) |> common_changeset()
|> cast_embed(:banner)
|> unique_username_validator() |> unique_username_validator()
|> unique_constraint(:preferred_username,
name: :actors_preferred_username_domain_type_index
)
|> unique_constraint(:url, name: :actors_url_index)
|> validate_required(@registration_required_attrs) |> validate_required(@registration_required_attrs)
end end
@ -254,13 +239,8 @@ defmodule Mobilizon.Actors.Actor do
%__MODULE__{} %__MODULE__{}
|> cast(attrs, @remote_actor_creation_attrs) |> cast(attrs, @remote_actor_creation_attrs)
|> validate_required(@remote_actor_creation_required_attrs) |> validate_required(@remote_actor_creation_required_attrs)
|> cast_embed(:avatar) |> common_changeset()
|> cast_embed(:banner)
|> unique_username_validator() |> unique_username_validator()
|> unique_constraint(:preferred_username,
name: :actors_preferred_username_domain_type_index
)
|> unique_constraint(:url, name: :actors_url_index)
|> validate_length(:summary, max: 5000) |> validate_length(:summary, max: 5000)
|> validate_length(:preferred_username, max: 100) |> validate_length(:preferred_username, max: 100)
@ -269,6 +249,16 @@ defmodule Mobilizon.Actors.Actor do
changeset changeset
end end
@spec common_changeset(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp common_changeset(%Ecto.Changeset{} = changeset) do
changeset
|> cast_embed(:avatar)
|> cast_embed(:banner)
|> unique_constraint(:url, name: :actors_url_index)
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index)
|> validate_format(:preferred_username, ~r/[a-z0-9_]+/)
end
@doc """ @doc """
Changeset for relay creation. Changeset for relay creation.
""" """