feat(calendar): let a feed leave out the per-day summary events (#2602)

Both feed routes and the one-off export.ics take ?days=0, validated by a
shared query contract, and then skip the all-day "Day N" event per day; the
default is unchanged. The subscribe dialog gets a Day overview events switch
that adds the parameter to its links, export_trip_ics takes dayEvents and the
feed tools explain the parameter.
This commit is contained in:
Maurice
2026-11-13 09:30:45 +01:00
parent b9231ffb53
commit d5d9428cf2
47 changed files with 434 additions and 42 deletions
+10
View File
@@ -0,0 +1,10 @@
import { tripCalendarQuerySchema } from '@trek/shared';
import { createZodDto } from 'nestjs-zod';
/**
* Query DTO of every route that serves a trip calendar: the download in the
* trips controller and the two public feeds. The global ZodValidationPipe
* validates it by metatype, like the body DTOs.
*/
export class TripCalendarQueryDto extends createZodDto(tripCalendarQuerySchema) {}
+22 -6
View File
@@ -14,7 +14,7 @@ import { addDays } from '../days/days.service';
import { ReservationsService } from '../reservations/reservations.service';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
import { isTransportReservationType } from '@trek/shared';
import { isTransportReservationType, type TripCalendarQuery } from '@trek/shared';
/** The VCALENDAR preamble every TREK calendar starts with, single-trip or merged. */
export const CALENDAR_HEADER =
@@ -32,6 +32,17 @@ export interface TripCalendar {
events: string[];
}
/** What a trip's calendar holds beyond the trip itself, its timed stops and its bookings. */
export interface TripCalendarOptions {
/** The all-day summary per day with untimed stops or notes ("Day 1", ...). On unless set to false (#2602). */
dayEvents?: boolean;
}
/** The options a calendar request asks for: `days=0` leaves out the per-day summaries. */
export function calendarOptionsFromQuery(query: TripCalendarQuery): TripCalendarOptions {
return { dayEvents: query.days !== '0' };
}
// ── ICS folding ─────────────────────────────────────────────────────────────
// RFC 5545 §3.1: content lines longer than 75 octets must be folded with a CRLF
@@ -240,7 +251,7 @@ export class CalendarService {
* because a user-supplied SUMMARY can legitimately contain the literal
* "END:VEVENT". Handing out the parts removes the need to reassemble them.
*/
async buildTripCalendar(tripId: string | number): Promise<TripCalendar> {
async buildTripCalendar(tripId: string | number, options: TripCalendarOptions = {}): Promise<TripCalendar> {
const trip = await this.tripsRepo.findRaw(tripId);
if (!trip) throw new NotFoundError('Trip not found');
@@ -363,8 +374,10 @@ export class CalendarService {
events.push(ev);
}
// Build all-day summary event if there are untimed activities or notes
if (untimed.length > 0 || notes.length > 0) {
// Build all-day summary event if there are untimed activities or notes,
// unless the subscriber asked for a calendar without them: the summary
// sits above every day and can crowd out everything else there (#2602).
if (options.dayEvents !== false && (untimed.length > 0 || notes.length > 0)) {
const dayTitle = day.title || `Day ${day.day_number}`;
const endStr = fmtDate(addDays(day.date, 1));
@@ -914,8 +927,11 @@ export class CalendarService {
}
/** One trip's calendar as a finished, foldable VCALENDAR document. */
async exportICS(tripId: string | number): Promise<{ ics: string; filename: string }> {
const cal = await this.buildTripCalendar(tripId);
async exportICS(
tripId: string | number,
options: TripCalendarOptions = {},
): Promise<{ ics: string; filename: string }> {
const cal = await this.buildTripCalendar(tripId, options);
const ics =
CALENDAR_HEADER +
`X-WR-CALNAME:${cal.calName}\r\n` +
+17 -6
View File
@@ -4,11 +4,13 @@ import type { User } from '../../types';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { Public } from '../auth-core/public.decorator';
import { TripCalendarQueryDto } from '../calendar/calendar.dto';
import { calendarOptionsFromQuery } from '../calendar/calendar.service';
import { contentDisposition } from '../common/content-disposition';
import { RequirePermission, TripAccessGuard } from '../permissions/trip-access.guard';
import { Trip } from '../permissions/trip.decorator';
import { FeedsService } from './feeds.service';
import { Controller, Delete, Get, Param, Post, Put, Req, Res, UseGuards } from '@nestjs/common';
import { Controller, Delete, Get, Param, Post, Put, Query, Req, Res, UseGuards } from '@nestjs/common';
import type { Request, Response } from 'express';
@@ -27,7 +29,8 @@ function resolveFeedBase(req: Request): string {
/**
* Public subscribable ICS feed endpoints — no auth required.
* The secret token in the URL acts as the access credential.
* The secret token in the URL acts as the access credential. `?days=0` leaves
* the per-day summaries out of the feed (#2602); the subscribe dialog adds it.
*/
@Controller('api/feed')
@Public('the secret token in the feed URL is the credential; calendar clients never send a session')
@@ -35,8 +38,12 @@ export class FeedsPublicController {
constructor(private readonly feeds: FeedsService) {}
@Get('trip/:token.ics')
async tripFeed(@Param('token') token: string, @Res() res: Response): Promise<void> {
const result = await this.feeds.buildTripIcs(token);
async tripFeed(
@Param('token') token: string,
@Query() query: TripCalendarQueryDto,
@Res() res: Response,
): Promise<void> {
const result = await this.feeds.buildTripIcs(token, calendarOptionsFromQuery(query));
if (!result) {
res.status(404).json({ error: 'Feed not found' });
return;
@@ -49,8 +56,12 @@ export class FeedsPublicController {
}
@Get('user/:token.ics')
async userFeed(@Param('token') token: string, @Res() res: Response): Promise<void> {
const result = await this.feeds.buildUserIcs(token);
async userFeed(
@Param('token') token: string,
@Query() query: TripCalendarQueryDto,
@Res() res: Response,
): Promise<void> {
const result = await this.feeds.buildUserIcs(token, calendarOptionsFromQuery(query));
if (!result) {
res.status(404).json({ error: 'Feed not found' });
return;
+20 -6
View File
@@ -18,6 +18,14 @@ import { FeedsService } from './feeds.service';
import { InjectRepository } from '@mikro-orm/nestjs';
import { idSchema } from '@trek/shared';
/**
* The feed routes take `?days=0` (#2602), which the subscribe dialog appends
* on request. The tools hand back the plain URL, so their descriptions say how
* to ask for that variant instead of minting a second kind of link.
*/
const DAY_EVENTS_HINT =
' Add ?days=0 to the URL for a calendar without the all-day "Day 1", "Day 2" summaries; the trip itself, timed stops and bookings stay.';
/**
* Calendar-feed MCP surface, mirroring the two authenticated token controllers
* in feeds.controller.ts. Not a port of a legacy registrar: the feeds domain
@@ -72,7 +80,8 @@ export class FeedsMcp {
@Tool({
name: 'get_trip_calendar_feed',
description:
'Get the subscribable calendar feed URL of one trip, or null when the feed is switched off. This is a live subscription a calendar app re-reads hourly, so it keeps up with the itinerary. Prefer export_trip_ics when the user wants a one-off .ics file to import once and be done.',
'Get the subscribable calendar feed URL of one trip, or null when the feed is switched off. This is a live subscription a calendar app re-reads hourly, so it keeps up with the itinerary. Prefer export_trip_ics when the user wants a one-off .ics file to import once and be done.' +
DAY_EVENTS_HINT,
inputSchema: {
tripId: idSchema,
},
@@ -88,7 +97,8 @@ export class FeedsMcp {
@Tool({
name: 'enable_trip_calendar_feed',
description:
"Switch on the subscribable calendar feed of one trip and return its URL. Safe to repeat: a trip that already has a feed keeps the URL it has, so subscriptions somebody already set up survive. The URL carries a secret token and asks for no login, so whoever holds it can read the trip's dates.",
"Switch on the subscribable calendar feed of one trip and return its URL. Safe to repeat: a trip that already has a feed keeps the URL it has, so subscriptions somebody already set up survive. The URL carries a secret token and asks for no login, so whoever holds it can read the trip's dates." +
DAY_EVENTS_HINT,
inputSchema: {
tripId: idSchema,
},
@@ -104,7 +114,8 @@ export class FeedsMcp {
@Tool({
name: 'rotate_trip_calendar_feed',
description:
"Issue a fresh URL for one trip's calendar feed. Every calendar subscribed to the old URL silently stops updating, so use this when the old link leaked or the user asked to cut it off, not to switch the feed on: enable_trip_calendar_feed does that without breaking anything.",
"Issue a fresh URL for one trip's calendar feed. Every calendar subscribed to the old URL silently stops updating, so use this when the old link leaked or the user asked to cut it off, not to switch the feed on: enable_trip_calendar_feed does that without breaking anything." +
DAY_EVENTS_HINT,
inputSchema: {
tripId: idSchema,
},
@@ -142,7 +153,8 @@ export class FeedsMcp {
@Tool({
name: 'get_all_trips_calendar_feed',
description:
'Get the subscribable calendar feed URL covering every trip the user can open, or null when it is switched off. One feed for the whole travel calendar; get_trip_calendar_feed is the per-trip one.',
'Get the subscribable calendar feed URL covering every trip the user can open, or null when it is switched off. One feed for the whole travel calendar; get_trip_calendar_feed is the per-trip one.' +
DAY_EVENTS_HINT,
inputSchema: {},
annotations: TOOL_ANNOTATIONS_READONLY,
access: { group: 'trips', mode: 'share' },
@@ -154,7 +166,8 @@ export class FeedsMcp {
@Tool({
name: 'enable_all_trips_calendar_feed',
description:
"Switch on the calendar feed covering every trip the user can open and return its URL. Safe to repeat: an existing feed keeps its URL. The URL needs no login and follows the user's trips as they change, so a trip added later shows up in it without anyone re-subscribing.",
"Switch on the calendar feed covering every trip the user can open and return its URL. Safe to repeat: an existing feed keeps its URL. The URL needs no login and follows the user's trips as they change, so a trip added later shows up in it without anyone re-subscribing." +
DAY_EVENTS_HINT,
inputSchema: {},
annotations: TOOL_ANNOTATIONS_WRITE,
access: { group: 'trips', mode: 'share' },
@@ -166,7 +179,8 @@ export class FeedsMcp {
@Tool({
name: 'rotate_all_trips_calendar_feed',
description:
'Issue a fresh URL for the all-trips calendar feed. Every calendar subscribed to the old URL stops updating. Use it when that link leaked; enable_all_trips_calendar_feed is the way to switch the feed on without breaking existing subscriptions.',
'Issue a fresh URL for the all-trips calendar feed. Every calendar subscribed to the old URL stops updating. Use it when that link leaked; enable_all_trips_calendar_feed is the way to switch the feed on without breaking existing subscriptions.' +
DAY_EVENTS_HINT,
inputSchema: {},
annotations: TOOL_ANNOTATIONS_NON_IDEMPOTENT,
access: { group: 'trips', mode: 'share' },
+11 -5
View File
@@ -2,7 +2,7 @@ import { Trips } from '../../db/entities/Trips.entity';
import { Users } from '../../db/entities/Users.entity';
import type { TripsRepository } from '../../db/repositories/Trips.repository';
import type { UsersRepository } from '../../db/repositories/Users.repository';
import { CalendarService, CALENDAR_HEADER, foldICS } from '../calendar/calendar.service';
import { CalendarService, CALENDAR_HEADER, foldICS, type TripCalendarOptions } from '../calendar/calendar.service';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
@@ -96,11 +96,14 @@ export class FeedsService {
// ── ICS generation ───────────────────────────────────────────────────────
async buildTripIcs(token: string): Promise<{ ics: string; filename: string } | null> {
async buildTripIcs(
token: string,
options: TripCalendarOptions = {},
): Promise<{ ics: string; filename: string } | null> {
const tripId = await this.tripsRepo.findIdByFeedToken(token);
if (tripId === undefined) return null;
try {
const cal = await this.calendar.buildTripCalendar(tripId);
const cal = await this.calendar.buildTripCalendar(tripId, options);
// Same document as the one-time download, plus the subscription refresh
// hints so clients re-fetch hourly. Assembled from the calendar's parts
// rather than string-surgeried into the finished text.
@@ -118,7 +121,10 @@ export class FeedsService {
}
}
async buildUserIcs(token: string): Promise<{ ics: string; calName: string } | null> {
async buildUserIcs(
token: string,
options: TripCalendarOptions = {},
): Promise<{ ics: string; calName: string } | null> {
const user = await this.usersRepo.findIdAndUsernameByFeedToken(token);
if (!user) return null;
@@ -144,7 +150,7 @@ export class FeedsService {
let events = '';
for (const id of tripIds) {
try {
const cal = await this.calendar.buildTripCalendar(id);
const cal = await this.calendar.buildTripCalendar(id, options);
for (const [tzid, block] of cal.timezones) {
if (!zones.has(tzid)) zones.set(tzid, block);
}
+9 -3
View File
@@ -5,7 +5,8 @@ import { AuditService } from '../audit/audit.service';
import { getClientIp } from '../audit/client-ip';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { CalendarService } from '../calendar/calendar.service';
import { TripCalendarQueryDto } from '../calendar/calendar.dto';
import { CalendarService, calendarOptionsFromQuery } from '../calendar/calendar.service';
import { contentDisposition } from '../common/content-disposition';
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../common/demo-write';
import { StorageReleaseService } from '../storage/storage-release.service';
@@ -340,12 +341,17 @@ export class TripsController {
}
@Get(':id/export.ics')
async exportIcs(@CurrentUser() user: User, @Param('id') id: string, @Res() res: Response) {
async exportIcs(
@CurrentUser() user: User,
@Param('id') id: string,
@Query() query: TripCalendarQueryDto,
@Res() res: Response,
) {
if (!(await this.trips.canAccessTrip(id, user.id))) {
throw new HttpException({ error: 'Trip not found' }, 404);
}
try {
const { ics, filename } = await this.calendar.exportICS(id);
const { ics, filename } = await this.calendar.exportICS(id, calendarOptionsFromQuery(query));
res.setHeader('Content-Type', 'text/calendar; charset=utf-8');
res.setHeader('Content-Disposition', contentDisposition(filename, 'attachment'));
res.send(ics);
+8 -2
View File
@@ -691,14 +691,20 @@ export class TripsMcp {
"Export a trip's itinerary and reservations as iCalendar (.ics) format text. Useful for importing into calendar apps.",
inputSchema: {
tripId: idSchema,
dayEvents: z
.boolean()
.optional()
.describe(
'false leaves out the all-day summary each day with untimed places or notes gets ("Day 1", ...), the same as days=0 on the download; the trip itself, timed stops and bookings stay. Default true.',
),
},
annotations: TOOL_ANNOTATIONS_READONLY,
access: (ctx) => canReadTrips(ctx.scopes),
})
async exportTripIcs({ tripId }: { tripId: number }, ctx: McpContext) {
async exportTripIcs({ tripId, dayEvents }: { tripId: number; dayEvents?: boolean }, ctx: McpContext) {
if (!(await this.trips.canAccessTrip(tripId, ctx.userId))) return noAccess();
try {
const { ics, filename } = await this.calendar.exportICS(tripId);
const { ics, filename } = await this.calendar.exportICS(tripId, { dayEvents });
return ok({ ics, filename });
} catch {
return { content: [{ type: 'text' as const, text: 'Trip not found.' }], isError: true };