mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
maps: OSM details carry the values installations published for the element
A place found through Photon or Overpass, a relation or a way, is no row of the TREK index, so the community values the index lays over its own rows never reached it. Opening its details now asks /v1/community for the element and lays what is published over the record, with the version, where the TREK index is in use. No answer leaves the details as they were.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { trekCommunityByKeys, type TrekCommunityValues } from './trek-places.client';
|
||||
import { normalizePlaceWebsite } from '@trek/shared';
|
||||
|
||||
/**
|
||||
* Community values for places TREK did not find in its own index: a relation
|
||||
* Photon returned, a way from Overpass. The index rows carry them already
|
||||
* (the service lays them over its own answers); these places get them from
|
||||
* /v1/community, so a value one installation sent shows on the next open of
|
||||
* the same place everywhere.
|
||||
*/
|
||||
|
||||
/** The service's key of an OSM details id: `relation:3389505` is `osm:relation/3389505`. */
|
||||
export function communityKeyOf(placeId: string): string | null {
|
||||
const match = /^(node|way|relation):(\d+)$/.exec(placeId);
|
||||
return match ? `osm:${match[1]}/${match[2]}` : null;
|
||||
}
|
||||
|
||||
/** The details record with what the community published laid over it; untouched where nothing was. */
|
||||
export function withCommunityValues(
|
||||
details: Record<string, unknown>,
|
||||
values: TrekCommunityValues | undefined,
|
||||
): Record<string, unknown> {
|
||||
if (!values?.community) return details;
|
||||
const out: Record<string, unknown> = { ...details };
|
||||
const website = normalizePlaceWebsite(values.contact?.website);
|
||||
if (website) out.website = website;
|
||||
if (values.contact?.phone) out.phone = values.contact.phone;
|
||||
if (values.contact?.email) out.email = values.contact.email;
|
||||
if (values.hours?.osm) out.opening_hours = values.hours.osm;
|
||||
if (values.description?.text) out.description = values.description.text;
|
||||
if (typeof values.community.version === 'number') out.community_version = values.community.version;
|
||||
return out;
|
||||
}
|
||||
|
||||
/** OSM details as Overpass and Photon gave them, with the community's values for the same element. */
|
||||
export async function osmDetailsWithCommunity(
|
||||
placeId: string,
|
||||
details: Record<string, unknown> | null,
|
||||
): Promise<Record<string, unknown> | null> {
|
||||
const key = communityKeyOf(placeId);
|
||||
if (!details || !key) return details;
|
||||
const published = await trekCommunityByKeys([key]);
|
||||
return withCommunityValues(details, published[key]);
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
import { PlaceDetailsCache } from '../../db/entities/PlaceDetailsCache.entity';
|
||||
import type { PlaceDetailsCacheRepository } from '../../db/repositories/PlaceDetailsCache.repository';
|
||||
import { osmDetailsWithCommunity } from './community-overlay';
|
||||
import { indexPlaceDetails } from './maps-index.helpers';
|
||||
import { toApiLang, isGooglePlaceId, OSM_PLACE_ID } from './maps.helpers';
|
||||
import { PlacesProviderSelector } from './places-provider.selector';
|
||||
@@ -84,7 +85,11 @@ export class PlaceDetailsResolver {
|
||||
if (isAmapPlaceId(placeId)) return this.amapDetails(userId, placeId, lang);
|
||||
|
||||
// OSM details: placeId is "node:123456" or "way:123456" etc.
|
||||
if (placeId.includes(':')) return { place: await this.osm.elementDetails(placeId, lang) };
|
||||
if (placeId.includes(':')) {
|
||||
const place = await this.osm.elementDetails(placeId, lang);
|
||||
// What installations published for this element, like the index rows carry it.
|
||||
return { place: indexEnabled ? await osmDetailsWithCommunity(placeId, place) : place };
|
||||
}
|
||||
|
||||
// Google details
|
||||
// 'en' default, aligned with search/autocomplete and the MCP tools' ?? 'en'
|
||||
|
||||
@@ -637,6 +637,31 @@ export async function trekPlacesById(gers: string, opts: { lang?: string } = {})
|
||||
}
|
||||
}
|
||||
|
||||
/** What the service publishes for a place found elsewhere (Photon, Overpass): the overlay of its own rows. */
|
||||
export interface TrekCommunityValues {
|
||||
contact?: { website?: string | null; phone?: string | null; email?: string | null } | null;
|
||||
hours?: { osm?: string | null } | null;
|
||||
description?: { text?: string | null } | null;
|
||||
community?: { version?: number; fields?: string[] } | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The published community values of places that are no rows of the index, by
|
||||
* key (`osm:relation/3389505`). Nothing published, or the service not
|
||||
* answering, is an empty map: the details are shown as they are.
|
||||
*/
|
||||
export async function trekCommunityByKeys(keys: string[]): Promise<Record<string, TrekCommunityValues>> {
|
||||
if (keys.length === 0) return {};
|
||||
try {
|
||||
const body = await getJson<{ places?: Record<string, TrekCommunityValues> }>('/v1/community', {
|
||||
keys: keys.slice(0, 50).join(','),
|
||||
});
|
||||
return body.places ?? {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a TREK place onto the record shape the client already reads.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user