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()], tags: [Tag.t()],
participants: [Actor.t()], participants: [Actor.t()],
contacts: [Actor.t()], contacts: [Actor.t()],
language: String.t() language: String.t(),
metadata: [EventMetadata.t()]
} }
@update_required_attrs [:title, :begins_on, :organizer_actor_id] @update_required_attrs [:title, :begins_on, :organizer_actor_id]

View File

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