From d98e68203e8323f68c59da42de99bc5e2f6dc09c Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 20 Apr 2021 15:02:24 +0200 Subject: [PATCH] Handle sending mail more properly With custom sentry reporting issues Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/activity_pub.ex | 2 +- lib/graphql/resolvers/participant.ex | 2 +- lib/graphql/resolvers/user.ex | 4 +-- lib/service/workers/notification.ex | 8 +++--- lib/web/email/event.ex | 2 +- lib/web/email/follow.ex | 2 +- lib/web/email/group.ex | 8 +++--- lib/web/email/mailer.ex | 28 +++++++++++++++++++++ lib/web/email/participation.ex | 4 +-- lib/web/email/user.ex | 4 +-- 10 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lib/federation/activity_pub/activity_pub.ex b/lib/federation/activity_pub/activity_pub.ex index 608955b0b..483f7c7d2 100644 --- a/lib/federation/activity_pub/activity_pub.ex +++ b/lib/federation/activity_pub/activity_pub.ex @@ -592,7 +592,7 @@ defmodule Mobilizon.Federation.ActivityPub do Enum.each(Users.list_moderators(), fn moderator -> moderator |> Admin.report(report) - |> Mailer.deliver_later() + |> Mailer.send_email_later() end) {:ok, activity, report} diff --git a/lib/graphql/resolvers/participant.ex b/lib/graphql/resolvers/participant.ex index ede77f0dd..6b20a3641 100644 --- a/lib/graphql/resolvers/participant.ex +++ b/lib/graphql/resolvers/participant.ex @@ -70,7 +70,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Participant do participant, Map.get(args, :locale, "en") ) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() end {:ok, participant} diff --git a/lib/graphql/resolvers/user.ex b/lib/graphql/resolvers/user.ex index a07da0074..467305b9d 100644 --- a/lib/graphql/resolvers/user.ex +++ b/lib/graphql/resolvers/user.ex @@ -358,11 +358,11 @@ defmodule Mobilizon.GraphQL.Resolvers.User do {:ok, %User{} = user} <- Users.update_user_email(user, new_email) do user |> Email.User.send_email_reset_old_email() - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() user |> Email.User.send_email_reset_new_email() - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() {:ok, user} else diff --git a/lib/service/workers/notification.ex b/lib/service/workers/notification.ex index a3a7ff9a9..19202f8b2 100644 --- a/lib/service/workers/notification.ex +++ b/lib/service/workers/notification.ex @@ -23,7 +23,7 @@ defmodule Mobilizon.Service.Workers.Notification do Users.get_user_with_settings!(user_id) do email |> Notification.before_event_notification(participant, locale) - |> Mailer.deliver_later() + |> Mailer.send_email_later() :ok end @@ -48,7 +48,7 @@ defmodule Mobilizon.Service.Workers.Notification do true <- length(participations) > 0 do user |> Notification.on_day_notification(participations, total, locale) - |> Mailer.deliver_later() + |> Mailer.send_email_later() :ok else @@ -77,7 +77,7 @@ defmodule Mobilizon.Service.Workers.Notification do true <- length(participations) > 0 do user |> Notification.weekly_notification(participations, total, locale) - |> Mailer.deliver_later() + |> Mailer.send_email_later() :ok else @@ -99,7 +99,7 @@ defmodule Mobilizon.Service.Workers.Notification do Events.list_participants_for_event(event_id, [:not_approved]) do user |> Notification.pending_participation_notification(event, total) - |> Mailer.deliver_later() + |> Mailer.send_email_later() :ok else diff --git a/lib/web/email/event.ex b/lib/web/email/event.ex index 2a34fb347..5b3e39ddc 100644 --- a/lib/web/email/event.ex +++ b/lib/web/email/event.ex @@ -143,6 +143,6 @@ defmodule Mobilizon.Web.Email.Event do ) do email |> Email.Event.event_updated(actor, old_event, event, diff, timezone, locale) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() end end diff --git a/lib/web/email/follow.ex b/lib/web/email/follow.ex index ef12e495f..25c3c953d 100644 --- a/lib/web/email/follow.ex +++ b/lib/web/email/follow.ex @@ -62,7 +62,7 @@ defmodule Mobilizon.Web.Email.Follow do |> assign(:follower, follower) |> assign(:subject, subject) |> render(:instance_follow) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() :ok end diff --git a/lib/web/email/group.ex b/lib/web/email/group.ex index 440c0f20b..2c3029007 100644 --- a/lib/web/email/group.ex +++ b/lib/web/email/group.ex @@ -42,7 +42,7 @@ defmodule Mobilizon.Web.Email.Group do |> assign(:group, group) |> assign(:subject, subject) |> render(:group_invite) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() :ok end @@ -70,7 +70,7 @@ defmodule Mobilizon.Web.Email.Group do |> assign(:group, group) |> assign(:subject, subject) |> render(:group_member_removal) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() :ok end @@ -107,7 +107,7 @@ defmodule Mobilizon.Web.Email.Group do |> assign(:subject, subject) |> assign(:instance, instance) |> render(:group_suspension) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() :ok end @@ -147,7 +147,7 @@ defmodule Mobilizon.Web.Email.Group do |> assign(:instance, instance) |> assign(:author, author) |> render(:group_deletion) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() :ok else diff --git a/lib/web/email/mailer.ex b/lib/web/email/mailer.ex index 87316f1ce..3b286834c 100644 --- a/lib/web/email/mailer.ex +++ b/lib/web/email/mailer.ex @@ -3,4 +3,32 @@ defmodule Mobilizon.Web.Email.Mailer do Mobilizon Mailer. """ use Bamboo.Mailer, otp_app: :mobilizon + + def send_email_later(email) do + try do + Mobilizon.Web.Email.Mailer.deliver_later!(email) + rescue + error -> + Sentry.capture_exception(error, + stacktrace: __STACKTRACE__, + extra: %{extra: "Error while sending email"} + ) + + reraise error, __STACKTRACE__ + end + end + + def send_email(email) do + try do + Mobilizon.Web.Email.Mailer.deliver_now!(email) + rescue + error -> + Sentry.capture_exception(error, + stacktrace: __STACKTRACE__, + extra: %{extra: "Error while sending email"} + ) + + reraise error, __STACKTRACE__ + end + end end diff --git a/lib/web/email/participation.ex b/lib/web/email/participation.ex index 132c0d8a2..dbd90e694 100644 --- a/lib/web/email/participation.ex +++ b/lib/web/email/participation.ex @@ -28,7 +28,7 @@ defmodule Mobilizon.Web.Email.Participation do email |> participation_updated(participation, locale) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() end :ok @@ -40,7 +40,7 @@ defmodule Mobilizon.Web.Email.Participation do with %User{locale: locale} = user <- Users.get_user!(user_id) do user |> participation_updated(participation, locale) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() :ok end diff --git a/lib/web/email/user.ex b/lib/web/email/user.ex index 01d315434..5f45d3e24 100644 --- a/lib/web/email/user.ex +++ b/lib/web/email/user.ex @@ -90,7 +90,7 @@ defmodule Mobilizon.Web.Email.User do def send_confirmation_email(%User{} = user, locale \\ "en") do user |> Email.User.confirmation_email(locale) - |> Email.Mailer.deliver_later() + |> Email.Mailer.send_email_later() end @doc """ @@ -135,7 +135,7 @@ defmodule Mobilizon.Web.Email.User do {:ok, %Bamboo.Email{} = mail} <- user_updated |> Email.User.reset_password_email(locale) - |> Email.Mailer.deliver_later() do + |> Email.Mailer.send_email_later() do {:ok, mail} else {:error, reason} -> {:error, reason}