Skip to content

Commit

Permalink
refactor(Refactor to create and process leg tranistion based notifica…
Browse files Browse the repository at this point in the history
…tion types):
  • Loading branch information
br648 committed Dec 10, 2024
1 parent 128fb32 commit 91d6619
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public enum Message {
TRIP_INVITE_COMPANION,
TRIP_INVITE_PRIMARY_TRAVELER,
TRIP_INVITE_OBSERVER,
TRIP_NAME_UNDEFINED,
TRIP_TRAVELER_GENERIC_NAME,
TRIP_NOT_FOUND_NOTIFICATION,
TRIP_NO_LONGER_POSSIBLE_NOTIFICATION,
TRIP_REMINDER_NOTIFICATION,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.opentripplanner.middleware.models;

import org.opentripplanner.middleware.i18n.Message;
import org.opentripplanner.middleware.tripmonitor.jobs.NotificationType;
import org.opentripplanner.middleware.triptracker.TravelerPosition;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class LegTransitionNotification {

public String travelerName;
public NotificationType notificationType;
public TravelerPosition travelerPosition;
public Locale observerLocale;
public TripMonitorNotification tripMonitorNotification;

public LegTransitionNotification(
String travelerName,
NotificationType notificationType,
TravelerPosition travelerPosition,
Locale observerLocale
) {
this.travelerName = travelerName;
this.notificationType = notificationType;
this.travelerPosition = travelerPosition;
this.observerLocale = observerLocale;
this.tripMonitorNotification = createTripMonitorNotification(notificationType);
}

/**
* Create {@link TripMonitorNotification} for leg transition based on notification type.
*/
@Nullable
public TripMonitorNotification createTripMonitorNotification(NotificationType notificationType) {
String body;
switch (notificationType) {
case MODE_CHANGE_NOTIFICATION:
body = String.format(
Message.MODE_CHANGE_NOTIFICATION.get(travelerPosition.locale),
getTravelerName(),
travelerPosition.expectedLeg.mode,
travelerPosition.nextLeg.mode
);
break;
case DEPARTED_NOTIFICATION:
body = String.format(
Message.DEPARTED_NOTIFICATION.get(observerLocale),
getTravelerName(),
travelerPosition.expectedLeg.from.name
);
break;
case ARRIVED_NOTIFICATION:
body = String.format(
Message.ARRIVED_NOTIFICATION.get(travelerPosition.locale),
getTravelerName(),
travelerPosition.expectedLeg.to.name
);
break;
default:
body = null;
}
return (body != null) ? new TripMonitorNotification(notificationType, body) : null;
}

/**
* Get the traveler's name if available, if not provide a generic traveler name.
*/
private String getTravelerName() {
if (travelerName != null) {
return travelerName;
} else {
return Message.TRIP_TRAVELER_GENERIC_NAME.get(observerLocale);
}
}

/**
* Create locale specific notifications.
*/
public static TripMonitorNotification[] createLegTransitionNotifications(
List<NotificationType> legTransitionTypes,
String travelerName,
TravelerPosition travelerPosition,
Locale observerLocale
) {
List<TripMonitorNotification> tripMonitorNotifications = new ArrayList<>();
// Create locale specific notifications.
for (NotificationType legTransitionType : legTransitionTypes) {
LegTransitionNotification legTransitionNotification = new LegTransitionNotification(
travelerName,
legTransitionType,
travelerPosition,
observerLocale
);
if (legTransitionNotification.tripMonitorNotification != null) {
tripMonitorNotifications.add(legTransitionNotification.tripMonitorNotification);
}
}
return tripMonitorNotifications.toArray(new TripMonitorNotification[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.opentripplanner.middleware.i18n.Message;
import org.opentripplanner.middleware.tripmonitor.jobs.NotificationType;
import org.opentripplanner.middleware.triptracker.TravelerPosition;
import org.opentripplanner.middleware.utils.DateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -111,53 +110,4 @@ public static TripMonitorNotification createInitialReminderNotification(
)
);
}

/**
* Creates a departed notification.
*/
public static TripMonitorNotification createDepartedNotification(
NotificationType legTransitionType,
TravelerPosition travelerPosition
) {
return new TripMonitorNotification(
legTransitionType,
String.format(
Message.DEPARTED_NOTIFICATION.get(travelerPosition.locale),
travelerPosition.expectedLeg.from.name
)
);
}

/**
* Creates a mode change notification.
*/
public static TripMonitorNotification createModeChangeNotification(
NotificationType legTransitionType,
TravelerPosition travelerPosition
) {
return new TripMonitorNotification(
legTransitionType,
String.format(
Message.MODE_CHANGE_NOTIFICATION.get(travelerPosition.locale),
travelerPosition.expectedLeg.mode,
travelerPosition.nextLeg.mode
)
);
}

/**
* Creates an arrived notification.
*/
public static TripMonitorNotification createArrivedNotification(
NotificationType legTransitionType,
TravelerPosition travelerPosition
) {
return new TripMonitorNotification(
legTransitionType,
String.format(
Message.ARRIVED_NOTIFICATION.get(travelerPosition.locale),
travelerPosition.expectedLeg.to.name
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import org.opentripplanner.middleware.i18n.Message;
import org.opentripplanner.middleware.models.ItineraryExistence;
import org.opentripplanner.middleware.models.LegTransitionNotification;
import org.opentripplanner.middleware.models.MonitoredTrip;
import org.opentripplanner.middleware.models.OtpUser;
import org.opentripplanner.middleware.models.TripMonitorAlertNotification;
import org.opentripplanner.middleware.models.TripMonitorNotification;
import org.opentripplanner.middleware.otp.OtpGraphQLVariables;
import org.opentripplanner.middleware.otp.response.Leg;
import org.opentripplanner.middleware.tripmonitor.TripStatus;
import org.opentripplanner.middleware.otp.OtpDispatcher;
import org.opentripplanner.middleware.otp.response.Itinerary;
Expand All @@ -26,7 +26,6 @@
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.annotation.Nullable;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
Expand All @@ -43,9 +42,8 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.IntStream;

import static org.opentripplanner.middleware.utils.I18nUtils.label;
import static com.mongodb.client.model.Filters.eq;

/**
* This job handles the primary functions for checking a {@link MonitoredTrip}, including:
Expand Down Expand Up @@ -252,11 +250,29 @@ private boolean isTrackingOngoing() {
return trip.journeyState.tripStatus == TripStatus.TRIP_ACTIVE && TripTrackingData.getOngoingTrackedJourney(trip.id) != null;
}

public void processLegTransition(List<NotificationType> legTransitionTypes , TravelerPosition travelerPosition) {
for (NotificationType legTransitionType : legTransitionTypes) {
enqueueNotification(createLegTransitionNotification(legTransitionType, travelerPosition));
/**
* Process leg transition notifications.
*/
public void processLegTransition(List<NotificationType> legTransitionTypes, TravelerPosition travelerPosition) {
OtpUser otpUser = getOtpUser();
if (otpUser != null) {
otpUser.relatedUsers.forEach(relatedUser -> {
if (relatedUser.isConfirmed()) {
OtpUser observer = Persistence.otpUsers.getOneFiltered(eq("email", relatedUser.email));
if (observer != null) {
enqueueNotification(
LegTransitionNotification.createLegTransitionNotifications(
legTransitionTypes,
otpUser.name,
travelerPosition,
I18nUtils.getOtpUserLocale(observer)
)
);
sendNotifications(observer);
}
}
});
}
sendNotifications();
}

/**
Expand Down Expand Up @@ -509,26 +525,8 @@ public TripMonitorNotification checkTripForDelay(NotificationType delayType) {
return null;
}

@Nullable
public TripMonitorNotification createLegTransitionNotification(
NotificationType legTransitionType,
TravelerPosition travelerPosition
) {
switch (legTransitionType) {
case MODE_CHANGE_NOTIFICATION:
return TripMonitorNotification.createModeChangeNotification(legTransitionType, travelerPosition);
case DEPARTED_NOTIFICATION:
return TripMonitorNotification.createDepartedNotification(legTransitionType, travelerPosition);
case ARRIVED_NOTIFICATION:
return TripMonitorNotification.createArrivedNotification(legTransitionType, travelerPosition);
default:
return null;
}
}

/**
* Compose a message for any enqueued notifications and send to {@link OtpUser} based on their notification
* preferences.
* Send notification to user associated with the trip.
*/
private void sendNotifications() {
OtpUser otpUser = getOtpUser();
Expand All @@ -537,6 +535,14 @@ private void sendNotifications() {
// TODO: Bugsnag / delete monitored trip?
return;
}
sendNotifications(otpUser);
}

/**
* Compose a message for any enqueued notifications and send to {@link OtpUser} based on their notification
* preferences.
*/
private void sendNotifications(OtpUser otpUser) {
// Update push notification devices count, which may change asynchronously
NotificationUtils.updatePushDevices(otpUser);

Expand All @@ -554,9 +560,12 @@ private void sendNotifications() {
return;
}

Locale locale = getOtpUserLocale();
String tripNameOrReminder = hasInitialReminder ? initialReminderNotification.body : trip.tripName;
if (tripNameOrReminder == null) {
tripNameOrReminder = Message.TRIP_NAME_UNDEFINED.get(locale);
}

Locale locale = getOtpUserLocale();
// A HashMap is needed instead of a Map for template data to be serialized to the template renderer.
Map<String, Object> templateData = new HashMap<>();
templateData.putAll(Map.of(
Expand Down
8 changes: 5 additions & 3 deletions src/main/resources/Message.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ ACCEPT_DEPENDENT_EMAIL_LINK_TEXT = Accept trusted companion
ACCEPT_DEPENDENT_EMAIL_SUBJECT = Trusted companion request
ACCEPT_DEPENDENT_EMAIL_MANAGE = Manage settings
ACCEPT_DEPENDENT_ERROR = Unable to accept trusted companion.
ARRIVED_NOTIFICATION = The traveler has arrived at %s.
ARRIVED_NOTIFICATION = %s has arrived at %s.
LABEL_AND_CONTENT = %s: %s
MODE_CHANGE_NOTIFICATION = The traveler has changed mode from %s to %s.
DEPARTED_NOTIFICATION = The traveler has departed %s.
MODE_CHANGE_NOTIFICATION = %s has changed transit from %s to %s.
DEPARTED_NOTIFICATION = %s has departed %s.
SMS_STOP_NOTIFICATIONS = To stop receiving notifications, reply STOP.
TRIP_EMAIL_SUBJECT = %s Notification
TRIP_EMAIL_SUBJECT_FOR_USER = Trip for %s Notification
Expand Down Expand Up @@ -35,6 +35,8 @@ TRIP_DELAY_MINUTES = %d minutes
TRIP_INVITE_COMPANION = %s added you as a companion for their trip.
TRIP_INVITE_PRIMARY_TRAVELER = %s made you the primary traveler on this trip.
TRIP_INVITE_OBSERVER = %s added you as an observer for their trip.
TRIP_NAME_UNDEFINED = Trip name undefined
TRIP_TRAVELER_GENERIC_NAME = Traveler
TRIP_NOT_FOUND_NOTIFICATION = Your itinerary was not found in today's trip planner results. Please check real-time conditions and plan a new trip.
TRIP_NO_LONGER_POSSIBLE_NOTIFICATION = Your itinerary is no longer possible on any monitored day of the week. Please plan and save a new trip.
TRIP_REMINDER_NOTIFICATION = Reminder for %s at %s.
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/Message_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ TRIP_DELAY_MINUTES = %d minutes
TRIP_INVITE_COMPANION = %s vous a ajouté comme accompagnateur·trice pour un trajet.
TRIP_INVITE_PRIMARY_TRAVELER = %s vous a fait le voyageur principal sur ce trajet.
TRIP_INVITE_OBSERVER = %s vous a ajouté comme observateur·trice pour un trajet.
TRIP_NAME_UNDEFINED = TODO
TRIP_TRAVELER_GENERIC_NAME = TODO
TRIP_NOT_FOUND_NOTIFICATION = Votre trajet est introuvable aujourd'hui. Veuillez vérifier les conditions en temps-réel et recherchez un nouveau trajet.
TRIP_NO_LONGER_POSSIBLE_NOTIFICATION = Votre trajet n'est plus possible dans aucun jour de suivi de la semaine. Veuillez rechercher et enregistrer un nouveau trajet.
TRIP_REMINDER_NOTIFICATION = Rappel pour %s à %s.
Expand Down
Loading

0 comments on commit 91d6619

Please sign in to comment.