From fea97f37138b05dbf238ebc0def96135cd03d1d0 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 15 Nov 2021 11:03:06 +0100 Subject: [PATCH] Fix creating an mz activity when updating an event with some subfields Signed-off-by: Thomas Citharel --- lib/mobilizon/events/event.ex | 3 ++- lib/mobilizon/events/events.ex | 48 ++++++++++++++-------------------- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/lib/mobilizon/events/event.ex b/lib/mobilizon/events/event.ex index 3b09c3c3..61119749 100644 --- a/lib/mobilizon/events/event.ex +++ b/lib/mobilizon/events/event.ex @@ -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] diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index f349ff42..cae534bd 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -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: - Map.put( - changes, - :physical_address, - changeset - |> Ecto.Changeset.apply_changes() - |> Map.from_struct() - |> Map.take(Address.__schema__(:fields)) - ) + @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, + 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.