diff --git a/src/main/java/org/opentripplanner/middleware/models/OtpUser.java b/src/main/java/org/opentripplanner/middleware/models/OtpUser.java index 061a31326..1c9e32a02 100644 --- a/src/main/java/org/opentripplanner/middleware/models/OtpUser.java +++ b/src/main/java/org/opentripplanner/middleware/models/OtpUser.java @@ -6,6 +6,7 @@ import org.opentripplanner.middleware.auth.Auth0Users; import org.opentripplanner.middleware.auth.RequestingUser; import org.opentripplanner.middleware.persistence.Persistence; +import org.opentripplanner.middleware.utils.NotificationUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -135,6 +136,10 @@ public boolean delete(boolean deleteAuth0User) { } } + // Delete push devices + NotificationUtils.deletePushDevices(email); + + // If a related user, invalidate relationship with all dependents. for (String userId : dependents) { OtpUser dependent = Persistence.otpUsers.getById(userId); diff --git a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java index df51d635e..3e378d8a8 100644 --- a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java +++ b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java @@ -408,6 +408,35 @@ public static int getPushInfo(String toUser) { return 0; } + /** + * Deletes devices registered for push notifications, typically when a user deletes their account. + * Calls Push API's DELETE endpoint. + * @param toUser email address of user for which to delete device registration. + */ + public static void deletePushDevices(String toUser) { + // If Push API config properties aren't set, no info can be obtained. + if (PUSH_API_KEY == null || PUSH_API_URL == null) return; + try { + Map headers = Map.of("Accept", "application/json"); + var httpResponse = HttpUtils.httpRequestRawResponse( + URI.create(getPushDevicesUrl(String.format( + "%s/device/deregister?api_key=%s&user=", + PUSH_API_URL, + PUSH_API_KEY + ), toUser)), + 1000, + HttpMethod.DELETE, + headers, + null + ); + if (httpResponse.status != 200) { + LOG.error("Error {} deleting push notification devices for {}", httpResponse.status, toUser); + } + } catch (Exception e) { + LOG.error("Error deleting push notification devices for {}", toUser, e); + } + } + /** * Return the number of unique, non null, device names. */