Fix creating an mz activity when updating an event with some subfields

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-11-15 11:03:06 +01:00
parent d31bdc99f8
commit fea97f3713
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
2 changed files with 21 additions and 30 deletions

View file

@ -64,7 +64,8 @@ defmodule Mobilizon.Events.Event do
tags: [Tag.t()],
participants: [Actor.t()],
contacts: [Actor.t()],
language: String.t()
language: String.t(),
metadata: [EventMetadata.t()]
}
@update_required_attrs [:title, :begins_on, :organizer_actor_id]

View file

@ -19,7 +19,6 @@ defmodule Mobilizon.Events do
alias Mobilizon.Events.{
Event,
EventOptions,
EventParticipantStats,
FeedToken,
Participant,
@ -329,37 +328,28 @@ defmodule Mobilizon.Events do
defp build_changes(changes) do
changes
|> Map.take(Event.__schema__(:fields))
|> maybe_add_address(changes)
|> maybe_add_options(changes)
|> maybe_add_field(:physical_address)
|> maybe_add_field(:options)
|> maybe_add_field(:metadata)
end
@spec maybe_add_address(map(), map()) :: map()
defp maybe_add_address(changes, %{physical_address: %Ecto.Changeset{} = changeset}),
do:
@spec maybe_add_field(map(), atom()) :: map()
defp maybe_add_field(changes, field) do
case Map.get(changes, field) do
%Ecto.Changeset{} = changeset ->
Map.put(
changes,
:physical_address,
field,
changeset
|> Ecto.Changeset.apply_changes()
|> Map.from_struct()
|> Map.take(Address.__schema__(:fields))
)
defp maybe_add_address(changes, _), do: Map.drop(changes, [:physical_address])
@spec maybe_add_options(map(), map()) :: map()
defp maybe_add_options(changes, %{options: %Ecto.Changeset{} = changeset}),
do:
Map.put(
changes,
:options,
changeset
|> Ecto.Changeset.apply_changes()
|> Map.from_struct()
|> Map.take(EventOptions.__schema__(:fields))
)
defp maybe_add_options(changes, _), do: Map.drop(changes, [:options])
_ ->
Map.drop(changes, [field])
end
end
@doc """
Deletes an event.