Skip to content

Commit

Permalink
Merge pull request #270 from ibi-group/delete-push-devices
Browse files Browse the repository at this point in the history
Delete push devices on account deletion
  • Loading branch information
binh-dam-ibigroup authored Nov 21, 2024
2 parents 62ac8df + 4dd9239 commit 7a00826
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

0 comments on commit 7a00826

Please sign in to comment.