Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete push devices on account deletion #270

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading