Skip to content

Commit

Permalink
Move NotificationUtils#getPushInfo() call into CheckMonitoredTrip class.
Browse files Browse the repository at this point in the history
  • Loading branch information
JymDyerIBI committed Aug 24, 2023
1 parent ce21e89 commit a3e08ab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -281,6 +276,9 @@ public static boolean sendEmailViaSparkpost(
}

/**
* Get number of push notification devices. Calls Push API's <code>get</code> endpoint, the only reliable way
* to obtain this value, as the <code>publish</code> endpoint returns success even for zero devices.
*
* @param toUser email address of user that devices are indexed by
*/
public static int getPushInfo(String toUser) {
Expand Down

0 comments on commit a3e08ab

Please sign in to comment.