diff --git a/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java b/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java index 6bb536f8f..5d29fdccf 100644 --- a/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java +++ b/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java @@ -80,7 +80,11 @@ protected void buildEndpoint(ApiEndpoint baseEndpoint) { @Override protected OtpUser getUserProfile(RequestingUser profile) { - return profile.otpUser; + OtpUser otpUser = profile.otpUser; + if (otpUser != null) { + NotificationUtils.updatePushDevices(otpUser); + } + return otpUser; } /** 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 76e225f1d..1287be92f 100644 --- a/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java +++ b/src/main/java/org/opentripplanner/middleware/tripmonitor/jobs/CheckMonitoredTrip.java @@ -410,11 +410,8 @@ private void sendNotifications() { return; } // 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); - } + NotificationUtils.updatePushDevices(otpUser); + if (notifications.size() == 0) { // FIXME: Change log level LOG.info("No notifications queued for trip. Skipping notify."); diff --git a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java index 12ecb24f9..3ba4164f3 100644 --- a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java +++ b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java @@ -14,6 +14,7 @@ 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.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -324,6 +325,19 @@ public static int getPushInfo(String toUser) { return 0; } + /** + * Poll the push middleware for the number of devices registered to receive push notifications + * for the specified user, and update the corresponding field in memory and Mongo. + * @param otpUser The {@link OtpUser} for which to check and update push devices. + */ + public static void updatePushDevices(OtpUser otpUser) { + int numPushDevices = getPushInfo(otpUser.email); + if (numPushDevices != otpUser.pushDevices) { + otpUser.pushDevices = numPushDevices; + Persistence.otpUsers.replace(otpUser.id, otpUser); + } + } + static class NotificationInfo { public String user; public String message;