feat(journey): complete sharing, photo drafts and Studio maps

Connect the Journey backend to the web and mobile UI, including public access and subscriptions, day recordings, photo drafts and per-map framing. Preserve legacy recording responses and cover the new behavior in every locale.
This commit is contained in:
Maurice
2026-11-30 16:55:22 +01:00
parent 458227f489
commit ede109dce8
90 changed files with 5306 additions and 292 deletions
@@ -76,7 +76,7 @@ export const ASSISTANT_DENIED_TOOLS: Readonly<Record<string, string>> = {
create_journey_share_link: 'mints a public link token',
delete_journey_share_link: 'belongs with the share links above',
add_journey_comment: "speaks in the user's name to the journey's people and the readers of its share link",
list_journey_subscribers:'reads the email addresses of people without an account, which would reach the AI provider',
list_journey_subscribers: 'reads the email addresses of people without an account, which would reach the AI provider',
remove_journey_subscriber: 'belongs with the followers above',
get_trip_calendar_feed: 'reads a calendar feed token',
enable_trip_calendar_feed: 'mints a calendar feed token',
@@ -65,8 +65,14 @@ export class JourneyRecordingsController {
@Get()
@ResponseContract(journeyRecordedDaysResponseSchema)
async list(@CurrentUser() user: User, @Param('id', rowId('Journey not found')) id: number) {
return { days: await this.recordings.list(user.id, id) };
async list(
@CurrentUser() user: User,
@Param('id', rowId('Journey not found')) id: number,
@Query('includeGpx') includeGpx?: string,
) {
const days = await this.recordings.list(user.id, id);
// Older clients validate the entire response against a Dawarich-only source enum.
return { days: includeGpx === 'true' ? days : days.filter((day) => day.source === 'dawarich') };
}
/** JRN-03: whether the journey prepares its recorded days, and how the caller's last runs went. */
@@ -112,7 +112,7 @@ export class JourneyRecordingsMcp {
@Tool({
name: 'import_journey_gpx_days',
description:
"Read the recorded route of a GPX file (its track segments with times; waypoints and routes are left out) into a journey as recorded days with source gpx, one per local day the file covers, so the journey map draws them, also on days without an entry. Each point names its day in the zone of where it was recorded unless utcOffsetMinutes fixes one zone; date keeps only that one local day. A day the journey kept from a GPX file before is replaced, one kept exactly so is left as it is, and a day kept from Dawarich is never touched. Points without a time with a zone are counted as untimedPoints and left out. Never changes an entry or a text of the journey. Everyone who can open the journey sees the route on its map. Needs edit access to the journey.",
'Read the recorded route of a GPX file (its track segments with times; waypoints and routes are left out) into a journey as recorded days with source gpx, one per local day the file covers, so the journey map draws them, also on days without an entry. Each point names its day in the zone of where it was recorded unless utcOffsetMinutes fixes one zone; date keeps only that one local day. A day the journey kept from a GPX file before is replaced, one kept exactly so is left as it is, and a day kept from Dawarich is never touched. Points without a time with a zone are counted as untimedPoints and left out. Never changes an entry or a text of the journey. Everyone who can open the journey sees the route on its map. Needs edit access to the journey.',
inputSchema: { journeyId: idSchema, gpx: journeyGpxTextSchema, ...journeyGpxImportOptionsSchema.shape },
annotations: TOOL_ANNOTATIONS_WRITE,
when: journeyAddonOn,
@@ -6,14 +6,14 @@ import { RateLimitService } from '../common/rate-limit.service';
import { ResponseContract } from '../common/response-contract';
import { rowId } from '../common/row-id.pipe';
import { JourneyCommentsService } from './journey-comments.service';
import { unlockProofOf } from './journey-share-access';
import { JOURNEY_SHARE_CACHE_CONTROL, unlockProofOf } from './journey-share-access';
import {
JourneyPublicCommentCreateDto,
JourneyPublicSubscribeDto,
JourneySubscriptionTokenDto,
} from './journey-social.dto';
import { JourneySubscriptionsService } from './journey-subscriptions.service';
import { Body, Controller, Get, HttpCode, HttpException, Param, Post, Req, UseGuards } from '@nestjs/common';
import { Body, Controller, Get, Header, HttpCode, HttpException, Param, Post, Req, UseGuards } from '@nestjs/common';
import {
journeyPublicCommentsResponseSchema,
journeySubscriptionConfirmResponseSchema,
@@ -57,6 +57,7 @@ export class JourneyPublicSocialController {
) {}
@Get(':token/comments')
@Header('Cache-Control', JOURNEY_SHARE_CACHE_CONTROL)
@ResponseContract(journeyPublicCommentsResponseSchema)
listComments(@Param('token') token: string, @Req() req: Request) {
return this.comments.listPublic(token, unlockProofOf(req));
@@ -14,14 +14,27 @@ import {
import { StorageService } from '../storage/storage.service';
import {
JOURNEY_UNLOCK_COOKIE,
keepCachesPrivate,
JOURNEY_SHARE_CACHE_CONTROL,
preventSharePhotoCaching,
unlockCookieOptions,
unlockProofOf,
viewerOf,
} from './journey-share-access';
import { JourneyPublicUnlockDto } from './journey.dto';
import { JourneyService } from './journey.service';
import { Body, Controller, Get, HttpCode, HttpException, Param, Post, Req, Res, UseGuards } from '@nestjs/common';
import {
Body,
Controller,
Get,
Header,
HttpCode,
HttpException,
Param,
Post,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import { journeyPublicMapLayersResponseSchema, journeyPublicResponseSchema, successResponseSchema } from '@trek/shared';
import type { Request, Response } from 'express';
@@ -59,8 +72,8 @@ function sharedPhotoKind(kind: string): 'thumbnail' | 'preview' | 'original' {
* Every route but the unlock first asks `assertPublicAccess` (#2553): the link
* exists (404), has not run out (410) and, when it has a password, the browser
* holds the unlock cookie for it (401). The photo proxies ask too, so a locked
* link hands out no picture even to somebody who knows a photo's address, and
* they answer a protected link's photos as `private`, so no shared cache keeps one.
* link hands out no picture even to somebody who knows a photo's address.
* Shared content cannot be reused from a cache after the owner changes access.
*/
@Public('share-token validated: the whole point is a link that works without an account')
@UseGuards(AddonGuard)
@@ -80,21 +93,32 @@ export class JourneyPublicController {
* this route instead of going out to the public.
*/
@Get(':token')
@Header('Cache-Control', JOURNEY_SHARE_CACHE_CONTROL)
@ResponseContract(journeyPublicResponseSchema)
async get(@Param('token') token: string, @Req() req: Request) {
await this.journey.assertPublicAccess(token, unlockProofOf(req));
const data = await this.journey.getPublicJourney(token, viewerOf(req));
const data = await this.journey.getPublicJourney(token);
if (!data) {
throw new HttpException({ error: 'Not found' }, 404);
}
return data;
}
@Post(':token/view')
@HttpCode(200)
@ResponseContract(successResponseSchema)
async view(@Param('token') token: string, @Req() req: Request) {
const link = await this.journey.assertPublicAccess(token, unlockProofOf(req));
await this.journey.recordPublicView(link.id, viewerOf(req));
return { success: true as const };
}
/**
* What the shared map shows beside the entries and photos (JRN-04), cut to
* the link's flags on the server: nothing while the map is not shared.
*/
@Get(':token/map-layers')
@Header('Cache-Control', JOURNEY_SHARE_CACHE_CONTROL)
@ResponseContract(journeyPublicMapLayersResponseSchema)
async mapLayers(@Param('token') token: string, @Req() req: Request) {
await this.journey.assertPublicAccess(token, unlockProofOf(req));
@@ -146,8 +170,8 @@ export class JourneyPublicController {
@Res() res: Response,
@Req() req: Request,
): Promise<void> {
const link = await this.journey.assertPublicAccess(token, unlockProofOf(req));
if (link.password_hash) keepCachesPrivate(res);
await this.journey.assertPublicAccess(token, unlockProofOf(req));
preventSharePhotoCaching(res);
const valid = await this.journey.validateShareTokenForPhoto(token, photoId);
if (!valid) {
throw new HttpException({ error: 'Not found' }, 404);
@@ -164,8 +188,8 @@ export class JourneyPublicController {
@Res() res: Response,
@Req() req: Request,
): Promise<void> {
const link = await this.journey.assertPublicAccess(token, unlockProofOf(req));
if (link.password_hash) keepCachesPrivate(res);
await this.journey.assertPublicAccess(token, unlockProofOf(req));
preventSharePhotoCaching(res);
const valid = await this.journey.validateShareTokenForAsset(token, assetId);
if (!valid) {
throw new HttpException({ error: 'Not found' }, 404);
@@ -191,7 +215,6 @@ export class JourneyPublicController {
if (!rendition) {
throw new HttpException({ error: 'Not found' }, 404);
}
res.set('Cache-Control', 'public, max-age=86400');
await this.storage.sendToResponse('journey', rendition, res);
return;
}
+16 -22
View File
@@ -26,14 +26,19 @@ const SHARE_PASSWORD_BCRYPT_COST = 12;
/** How long a browser stays unlocked unless the link runs out first: 30 days. */
const UNLOCK_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
/** Keep every UTF-8 byte of the password in the fixed-length value bcrypt sees. */
function passwordMaterial(password: string): string {
return createHash('sha256').update(password).digest('base64url');
}
/** The password's hash for the link row. Async, so a hash never blocks the event loop. */
export function hashSharePassword(password: string): Promise<string> {
return bcrypt.hash(password, SHARE_PASSWORD_BCRYPT_COST);
return bcrypt.hash(passwordMaterial(password), SHARE_PASSWORD_BCRYPT_COST);
}
/** Whether `password` is the one the link was given. */
export function checkSharePassword(password: string, hash: string): Promise<boolean> {
return bcrypt.compare(password, hash);
return bcrypt.compare(passwordMaterial(password), hash);
}
/**
@@ -96,28 +101,17 @@ export function unlockCookieOptions(
};
}
/** A `Cache-Control` value with `public` turned into `private`; anything else stays as it is. */
export function privateCacheControl(value: number | string | readonly string[]): number | string | string[] {
const toPrivate = (v: string) => v.replace(/\bpublic\b/gi, 'private');
if (typeof value === 'number') return value;
if (typeof value === 'string') return toPrivate(value);
return value.map(toPrivate);
}
export const JOURNEY_SHARE_CACHE_CONTROL = 'private, no-store';
/**
* Keeps a protected link's photo out of shared caches. The photo proxies of
* the memories domain answer `public, max-age=…`, which is right for an open
* link, but a proxy or CDN between TREK and the reader would then hand a
* protected link's photo to somebody who never typed the password. Every
* `Cache-Control` the response is given from here on is made `private`, so
* only the reader's own browser keeps it.
*/
export function keepCachesPrivate(res: Response): void {
/** Reuse the provider streams while keeping every photo request subject to current link access. */
export function preventSharePhotoCaching(res: Response): void {
res.setHeader('Cache-Control', JOURNEY_SHARE_CACHE_CONTROL);
const setHeader = res.setHeader.bind(res);
res.setHeader = ((name: string, value: number | string | readonly string[]) => {
const isCacheControl = name.toLowerCase() === 'cache-control';
return setHeader(name, isCacheControl ? privateCacheControl(value) : value);
}) as Response['setHeader'];
res.setHeader = ((name: string, value: number | string | readonly string[]) =>
setHeader(
name,
name.toLowerCase() === 'cache-control' ? JOURNEY_SHARE_CACHE_CONTROL : value,
)) as Response['setHeader'];
}
/** Who is asking, as far as the view count needs to tell readers apart: their address and browser. */
@@ -377,16 +377,11 @@ export class JourneyShareService {
}
/**
* The shared journey. `viewer` (address and browser, see `viewerOf`) is who
* opened it, for the owner's view count; left out, nothing is counted.
* The shared journey.
* `canMail` says whether the instance can send mail at all: a reader is only
* offered to follow the link by email when it can (#1669).
*/
async getPublicJourney(
token: string,
viewer?: string,
options: { canMail?: boolean } = {},
): Promise<JourneyPublicResponse | null> {
async getPublicJourney(token: string, options: { canMail?: boolean } = {}): Promise<JourneyPublicResponse | null> {
// JS11 — exact-match token lookup, no LIKE/COLLATE (R4).
const row = await this.shareTokensRepo.findByToken(token);
if (!row) return null;
@@ -395,8 +390,6 @@ export class JourneyShareService {
const journey = await this.journeysRepo.findById(row.journey_id);
if (!journey) return null;
if (viewer !== undefined) await this.countView(row.id, viewer);
// Entries with photos — JS13 (Plan 4 Task 8b: relocated to `JourneyEntriesRepository`).
const entries = await this.entriesRepo.listPublicEntries(row.journey_id);
@@ -497,7 +490,7 @@ export class JourneyShareService {
*
* @txStandalone a counter bumped in one statement, independent of the read around it.
*/
private async countView(linkId: number, viewer: string): Promise<void> {
async recordPublicView(linkId: number, viewer: string): Promise<void> {
try {
if (!(await this.views.firstToday(viewKey(linkId, viewer), new Date()))) return;
await this.shareTokensRepo.recordView(linkId);
+5 -2
View File
@@ -220,8 +220,11 @@ export class JourneyService {
unlockPublicLink(token: string, password: string) {
return this.share.unlockPublicLink(token, password);
}
async getPublicJourney(token: string, viewer?: string) {
return this.share.getPublicJourney(token, viewer, { canMail: await this.mailer.isSmtpConfigured() });
async getPublicJourney(token: string) {
return this.share.getPublicJourney(token, { canMail: await this.mailer.isSmtpConfigured() });
}
recordPublicView(linkId: number, viewer: string) {
return this.share.recordPublicView(linkId, viewer);
}
getPublicMapLayers(token: string) {
return this.map.publicMapLayers(token);
@@ -109,12 +109,7 @@ export class PhotoResolverService {
* told the asset goes to the public, so it can send a rendition without the
* stored location in place of the original file.
*/
async streamPublicPhoto(
res: Response,
ownerId: number,
photoId: number,
kind: PhotoAssetKind,
): Promise<void> {
async streamPublicPhoto(res: Response, ownerId: number, photoId: number, kind: PhotoAssetKind): Promise<void> {
await this.stream(res, ownerId, photoId, kind, undefined, 'public');
}