mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
places: show the community's published pictures first in Pick a picture
The service delivers the pictures review approved for a place, but TREK never read them. A new TrekCommunityService in the maps domain asks the TREK Places API for them (the index row for a gers place, /v1/community for an OSM one), accepts only paths of the service's picture store, and fetches the bytes with the installation's key, capped at 4 MB. Enrichment puts them ahead of Google and Commons, through the same photo cache, credited as TREK Community under CC BY 4.0. The enrichment cache now follows the community version for OSM places too, so a newly approved picture shows on the next open. Also lowers the client's no-non-null-assertion baseline to 380: moving Use this text into the description card removed two.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { buildOsmDetails } from './maps.helpers';
|
||||
import { communityKeyOf } from './trek-community.service';
|
||||
import { trekCommunityByKeys, type TrekCommunityValues } from './trek-places.client';
|
||||
import { normalizePlaceWebsite } from '@trek/shared';
|
||||
|
||||
@@ -10,12 +11,6 @@ import { normalizePlaceWebsite } from '@trek/shared';
|
||||
* 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>,
|
||||
|
||||
@@ -18,6 +18,7 @@ import { GooglePlacesClient } from './providers/google-places.provider';
|
||||
import { OsmClient } from './providers/osm.client';
|
||||
import { PhotonClient } from './providers/photon.client';
|
||||
import { WikimediaClient } from './providers/wikimedia.client';
|
||||
import { TrekCommunityService } from './trek-community.service';
|
||||
import { TrekPlacesLayersService } from './trek-places-layers.service';
|
||||
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
||||
import { Module } from '@nestjs/common';
|
||||
@@ -66,7 +67,8 @@ import { Module } from '@nestjs/common';
|
||||
PlaceDetailsResolver,
|
||||
PoisAlongResolver,
|
||||
TrekPlacesLayersService,
|
||||
TrekCommunityService,
|
||||
],
|
||||
exports: [MapsService, GooglePlacesClient, OsmClient, WikimediaClient, TrekPlacesLayersService],
|
||||
exports: [MapsService, GooglePlacesClient, OsmClient, WikimediaClient, TrekPlacesLayersService, TrekCommunityService],
|
||||
})
|
||||
export class MapsModule {}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import { trekCommunityByKeys, trekPlacesBaseUrl, trekPlacesById } from './trek-places.client';
|
||||
import { identifiedFetch } from './trek-places.fetch';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
/** A published picture's path as the service writes it; anything else is never fetched. */
|
||||
const IMAGE_PATH = /^\/v1\/contrib\/img\/[A-Za-z0-9_-]{8,64}\.webp$/;
|
||||
const IMAGE_MAX_BYTES = 4_000_000;
|
||||
const IMAGE_TIMEOUT_MS = 8000;
|
||||
|
||||
/** 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 pictures the TREK community published for a place, read from the TREK
|
||||
* Places API: which ones (cover first, then the order review set) and their
|
||||
* bytes. An index place carries them on its own row, an OSM place gets them
|
||||
* from /v1/community. The caller checks that the API is switched on.
|
||||
*/
|
||||
@Injectable()
|
||||
export class TrekCommunityService {
|
||||
/** Paths of the place's published pictures, cover first; empty when there are none or the API fails. */
|
||||
async photoPaths(placeId: string): Promise<string[]> {
|
||||
let images: { url?: string; cover?: boolean }[] | null | undefined;
|
||||
try {
|
||||
if (placeId.startsWith('gers:')) {
|
||||
images = (await trekPlacesById(placeId.slice('gers:'.length)))?.images;
|
||||
} else {
|
||||
const key = communityKeyOf(placeId);
|
||||
if (key) images = (await trekCommunityByKeys([key]))[key]?.images;
|
||||
}
|
||||
} catch {
|
||||
// The service being down must never cost the place its other pictures.
|
||||
return [];
|
||||
}
|
||||
if (!Array.isArray(images)) return [];
|
||||
const ordered = [...images.filter((i) => i?.cover), ...images.filter((i) => !i?.cover)];
|
||||
return ordered.map((i) => i?.url).filter((u): u is string => typeof u === 'string' && IMAGE_PATH.test(u));
|
||||
}
|
||||
|
||||
/**
|
||||
* The bytes of one published picture, fetched with this installation's key,
|
||||
* or null. Only a path of the service's own picture store is accepted: the
|
||||
* value comes from the service's answer and becomes an authenticated download.
|
||||
*/
|
||||
async imageBytes(path: string): Promise<Buffer | null> {
|
||||
if (!IMAGE_PATH.test(path)) return null;
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), IMAGE_TIMEOUT_MS);
|
||||
try {
|
||||
const res = await identifiedFetch(
|
||||
new URL(trekPlacesBaseUrl() + path),
|
||||
{ Accept: 'image/webp' },
|
||||
controller.signal,
|
||||
);
|
||||
if (!res.ok) return null;
|
||||
const declared = Number(res.headers.get('content-length') ?? 0);
|
||||
if (declared > IMAGE_MAX_BYTES) {
|
||||
await res.body?.cancel();
|
||||
return null;
|
||||
}
|
||||
const bytes = Buffer.from(await res.arrayBuffer());
|
||||
return bytes.length && bytes.length <= IMAGE_MAX_BYTES ? bytes : null;
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,7 @@ export interface TrekPlace {
|
||||
hours: { osm: string; source: string | null; sourceUrl: string | null } | null;
|
||||
/** Quoted from the place's own site, when it publishes one for machines. */
|
||||
description?: { text: string; source: string | null; sourceUrl: string | null } | null;
|
||||
images?: { url?: string; cover?: boolean }[] | null;
|
||||
/** Bumped by the service whenever the community values it overlays for this place change. */
|
||||
community?: { version?: number } | null;
|
||||
score?: number;
|
||||
@@ -642,6 +643,7 @@ export interface TrekCommunityValues {
|
||||
contact?: { website?: string | null; phone?: string | null; email?: string | null } | null;
|
||||
hours?: { osm?: string | null } | null;
|
||||
description?: { text?: string | null } | null;
|
||||
images?: { url?: string; cover?: boolean }[] | null;
|
||||
community?: { version?: number; fields?: string[] } | null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user