fix(maps): a place's marker photo is its community picture where it has one

An OSM or index place with a reviewed TREK Community picture showed it in
its gallery but only a category pin in the lists and on the map: the
marker photo asked Google and Wikimedia, never the community's pictures.
It now asks them first, the order the gallery shows them in, and only
while the TREK Places API is on.
This commit is contained in:
Maurice
2026-11-30 13:14:23 +01:00
parent 51c45d0573
commit 07d54d6f6e
5 changed files with 140 additions and 11 deletions
+32 -2
View File
@@ -1,5 +1,6 @@
/**
* The marker photo of a place: Google's first photo for a Google id, otherwise
* The marker photo of a place: the first reviewed TREK Community picture of an
* index or OSM place, else Google's first photo for a Google id, otherwise
* (and as the fallback) a Wikipedia or Commons picture near its coordinates,
* cached on disk and served through /api/maps/place-photo/:id/bytes.
*
@@ -8,6 +9,7 @@
* failed" (minutes), the in-flight dedupe of concurrent requests for one place
* and the shared photo-fetch slots. MapsService.getPlacePhoto delegates to it.
*/
import { readEnv } from '../../app-config';
import { Places } from '../../db/entities/Places.entity';
import type { PlacesRepository } from '../../db/repositories/Places.repository';
import { PlaceChangesService } from '../place-changes/place-changes.service';
@@ -18,6 +20,7 @@ import { withPhotoFetchSlot } from './photo-fetch-slot';
import { PlacesProviderSelector } from './places-provider.selector';
import { GooglePlacesClient } from './providers/google-places.provider';
import { WikimediaClient } from './providers/wikimedia.client';
import { isCommunityPlace, TrekCommunityService } from './trek-community.service';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
@@ -31,11 +34,14 @@ export class PlacePhotoResolver {
private readonly selector: PlacesProviderSelector,
// PLC-14: the picture filled in here is a change of each place it lands on.
private readonly changes: PlaceChangesService,
private readonly community: TrekCommunityService,
) {}
/**
* The photo for one place: disk hit, negative cache, in-flight dedupe, then
* Google (for a Google id) and Wikimedia, under the shared photo-fetch slots.
* the community's pictures (for an index or OSM place), Google (for a Google
* id) and Wikimedia, under the shared photo-fetch slots: the order the place's
* own gallery shows them in.
*
* @txIndependent cache bookkeeping around network fetches: the stored photo,
* the negative-cache entry and the image_url backfill are each complete on
@@ -125,6 +131,30 @@ export class PlacePhotoResolver {
return { attribution: outcome.attribution };
};
// A picture the community took there and a person reviewed, the one the
// place's gallery leads with. Only index and OSM places have any, and with
// the TREK Places API off nothing leaves for it.
const fetchCommunityPhoto = async (): Promise<{ attribution: string | null } | null> => {
if (!isCommunityPlace(placeId) || !readEnv().maps.trekPlacesEnabled) return null;
const [path] = await this.community.photoPaths(placeId);
if (!path) return null;
const bytes = await this.community.imageBytes(path);
if (!bytes) {
providerFailed = true;
return null;
}
try {
const cached = await this.photoCache.put(placeId, bytes, 'TREK Community');
return { attribution: cached.attribution };
} catch {
providerFailed = true;
return null;
}
};
const communityPhoto = await fetchCommunityPhoto();
if (communityPhoto) return communityPhoto;
// 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.
@@ -14,6 +14,11 @@ export function communityKeyOf(placeId: string): string | null {
return match ? `osm:${match[1]}/${match[2]}` : null;
}
/** Whether the TREK Places API can hold community values for the place: an index place or an OSM element. */
export function isCommunityPlace(placeId: string): boolean {
return placeId.startsWith('gers:') || communityKeyOf(placeId) !== null;
}
/**
* The picture store path of a URL the service gave out: as it is when relative,
* the path when absolute on the configured service, null for anything else.
@@ -23,7 +23,7 @@ import {
import { GooglePlacesClient } from '../maps/providers/google-places.provider';
import { OsmClient } from '../maps/providers/osm.client';
import { WikimediaClient } from '../maps/providers/wikimedia.client';
import { communityKeyOf, TrekCommunityService } from '../maps/trek-community.service';
import { isCommunityPlace, TrekCommunityService } from '../maps/trek-community.service';
import { trekPlacesById } from '../maps/trek-places.client';
import { PlacePhotoCacheService } from '../place-photos/place-photo-cache.service';
import { InjectRepository } from '@mikro-orm/nestjs';
@@ -173,11 +173,6 @@ interface CachePayload extends CachedEnrichment {
* A sender who puts another number here only makes their own request compute
* afresh: nothing of the record is what gets cached.
*/
/** Whether the TREK Places API can hold community values for the place: an index place or an OSM element. */
function isCommunityPlace(placeId: string): boolean {
return placeId.startsWith('gers:') || communityKeyOf(placeId) !== null;
}
function communityVersion(placeId: string, details: Record<string, unknown> | null | undefined): number | null {
if (!isCommunityPlace(placeId)) return null;
const version = details?.community_version;
+12 -1
View File
@@ -12,6 +12,7 @@ import { PoisAlongResolver } from '../../src/nest/maps/pois-along.resolver';
import { GooglePlacesClient } from '../../src/nest/maps/providers/google-places.provider';
import { OsmClient } from '../../src/nest/maps/providers/osm.client';
import { WikimediaClient } from '../../src/nest/maps/providers/wikimedia.client';
import type { TrekCommunityService } from '../../src/nest/maps/trek-community.service';
import type { PlacePhotoCacheService } from '../../src/nest/place-photos/place-photo-cache.service';
import { noGoogleQuota } from './google-quota';
import { photonOff } from './photon';
@@ -44,7 +45,17 @@ export function buildMapsParts(
const osm = new OsmClient(photonOff());
const wiki = new WikimediaClient();
const selector = new PlacesProviderSelector(appSettings, users, googleQuota);
const photos = new PlacePhotoResolver(photoCache, places, google, wiki, selector, passThroughPlaceChanges());
// No community pictures here: a hand-built service never reaches the TREK Places API.
const community = { photoPaths: async () => [], imageBytes: async () => null } as unknown as TrekCommunityService;
const photos = new PlacePhotoResolver(
photoCache,
places,
google,
wiki,
selector,
passThroughPlaceChanges(),
community,
);
const links = new MapsUrlResolver(osm);
const details = new PlaceDetailsResolver(placeDetailsCache, google, osm, selector);
const along = new PoisAlongResolver(osm);
@@ -9,10 +9,11 @@ import { PlacePhotoResolver } from '../../../src/nest/maps/place-photo.resolver'
import type { PlacesProviderSelector } from '../../../src/nest/maps/places-provider.selector';
import type { GooglePlacesClient } from '../../../src/nest/maps/providers/google-places.provider';
import type { WikimediaClient } from '../../../src/nest/maps/providers/wikimedia.client';
import type { TrekCommunityService } from '../../../src/nest/maps/trek-community.service';
import type { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
import { passThroughPlaceChanges } from '../../helpers/place-changes';
import { describe, expect, it, vi } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
function cache(over: Record<string, unknown> = {}) {
return {
@@ -35,11 +36,16 @@ function resolver(
wikiOutcome: unknown,
key: string | null = null,
googleOutcome: unknown = { kind: 'none' },
communityPhoto: { paths: string[]; bytes: Buffer | null } = { paths: [], bytes: null },
) {
const places = { setImageUrlIfUnset: vi.fn(async () => 1) };
const google = { firstPhoto: vi.fn(async () => googleOutcome) };
const wiki = { downloadPhoto: vi.fn(async () => wikiOutcome) };
const selector = { getMapsKey: vi.fn(async () => key) };
const community = {
photoPaths: vi.fn(async () => communityPhoto.paths),
imageBytes: vi.fn(async () => communityPhoto.bytes),
};
const r = new PlacePhotoResolver(
c as unknown as PlacePhotoCacheService,
places as unknown as PlacesRepository,
@@ -47,8 +53,9 @@ function resolver(
wiki as unknown as WikimediaClient,
selector as unknown as PlacesProviderSelector,
passThroughPlaceChanges(),
community as unknown as TrekCommunityService,
);
return { r, places, google, wiki };
return { r, places, google, wiki, community };
}
describe('PlacePhotoResolver', () => {
@@ -92,4 +99,85 @@ describe('PlacePhotoResolver', () => {
expect(wiki.downloadPhoto).toHaveBeenCalledWith(1, 2, undefined);
expect(c.markError).toHaveBeenCalledWith('ChIJabc', 'provider-error');
});
describe('the community pictures of an index or OSM place', () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it('PHOTO-R-005: lead, the way the place gallery shows them, and no other source is asked', async () => {
const c = cache();
const { r, google, wiki, community } = resolver(
c,
{ kind: 'none' },
'key',
{ kind: 'none' },
{
paths: ['/images/a.jpg', '/images/b.jpg'],
bytes: Buffer.from([1]),
},
);
expect(await r.resolve(1, 'node:410711907', 54.08, 12.12, 'Helgas Kitchen')).toEqual({
photoUrl: '/api/maps/place-photo/node%3A410711907/bytes',
attribution: 'TREK Community',
});
expect(community.imageBytes).toHaveBeenCalledWith('/images/a.jpg');
expect(c.put).toHaveBeenCalledWith('node:410711907', Buffer.from([1]), 'TREK Community');
expect(google.firstPhoto).not.toHaveBeenCalled();
expect(wiki.downloadPhoto).not.toHaveBeenCalled();
});
it('PHOTO-R-006: a picture that would not load falls back and is remembered only briefly', async () => {
const c = cache();
const { r, wiki } = resolver(
c,
{ kind: 'none' },
null,
{ kind: 'none' },
{ paths: ['/images/a.jpg'], bytes: null },
);
expect(await r.resolve(1, 'way:7', 1, 2)).toEqual({ photoUrl: null, attribution: null });
expect(wiki.downloadPhoto).toHaveBeenCalled();
expect(c.markError).toHaveBeenCalledWith('way:7', 'provider-error');
});
it('PHOTO-R-007: a picture the disk cache cannot store falls back too', async () => {
const c = cache({
put: vi.fn(async () => {
throw new Error('disk full');
}),
});
const { r } = resolver(
c,
{ kind: 'none' },
null,
{ kind: 'none' },
{ paths: ['/images/a.jpg'], bytes: Buffer.from([1]) },
);
expect(await r.resolve(1, 'relation:3', 1, 2)).toEqual({ photoUrl: null, attribution: null });
expect(c.markError).toHaveBeenCalledWith('relation:3', 'provider-error');
});
it('PHOTO-R-008: are not asked for with the TREK Places API off, nor for a place that cannot have any', async () => {
const off = resolver(
cache(),
{ kind: 'none' },
null,
{ kind: 'none' },
{ paths: ['/images/a.jpg'], bytes: Buffer.from([1]) },
);
vi.stubEnv('TREK_PLACES_ENABLED', 'false');
await off.r.resolve(1, 'node:1', 1, 2);
expect(off.community.photoPaths).not.toHaveBeenCalled();
vi.unstubAllEnvs();
const coords = resolver(cache(), { kind: 'none' });
await coords.r.resolve(1, 'coords:1,2', 1, 2);
expect(coords.community.photoPaths).not.toHaveBeenCalled();
const none = resolver(cache(), { kind: 'none' }, null, { kind: 'none' }, { paths: [], bytes: null });
await none.r.resolve(1, 'gers:abc', 1, 2);
expect(none.community.photoPaths).toHaveBeenCalledWith('gers:abc');
expect(none.community.imageBytes).not.toHaveBeenCalled();
});
});
});