From a3e08ab5a912c9a8a2458ecf58803136f850c9f7 Mon Sep 17 00:00:00 2001 From: Jym Dyer Date: Thu, 24 Aug 2023 14:21:48 -0700 Subject: [PATCH] Move NotificationUtils#getPushInfo() call into CheckMonitoredTrip class. --- .../middleware/tripmonitor/jobs/CheckMonitoredTrip.java | 7 +++++++ .../middleware/utils/NotificationUtils.java | 8 +++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java b/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java index 2ae84ee8f..f44ee17d8 100644 --- a/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java +++ b/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java @@ -407,6 +407,13 @@ private void sendNotifications() { boolean successPush = false; boolean successSms = false; + // Update push notification devices count, which may change asynchronously + int numPushDevices = NotificationUtils.getPushInfo(otpUser.email); + if (numPushDevices != otpUser.pushDevices) { + otpUser.pushDevices = numPushDevices; + Persistence.otpUsers.replace(otpUser.id, otpUser); + } + if (otpUser.notificationChannel.contains(OtpUser.Notification.EMAIL)) { successEmail = sendEmail(otpUser, templateData); } diff --git a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java index aa70a2981..ae0299a37 100644 --- a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java +++ b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java @@ -13,7 +13,6 @@ import org.opentripplanner.middleware.bugsnag.BugsnagReporter; import org.opentripplanner.middleware.models.AdminUser; import org.opentripplanner.middleware.models.OtpUser; -import org.opentripplanner.middleware.persistence.Persistence; import org.opentripplanner.middleware.utils.HttpUtils; import org.opentripplanner.middleware.utils.JsonUtils; import org.slf4j.Logger; @@ -54,10 +53,6 @@ public static String sendPush(OtpUser otpUser, String textTemplate, Object templ try { String body = TemplateUtils.renderTemplate(textTemplate, templateData); String toUser = otpUser.email; - // Calls the `get` endpoint, the only reliable way to get number of push notification devices, - // as the `publish` endpoint returns success for zero devices and/or if devices turn them off. - otpUser.pushDevices = getPushInfo(toUser); - Persistence.otpUsers.replace(otpUser.id, otpUser); return otpUser.pushDevices > 0 ? sendPush(toUser, body) : "OK"; } catch (TemplateException | IOException e) { // This catch indicates there was an error rendering the template. Note: TemplateUtils#renderTemplate @@ -281,6 +276,9 @@ public static boolean sendEmailViaSparkpost( } /** + * Get number of push notification devices. Calls Push API's get endpoint, the only reliable way + * to obtain this value, as the publish endpoint returns success even for zero devices. + * * @param toUser email address of user that devices are indexed by */ public static int getPushInfo(String toUser) {