mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
04458bee12
Community opening hours laid over OSM details now go through the same parser as OSM's own, so they arrive as one line per day with open-now and the periods, and a community description lands in the details' summary. A place copied from a collection no longer records its values as an import: without an origin row a value is never sent anyway, and a later edit marks only the fields it changes.
62 lines
2.6 KiB
TypeScript
62 lines
2.6 KiB
TypeScript
import { buildOsmDetails } from './maps.helpers';
|
|
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) {
|
|
// The same shape OSM's own hours take in the details: one line per day, open now, the periods.
|
|
const hours = buildOsmDetails({ opening_hours: values.hours.osm }, '', '');
|
|
if (hours.opening_hours) {
|
|
out.opening_hours = hours.opening_hours;
|
|
out.open_now = hours.open_now;
|
|
out.opening_periods = hours.opening_periods;
|
|
}
|
|
}
|
|
if (values.description?.text) out.summary = values.description.text;
|
|
if (typeof values.community.version === 'number') out.community_version = values.community.version;
|
|
return out;
|
|
}
|
|
|
|
/** Records built from TREK's own details layer, which carry the community answer already. */
|
|
const asked = new WeakSet<object>();
|
|
|
|
export function markCommunityAsked(details: Record<string, unknown>): Record<string, unknown> {
|
|
asked.add(details);
|
|
return details;
|
|
}
|
|
|
|
/** 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 || asked.has(details)) return details;
|
|
const published = await trekCommunityByKeys([key]);
|
|
return withCommunityValues(details, published[key]);
|
|
}
|