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

Add configurable strict mode to itinerary filter #1024

Merged
merged 6 commits into from
Oct 16, 2023
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
3 changes: 3 additions & 0 deletions example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ itinerary:
# Whether the plan first/previous/next/last buttons should be shown along with
# plan trip itineraries.
showPlanFirstLastButtons: false
# Filters out trips returned by OTP by default, unless specifically requested.
# e.g. filters out walk-only itineraries if user has not explicitly asked for them.
strictItineraryFiltering: false
# Whether to render route names and colors in the blocks inside
# the batch ui rows
renderRouteNamesInBlocks: true
Expand Down
31 changes: 27 additions & 4 deletions lib/actions/apiV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import { RoutingQueryCallResult } from './api-constants'
import { setItineraryView } from './ui'
import { zoomToPlace } from './map'

const { generateCombinations, generateOtp2Query } = coreUtils.queryGen
const { generateCombinations, generateOtp2Query, SIMPLIFICATIONS } =
coreUtils.queryGen
const { getTripOptionsFromQuery, getUrlParams } = coreUtils.query
const { convertGraphQLResponseToLegacy } = coreUtils.itinerary
const { randId } = coreUtils.storage
Expand Down Expand Up @@ -825,6 +826,8 @@ export function routingQuery(searchId = null, updateSearchInReducer) {
config?.modes?.initialState?.enabledModeButtons ||
{}

const strictModes = config?.itinerary?.strictItineraryFiltering

// Filter mode definitions based on active mode keys
const activeModeButtons = config.modes?.modeButtons.filter((mb) =>
activeModeKeys.includes(mb.key)
Expand Down Expand Up @@ -919,11 +922,31 @@ export function routingQuery(searchId = null, updateSearchInReducer) {
routingError,
{
rewritePayload: (response, dispatch, getState) => {
const withCollapsedShortNames =
response.data?.plan?.itineraries?.map((itin) => ({
const itineraries = response.data?.plan?.itineraries

// Convert user-selected transit modes from mode selector into modes recognized by OTP.
const activeModeStrings = activeModes.map(
(am) => SIMPLIFICATIONS[am.mode]
)

let filteredItineraries = itineraries
// If "strictItineraryFiltering" is enabled, only return itineraries that contain at least one explicitly requested mode...
if (strictModes) {
filteredItineraries = itineraries.filter((itin) =>
itin.legs.some((leg) =>
activeModeStrings.includes(SIMPLIFICATIONS[leg.mode])
)
)
// ... Otherwise return all itineraries.
}

// Filter itineraries to collapse short names and hide unnecessary errors.
const withCollapsedShortNames = filteredItineraries.map(
(itin) => ({
amy-corson-ibigroup marked this conversation as resolved.
Show resolved Hide resolved
...itin,
legs: itin.legs?.map(convertGraphQLResponseToLegacy)
}))
})
)

/* It is possible for a NO_TRANSIT_CONNECTION error to be
returned even if trips were returned, since it is on a mode-by-mode basis.
Expand Down
Loading