Use user timezone in emails

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-11-16 18:28:52 +01:00
parent 06a1233fc2
commit fdc8536c6f
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
7 changed files with 80 additions and 20 deletions

View File

@ -30,7 +30,7 @@ defmodule Mobilizon.Events do
alias Mobilizon.Service.Workers alias Mobilizon.Service.Workers
alias Mobilizon.Share alias Mobilizon.Share
alias Mobilizon.Storage.{Page, Repo} alias Mobilizon.Storage.{Page, Repo}
alias Mobilizon.Users.User alias Mobilizon.Users.{Setting, User}
alias Mobilizon.Web.Email alias Mobilizon.Web.Email
@ -1587,16 +1587,22 @@ defmodule Mobilizon.Events do
) )
end end
@doc """
List emails for local users (including anonymous ones) participating to an event
Returns {participation, actor, user, user_settings}
"""
@spec list_local_emails_user_participants_for_event_query(String.t()) :: Ecto.Query.t() @spec list_local_emails_user_participants_for_event_query(String.t()) :: Ecto.Query.t()
def list_local_emails_user_participants_for_event_query(event_id) do def list_local_emails_user_participants_for_event_query(event_id) do
Participant Participant
|> join(:inner, [p], a in Actor, on: p.actor_id == a.id and is_nil(a.domain)) |> join(:inner, [p], a in Actor, on: p.actor_id == a.id and is_nil(a.domain))
|> join(:left, [_p, a], u in User, on: a.user_id == u.id) |> join(:left, [_p, a], u in User, on: a.user_id == u.id)
|> join(:left, [_p, _a, u], s in Setting, on: s.user_id == u.id)
|> where( |> where(
[p], [p],
p.event_id == ^event_id and p.role not in [^:not_approved, ^:not_confirmed, ^:rejected] p.event_id == ^event_id and p.role not in [^:not_approved, ^:not_confirmed, ^:rejected]
) )
|> select([p, a, u], {p, a, u}) |> select([p, a, u, s], {p, a, u, s})
end end
@spec list_participations_for_user_query(integer()) :: Ecto.Query.t() @spec list_participations_for_user_query(integer()) :: Ecto.Query.t()

View File

@ -13,7 +13,7 @@ defmodule Mobilizon.Web.Email.Event do
alias Mobilizon.Events alias Mobilizon.Events
alias Mobilizon.Events.{Event, Participant} alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.Storage.Repo alias Mobilizon.Storage.Repo
alias Mobilizon.Users.User alias Mobilizon.Users.{Setting, User}
alias Mobilizon.Web.Email alias Mobilizon.Web.Email
alias Mobilizon.Web.Gettext, as: GettextBackend alias Mobilizon.Web.Gettext, as: GettextBackend
@ -28,6 +28,7 @@ defmodule Mobilizon.Web.Email.Event do
%Event{} = old_event, %Event{} = old_event,
%Event{} = event, %Event{} = event,
changes, changes,
timezone \\ "Etc/UTC",
locale \\ "en" locale \\ "en"
) do ) do
GettextBackend.put_locale(locale) GettextBackend.put_locale(locale)
@ -44,6 +45,7 @@ defmodule Mobilizon.Web.Email.Event do
|> assign(:old_event, old_event) |> assign(:old_event, old_event)
|> assign(:changes, changes) |> assign(:changes, changes)
|> assign(:subject, subject) |> assign(:subject, subject)
|> assign(:timezone, timezone)
|> render(:event_updated) |> render(:event_updated)
end end
@ -75,18 +77,42 @@ defmodule Mobilizon.Web.Email.Event do
defp send_notification_for_event_update_to_participant( defp send_notification_for_event_update_to_participant(
{%Participant{} = _participant, %Actor{} = actor, {%Participant{} = _participant, %Actor{} = actor,
%User{locale: locale, email: email} = _user}, %User{locale: locale, email: email} = _user, %Setting{timezone: timezone}},
%Event{} = old_event, %Event{} = old_event,
%Event{} = event, %Event{} = event,
diff diff
) do ) do
email do_send_notification_for_event_update_to_participant(
|> Email.Event.event_updated(actor, old_event, event, diff, locale) email,
|> Email.Mailer.deliver_later() actor,
old_event,
event,
diff,
timezone,
locale
)
end end
defp send_notification_for_event_update_to_participant( defp send_notification_for_event_update_to_participant(
{%Participant{metadata: %{email: email}} = _participant, %Actor{} = actor, nil}, {%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(
email,
actor,
old_event,
event,
diff,
"Etc/UTC",
locale
)
end
defp send_notification_for_event_update_to_participant(
{%Participant{metadata: %{email: email}} = _participant, %Actor{} = actor, nil, nil},
%Event{} = old_event, %Event{} = old_event,
%Event{} = event, %Event{} = event,
diff diff
@ -94,8 +120,28 @@ defmodule Mobilizon.Web.Email.Event do
when not is_nil(email) do when not is_nil(email) do
locale = Gettext.get_locale() locale = Gettext.get_locale()
do_send_notification_for_event_update_to_participant(
email,
actor,
old_event,
event,
diff,
"Etc/UTC",
locale
)
end
defp do_send_notification_for_event_update_to_participant(
email,
actor,
old_event,
event,
diff,
timezone,
locale
) do
email email
|> Email.Event.event_updated(actor, old_event, event, diff, locale) |> Email.Event.event_updated(actor, old_event, event, diff, timezone, locale)
|> Email.Mailer.deliver_later() |> Email.Mailer.deliver_later()
end end
end end

View File

@ -74,7 +74,7 @@
<%= gettext "Start" %> <%= gettext "Start" %>
</td> </td>
<td bgcolor="#ffffff" align="left"> <td bgcolor="#ffffff" align="left">
<b><%= datetime_to_string(@event.begins_on, @locale) %></b> <b><%= @event.begins_on |> datetime_tz_convert(@timezone) |> datetime_to_string(@locale) %></b>
</td> </td>
</tr> </tr>
<% end %> <% end %>
@ -84,7 +84,7 @@
<%= gettext "End" %> <%= gettext "End" %>
</td> </td>
<td bgcolor="#ffffff" align="left"> <td bgcolor="#ffffff" align="left">
<b><%= datetime_to_string(@event.ends_on, @locale) %></b> <b><%= @event.ends_on |> datetime_tz_convert(@timezone) |> datetime_to_string(@locale) %></b>
</td> </td>
</tr> </tr>
<% end %> <% end %>

View File

@ -15,10 +15,10 @@
<%= gettext "New title: %{title}", title: @event.title %> <%= gettext "New title: %{title}", title: @event.title %>
<% end %> <% end %>
<%= if MapSet.member?(@changes, :begins_on) do %> <%= if MapSet.member?(@changes, :begins_on) do %>
<%= gettext "Start %{begins_on}", begins_on: datetime_to_string(@event.begins_on, @locale) %> <%= gettext "Start %{begins_on}", begins_on: @event.begins_on |> datetime_tz_convert(@timezone) |> datetime_to_string(@locale) %>
<% end %> <% end %>
<%= if MapSet.member?(@changes, :ends_on) && !is_nil(@event.ends_on) do %> <%= if MapSet.member?(@changes, :ends_on) && !is_nil(@event.ends_on) do %>
<%= gettext "End %{ends_on}", ends_on: datetime_to_string(@event.ends_on, @locale) %> <%= gettext "End %{ends_on}", ends_on: @event.ends_on |> datetime_tz_convert(@timezone) |> datetime_to_string(@locale) %>
<% end %> <% end %>
<%= gettext "Visit the updated event page: %{link}", link: page_url(Mobilizon.Web.Endpoint, :event, @event.uuid) %> <%= gettext "Visit the updated event page: %{link}", link: page_url(Mobilizon.Web.Endpoint, :event, @event.uuid) %>
<%= ngettext "Would you wish to cancel your attendance, visit the event page through the link above and click the « Attending » button.", "Would you wish to cancel your attendance to one or several events, visit the event pages through the links above and click the « Attending » button.", 1 %> <%= ngettext "Would you wish to cancel your attendance, visit the event page through the link above and click the « Attending » button.", "Would you wish to cancel your attendance to one or several events, visit the event pages through the links above and click the « Attending » button.", 1 %>

View File

@ -46,7 +46,7 @@
<%= for participation <- @participations do %> <%= for participation <- @participations do %>
<li> <li>
<strong> <strong>
<%= participation.event.begins_on |> DateTime.shift_zone!(@timezone) |> datetime_to_string(@locale) %> <%= participation.event.begins_on |> datetime_tz_convert(@timezone) |> datetime_to_string(@locale) %>
</strong> </strong>
<a href="<%= page_url(Mobilizon.Web.Endpoint, :event, participation.event.uuid) %>" target="_blank"> <a href="<%= page_url(Mobilizon.Web.Endpoint, :event, participation.event.uuid) %>" target="_blank">
<%= participation.event.title %> <%= participation.event.title %>
@ -56,7 +56,7 @@
</ul> </ul>
<% else %> <% else %>
<strong> <strong>
<%= @participation.event.begins_on |> DateTime.shift_zone!(@timezone) |> datetime_to_string(@locale) %> <%= @participation.event.begins_on |> datetime_tz_convert(@timezone) |> datetime_to_string(@locale) %>
</strong> </strong>
<a href="<%= page_url(Mobilizon.Web.Endpoint, :event, @participation.event.uuid) %>" target="_blank"> <a href="<%= page_url(Mobilizon.Web.Endpoint, :event, @participation.event.uuid) %>" target="_blank">
<%= @participation.event.title %> <%= @participation.event.title %>

View File

@ -1,14 +1,11 @@
<%= gettext "What's up this week?" %> <%= gettext "What's up this week?" %>
== ==
<%= ngettext "You have one event this week:", "You have %{total} events this week:", @total, total: @total %> <%= ngettext "You have one event this week:", "You have %{total} events this week:", @total, total: @total %>
<%= if @total > 1 do %> <%= if @total > 1 do %>
<%= for participation <- @participations do %> <%= for participation <- @participations do %>
- <%= participation.event.begins_on |> DateTime.shift_zone!(@timezone) |> datetime_to_string(@locale) %> - <%= participation.event.title %> <%= page_url(Mobilizon.Web.Endpoint, :event, participation.event.uuid) %> - <%= participation.event.begins_on |> datetime_tz_convert(@timezone) |> datetime_to_string(@locale) %> - <%= participation.event.title %> <%= page_url(Mobilizon.Web.Endpoint, :event, participation.event.uuid) %>
<% end %> <% end %>
<% else %> <% else %>
<%= DateTime.shift_zone!(@participation.event.begins_on, @timezone) |> datetime_to_string(@locale) %> - <%= @participation.event.title %> <%= page_url(Mobilizon.Web.Endpoint, :event, @participation.event.uuid) %> <%= @participation.event.begins_on |> datetime_tz_convert(@timezone) |> datetime_to_string(@locale) %> - <%= @participation.event.title %> <%= page_url(Mobilizon.Web.Endpoint, :event, @participation.event.uuid) %>
<% end %> <% end %>
<%= ngettext "Would you wish to cancel your attendance, visit the event page through the link above and click the « Attending » button.", "Would you wish to cancel your attendance to one or several events, visit the event pages through the links above and click the « Attending » button.", @total %> <%= ngettext "Would you wish to cancel your attendance, visit the event page through the link above and click the « Attending » button.", "Would you wish to cancel your attendance to one or several events, visit the event pages through the links above and click the « Attending » button.", @total %>

View File

@ -16,4 +16,15 @@ defmodule Mobilizon.Web.EmailView do
string string
end end
end end
@spec datetime_tz_convert(DateTime.t(), String.t()) :: DateTime.t()
def datetime_tz_convert(%DateTime{} = datetime, timezone) do
case DateTime.shift_zone(datetime, timezone) do
{:ok, datetime_with_user_tz} ->
datetime_with_user_tz
_ ->
datetime
end
end
end end