From 4de78f58e01191ad758385e25c393d06eb1c0619 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 13 Oct 2021 12:55:49 +0200 Subject: [PATCH] Allow anonymous participants to have timezone metadata Signed-off-by: Thomas Citharel --- .../ParticipationWithoutAccount.vue | 1 + js/src/graphql/event.ts | 2 ++ lib/graphql/schema/events/participant.ex | 1 + lib/mobilizon/events/participant_metadata.ex | 3 +- lib/web/email/event.ex | 29 ++++++++++++++----- lib/web/email/participation.ex | 9 ++++-- test/graphql/resolvers/event_test.exs | 6 ++-- 7 files changed, 38 insertions(+), 13 deletions(-) diff --git a/js/src/components/Participation/ParticipationWithoutAccount.vue b/js/src/components/Participation/ParticipationWithoutAccount.vue index 3337c51e0..ae26b2e65 100644 --- a/js/src/components/Participation/ParticipationWithoutAccount.vue +++ b/js/src/components/Participation/ParticipationWithoutAccount.vue @@ -200,6 +200,7 @@ export default class ParticipationWithoutAccount extends Vue { email: this.anonymousParticipation.email, message: this.anonymousParticipation.message, locale: this.$i18n.locale, + timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, }, update: ( store: ApolloCache<{ joinEvent: IParticipant }>, diff --git a/js/src/graphql/event.ts b/js/src/graphql/event.ts index 4e9a8baba..8be02ff0c 100644 --- a/js/src/graphql/event.ts +++ b/js/src/graphql/event.ts @@ -380,6 +380,7 @@ export const JOIN_EVENT = gql` $email: String $message: String $locale: String + $timezone: String ) { joinEvent( eventId: $eventId @@ -387,6 +388,7 @@ export const JOIN_EVENT = gql` email: $email message: $message locale: $locale + timezone: $timezone ) { ...ParticipantQuery } diff --git a/lib/graphql/schema/events/participant.ex b/lib/graphql/schema/events/participant.ex index 34093014e..d34d1c64a 100644 --- a/lib/graphql/schema/events/participant.ex +++ b/lib/graphql/schema/events/participant.ex @@ -91,6 +91,7 @@ defmodule Mobilizon.GraphQL.Schema.Events.ParticipantType do arg(:email, :string, description: "The anonymous participant's email") arg(:message, :string, description: "The anonymous participant's message") arg(:locale, :string, description: "The anonymous participant's locale") + arg(:timezone, :string, description: "The anonymous participant's timezone") resolve(&Participant.actor_join_event/3) end diff --git a/lib/mobilizon/events/participant_metadata.ex b/lib/mobilizon/events/participant_metadata.ex index 279575f9b..8b002d095 100644 --- a/lib/mobilizon/events/participant_metadata.ex +++ b/lib/mobilizon/events/participant_metadata.ex @@ -15,7 +15,7 @@ defmodule Mobilizon.Events.Participant.Metadata do locale: String.t() } - @attrs [:email, :confirmation_token, :cancellation_token, :message, :locale] + @attrs [:email, :confirmation_token, :cancellation_token, :message, :locale, :timezone] @derive Jason.Encoder embedded_schema do @@ -24,6 +24,7 @@ defmodule Mobilizon.Events.Participant.Metadata do field(:cancellation_token, :string) field(:message, :string) field(:locale, :string) + field(:timezone, :string) end @doc false diff --git a/lib/web/email/event.ex b/lib/web/email/event.ex index 120f1d577..dd0a0b1c5 100644 --- a/lib/web/email/event.ex +++ b/lib/web/email/event.ex @@ -14,15 +14,24 @@ defmodule Mobilizon.Web.Email.Event do alias Mobilizon.Events.{Event, Participant} alias Mobilizon.Storage.Repo alias Mobilizon.Users.{Setting, User} - alias Mobilizon.Web.Email + alias Mobilizon.Web.JsonLD.ObjectView @important_changes [:title, :begins_on, :ends_on, :status, :physical_address] - @spec event_updated(String.t(), Actor.t(), Event.t(), Event.t(), MapSet.t(), String.t()) :: + @spec event_updated( + Participant.t(), + String.t(), + Actor.t(), + Event.t(), + Event.t(), + MapSet.t(), + String.t() + ) :: Bamboo.Email.t() def event_updated( email, + %Participant{} = participant, %Actor{} = actor, %Event{} = old_event, %Event{} = event, @@ -83,13 +92,14 @@ defmodule Mobilizon.Web.Email.Event do MapSet.t() ) :: Bamboo.Email.t() defp send_notification_for_event_update_to_participant( - {%Participant{} = _participant, %Actor{} = actor, + {%Participant{} = participant, %Actor{} = actor, %User{locale: locale, email: email} = _user, %Setting{timezone: timezone}}, %Event{} = old_event, %Event{} = event, diff ) do do_send_notification_for_event_update_to_participant( + participant, email, actor, old_event, @@ -101,13 +111,14 @@ defmodule Mobilizon.Web.Email.Event do end defp send_notification_for_event_update_to_participant( - {%Participant{} = _participant, %Actor{} = actor, + {%Participant{} = participant, %Actor{} = actor, %User{locale: locale, email: email} = _user, nil}, %Event{} = old_event, %Event{} = event, diff ) do do_send_notification_for_event_update_to_participant( + participant, email, actor, old_event, @@ -119,7 +130,8 @@ defmodule Mobilizon.Web.Email.Event do end defp send_notification_for_event_update_to_participant( - {%Participant{metadata: %{email: email}} = _participant, %Actor{} = actor, nil, nil}, + {%Participant{metadata: %{email: email} = participant_metadata} = participant, + %Actor{} = actor, nil, nil}, %Event{} = old_event, %Event{} = event, diff @@ -128,17 +140,19 @@ defmodule Mobilizon.Web.Email.Event do locale = Gettext.get_locale() do_send_notification_for_event_update_to_participant( + participant, email, actor, old_event, event, diff, - "Etc/UTC", + Map.get(participant_metadata, :timezone, "Etc/UTC"), locale ) end @spec do_send_notification_for_event_update_to_participant( + Participant.t(), String.t(), Actor.t(), Event.t(), @@ -148,6 +162,7 @@ defmodule Mobilizon.Web.Email.Event do String.t() ) :: Bamboo.Email.t() defp do_send_notification_for_event_update_to_participant( + participant, email, actor, old_event, @@ -157,7 +172,7 @@ defmodule Mobilizon.Web.Email.Event do locale ) do email - |> Email.Event.event_updated(actor, old_event, event, diff, timezone, locale) + |> Email.Event.event_updated(participant, actor, old_event, event, diff, timezone, locale) |> Email.Mailer.send_email_later() end end diff --git a/lib/web/email/participation.ex b/lib/web/email/participation.ex index 7fea008fb..c363c1a9e 100644 --- a/lib/web/email/participation.ex +++ b/lib/web/email/participation.ex @@ -59,7 +59,7 @@ defmodule Mobilizon.Web.Email.Participation do def participation_updated( email, - %Participant{event: event, role: :rejected}, + %Participant{event: event, role: :rejected} = participant, locale ) do Gettext.put_locale(locale) @@ -73,13 +73,15 @@ defmodule Mobilizon.Web.Email.Participation do Email.base_email(to: email, subject: subject) |> assign(:locale, locale) |> assign(:event, event) + |> assign(:jsonLDMetadata, json_ld(participant)) |> assign(:subject, subject) |> render(:event_participation_rejected) end def participation_updated( email, - %Participant{event: %Event{join_options: :free} = event, role: :participant}, + %Participant{event: %Event{join_options: :free} = event, role: :participant} = + participant, locale ) do Gettext.put_locale(locale) @@ -94,12 +96,13 @@ defmodule Mobilizon.Web.Email.Participation do |> assign(:locale, locale) |> assign(:event, event) |> assign(:subject, subject) + |> assign(:jsonLDMetadata, json_ld(participant)) |> render(:event_participation_confirmed) end def participation_updated( email, - %Participant{event: event, role: :participant}, + %Participant{event: event, role: :participant} = participant, locale ) do Gettext.put_locale(locale) diff --git a/test/graphql/resolvers/event_test.exs b/test/graphql/resolvers/event_test.exs index b426ea8ea..6ec807077 100644 --- a/test/graphql/resolvers/event_test.exs +++ b/test/graphql/resolvers/event_test.exs @@ -932,11 +932,11 @@ defmodule Mobilizon.Web.Resolvers.EventTest do test "update_event/3 updates an event", %{conn: conn, actor: actor, user: user} do event = insert(:event, organizer_actor: actor) - _creator = insert(:participant, event: event, actor: actor, role: :creator) + creator = insert(:participant, event: event, actor: actor, role: :creator) participant_user = insert(:user) participant_actor = insert(:actor, user: participant_user) - _participant = + participant = insert(:participant, event: event, actor: participant_actor, role: :participant) address = insert(:address) @@ -1009,6 +1009,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do assert_delivered_email( Email.Event.event_updated( + creator, user.email, actor, event, @@ -1019,6 +1020,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do assert_delivered_email( Email.Event.event_updated( + participant, participant_user.email, participant_actor, event,