forked from potsda.mn/mobilizon
Add code to participants
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
4de78f58e0
commit
555ae867ea
|
@ -17,6 +17,7 @@ defmodule Mobilizon.Events.Participant do
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
id: String.t(),
|
id: String.t(),
|
||||||
role: ParticipantRole.t(),
|
role: ParticipantRole.t(),
|
||||||
|
code: String.t(),
|
||||||
url: String.t(),
|
url: String.t(),
|
||||||
event: Event.t(),
|
event: Event.t(),
|
||||||
actor: Actor.t(),
|
actor: Actor.t(),
|
||||||
|
@ -24,7 +25,8 @@ defmodule Mobilizon.Events.Participant do
|
||||||
}
|
}
|
||||||
|
|
||||||
@required_attrs [:url, :role, :event_id, :actor_id]
|
@required_attrs [:url, :role, :event_id, :actor_id]
|
||||||
@attrs @required_attrs
|
@optional_attrs [:code]
|
||||||
|
@attrs @required_attrs ++ @optional_attrs
|
||||||
|
|
||||||
@timestamps_opts [type: :utc_datetime]
|
@timestamps_opts [type: :utc_datetime]
|
||||||
|
|
||||||
|
@ -32,6 +34,7 @@ defmodule Mobilizon.Events.Participant do
|
||||||
schema "participants" do
|
schema "participants" do
|
||||||
field(:role, ParticipantRole, default: :participant)
|
field(:role, ParticipantRole, default: :participant)
|
||||||
field(:url, :string)
|
field(:url, :string)
|
||||||
|
field(:code, :string)
|
||||||
|
|
||||||
embeds_one(:metadata, Metadata, on_replace: :delete)
|
embeds_one(:metadata, Metadata, on_replace: :delete)
|
||||||
|
|
||||||
|
@ -64,6 +67,7 @@ defmodule Mobilizon.Events.Participant do
|
||||||
|> cast(attrs, @attrs)
|
|> cast(attrs, @attrs)
|
||||||
|> cast_embed(:metadata)
|
|> cast_embed(:metadata)
|
||||||
|> ensure_url()
|
|> ensure_url()
|
||||||
|
|> add_code()
|
||||||
|> validate_required(@required_attrs)
|
|> validate_required(@required_attrs)
|
||||||
|> unique_constraint(:actor_id, name: :participants_event_id_actor_id_index)
|
|> unique_constraint(:actor_id, name: :participants_event_id_actor_id_index)
|
||||||
end
|
end
|
||||||
|
@ -93,4 +97,25 @@ defmodule Mobilizon.Events.Participant do
|
||||||
|
|
||||||
@spec generate_url(String.t()) :: String.t()
|
@spec generate_url(String.t()) :: String.t()
|
||||||
defp generate_url(uuid), do: "#{Endpoint.url()}/join/event/#{uuid}"
|
defp generate_url(uuid), do: "#{Endpoint.url()}/join/event/#{uuid}"
|
||||||
|
|
||||||
|
@spec add_code(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
||||||
|
defp add_code(%Ecto.Changeset{} = changeset) do
|
||||||
|
case fetch_field(changeset, :code) do
|
||||||
|
{:data, nil} -> put_change(changeset, :code, generate_code())
|
||||||
|
{_, _code} -> changeset
|
||||||
|
:error -> put_change(changeset, :code, generate_code())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# No lookalike symbols
|
||||||
|
@symbols '6789BCDFGHJKLMNPQRTW'
|
||||||
|
@symbol_count Enum.count(@symbols)
|
||||||
|
@code_length 6
|
||||||
|
|
||||||
|
@spec generate_code :: String.t()
|
||||||
|
defp generate_code do
|
||||||
|
for _ <- 1..@code_length,
|
||||||
|
into: "",
|
||||||
|
do: <<Enum.at(@symbols, :rand.uniform(@symbol_count))>>
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
defmodule Mobilizon.Storage.Repo.Migrations.AddCodeToParticipants do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
alter table(:participants) do
|
||||||
|
add(:code, :string)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue