mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +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:
@@ -3,7 +3,8 @@
|
||||
* what is laid over the record, and that a place without published values, or
|
||||
* a service that does not answer, leaves the details as they were.
|
||||
*/
|
||||
import { communityKeyOf, osmDetailsWithCommunity, withCommunityValues } from '../../../src/nest/maps/community-overlay';
|
||||
import { osmDetailsWithCommunity, withCommunityValues } from '../../../src/nest/maps/community-overlay';
|
||||
import { communityKeyOf } from '../../../src/nest/maps/trek-community.service';
|
||||
import { trekCommunityByKeys } from '../../../src/nest/maps/trek-places.client';
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
@@ -16,6 +16,7 @@ import type { MapsService } from '../../../src/nest/maps/maps.service';
|
||||
import type { GooglePlacesClient } from '../../../src/nest/maps/providers/google-places.provider';
|
||||
import type { OsmClient } from '../../../src/nest/maps/providers/osm.client';
|
||||
import type { WikimediaClient } from '../../../src/nest/maps/providers/wikimedia.client';
|
||||
import type { TrekCommunityService } from '../../../src/nest/maps/trek-community.service';
|
||||
import { PlaceEnrichmentService } from '../../../src/nest/place-enrichment/place-enrichment.service';
|
||||
import type { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
|
||||
|
||||
@@ -49,7 +50,11 @@ vi.mock('../../../src/utils/ssrfGuard', () => ({
|
||||
|
||||
/** Every seam enrichment reaches: the maps orchestrator and the outbound clients it injects beside it. */
|
||||
type Seams<T> = { [K in keyof T]: T[K] };
|
||||
type EnrichmentSeams = Seams<MapsService> & Seams<GooglePlacesClient> & Seams<OsmClient> & Seams<WikimediaClient>;
|
||||
type EnrichmentSeams = Seams<MapsService> &
|
||||
Seams<GooglePlacesClient> &
|
||||
Seams<OsmClient> &
|
||||
Seams<WikimediaClient> &
|
||||
Seams<TrekCommunityService>;
|
||||
|
||||
const REQ = { lat: 54.088, lng: 12.1409, name: "L'Osteria Rostock", placeId: 'ChIJosteria', lang: 'de' };
|
||||
|
||||
@@ -86,6 +91,9 @@ function mapsStub(over: Partial<Record<keyof EnrichmentSeams, unknown>> = {}) {
|
||||
async () => null as { tags: Record<string, string>; osmUrl: string | null; matchedName: string } | null,
|
||||
),
|
||||
details: vi.fn(async () => ({ place: null })),
|
||||
trekPlacesEnabled: vi.fn(() => true),
|
||||
photoPaths: vi.fn(async () => [] as string[]),
|
||||
imageBytes: vi.fn(async () => null as Buffer | null),
|
||||
...over,
|
||||
} as unknown as EnrichmentSeams;
|
||||
}
|
||||
@@ -113,6 +121,7 @@ const make = (maps: EnrichmentSeams) =>
|
||||
maps as unknown as GooglePlacesClient,
|
||||
maps as unknown as OsmClient,
|
||||
maps as unknown as WikimediaClient,
|
||||
maps as unknown as TrekCommunityService,
|
||||
);
|
||||
|
||||
/** A place whose own identity is empty but that belongs to a chain. */
|
||||
|
||||
@@ -13,6 +13,7 @@ import type { MapsService } from '../../../src/nest/maps/maps.service';
|
||||
import type { GooglePlacesClient } from '../../../src/nest/maps/providers/google-places.provider';
|
||||
import type { OsmClient } from '../../../src/nest/maps/providers/osm.client';
|
||||
import type { WikimediaClient } from '../../../src/nest/maps/providers/wikimedia.client';
|
||||
import type { TrekCommunityService } from '../../../src/nest/maps/trek-community.service';
|
||||
import { PlaceEnrichmentService } from '../../../src/nest/place-enrichment/place-enrichment.service';
|
||||
import type { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
|
||||
|
||||
@@ -54,7 +55,11 @@ vi.mock('../../../src/nest/maps/trek-places.client', async (importOriginal) => (
|
||||
|
||||
/** Every seam enrichment reaches: the maps orchestrator and the outbound clients it injects beside it. */
|
||||
type Seams<T> = { [K in keyof T]: T[K] };
|
||||
type EnrichmentSeams = Seams<MapsService> & Seams<GooglePlacesClient> & Seams<OsmClient> & Seams<WikimediaClient>;
|
||||
type EnrichmentSeams = Seams<MapsService> &
|
||||
Seams<GooglePlacesClient> &
|
||||
Seams<OsmClient> &
|
||||
Seams<WikimediaClient> &
|
||||
Seams<TrekCommunityService>;
|
||||
|
||||
const GOOGLE_REQ = { lat: 53.6323, lng: 10.0067, name: 'Hamburg Airport', placeId: 'ChIJham', lang: 'de' };
|
||||
const OSM_REQ = { lat: 52.525, lng: 13.3694, name: 'Berlin Hauptbahnhof', placeId: 'relation:3600565', lang: 'de' };
|
||||
@@ -86,6 +91,8 @@ function mapsStub(over: Partial<Record<keyof EnrichmentSeams, unknown>> = {}) {
|
||||
),
|
||||
details: vi.fn(async () => ({ place: null })),
|
||||
trekPlacesEnabled: vi.fn(() => true),
|
||||
photoPaths: vi.fn(async () => [] as string[]),
|
||||
imageBytes: vi.fn(async () => null as Buffer | null),
|
||||
...over,
|
||||
} as unknown as EnrichmentSeams;
|
||||
}
|
||||
@@ -113,6 +120,7 @@ const make = (maps: EnrichmentSeams) =>
|
||||
maps as unknown as GooglePlacesClient,
|
||||
maps as unknown as OsmClient,
|
||||
maps as unknown as WikimediaClient,
|
||||
maps as unknown as TrekCommunityService,
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -19,6 +19,7 @@ import type { MapsService } from '../../../src/nest/maps/maps.service';
|
||||
import type { GooglePlacesClient } from '../../../src/nest/maps/providers/google-places.provider';
|
||||
import type { OsmClient } from '../../../src/nest/maps/providers/osm.client';
|
||||
import type { WikimediaClient } from '../../../src/nest/maps/providers/wikimedia.client';
|
||||
import type { TrekCommunityService } from '../../../src/nest/maps/trek-community.service';
|
||||
import {
|
||||
candidateKey,
|
||||
PlaceEnrichmentService,
|
||||
@@ -60,7 +61,11 @@ vi.mock('../../../src/nest/maps/trek-places.client', async (importOriginal) => (
|
||||
|
||||
/** Every seam enrichment reaches: the maps orchestrator and the outbound clients it injects beside it. */
|
||||
type Seams<T> = { [K in keyof T]: T[K] };
|
||||
type EnrichmentSeams = Seams<MapsService> & Seams<GooglePlacesClient> & Seams<OsmClient> & Seams<WikimediaClient>;
|
||||
type EnrichmentSeams = Seams<MapsService> &
|
||||
Seams<GooglePlacesClient> &
|
||||
Seams<OsmClient> &
|
||||
Seams<WikimediaClient> &
|
||||
Seams<TrekCommunityService>;
|
||||
|
||||
function appSettingsStub(): AppSettingsRepository {
|
||||
return { getValue: mockGetValue } as unknown as AppSettingsRepository;
|
||||
@@ -99,6 +104,8 @@ function mapsStub(over: Partial<Record<keyof EnrichmentSeams, unknown>> = {}) {
|
||||
),
|
||||
details: vi.fn(async () => ({ place: null })),
|
||||
trekPlacesEnabled: vi.fn(() => true),
|
||||
photoPaths: vi.fn(async () => [] as string[]),
|
||||
imageBytes: vi.fn(async () => null as Buffer | null),
|
||||
...over,
|
||||
} as unknown as EnrichmentSeams;
|
||||
}
|
||||
@@ -124,6 +131,7 @@ function make(maps: EnrichmentSeams, cache: PlacePhotoCacheService) {
|
||||
maps as unknown as GooglePlacesClient,
|
||||
maps as unknown as OsmClient,
|
||||
maps as unknown as WikimediaClient,
|
||||
maps as unknown as TrekCommunityService,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -227,6 +235,38 @@ describe('collectPhotos', () => {
|
||||
expect(maps.fetchGooglePhotoRefs).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ENRICH-005b: puts the community pictures first, credited to the TREK community', async () => {
|
||||
const maps = mapsStub({
|
||||
getMapsKey: vi.fn(() => 'key'),
|
||||
fetchGooglePhotoRefs: vi.fn(async () => [{ name: 'places/p/photos/a', attribution: 'Bob' }]),
|
||||
fetchGooglePhotoBytes: vi.fn(async () => Buffer.from([1, 2, 3])),
|
||||
fetchCommonsCandidates: vi.fn(async () => [commonsCandidate()]),
|
||||
photoPaths: vi.fn(async () => ['/v1/contrib/img/0123456789abcdef.webp']),
|
||||
imageBytes: vi.fn(async () => Buffer.from([4, 5, 6])),
|
||||
});
|
||||
|
||||
const out = await make(maps, cacheStub()).enrich(1, OSM_REQ);
|
||||
|
||||
expect(maps.photoPaths).toHaveBeenCalledWith('way:12345');
|
||||
expect(maps.imageBytes).toHaveBeenCalledWith('/v1/contrib/img/0123456789abcdef.webp');
|
||||
expect(out.photos[0]).toMatchObject({
|
||||
source: 'community',
|
||||
attribution: 'TREK Community',
|
||||
license: 'CC BY 4.0',
|
||||
sourceUrl: null,
|
||||
});
|
||||
});
|
||||
|
||||
it('ENRICH-005c: asks no community pictures for a Google place or when the API is off', async () => {
|
||||
const maps = mapsStub({ photoPaths: vi.fn(async () => ['/v1/contrib/img/0123456789abcdef.webp']) });
|
||||
await make(maps, cacheStub()).enrich(1, REQ);
|
||||
expect(maps.photoPaths).not.toHaveBeenCalled();
|
||||
|
||||
const off = mapsStub({ trekPlacesEnabled: vi.fn(() => false), photoPaths: vi.fn(async () => []) });
|
||||
await make(off, cacheStub()).enrich(1, OSM_REQ);
|
||||
expect(off.photoPaths).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ENRICH-005: puts Google pictures ahead of Commons when a key is configured', async () => {
|
||||
const maps = mapsStub({
|
||||
getMapsKey: vi.fn(() => 'key'),
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* The community's pictures for a place: which ones the service publishes
|
||||
* (cover first, only paths of its own picture store) and their bytes, fetched
|
||||
* with the installation's key and capped.
|
||||
*/
|
||||
import { TrekCommunityService } from '../../../src/nest/maps/trek-community.service';
|
||||
import { trekCommunityByKeys, trekPlacesById } from '../../../src/nest/maps/trek-places.client';
|
||||
import { identifiedFetch } from '../../../src/nest/maps/trek-places.fetch';
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('../../../src/nest/maps/trek-places.client', () => ({
|
||||
trekCommunityByKeys: vi.fn(),
|
||||
trekPlacesById: vi.fn(),
|
||||
trekPlacesBaseUrl: () => 'https://places.example',
|
||||
}));
|
||||
vi.mock('../../../src/nest/maps/trek-places.fetch', () => ({ identifiedFetch: vi.fn() }));
|
||||
|
||||
const byKeys = vi.mocked(trekCommunityByKeys);
|
||||
const byId = vi.mocked(trekPlacesById);
|
||||
const fetchWithKey = vi.mocked(identifiedFetch);
|
||||
|
||||
const A = '/v1/contrib/img/aaaaaaaaaaaaaaaa.webp';
|
||||
const B = '/v1/contrib/img/bbbbbbbbbbbbbbbb.webp';
|
||||
|
||||
beforeEach(() => {
|
||||
byKeys.mockReset();
|
||||
byId.mockReset();
|
||||
fetchWithKey.mockReset();
|
||||
});
|
||||
|
||||
describe('TrekCommunityService.photoPaths', () => {
|
||||
it('TCOM-001: reads an OSM place from /v1/community, cover first', async () => {
|
||||
byKeys.mockResolvedValue({ 'osm:way/7': { images: [{ url: A }, { url: B, cover: true }] } });
|
||||
expect(await new TrekCommunityService().photoPaths('way:7')).toEqual([B, A]);
|
||||
expect(byKeys).toHaveBeenCalledWith(['osm:way/7']);
|
||||
});
|
||||
|
||||
it('TCOM-002: reads an index place from its own row', async () => {
|
||||
byId.mockResolvedValue({ images: [{ url: A, cover: true }] } as never);
|
||||
expect(await new TrekCommunityService().photoPaths('gers:abc')).toEqual([A]);
|
||||
expect(byId).toHaveBeenCalledWith('abc');
|
||||
});
|
||||
|
||||
it('TCOM-003: drops anything that is not a path of the picture store', async () => {
|
||||
byKeys.mockResolvedValue({
|
||||
'osm:node/1': {
|
||||
images: [{ url: 'https://evil.example/x.webp' }, { url: '/v1/contrib/img/../gate' }, { url: A }],
|
||||
},
|
||||
});
|
||||
expect(await new TrekCommunityService().photoPaths('node:1')).toEqual([A]);
|
||||
});
|
||||
|
||||
it('TCOM-004: nothing for another kind of place, or when the service fails', async () => {
|
||||
expect(await new TrekCommunityService().photoPaths('ChIJgoogle')).toEqual([]);
|
||||
byId.mockRejectedValue(new Error('down'));
|
||||
expect(await new TrekCommunityService().photoPaths('gers:abc')).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('TrekCommunityService.imageBytes', () => {
|
||||
it('TCOM-005: fetches a store path with the key and returns the bytes', async () => {
|
||||
fetchWithKey.mockResolvedValue(new Response(new Uint8Array([1, 2, 3]), { status: 200 }));
|
||||
expect(await new TrekCommunityService().imageBytes(A)).toEqual(Buffer.from([1, 2, 3]));
|
||||
expect(String(fetchWithKey.mock.calls[0]?.[0])).toBe(`https://places.example${A}`);
|
||||
});
|
||||
|
||||
it('TCOM-006: never fetches another path, and a failed or oversized answer is null', async () => {
|
||||
const svc = new TrekCommunityService();
|
||||
expect(await svc.imageBytes('/v1/place/abc')).toBeNull();
|
||||
expect(fetchWithKey).not.toHaveBeenCalled();
|
||||
fetchWithKey.mockResolvedValueOnce(new Response(null, { status: 404 }));
|
||||
expect(await svc.imageBytes(A)).toBeNull();
|
||||
fetchWithKey.mockResolvedValueOnce(
|
||||
new Response(new Uint8Array([1]), { status: 200, headers: { 'content-length': '9000000' } }),
|
||||
);
|
||||
expect(await svc.imageBytes(A)).toBeNull();
|
||||
fetchWithKey.mockRejectedValueOnce(new Error('timeout'));
|
||||
expect(await svc.imageBytes(A)).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user