Skip to content

Commit

Permalink
Merge pull request #193 from ibi-group/improve-retrieving-push-devices
Browse files Browse the repository at this point in the history
Retrieve push devices when returning user profile
  • Loading branch information
binh-dam-ibigroup authored Nov 15, 2023
2 parents d81418f + c7b05af commit f1bc548
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f1bc548

Please sign in to comment.