Files
TREK/server/src/nest/maps/maps.service.ts
T

1358 lines
65 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { isOutsideChina, normalizePlaceWebsite } from '@trek/shared';
import type {
MapsSearchResult,
MapsAutocompleteResult,
MapsPlaceDetailsResult,
MapsPlacePhotoResult,
MapsReverseResult,
MapsResolveUrlResult,
} from '@trek/shared';
import { readEnv } from '../../app-config';
import { safeFetchFollow, SsrfBlockedError } from '../../utils/ssrfGuard';
import { discardBody, exceedsDeclaredLength, readCappedText } from '../../utils/cappedFetch';
import { resolveApiKey, type ApiKeySource } from '../settings/instance-api-keys';
import { InjectRepository } from '@mikro-orm/nestjs';
import { AppSettings } from '../../db/entities/AppSettings.entity';
import type { AppSettingsRepository } from '../../db/repositories/AppSettings.repository';
import { Users } from '../../db/entities/Users.entity';
import type { UsersRepository } from '../../db/repositories/Users.repository';
import { PlaceDetailsCache } from '../../db/entities/PlaceDetailsCache.entity';
import type { PlaceDetailsCacheRepository } from '../../db/repositories/PlaceDetailsCache.repository';
import { Places } from '../../db/entities/Places.entity';
import type { PlacesRepository } from '../../db/repositories/Places.repository';
import { isPlacesProviderChoice, type PlacesProviderChoice } from './providers/places-provider';
import { GooglePlacesClient } from './providers/google-places.provider';
import { OsmClient, POI_RESULT_CAP, type PoiSearchResult } from './providers/osm.client';
import { WikimediaClient, type BrandLogo } from './providers/wikimedia.client';
import { withPhotoFetchSlot } from './photo-fetch-slot';
import {
AMAP_SHORT_HOSTS,
AmapPlacesProvider,
AmapTipStash,
isAmapHost,
isAmapPlaceId,
parseAmapUrl,
} from './providers/amap.provider';
// ── Photo cache (disk-backed) ────────────────────────────────────────────────
import { PlacePhotoCacheService } from '../place-photos/place-photo-cache.service';
import { GoogleQuotaService } from '../google-quota/google-quota.service';
import type { GeoLane } from '../geo/nominatim.client';
import {
trekPlacesSearch,
indexHitsOnly,
isOsmHit,
osmPlaceId,
trekPlacesById,
trekPlacesArea,
trekPlacesNearby,
toPlaceRecord,
POI_CATEGORY_TO_TREK,
type TrekPlace,
} from './trek-places.client';
import {
UA,
toApiLang,
buildOsmDetails,
isGooglePlaceId,
googleFtidFromMapsUrl,
OSM_PLACE_ID,
parsePoiCategories,
mergeSearchResults,
} from './maps.helpers';
import {
NEARBY_DEFAULT_LIMIT,
NEARBY_DEFAULT_RADIUS_M,
nearbyCacheKey,
nearestFirst,
} from './maps-nearby.helpers';
// ── Interfaces ───────────────────────────────────────────────────────────────
export { readBrandIdentity, readWikiIdentity, type WikiIdentity } from './providers/wiki-identity';
export { withPhotoFetchSlot } from './photo-fetch-slot';
export type { BrandLogo, CommonsCandidate } from './providers/wikimedia.client';
// A Google Maps place page is a few hundred KB; the coordinates sit in the
// embedded map data near the top, so two megabytes is plenty and keeps an
// unbounded body out of memory.
const MAX_MAPS_PAGE_BYTES = 2_000_000;
export const GOOGLE_SHORT_HOSTS = ['goo.gl', 'maps.app.goo.gl'];
/**
* Google Maps lives on every country domain — google.de, maps.google.co.uk,
* google.com.au — so the host is matched by shape. A fixed list of .com hosts
* would quietly stop resolving the ccTLD links people actually paste. The TLD
* labels stay short (2-3 letters, optionally two of them) so that
* `google.evil.com` is not a Google host.
*/
export function isGoogleMapsHost(hostname: string): boolean {
return GOOGLE_SHORT_HOSTS.includes(hostname)
|| /^(www\.|maps\.)?google\.[a-z]{2,3}(\.[a-z]{2})?$/.test(hostname);
}
// Places near a point (#976): cached longer than the POI boxes, because the
// likely caller is an import asking the same photo location again, and Google
// bills every one of those.
const NEARBY_CACHE = new Map<string, { at: number; value: { places: Record<string, unknown>[]; source: string } }>();
const NEARBY_CACHE_TTL_MS = 30 * 60 * 1000;
const NEARBY_CACHE_MAX = 500;
type LocationBias = { low: { lat: number; lng: number }; high: { lat: number; lng: number } };
/**
* The app_settings row that names the keyed places provider.
*
* A setting rather than "whichever key is configured": an install can hold both
* credentials (a team split between China and elsewhere), and then only an
* admin can say which one should answer. Absent, which is every install that
* predates Amap, means `auto`, which keeps Google.
*/
export const PLACES_PROVIDER_SETTING = 'places_provider';
/**
* The admin switch that hands search and suggestions to Google alone. Off, the
* index and OpenStreetMap answer first and Google is only asked when they find
* nothing, which is what every install has had since 4.3.0.
*/
export const PLACES_GOOGLE_ONLY_SETTING = 'places_google_only';
/**
* A details row as the cache holds it. A row written before #2483 still has a
* source's website as it came, `www.hotel.cn` from Amap for one, and keeps for a
* week (an expanded one until a refresh), so the website is normalized on the
* way out of the cache as well as on the way in.
*/
function cachedDetails(payload: string): Record<string, unknown> | null {
const place = JSON.parse(payload) as Record<string, unknown> | null;
return place && 'website' in place ? { ...place, website: normalizePlaceWebsite(place.website) } : place;
}
/**
* Whoever holds the keyed slot beside the index for one request: Google's
* credential, an Amap provider, or nobody (the OpenStreetMap stack alone).
*/
type KeyedProvider =
| { id: 'google'; key: string; source: ApiKeySource | null }
| { id: 'amap'; provider: AmapPlacesProvider };
/**
* /api/maps domain service — geocoding, the provider fan-out
* (Nominatim/Overpass/Google), the place-details/photo caches and the SSRF
* guard on every outbound URL. DI-native since the maps fold: the legacy
* services/mapsService.ts functions live here as methods over the injected
* repositories (byte-identical SQL and behaviour). Every consumer injects
* this class; pure helpers live in maps.helpers.ts.
*
* The per-endpoint kill-switches are settings reads the legacy route does
* inline; they're encapsulated here as `*Disabled()` helpers over the same
* `app_settings` rows (`AppSettingsRepository`, already injected pre-Plan-3h
* for the API-key resolution logic).
*
* Plan 3h Task 4 (R8): the file's last 9 raw statements are converted —
* MAP1/MAP2 reuse the already-injected `AppSettingsRepository`; MAP3-8
* (`place_details_cache`) reuse `PlaceDetailsCacheRepository` — built for
* `place-enrichment.service.ts`, an unrelated domain reading the SAME
* table — no new repository; MAP9 (`places.image_url`) is one additive
* `PlacesRepository` method.
*/
@Injectable()
export class MapsService {
constructor(
private readonly photoCache: PlacePhotoCacheService,
@InjectRepository(AppSettings) private readonly appSettings: AppSettingsRepository,
@InjectRepository(Users) private readonly usersRepo: UsersRepository,
@InjectRepository(PlaceDetailsCache) private readonly placeDetailsCache: PlaceDetailsCacheRepository,
@InjectRepository(Places) private readonly placesRepo: PlacesRepository,
private readonly googleQuota: GoogleQuotaService,
private readonly googlePlaces: GooglePlacesClient,
private readonly osm: OsmClient,
private readonly wiki: WikimediaClient,
) {}
/** Amap autocomplete tips for the details fallback. Here rather than on the provider:
* a provider is built per request, and a pick is two requests. */
private readonly amapTips = new AmapTipStash();
private async isSettingDisabled(key: string): Promise<boolean> {
const value = await this.appSettings.getValue(key);
return value === 'false';
}
/**
* Whether the index answers on this instance.
*
* On unless TREK_PLACES_ENABLED says otherwise, because the index is the path
* we want people on and an upgrade must not quietly drop back to Nominatim,
* whose usage policy forbids what TREK was doing with it.
*
* An environment variable rather than an admin switch on purpose. This decides
* whether a search leaves the instance at all, which is a property of the
* deployment: an operator pins it in their compose file, and it cannot be
* turned off from a browser by whoever holds an admin account that day.
*/
trekPlacesEnabled(): boolean {
return readEnv().maps.trekPlacesEnabled;
}
/**
* The places in a trip's area, for the offline cache.
*
* The one call TREK makes that is not driven by something a user just typed.
* It runs when a trip is prepared for offline use, alongside the map tiles,
* and it is the reason searching a trip works on a plane: without it the
* offline instance has the trip's own places and nothing else, so "find a
* pharmacy near the hotel" has nothing to answer from.
*
* Returns null rather than throwing when the index is switched off or the
* service is unreachable, because this is a nice-to-have running in the
* background of a sync — a failure here must not fail the sync.
*/
async placesInArea(
bbox: { minLat: number; minLng: number; maxLat: number; maxLng: number },
limit?: number,
): Promise<{ results: Record<string, unknown>[]; truncated: boolean } | null> {
if (!this.trekPlacesEnabled()) return null;
try {
const area = await trekPlacesArea(bbox, limit);
return { results: area.results.map(toPlaceRecord), truncated: area.truncated };
} catch (err) {
console.warn('TREK Places area lookup failed:', (err as Error).message);
return null;
}
}
autocompleteDisabled(): Promise<boolean> {
return this.isSettingDisabled('places_autocomplete_enabled');
}
detailsDisabled(): Promise<boolean> {
return this.isSettingDisabled('places_details_enabled');
}
photosDisabled(): Promise<boolean> {
return this.isSettingDisabled('places_photos_enabled');
}
// ── Controller-facing surface ──────────────────────────────────────────────
search(
userId: number,
query: string,
lang?: string,
locationBias?: { lat: number; lng: number; radius?: number },
provider?: 'google',
): Promise<MapsSearchResult> {
return this.searchPlaces(userId, query, lang, locationBias, { googleOnly: provider === 'google' }) as Promise<MapsSearchResult>;
}
autocomplete(userId: number, input: string, lang?: string, locationBias?: LocationBias, sessionToken?: string): Promise<MapsAutocompleteResult> {
return this.autocompletePlaces(userId, input, lang, locationBias, sessionToken) as Promise<MapsAutocompleteResult>;
}
details(userId: number, placeId: string, lang?: string, sessionToken?: string): Promise<MapsPlaceDetailsResult> {
return this.getPlaceDetails(userId, placeId, lang, sessionToken) as Promise<MapsPlaceDetailsResult>;
}
detailsExpanded(userId: number, placeId: string, lang: string | undefined, refresh: boolean): Promise<MapsPlaceDetailsResult> {
return this.getPlaceDetailsExpanded(userId, placeId, lang, refresh) as Promise<MapsPlaceDetailsResult>;
}
photo(userId: number, placeId: string, lat: number, lng: number, name?: string): Promise<MapsPlacePhotoResult> {
return this.getPlacePhoto(userId, placeId, lat, lng, name) as Promise<MapsPlacePhotoResult>;
}
photoBytesKey(placeId: string): Promise<string | null> {
return this.photoCache.serveKey(placeId);
}
reverse(lat: string, lng: string, lang?: string): Promise<MapsReverseResult> {
return this.reverseGeocode(lat, lng, lang) as Promise<MapsReverseResult>;
}
resolveUrl(url: string): Promise<MapsResolveUrlResult> {
return this.resolveGoogleMapsUrl(url) as Promise<MapsResolveUrlResult>;
}
/** The logo of a brand, by its Wikidata id, as bytes (null: the pin keeps its category icon). */
brandLogo(wikidataId: string): Promise<BrandLogo | null> {
return this.wiki.brandLogo(wikidataId);
}
// POI search by category within a viewport. Never calls Google.
//
// The index answers first: it holds the same places, indexed in an R-Tree, and
// the public Overpass mirrors this used to depend on are regularly overloaded.
// A miss or a failure drops through to Overpass exactly as before.
async pois(
category: string,
bbox: { south: number; west: number; north: number; east: number },
lang?: string,
limit = 60,
): Promise<PoiSearchResult> {
// One or several categories, the way searchOverpassPois reads them. The
// corridor search sends a comma-separated list, and looking the whole string
// up as one key missed every time — so every corridor query fell through to
// Overpass, on the one path where that hurts most: a single search fans out
// over sixteen boxes, and each of those races four public mirrors.
const wanted = parsePoiCategories(category);
// Which category each Overture term belongs to, so a hit can be labelled
// with the category that actually produced it rather than with the whole
// list. The client colours and groups its markers by that field.
const categoryOfTerm = new Map<string, string>();
for (const key of wanted) {
for (const term of POI_CATEGORY_TO_TREK[key] ?? []) categoryOfTerm.set(term, key);
}
// All or nothing: a category the index has no terms for has to be answered
// by Overpass, and a mixed answer would silently drop it.
const indexKnowsAll = wanted.length > 0 && wanted.every(key => POI_CATEGORY_TO_TREK[key]?.length);
const terms = [...categoryOfTerm.keys()];
// The index matches a term as a SUBSTRING of `category` and `category_path`
// (see POI_CATEGORY_TO_TREK), so an exact lookup misses every leaf that is
// not literally a term: `italian_restaurant` is a hit for `restaurant` and
// finds nothing here. Falling through to wanted[0] then labelled it with
// whichever pill the user happened to tap first, and the corridor panel
// groups and colours on exactly that field, so a trattoria came back as a
// petrol station. Longest match wins, because `fast_food` must not lose to
// `food` when both are terms of different categories.
const labelFor = (leaf: string | null, path: string | null): string | undefined => {
const haystack = `${leaf ?? ''} ${path ?? ''}`;
let best: string | undefined;
for (const [term, key] of categoryOfTerm) {
if (!haystack.includes(term)) continue;
if (best === undefined || term.length > best.length) best = term;
}
return best === undefined ? undefined : categoryOfTerm.get(best);
};
if (this.trekPlacesEnabled() && indexKnowsAll) {
try {
const lat = (bbox.south + bbox.north) / 2;
const lng = (bbox.west + bbox.east) / 2;
// Half the diagonal, so the circle covers the viewport corners rather
// than leaving the edges of the map empty.
const reach = Math.round(
Math.hypot(
(bbox.north - bbox.south) * 111_320,
(bbox.east - bbox.west) * 111_320 * Math.cos((lat * Math.PI) / 180),
) / 2,
);
const radius = Math.min(20000, Math.max(300, reach));
// The same budget the Overpass path spends: per category, capped, so a
// mixed search does not spend the whole allowance on whichever kind
// happens to be densest.
const cap = Math.min(limit * wanted.length, POI_RESULT_CAP);
const found = await trekPlacesNearby(lat, lng, {
radius,
limit: cap,
category: terms.join(','),
});
if (found.length > 0) {
// Same shape the Overpass path produces, so the client and the map
// renderer need no branch — but named as what it is. Overture is not
// OpenStreetMap: it carries OSM among other sources under other
// licences, and this branch argues elsewhere that naming a source is
// a licence obligation. The wire contract keeps `source` an open
// string, so widening it costs nothing.
//
// One thing the index cannot do is localise. The Overpass path picks
// `name:<lang>` and falls back to `int_name`; the service has no
// language parameter at all, so a German user exploring Tokyo gets
// the Japanese primary names here. Named rather than hidden: whoever
// adds localisation upstream should find this comment.
return {
pois: found.map(p => ({
osm_id: `gers:${p.gers}`,
name: p.name,
lat: p.lat,
lng: p.lng,
// The category that produced the hit, not the list that was
// asked for: a mixed search must not label a petrol station as
// "fuel,charging,restaurant".
category: labelFor(p.category ?? null, p.categoryPath ?? null) ?? wanted[0],
poi_type: p.category ?? wanted[0],
address: p.address?.freeform ?? null,
website: normalizePlaceWebsite(p.contact?.website),
phone: p.contact?.phone ?? null,
opening_hours: p.hours?.osm ?? null,
// The index carries the chain and its Wikidata item, which is what
// the logo on the pin is looked up from — so a branch of a chain
// gets its own mark here exactly as it does on the Overpass path.
brand: p.brand?.name ?? null,
brand_wikidata: p.brand?.wikidata ?? null,
// Sockets are an OSM thing; the index has no charging fields, so
// a station answered from here reports "not stated" rather than
// claiming it offers nothing.
charging: null,
// The index has no cuisine field, so this null is the truth
// rather than a field being dropped on the way through.
cuisine: null,
source: 'trek-places' as const,
})),
source: 'trek-places' as const,
truncated: found.length >= cap,
// A wide viewport is narrowed here too, and the caller is told so
// for the same reason the Overpass path tells it.
clamped: radius < reach,
};
}
} catch (err: unknown) {
console.warn('TREK Places nearby failed, falling back:', (err as Error).message);
}
}
return this.osm.searchOverpassPois(category, bbox, lang, limit);
}
// ── API key retrieval ──────────────────────────────────────────────────────
/**
* The Places credential for this request, and where it came from.
*
* Operator env first: a per-user key would route around whatever the
* operator's endpoint counts, and unset, that branch never runs. Then the
* instance-wide value the admin panel writes, then the caller's own row.
*
* What is deliberately gone is the old third step, "any admin's key" (#1939):
* it read a stranger's credential, which server/CLAUDE.md forbids, and made
* the answer depend on who was asking — the saving admin got their own key,
* everybody else got the lowest-id admin's and a 403 from Google. The source
* is returned so a provider error can say which of the three was used.
*/
async resolveMapsKey(userId: number): Promise<{ key: string | null; source: ApiKeySource | null }> {
return resolveApiKey(this.appSettings, this.usersRepo, 'maps_api_key', userId, readEnv().maps.placesApiKey);
}
/**
* The Google key to spend, or null. Also null once today's calls reached the
* admin's daily ceiling (#1582), which makes every caller fall back to what a
* keyless install does instead of failing.
*/
async getMapsKey(userId: number): Promise<string | null> {
if (await this.googleQuota.exhausted()) return null;
return (await this.resolveMapsKey(userId)).key;
}
/** The Amap credential, resolved through the identical three-step chain. */
async resolveAmapKey(userId: number): Promise<{ key: string | null; source: ApiKeySource | null }> {
return resolveApiKey(this.appSettings, this.usersRepo, 'amap_api_key', userId, readEnv().maps.amapApiKey);
}
// ── Keyed provider selection ───────────────────────────────────────────────
/**
* Which keyed provider the admin picked, or `auto`.
*
* An unrecognised stored value degrades to `auto` rather than throwing: this
* is read on the hot path of every search, and a hand-edited settings row must
* not take place search down.
*/
async placesProviderChoice(): Promise<PlacesProviderChoice> {
const value = await this.appSettings.getValue(PLACES_PROVIDER_SETTING);
return isPlacesProviderChoice(value) ? value : 'auto';
}
/**
* Who holds the keyed slot for this request, or null for the OpenStreetMap
* stack alone. The index and OpenStreetMap are asked either way; this only
* decides what answers once they have nothing.
*
* `auto`, the default and what every install that predates Amap has, prefers
* Google. That is deliberately the incumbent rather than "the newest provider
* wins": an existing install must not silently start querying somewhere
* else, with a different bill and different results, because a release added
* a provider. An admin who wants Amap says so.
*
* Key resolution is ordered to match: under `auto` the Amap chain is only
* walked when there is no Google key, so an install on Google issues exactly
* the database reads it always did.
*/
async keyedProvider(userId: number): Promise<KeyedProvider | null> {
const choice = await this.placesProviderChoice();
if (choice === 'openstreetmap') return null;
if (choice !== 'amap') {
const google = await this.resolveMapsKey(userId);
// Past the daily ceiling (#1582) the key is spent for today: answer as if
// there were none, so `auto` moves on and OpenStreetMap fills in.
if (google.key && !(await this.googleQuota.exhausted())) return { id: 'google', key: google.key, source: google.source };
// An explicit 'google' choice with no key is not a reason to query Amap
// instead: this install is on Google and is misconfigured. OSM answers,
// the way a keyless install has always been answered.
if (choice === 'google') return null;
}
const amap = await this.resolveAmapKey(userId);
return amap.key
? { id: 'amap', provider: new AmapPlacesProvider({ key: amap.key, source: amap.source, userId }, this.amapTips) }
: null;
}
/**
* Whether this search goes to Google and nowhere else.
*
* Two ways to ask for that, both born of the same moment: the index answered
* a query with something that is not the place the traveller meant, and with
* the index and OpenStreetMap answering first, Google was never consulted as
* long as they found anything at all. The caller can send one search to
* Google (`requested`, the "search Google instead" link under the results),
* and the admin can make that the rule for every search and suggestion (the
* switch beside the key). Either way it only holds when Google holds the key
* slot: on an install without a Google key, or one that picked Amap or
* OpenStreetMap, both change nothing, and the admin panel says so.
*
* Read after the keyed provider on purpose: that lookup already walked the
* key chain, and a setting read ahead of it would shift the order of the
* app_settings reads every test of the chain stubs by position.
*/
private async googleOnly(keyed: KeyedProvider | null, requested = false): Promise<boolean> {
if (keyed?.id !== 'google') return false;
if (requested) return true;
return (await this.appSettings.getValue(PLACES_GOOGLE_ONLY_SETTING)) === 'true';
}
/**
* Whether Amap answers before the index and OpenStreetMap (#1636): only when
* the admin picked Amap outright, not when it holds the slot by default, and
* only for a search centred inside China. Outside it Amap still answers, with
* the wrong place (the Eiffel Tower lands in Macau), so the gate is the point,
* not the provider.
*/
private async amapAnswersFirst(point?: { lat: number; lng: number }): Promise<boolean> {
if (!point || !Number.isFinite(point.lat) || !Number.isFinite(point.lng)) return false;
return (await this.placesProviderChoice()) === 'amap' && !isOutsideChina(point.lat, point.lng);
}
/** The Amap provider, when Amap holds the keyed slot; null otherwise. */
async resolvePlacesProvider(userId: number): Promise<AmapPlacesProvider | null> {
const keyed = await this.keyedProvider(userId);
return keyed?.id === 'amap' ? keyed.provider : null;
}
/**
* The Amap provider for an `amap:` id, regardless of which provider is
* currently selected.
*
* Places outlive the setting. An install that ran on Amap for a year and then
* switches to Google still holds its `amap:` places, and every one of those
* keeps opening against the Amap key that is still configured. Google ids do
* not come through here at all: they take the inline Google path, which
* resolves its own key the same way.
*
* Null means nobody can resolve it: a Google id, or an Amap place on an
* install that has since dropped its Amap key. Callers treat that as a miss,
* not an error.
*/
private async providerForPlaceId(userId: number, placeId: string): Promise<AmapPlacesProvider | null> {
if (!isAmapPlaceId(placeId)) return null;
const amap = await this.resolveAmapKey(userId);
return amap.key ? new AmapPlacesProvider({ key: amap.key, source: amap.source, userId }, this.amapTips) : null;
}
/**
* A coordinate for a name that came out of an import, from whichever source
* has it.
*
* Booking imports geocode every venue and every uncoordinated endpoint in one
* request loop, up to thirty sequential lookups. Every one of those used to be
* a Nominatim call on the background lane, waiting out its throttle. The index
* answers most of them without leaving our own infrastructure and without a
* throttle at all; a street address it does not know still falls through to
* Nominatim, which is the source that resolves addresses.
*
* Never throws: an import that cannot place a hotel still imports the hotel.
*/
async geocodeQuery(query: string): Promise<{ lat: number; lng: number } | null> {
if (this.trekPlacesEnabled()) {
try {
const found = await trekPlacesSearch(query, { limit: 1 });
const hit = found[0];
if (hit && Number.isFinite(hit.lat) && Number.isFinite(hit.lng)) {
return { lat: hit.lat, lng: hit.lng };
}
} catch (err: unknown) {
console.warn('TREK Places geocode failed, falling back:', (err as Error).message);
}
}
const hit = (await this.osm.searchNominatim(query, undefined, 'background'))[0];
return hit?.lat != null && hit?.lng != null ? { lat: hit.lat, lng: hit.lng } : null;
}
// ── Search places (Google or Nominatim fallback) ───────────────────────────
async searchPlaces(
userId: number,
query: string,
lang?: string,
locationBias?: { lat: number; lng: number; radius?: number },
opts: { googleIdentityOnly?: boolean; googleOnly?: boolean } = {},
): Promise<{ places: Record<string, unknown>[]; source: string }> {
const keyed = await this.keyedProvider(userId);
const { key: apiKey, source: keySource } = keyed?.id === 'google' ? keyed : { key: null, source: null };
// The TREK index answers first, whether or not a Google key exists. It is
// the only source here that may be stored, works offline as a country
// package, and costs the caller nothing; a key buys ratings and photos on
// top of it, not a better search.
//
// It never throws upward. A search that used to work must keep working when
// the service is slow or down, so a failure drops through to exactly what
// this method did before.
//
// `googleIdentityOnly` skips it. One caller does not want the best answer,
// it wants a Google id: list-import enrichment (#886) exists to attach a
// `google_place_id` to a bare imported pin, and `pickEnrichmentMatch`
// discards every candidate that has none. Index and OpenStreetMap records
// both carry `google_place_id: null`, so answering that caller from them
// returns matches it must throw away, and the import silently stays
// unenriched on an instance that pays for a key. It changes nothing for an
// instance without one: enrichment could never resolve anything there
// either, before this branch existed or after.
// Set once the OpenStreetMap half below has run, so the fallback does not ask
// the same question twice. `null` means it never ran.
let osmAnswer: Record<string, unknown>[] | null = null;
// Amap first where the admin picked it and the search is about China (#1636):
// there the index and OpenStreetMap are thin and Amap is the map people use.
// Its answer is kept, so the Amap slot further down never pays for the same
// question twice. A failure drops through to the usual order.
let amapAnswer: Record<string, unknown>[] | null = null;
if (keyed?.id === 'amap' && (await this.amapAnswersFirst(locationBias))) {
amapAnswer = await keyed.provider.searchText(query, lang, locationBias).catch((err: unknown) => {
console.warn('Amap search failed, falling back:', (err as Error).message);
return null;
});
if (amapAnswer && amapAnswer.length > 0) return { places: amapAnswer, source: 'amap' };
}
// A search sent to Google on purpose, or the admin's "Google only" switch,
// skips the pair the same way: the search then reads exactly as it did
// before 4.3.0 on an install with a key.
if (this.trekPlacesEnabled() && !(opts.googleIdentityOnly && apiKey) && !(await this.googleOnly(keyed, opts.googleOnly))) {
// Both at once. The index is a dataset of businesses and is very good
// at those; OpenStreetMap is where the temples, bridges, riverside
// walks and viewpoints are, and a travel search asks for those
// constantly. Concurrently, so the pair costs the slower one rather
// than the sum. This is the explicit search, not the keystroke path
// Nominatim's policy rules out.
//
// Each side catches its own failure. A rejection reaching Promise.all
// would throw away the answer the other side had already produced — and
// the index refusing a query is ordinary traffic, not an outage: a common
// single word without coordinates is turned down upstream as too
// expensive. That used to discard ten good OpenStreetMap results and ask
// Nominatim the same question a second time, behind its own 1.1 s
// process-wide throttle.
const [found, osm] = await Promise.all([
trekPlacesSearch(query, {
lat: locationBias?.lat,
lng: locationBias?.lng,
limit: 10,
}).then(indexHitsOnly).catch((err: unknown) => {
console.warn('TREK Places search failed, falling back:', (err as Error).message);
return [] as TrekPlace[];
}),
this.osm.searchNominatim(query, lang, 'interactive', locationBias).catch((err: unknown) => {
console.warn('OpenStreetMap search failed, index only:', (err as Error).message);
return [] as Record<string, unknown>[];
}),
]);
osmAnswer = osm;
const places = mergeSearchResults(found.map(toPlaceRecord), osm);
if (places.length > 0) {
// Names the sources that actually contributed, not the ones that were
// asked. Either side can come back empty — the index turns down a common
// single word without coordinates, and OpenStreetMap can be down — and
// the search log writes this into the corpus a candidate index is later
// scored against, so a list that is entirely OpenStreetMap must not be
// recorded as though the index had a hand in it.
const source = found.length > 0
? (osm.length > 0 ? 'trek-places+openstreetmap' : 'trek-places')
: 'openstreetmap';
return { places, source };
}
}
// Amap in the slot Google otherwise holds: asked only once the index and
// OpenStreetMap came back empty, exactly like the Google call below.
if (keyed?.id === 'amap') {
const places = amapAnswer ?? (await keyed.provider.searchText(query, lang, locationBias));
return { places, source: 'amap' };
}
if (!apiKey) {
// Reuse what OpenStreetMap already said rather than asking again. The
// first call carried a viewbox with bounded=0, which orders results
// without changing which ones exist, so a second call can only return the
// same empty list a throttle-interval later.
const places = osmAnswer ?? (await this.osm.searchNominatim(query, lang));
return { places, source: 'openstreetmap' };
}
// Google in its slot: the index and OpenStreetMap came back empty, or this
// search was sent to Google alone. Its errors carry Google's own status.
const places = await this.googlePlaces.provider({ key: apiKey, source: keySource, userId }).searchText(query, lang, locationBias);
return { places, source: 'google' };
}
// ── Places near a point (#976) ─────────────────────────────────────────────
/**
* Places of any kind around a coordinate, nearest first, in the shape a search
* answers with plus `distance_m`. For "what is here" where no name was typed:
* a photo's location, a pin on the map.
*
* Same order as the search: the TREK index first, free and storable; Google
* only with a key and only when the index has nothing (or the admin set Google
* only); OpenStreetMap otherwise. Google bills per call, so every answer is
* cached for a while and the circle and count are capped by the contract.
*/
async nearbyPlaces(
userId: number,
lat: number,
lng: number,
opts: { radius?: number; limit?: number; lang?: string } = {},
): Promise<{ places: Record<string, unknown>[]; source: string }> {
const radius = opts.radius ?? NEARBY_DEFAULT_RADIUS_M;
const limit = opts.limit ?? NEARBY_DEFAULT_LIMIT;
const lang = toApiLang(opts.lang);
const key = nearbyCacheKey(lat, lng, radius, limit, lang);
const cached = NEARBY_CACHE.get(key);
if (cached && Date.now() - cached.at < NEARBY_CACHE_TTL_MS) return cached.value;
const value = await this.lookUpNearby(userId, { lat, lng }, radius, limit, lang);
if (NEARBY_CACHE.size >= NEARBY_CACHE_MAX) NEARBY_CACHE.delete(NEARBY_CACHE.keys().next().value as string);
NEARBY_CACHE.set(key, { at: Date.now(), value });
return value;
}
private async lookUpNearby(
userId: number,
origin: { lat: number; lng: number },
radius: number,
limit: number,
lang: string,
): Promise<{ places: Record<string, unknown>[]; source: string }> {
const keyed = await this.keyedProvider(userId);
if (this.trekPlacesEnabled() && !(await this.googleOnly(keyed))) {
// Never throws upward, like the search: a slow index drops to the next source.
const found = await trekPlacesNearby(origin.lat, origin.lng, { radius, limit }).catch((err: unknown) => {
console.warn('TREK Places nearby failed, falling back:', (err as Error).message);
return [];
});
if (found.length > 0) return { places: nearestFirst(found.map(toPlaceRecord), origin, limit), source: 'trek-places' };
}
if (keyed?.id === 'google') {
const google = this.googlePlaces.provider({ key: keyed.key, source: keyed.source, userId });
const places = await google.searchNearby(origin, radius, limit, lang);
return { places: nearestFirst(places, origin, limit), source: 'google' };
}
const osmLang = lang.split('-')[0].toLowerCase();
return { places: await this.osm.nearby(origin, radius, limit, osmLang), source: 'openstreetmap' };
}
// ── Autocomplete (Google or Nominatim fallback) ────────────────────────────
async autocompletePlaces(
userId: number,
input: string,
lang?: string,
locationBias?: { low: { lat: number; lng: number }; high: { lat: number; lng: number } },
sessionToken?: string,
): Promise<MapsAutocompleteResult> {
const keyed = await this.keyedProvider(userId);
const { key: apiKey, source: keySource } = keyed?.id === 'google' ? keyed : { key: null, source: null };
// This is the path that mattered most. Nominatim's usage policy names
// autocomplete as unacceptable use in its own words, regardless of rate,
// and the whole TREK fleet shares one User-Agent there: one abusive install
// could get every instance blocked at once. The index removes that.
//
// Same contract as search: never throws upward, falls through to what this
// method did before. The admin's "Google only" switch skips the index here
// too, so the suggestions and the search agree on where they come from.
// Amap first inside China when the admin picked it, as in the search (#1636).
let amapTips: MapsAutocompleteResult['suggestions'] | null = null;
const boxCentre = locationBias
? { lat: (locationBias.low.lat + locationBias.high.lat) / 2, lng: (locationBias.low.lng + locationBias.high.lng) / 2 }
: undefined;
if (keyed?.id === 'amap' && (await this.amapAnswersFirst(boxCentre))) {
amapTips = await keyed.provider.autocomplete(input, lang, locationBias).catch((err: unknown) => {
console.warn('Amap autocomplete failed, falling back:', (err as Error).message);
return null;
});
if (amapTips && amapTips.length > 0) return { suggestions: amapTips, source: 'amap' };
}
if (this.trekPlacesEnabled() && !(await this.googleOnly(keyed))) {
try {
const centre = locationBias
? {
lat: (locationBias.low.lat + locationBias.high.lat) / 2,
lng: (locationBias.low.lng + locationBias.high.lng) / 2,
}
: undefined;
// Both layers here, unlike the explicit search above. That path asks
// Nominatim in parallel and would get the same OpenStreetMap places
// twice; this one asks nobody else, because Nominatim's usage policy
// names autocomplete as unacceptable use. So the layer is not a second
// opinion here, it is the only place the missing names live.
//
// Measured on the case that surfaced it: "Tokio station" typed from
// Tokyo returned a weigh station in Ritzville and a station in Mexico
// from the index alone, and "Tokio Hauptbahnhof" in second place with
// the layer. The index holds businesses; stations, temples and bridges
// are in OpenStreetMap, and so is every exonym a traveller types.
const found = await trekPlacesSearch(input, {
lat: centre?.lat,
lng: centre?.lng,
limit: 8,
sources: 'index,osm',
});
if (found.length > 0) {
return {
suggestions: found.map(p =>
isOsmHit(p)
? {
// The service's own id form, translated into the one this
// file already resolves. Leaving it as `osm:node/123` would
// hand the client an id getPlaceDetails does not know, and
// the failure would land after the user had picked it.
placeId: osmPlaceId(p),
mainText: p.name,
// The layer carries no address. The local name is what the
// place is called on the spot, which is more use under a
// translated label than an empty line.
secondaryText: p.local_name && p.local_name !== p.name ? p.local_name : '',
// Per row, because this list is two indexes interleaved.
// The name above the list says `trek-places`, which is true
// of the call and false of half the rows in it — the layer
// is OpenStreetMap, and a reader deciding whether to trust
// a suggestion is asking exactly that.
source: 'openstreetmap',
// Both indexes hand these over with the row. Carried rather
// than dropped so picking a suggestion has something to fall
// back on when the details lookup cannot answer.
lat: p.lat,
lng: p.lng,
}
: {
placeId: `gers:${p.gers}`,
mainText: p.name,
secondaryText: [p.address?.locality, p.address?.country].filter(Boolean).join(', '),
source: 'trek-places',
lat: p.lat,
lng: p.lng,
},
),
source: 'trek-places',
};
}
} catch (err: unknown) {
console.warn('TREK Places autocomplete failed, falling back:', (err as Error).message);
}
}
if (keyed?.id === 'amap') {
const suggestions = amapTips ?? (await keyed.provider.autocomplete(input, lang, locationBias));
return { suggestions, source: 'amap' };
}
if (!apiKey) {
return this.osm.autocompleteNominatim(input, lang);
}
const google = this.googlePlaces.provider({ key: apiKey, source: keySource, userId });
const suggestions = await google.autocomplete(input, lang, locationBias, sessionToken);
return { suggestions, source: 'google' };
}
// ── Place details (Google or OSM) ──────────────────────────────────────────
async getPlaceDetails(
userId: number,
placeId: string,
lang?: string,
sessionToken?: string,
): Promise<{ place: Record<string, unknown> | null }> {
// A place picked out of the TREK index. Checked BEFORE the generic
// colon branch below, which would otherwise read "gers" as an OSM type
// and ask Overpass for an element that does not exist.
if (placeId.startsWith('gers:')) {
// The switch is a deployment property, and off means nothing leaves for
// the index: a place saved while it was on is still opened from what the
// trip holds, not looked up again.
if (!this.trekPlacesEnabled()) return { place: null };
const found = await trekPlacesById(placeId.slice('gers:'.length)).catch(() => null);
if (!found) return { place: null };
// What the index does not have, the free sources still do: cuisine,
// wheelchair access, a menu link. OpenStreetMap has all of them for the
// same building, and without this the details a user saw while adding the
// place disappeared from its card afterwards, which reads like data loss.
//
// Matched by name and coordinate, with the same two gates
// resolveOsmIdentity applies everywhere: within range, and sharing a
// substantial word of the name. A confident description of the building
// next door is worse than none.
const osm = await this.osm.resolveOsmIdentity(found.name, found.lat, found.lng, {
lang,
maxDistanceM: 150,
}).catch(() => null);
const record = toPlaceRecord(found);
// Hours the index read off the operator's own site, run through the same
// expansion OSM's go through — the client reads a list of weekday lines,
// not the raw syntax, and handing it two shapes for one field would be a
// bug on every card that shows it.
//
// Measured across seven countries, OpenStreetMap has hours for 27.5
// percent of gastronomy; this covers part of the rest. It is the
// fallback, not the first choice: an OSM entry describes this exact
// object and gets corrected by people who walked past, where a chain's
// website often carries one set of hours for every branch.
const fromSite =
typeof found.hours?.osm === 'string' ? buildOsmDetails({ opening_hours: found.hours.osm }, '', '') : null;
if (!osm) {
return {
place: fromSite?.opening_hours
? {
...record,
opening_hours: fromSite.opening_hours,
open_now: fromSite.open_now,
opening_periods: fromSite.opening_periods,
}
: record,
};
}
const osmDetails = buildOsmDetails(osm.tags, '', '');
const hoursFrom = osmDetails.opening_hours ? osmDetails : fromSite;
return {
place: {
// Index first: its name, coordinate and contact details are the ones
// the user picked. OSM only fills what is still missing.
...osmDetails,
...record,
opening_hours: hoursFrom?.opening_hours ?? null,
open_now: hoursFrom?.open_now ?? null,
opening_periods: hoursFrom?.opening_periods ?? null,
website: record.website ?? osmDetails.website ?? null,
phone: record.phone ?? osmDetails.phone ?? null,
osm_id: placeId,
source: 'trek-places',
},
};
}
// An Amap id is `amap:<poiid>` and so carries a colon too. Before the OSM
// branch, which would otherwise send "amap" to Overpass as an element type
// and answer every Chinese place with an empty record.
if (isAmapPlaceId(placeId)) return this.amapDetails(userId, placeId, lang);
// OSM details: placeId is "node:123456" or "way:123456" etc.
if (placeId.includes(':')) {
// Only an element type with a numeric id is looked up. The id is written
// into an Overpass query and a Nominatim lookup as it came in, and nothing
// else with a colon in it (a legacy image URL, a coordinate pseudo-id) has
// a details source: answering those with an empty record cost two
// requests that could not succeed, and let anything after the colon be
// sent as a query of its own.
if (!OSM_PLACE_ID.test(placeId)) return { place: null };
const [osmType, osmId] = placeId.split(':');
// buildOsmDetails never yields name/address/coordinates — Nominatim is
// always the source for those (Overpass contributes the tag-derived rest).
const [element, nominatim] = await Promise.all([
this.osm.fetchOverpassDetails(osmType, osmId),
this.osm.lookupNominatim(osmType, osmId, lang),
]);
// Overpass has the fuller tag set and wins where both answer, but it is
// also the one that goes down — overpass-api.de is regularly overloaded.
// Nominatim's extratags carry the wikidata/wikipedia/commons tags too, so
// a place keeps its pictures and its description when Overpass times out
// instead of falling back to "photographed within 300m".
const details = buildOsmDetails(
{ ...(nominatim?.extratags ?? {}), ...(element?.tags ?? {}) },
osmType,
osmId,
);
return {
place: {
...details,
name: nominatim?.name || element?.tags?.name || '',
address: nominatim?.address || '',
lat: nominatim?.lat ?? null,
lng: nominatim?.lng ?? null,
osm_id: placeId,
},
};
}
// Google details
// 'en' default, aligned with search/autocomplete and the MCP tools' ?? 'en'
// (the 'de' the legacy service defaulted to was a development leftover;
// cache rows keyed 'de' for lang-less callers go cold once — 7-day TTL).
const langKey = toApiLang(lang);
const apiKey = await this.getMapsKey(userId);
// No key means no way to resolve a Google id: they have no OpenStreetMap
// equivalent to fall back to. That is an empty result, not a client error.
// Search and autocomplete already answer their keyless case with the OSM
// stack; this used to be the one place that threw instead, which turned an
// instance without a key into a stream of 400s whenever an older Google
// place was opened. Callers already treat a null place as a miss.
if (!apiKey) return { place: null };
// Check DB cache first (lean mask, expanded=0) — 7-day TTL
const DETAILS_TTL = 7 * 24 * 60 * 60 * 1000;
const cached = await this.placeDetailsCache.findEntry(placeId, langKey, 0);
if (cached && Date.now() - cached.fetched_at < DETAILS_TTL) return { place: cachedDetails(cached.payload_json) };
// The details call closes the autocomplete session this lookup belongs to,
// so Google bills the search once instead of per keystroke. A cache hit
// above never reaches here, which is billing-neutral: an unclosed session
// is charged as a plain autocomplete session.
const place = await this.googlePlaces.provider({ key: apiKey, source: null, userId }).placeDetails(placeId, lang, sessionToken);
try {
await this.placeDetailsCache.upsertEntry({ place_id: placeId, lang: langKey, expanded: 0, payload_json: JSON.stringify(place), fetched_at: Date.now() });
} catch (dbErr) {
console.error('Failed to cache place details:', dbErr);
}
return { place };
}
/**
* The Amap half of getPlaceDetails, behind the same cache the Google half
* uses. Keyed by place_id, and an Amap id carries its `amap:` prefix, so the
* two providers' rows cannot collide.
*
* No key for the id is an empty result, not a client error, for the same
* reason the Google half answers its keyless case that way: an Amap place
* opened on an install that has since dropped its Amap key is a miss.
*/
private async amapDetails(
userId: number,
placeId: string,
lang?: string,
): Promise<{ place: Record<string, unknown> | null }> {
const provider = await this.providerForPlaceId(userId, placeId);
if (!provider) return { place: null };
const langKey = toApiLang(lang);
const DETAILS_TTL = 7 * 24 * 60 * 60 * 1000;
const cached = await this.placeDetailsCache.findEntry(placeId, langKey, 0);
if (cached && Date.now() - cached.fetched_at < DETAILS_TTL) return { place: cachedDetails(cached.payload_json) };
const place = await provider.placeDetails(placeId, lang);
if (!place) return { place: null };
try {
await this.placeDetailsCache.upsertEntry({ place_id: placeId, lang: langKey, expanded: 0, payload_json: JSON.stringify(place), fetched_at: Date.now() });
} catch (dbErr) {
console.error('Failed to cache place details:', dbErr);
}
return { place };
}
async getPlaceDetailsExpanded(
userId: number,
placeId: string,
lang?: string,
refresh = false,
): Promise<{ place: Record<string, unknown> | null }> {
// Reviews and the editorial summary only exist at Google, but the id does not
// have to be a Google one — the client sends whatever the place carries. OSM ids
// keep the details they do have (Overpass, via the plain lookup); coordinate
// pseudo-ids and legacy image URLs have no details source at all. Neither may be
// forwarded to Google, which bills the 400 INVALID_ARGUMENT it answers with.
//
// Index ids degrade the same way, for the same reason: the plain lookup has a
// whole record for them — name, address, contact, hours — and only the reviews
// and the editorial summary are Google's to add. Answering `expand=1` with a
// null while `expand=0` answers in full would make the richer request the
// poorer one. An Amap id has no richer tier either, so it takes the plain
// lookup as well.
if (!isGooglePlaceId(placeId)) {
return OSM_PLACE_ID.test(placeId) || placeId.startsWith('gers:') || isAmapPlaceId(placeId)
? this.getPlaceDetails(userId, placeId, lang)
: { place: null };
}
const langKey = toApiLang(lang); // 'en' default — see getPlaceDetails
const apiKey = await this.getMapsKey(userId);
// Same as the lean lookup above: an empty result, not a client error.
if (!apiKey) return { place: null };
// Check DB cache for expanded result
if (!refresh) {
const cached = await this.placeDetailsCache.findEntry(placeId, langKey, 1);
if (cached) return { place: cachedDetails(cached.payload_json) };
}
const place = await this.googlePlaces.provider({ key: apiKey, source: null, userId }).placeDetailsExpanded(placeId, lang);
try {
await this.placeDetailsCache.upsertEntry({ place_id: placeId, lang: langKey, expanded: 1, payload_json: JSON.stringify(place), fetched_at: Date.now() });
} catch (dbErr) {
console.error('Failed to cache expanded place details:', dbErr);
}
return { place };
}
// ── Place photo (Google or Wikimedia, disk-cached) ─────────────────────────
async getPlacePhoto(
userId: number,
placeId: string,
lat: number,
lng: number,
name?: string,
): Promise<{ photoUrl: string | null; attribution: string | null }> {
// Disk cache hit — serve immediately, no Google call
const diskHit = await this.photoCache.get(placeId);
if (diskHit) return { photoUrl: diskHit.photoUrl, attribution: diskHit.attribution };
// "No photo for this place" is an empty result, not a missing resource: a trip
// view asks for one photo per place, so answering each miss with a 404 makes a
// normal itinerary render look like a 404 scan to fail2ban/CrowdSec and gets
// the user's IP banned. Every miss below returns photoUrl: null instead — the
// same shape the photos kill-switch already returns.
const noPhoto = { photoUrl: null, attribution: null };
// Recent miss — don't hammer the API
if (await this.photoCache.getErrored(placeId)) return noPhoto;
// Deduplicate concurrent requests for the same placeId
const existing = this.photoCache.getInFlight(placeId);
if (existing !== undefined) {
const result = await existing;
if (!result) return noPhoto;
return { photoUrl: `/api/maps/place-photo/${encodeURIComponent(placeId)}/bytes`, attribution: result.attribution };
}
// Tells the two empty outcomes apart for the negative cache below: a place that
// has no photo anywhere is worth remembering for a day, a provider that refused
// or timed out only for a few minutes.
let providerFailed = false;
const fetchPromise = (async (): Promise<{ attribution: string | null } | null> => {
return withPhotoFetchSlot(async () => {
const apiKey = await this.getMapsKey(userId);
// Coordinate-based Wikipedia/Wikimedia lookup. Used for coordinate-only
// (right-click) places and as a fallback when a Google place yields no photo,
// so a place added via search still gets a marker image when Google returns
// nothing. Returns null (without marking an error) so the caller decides.
const fetchWikimediaFallback = async (): Promise<{ attribution: string | null } | null> => {
const outcome = await this.wiki.downloadPhoto(lat, lng, name);
if (outcome.kind === 'failed') providerFailed = true;
if (outcome.kind !== 'photo') return null;
try {
const cached = await this.photoCache.put(placeId, outcome.bytes, outcome.attribution);
return { attribution: cached.attribution };
} catch {
providerFailed = true;
return null;
}
};
// Google Places photo for a Google place_id. Returns null on any miss — no
// key, request rejected, no photos, or a failed media download — so the
// caller can fall back to Wikimedia; the misses that were Google's fault
// flag providerFailed on the way out.
const fetchGooglePhoto = async (): Promise<{ attribution: string | null } | null> => {
if (!apiKey) return null;
const outcome = await this.googlePlaces.firstPhoto(placeId, apiKey);
if (outcome.kind === 'failed') providerFailed = true;
if (outcome.kind !== 'photo') return null;
const cached = await this.photoCache.put(placeId, outcome.bytes, outcome.attribution);
// Persist stable proxy URL to database
try {
await this.placesRepo.setImageUrlIfUnset(placeId, cached.photoUrl);
} catch (dbErr) {
console.error('Failed to persist photo URL to database:', dbErr);
}
return { attribution: outcome.attribution };
};
// Prefer the Google photo (higher quality); if Google yields nothing, fall
// back to the same coordinate-based Wikipedia/OSM lookup that right-click
// places use. Ids Google cannot resolve skip it entirely.
if (isGooglePlaceId(placeId)) {
const googlePhoto = await fetchGooglePhoto();
if (googlePhoto) return googlePhoto;
}
const fallback = await fetchWikimediaFallback();
if (fallback) return fallback;
await this.photoCache.markError(placeId, providerFailed ? 'provider-error' : 'no-photo');
return null;
});
})();
this.photoCache.setInFlight(placeId, fetchPromise);
const result = await fetchPromise;
if (!result) return noPhoto;
return { photoUrl: `/api/maps/place-photo/${encodeURIComponent(placeId)}/bytes`, attribution: result.attribution };
}
// ── Reverse geocoding ──────────────────────────────────────────────────────
async reverseGeocode(
lat: string,
lng: string,
lang?: string,
opts?: { lane?: GeoLane; timeoutMs?: number; locality?: boolean },
): Promise<{ name: string | null; address: string | null }> {
// Amap answers first when it holds the keyed slot, and only for a point it
// can possibly know: outside its box the call would cost a round trip to
// come back empty before Nominatim is asked anyway. Resolved at userId 0,
// because most callers here have no person behind them (a booking import,
// an Atlas tile, a right-click on a shared map): the chain stops at the
// operator env var and the instance-wide row, and nobody's personal key is
// read on somebody else's behalf (#1939). Nominatim stays the fallback, so
// an Amap outage does not take a right-click down with it.
const amap = await this.resolvePlacesProvider(0);
if (amap) {
const latNum = Number.parseFloat(lat);
const lngNum = Number.parseFloat(lng);
if (Number.isFinite(latNum) && Number.isFinite(lngNum) && !isOutsideChina(latNum, lngNum)) {
try {
const answer = await amap.reverse(latNum, lngNum, lang);
if (answer) return answer;
} catch (err) {
console.error('[Maps] amap reverse geocode failed, falling back to Nominatim:', (err as Error).message);
}
}
}
return this.osm.reverse(lat, lng, lang, opts);
}
// ── Resolve Google Maps URL ────────────────────────────────────────────────
async resolveGoogleMapsUrl(
url: string,
): Promise<{ lat: number; lng: number; name: string | null; address: string | null; google_ftid: string | null }> {
let resolvedUrl = url;
// Extract coordinates from a string (URL or page body). Google Maps encodes
// them several ways: /@lat,lng,zoom · !3dlat!4dlng (map data param) · ?q=/?ll=.
const extractCoords = (s: string): { lat: number; lng: number } | null => {
const at = s.match(/@(-?\d+\.\d+),(-?\d+\.\d+)/);
if (at) return { lat: Number.parseFloat(at[1]), lng: Number.parseFloat(at[2]) };
const data = s.match(/!3d(-?\d+\.\d+)!4d(-?\d+\.\d+)/);
if (data) return { lat: Number.parseFloat(data[1]), lng: Number.parseFloat(data[2]) };
const q = s.match(/[?&](?:q|ll)=(-?\d+\.\d+),(-?\d+\.\d+)/);
if (q) return { lat: Number.parseFloat(q[1]), lng: Number.parseFloat(q[2]) };
return null;
};
const followRedirects = async (target: string, init?: RequestInit): Promise<Response> => {
try {
return await safeFetchFollow(
target,
{ signal: AbortSignal.timeout(10000), ...init },
{ bypassInternalIpAllowed: true },
);
} catch (err) {
if (err instanceof SsrfBlockedError) {
throw Object.assign(new Error('URL blocked by SSRF check'), { status: 403 });
}
throw err;
}
};
// Follow redirects for short URLs (goo.gl, maps.app.goo.gl) and for Google Maps
// URLs that carry no inline coordinates — e.g. ?cid= links (the format
// get_place_details returns) and "Share"-button links. The redirect target
// usually carries the !3d!4d data param we can then parse. Redirects are
// followed manually so every hop is SSRF-re-checked.
const parsed = new URL(url);
const isShort = GOOGLE_SHORT_HOSTS.includes(parsed.hostname) || AMAP_SHORT_HOSTS.includes(parsed.hostname);
const isGoogleMaps = isGoogleMapsHost(parsed.hostname);
if (isShort || (isGoogleMaps && !extractCoords(url))) {
resolvedUrl = (await followRedirects(url)).url || resolvedUrl;
}
let resolvedHost = '';
try { resolvedHost = new URL(resolvedUrl).hostname; } catch { /* keep the empty host, both host branches are skipped */ }
// Amap links first, and on their own: they spell the coordinate `lng,lat`
// in GCJ-02, which the Google patterns below would read as a WGS-84
// `lat,lng` and put a Shanghai restaurant in the East China Sea.
// parseAmapUrl owns both the ordering and the datum conversion.
if (isAmapHost(resolvedHost)) {
const amap = parseAmapUrl(resolvedUrl);
// A POI page without a coordinate would need a keyed detail lookup, and
// this method has no user to resolve a key for: the same answer a Google
// page without coordinates gets.
if (!amap || !Number.isFinite(amap.lat) || !Number.isFinite(amap.lng)) {
throw Object.assign(new Error('Could not extract coordinates from URL'), { status: 400 });
}
const reverse = await this.reverseGeocode(String(amap.lat), String(amap.lng), undefined, { timeoutMs: 8000 });
return { lat: amap.lat, lng: amap.lng, name: amap.name || reverse.name, address: reverse.address, google_ftid: null };
}
let coords = extractCoords(resolvedUrl);
// Still nothing (e.g. a cid page whose final URL lacks coordinates): fetch the
// page body once and parse the coordinates out of the embedded map data.
// Only Google's own pages get read; the resolved host is what counts, so a
// short link that lands on maps.google.com still qualifies.
if (!coords && isGoogleMapsHost(resolvedHost)) {
try {
const pageRes = await followRedirects(resolvedUrl, {
headers: { 'User-Agent': UA },
});
if (exceedsDeclaredLength(pageRes, MAX_MAPS_PAGE_BYTES)) {
// Nothing here will read it, and an unread body keeps its socket.
discardBody(pageRes);
} else {
// The map data sits near the top of the document, so a truncated read
// still finds the coordinates; an oversized page degrades to the same
// 400 an unparseable one already produced.
const { text } = await readCappedText(pageRes, MAX_MAPS_PAGE_BYTES);
coords = extractCoords(text);
}
} catch (err) {
if ((err as { status?: number })?.status === 403) throw err; // SSRF block, surface it
// Otherwise fall through to the not-found error below.
}
}
// Extract place name from URL path: /place/Place+Name/@...
let placeName: string | null = null;
const placeMatch = resolvedUrl.match(/\/place\/([^/@]+)/);
if (placeMatch) {
placeName = decodeURIComponent(placeMatch[1].replaceAll(/\+/g, ' '));
}
if (!coords || Number.isNaN(coords.lat) || Number.isNaN(coords.lng)) {
throw Object.assign(new Error('Could not extract coordinates from URL'), { status: 400 });
}
const { lat, lng } = coords;
// Reverse geocode to get address. A non-ok answer (Nominatim 5xx/429) must
// not fail the whole resolution — the coordinates are already extracted, so
// fall back to the URL-derived name and a null address.
const nominatim = await this.osm.reverseRaw(lat, lng);
const name = placeName || nominatim.name || nominatim.address?.tourism || nominatim.address?.building || null;
const address = nominatim.display_name || null;
return { lat, lng, name, address, google_ftid: googleFtidFromMapsUrl(resolvedUrl) };
}
}