From f47f4d9a645201bc070cb19788ac46bf446516dd Mon Sep 17 00:00:00 2001
From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com>
Date: Fri, 15 Nov 2024 11:06:07 -0500
Subject: [PATCH 1/2] feat(NotificationUtils): Delete user push devices on
account deletion.
---
.../middleware/models/OtpUser.java | 5 ++++
.../middleware/utils/NotificationUtils.java | 29 +++++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/src/main/java/org/opentripplanner/middleware/models/OtpUser.java b/src/main/java/org/opentripplanner/middleware/models/OtpUser.java
index c42d90f6d..2fd67c16c 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..5cd341f7b 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/devices/get?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.
*/
From 4dd92396076434de1e7e147b5559fc7f61d9b101 Mon Sep 17 00:00:00 2001
From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com>
Date: Fri, 15 Nov 2024 13:33:14 -0500
Subject: [PATCH 2/2] fix(NotificationUtils): Use correct path to deregister
devices.
---
.../org/opentripplanner/middleware/utils/NotificationUtils.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java
index 5cd341f7b..3e378d8a8 100644
--- a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java
+++ b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java
@@ -420,7 +420,7 @@ public static void deletePushDevices(String toUser) {
Map headers = Map.of("Accept", "application/json");
var httpResponse = HttpUtils.httpRequestRawResponse(
URI.create(getPushDevicesUrl(String.format(
- "%s/devices/get?api_key=%s&user=",
+ "%s/device/deregister?api_key=%s&user=",
PUSH_API_URL,
PUSH_API_KEY
), toUser)),