fix(journey): handle large photo selections within the API limits

Allow larger JSON bodies on the draft-creation route, split oversized photo groups, and use the original geotagged times to assign photos between groups.
This commit is contained in:
Maurice
2026-11-30 17:06:03 +01:00
parent 21b8da53cf
commit f848446131
4 changed files with 64 additions and 9 deletions
+9 -1
View File
@@ -207,11 +207,16 @@ export async function buildApp(): Promise<INestApplication> {
* kilobytes, so those two routes are measured against TOUR_WRITE_MAX_BYTES.
* The planner's unsaved draft carries the same points and fixed legs (its
* contract caps it at TOUR_DRAFT_MAX_CHARS), so its write takes that ceiling
* too. Everything else keeps the tighter limit.
* too.
*
* Creating drafts from gallery photos sends the chosen groups together, with
* every photo ID plus titles and place names. Give this one route space for
* large galleries while keeping the tighter limit on ordinary writes.
*/
const bookBody = express.json({ limit: '8mb', verify: rawBodyKeeper });
const listFileBody = express.json({ limit: MAX_COLLECTION_FILE_BYTES * 2, verify: rawBodyKeeper });
const tourBody = express.json({ limit: TOUR_WRITE_MAX_BYTES, verify: rawBodyKeeper });
const photoDraftBody = express.json({ limit: '4mb', verify: rawBodyKeeper });
const json = express.json({ limit: '100kb', verify: rawBodyKeeper });
const urlencoded = express.urlencoded({ limit: '100kb', extended: true, verify: rawBodyKeeper });
const isMcp = (req: Request) => req.path === '/mcp' || req.path === '/mcp/';
@@ -227,6 +232,9 @@ export async function buildApp(): Promise<INestApplication> {
if (isBookWrite(req)) return bookBody(req, res, next);
if (isListFile(req)) return listFileBody(req, res, next);
if (isTourWrite(req)) return tourBody(req, res, next);
if (req.method === 'POST' && /^\/api\/journeys\/\d+\/entries\/from-photos\/?$/i.test(req.path)) {
return photoDraftBody(req, res, next);
}
return isMcp(req) ? next() : json(req, res, next);
});
instance.use(function urlencodedParser(req: Request, res: Response, next: NextFunction) {
@@ -1,6 +1,6 @@
import { haversineKm } from '../common/geo';
import { localParts, resolveTimeZone } from '../common/timezoneService';
import { getIntlLanguage, SUPPORTED_LANGUAGE_CODES } from '@trek/shared';
import { getIntlLanguage, JOURNEY_FROM_PHOTOS_MAX_PHOTOS_PER_GROUP, SUPPORTED_LANGUAGE_CODES } from '@trek/shared';
/**
* The pure half of making journal entries from gallery photos (#1920): when
@@ -196,15 +196,20 @@ function clusterDay(day: LocalCapture[], thresholds: ClusterThresholds): LocalCa
return clusters;
}
walk(located, true);
// Fix the cutoffs before adding photos: an unlocated photo cannot move a
// location group's boundary for the next one. Both lists are in time order.
const boundaries = clusters.slice(1).map((next, index) => {
const previous = clusters[index] as LocalCapture[];
return ((previous.at(-1) as LocalCapture).instant + (next[0] as LocalCapture).instant) / 2;
});
let index = 0;
for (const photo of unlocated) {
let best: { distance: number; cluster: LocalCapture[] } | null = null;
for (const cluster of clusters) {
const first = (cluster[0] as LocalCapture).instant;
const last = (cluster[cluster.length - 1] as LocalCapture).instant;
const distance = photo.instant < first ? first - photo.instant : Math.max(0, photo.instant - last);
if (!best || distance < best.distance) best = { distance, cluster };
let boundary = boundaries[index];
while (boundary !== undefined && photo.instant > boundary) {
index++;
boundary = boundaries[index];
}
(best as { cluster: LocalCapture[] }).cluster.push(photo);
clusters[index]?.push(photo);
}
for (const cluster of clusters) cluster.sort(byInstant);
return clusters;
@@ -220,6 +225,13 @@ export function clusterCaptures(captures: readonly LocalCapture[], thresholds: C
}
return [...days.values()]
.flatMap((day) => clusterDay(day, thresholds))
.flatMap((cluster) => {
const groups: LocalCapture[][] = [];
for (let start = 0; start < cluster.length; start += JOURNEY_FROM_PHOTOS_MAX_PHOTOS_PER_GROUP) {
groups.push(cluster.slice(start, start + JOURNEY_FROM_PHOTOS_MAX_PHOTOS_PER_GROUP));
}
return groups;
})
.sort((a, b) => byInstant(a[0] as LocalCapture, b[0] as LocalCapture));
}
@@ -193,6 +193,19 @@ describe('journey entries from photos e2e', () => {
expect(previewAfter.body.clusters).toEqual([]);
});
it('accepts a selection from a large gallery above the standard JSON limit', async () => {
const s = await scene();
const photoIds = Array.from({ length: 16_000 }, (_, index) => 100_000 + index);
const result = await request(app.getHttpServer())
.post(base(s.journey.id))
.set(as(s.editor.id))
.send({ groups: [{ photoIds: photoIds.slice(0, 8000) }, { photoIds: photoIds.slice(8000) }] });
expect(result.status).toBe(200);
expect(result.body.entries).toEqual([]);
expect(result.body.skippedPhotoIds).toEqual(photoIds);
});
it('JRNPHOTOS-E2E-003: refuses a viewer, a stranger and a body the contract refuses, writing nothing', async () => {
const s = await scene();
const { user: stranger } = await makeUser(orm);
@@ -15,6 +15,7 @@ import {
summarizeCluster,
type FromPhotosPhoto,
} from '../../../../src/nest/journey/journey-entries-from-photos.helpers';
import { JOURNEY_FROM_PHOTOS_MAX_PHOTOS_PER_GROUP } from '@trek/shared';
import { describe, expect, it } from 'vitest';
@@ -182,6 +183,18 @@ describe('clusterCaptures', () => {
]);
});
it('assigns consecutive photos around a cutoff using the original geotagged times', () => {
const venice = photo('2026-05-14T07:00:00Z', VENICE);
const verona = photo('2026-05-14T08:00:00Z', VERONA);
const halfway = photo('2026-05-14T07:30:00Z', null);
const closerToVerona = photo('2026-05-14T07:31:00Z', null);
expect(groups([venice, verona, halfway, closerToVerona])).toEqual([
[venice.id, halfway.id],
[closerToVerona.id, verona.id],
]);
});
it('JRNPHOTOS-HELP-013: a day with no geotagged photo is cut by time alone', () => {
const a = photo('2026-05-14T07:00:00Z', null);
const b = photo('2026-05-14T08:00:00Z', null);
@@ -189,6 +202,15 @@ describe('clusterCaptures', () => {
expect(groups([a, b, c], 'UTC')).toEqual([[a.id, b.id], [c.id]]);
});
it('keeps every photo in a group the create contract accepts', () => {
const limit = JOURNEY_FROM_PHOTOS_MAX_PHOTOS_PER_GROUP;
const photos = Array.from({ length: limit + 1 }, () => photo('2026-05-14T07:00:00Z', null));
const [first, second] = groups(photos, 'UTC');
expect(first).toHaveLength(limit);
expect(second).toEqual([photos[limit]?.id]);
});
it('JRNPHOTOS-HELP-014: days come in the order they were lived', () => {
const second = photo('2026-05-15T07:00:00Z', VENICE);
const first = photo('2026-05-14T07:00:00Z', VENICE);