feat(server): let a trip owner switch trip addons for one trip

A trip can now turn a trip addon (lists, costs, documents, collab, road
trip, tours) on or off for itself, whatever the instance switch says. No
row in the new trip_addon_overrides table means the trip follows the
instance; a row is the owner's choice. Going back to the instance setting
deletes the row, and no addon data is touched either way.

- GET/PUT /api/trips/:tripId/addons[/:addonId] (every member reads, the
  owner writes) and the MCP tools get_trip_addons and set_trip_addon, on
  the shared tripAddons contract; tripAddons:changed tells the members.
- Every trip-scoped addon surface asks the trip: REST through
  TripAddonGuard, MCP through a per-call gate on the named or pinned trip,
  plugin RPC and events, the share link, the offline bundle, reminders,
  doc sync, the assistant (tools, keep_attached_file), booking costs, the
  trip summary prompt, and the cross-trip tour reads (library, Atlas
  stats, collection save).
- Routes without a trip open while the addon is on in any of the user's
  trips. Switching an addon on that the instance has off ends the members'
  MCP sessions so they reconnect with its tools.
- A copied trip takes its owner's choices along.

Behaviour changes: an anonymous call to an addon route that is off now
gets 401 instead of 404, and with an instance switch off the packing,
todo and files plugin RPC methods, the files MCP tools, todo reminder
mails, core plugin events, plugin view contributions, the share link's
lists, costs and chat sections and the offline bundle's lists follow it.
This commit is contained in:
Maurice
2026-11-21 13:23:02 +01:00
parent 2f7b8357ce
commit 3f38621024
203 changed files with 3710 additions and 498 deletions
+33 -18
View File
@@ -1,4 +1,9 @@
import { ADDON_IDS } from '../../addons';
import type { User } from '../../types';
import { addonOffMessage } from '../addons/addons.constants';
import { AddonsService } from '../addons/addons.service';
import { RequireAddon } from '../addons/require-addon.decorator';
import { TripAddonGuard } from '../addons/trip-addon.guard';
import { RuntimeEnvService } from '../app-config/runtime-env.service';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
@@ -128,13 +133,20 @@ export const collabNoteFileFilter: Options['fileFilter'] = (_req, file, cb) => {
// route keeps its own check, because guards run before interceptors and a 404 sent
// while the client is still streaming the multipart body destroys the socket (the
// caller then sees ECONNRESET instead of the 404). Same as files.controller.ts.
//
// Collab is a trip addon: a trip it is off for answers 404 "Collab addon is not
// enabled" after the trip check, through TripAddonGuard beside TripAccessGuard
// and through requireTrip on the two upload routes. Notes, polls and messages
// stay where they are.
@UseGuards(JwtAuthGuard)
@RequireAddon(ADDON_IDS.COLLAB, 'Collab')
export class CollabController {
constructor(
private readonly collab: CollabService,
private readonly storage: StorageService,
// Whether DEMO_MODE is on, read on every upload: the demo account's are refused.
private readonly env: RuntimeEnvService,
private readonly addons: AddonsService,
) {}
private async requireTrip(tripId: string, user: User) {
@@ -142,6 +154,9 @@ export class CollabController {
if (!trip) {
throw new HttpException({ error: 'Trip not found' }, 404);
}
if (!(await this.addons.isEnabledForTrip(trip.id, ADDON_IDS.COLLAB))) {
throw new HttpException({ error: addonOffMessage(ADDON_IDS.COLLAB) }, 404);
}
return trip;
}
@@ -155,14 +170,14 @@ export class CollabController {
}
// ── Notes ───────────────────────────────────────────────────────────────
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@Get('notes')
@ResponseContract(collabNotesResponseSchema)
async listNotes(@CurrentUser() user: User, @Param('tripId') tripId: string) {
return { notes: await this.collab.listNotes(tripId) };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Post('notes')
@ResponseContract(collabNoteResponseSchema)
@@ -180,7 +195,7 @@ export class CollabController {
return { note };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Put('notes/:id')
@ResponseContract(collabNoteResponseSchema)
@@ -206,7 +221,7 @@ export class CollabController {
return { note };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Delete('notes/:id')
@ResponseContract(successResponseSchema)
@@ -281,7 +296,7 @@ export class CollabController {
return result;
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Delete('notes/:id/files/:fileId')
@ResponseContract(successResponseSchema)
@@ -305,14 +320,14 @@ export class CollabController {
}
// ── Shared links ────────────────────────────────────────────────────────
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@Get('links')
@ResponseContract(collabLinksResponseSchema)
async listLinks(@CurrentUser() user: User, @Param('tripId') tripId: string) {
return { links: await this.collab.listLinks(tripId) };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Post('links')
@ResponseContract(collabLinkResponseSchema)
@@ -331,7 +346,7 @@ export class CollabController {
return { link };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Put('links/:id')
@ResponseContract(collabLinkResponseSchema)
@@ -352,7 +367,7 @@ export class CollabController {
return { link };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Delete('links/:id')
@ResponseContract(successResponseSchema)
@@ -368,14 +383,14 @@ export class CollabController {
}
// ── Polls ───────────────────────────────────────────────────────────────
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@Get('polls')
@ResponseContract(collabPollsResponseSchema)
async listPolls(@CurrentUser() user: User, @Param('tripId') tripId: string) {
return { polls: await this.collab.listPolls(tripId) };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Post('polls')
@ResponseContract(collabPollResponseSchema)
@@ -396,7 +411,7 @@ export class CollabController {
return { poll };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Post('polls/:id/vote')
@HttpCode(200)
@@ -416,7 +431,7 @@ export class CollabController {
return { poll: result.poll };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Put('polls/:id/close')
@ResponseContract(collabPollResponseSchema)
@@ -434,7 +449,7 @@ export class CollabController {
return { poll };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Delete('polls/:id')
@ResponseContract(successResponseSchema)
@@ -452,7 +467,7 @@ export class CollabController {
}
// ── Messages ────────────────────────────────────────────────────────────
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@Get('messages')
@ResponseContract(collabMessagesResponseSchema)
async listMessages(@CurrentUser() user: User, @Param('tripId') tripId: string, @Query('before') before?: string) {
@@ -559,7 +574,7 @@ export class CollabController {
return { message: result.message };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Post('messages/:id/react')
@HttpCode(200)
@@ -584,7 +599,7 @@ export class CollabController {
return { reactions: result.reactions };
}
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@RequirePermission('collab_edit')
@Delete('messages/:id')
@ResponseContract(successResponseSchema)
@@ -614,7 +629,7 @@ export class CollabController {
// trip whose owner has narrowed that right, for people who may still read the
// chat. What keeps the fetcher from being a free outbound proxy is the budget
// inside the service, not a write permission.
@UseGuards(TripAccessGuard)
@UseGuards(TripAccessGuard, TripAddonGuard)
@Get('link-preview')
@ResponseContract(collabLinkPreviewResponseSchema)
async linkPreview(@CurrentUser() user: User, @Param('tripId') tripId: string, @Query('url') url?: string) {
+4 -1
View File
@@ -12,7 +12,7 @@ import {
errorResult,
ok,
} from '../../nest-mcp';
import { collabFeatureGate } from '../addons/addon-gate';
import { addonOffResource, collabFeatureGate } from '../addons/addon-gate';
import { AddonsService } from '../addons/addons.service';
import { uriRowId } from '../common/row-id';
import { McpToolGuardsService } from '../mcp-shared/mcp-tool-guards.service';
@@ -551,6 +551,7 @@ export class CollabMcp {
async tripCollabNotesResource(uri: URL, { tripId }: { tripId: string | string[] }, ctx: McpContext) {
const id = uriRowId(tripId);
if (id === null || !(await this.collab.verifyTripAccess(id, ctx.userId))) return accessDenied(uri.href);
if (!(await this.addons.isEnabledForTrip(id, ADDON_IDS.COLLAB))) return addonOffResource(uri, ADDON_IDS.COLLAB);
const notes = await this.collab.listNotes(id);
return jsonContent(uri.href, notes);
}
@@ -566,6 +567,7 @@ export class CollabMcp {
async tripCollabPollsResource(uri: URL, { tripId }: { tripId: string | string[] }, ctx: McpContext) {
const id = uriRowId(tripId);
if (id === null || !(await this.collab.verifyTripAccess(id, ctx.userId))) return accessDenied(uri.href);
if (!(await this.addons.isEnabledForTrip(id, ADDON_IDS.COLLAB))) return addonOffResource(uri, ADDON_IDS.COLLAB);
const polls = await this.collab.listPolls(id);
return jsonContent(uri.href, polls);
}
@@ -581,6 +583,7 @@ export class CollabMcp {
async tripCollabMessagesResource(uri: URL, { tripId }: { tripId: string | string[] }, ctx: McpContext) {
const id = uriRowId(tripId);
if (id === null || !(await this.collab.verifyTripAccess(id, ctx.userId))) return accessDenied(uri.href);
if (!(await this.addons.isEnabledForTrip(id, ADDON_IDS.COLLAB))) return addonOffResource(uri, ADDON_IDS.COLLAB);
const messages = await this.collab.listMessages(id);
return jsonContent(uri.href, messages);
}
+13 -12
View File
@@ -31,24 +31,24 @@ export class CollabRpc {
@PluginMethod('collab.listNotes', { permission: 'db:read:collab' })
listNotes(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
return this.guards.tripRead(params, ctx, async () => {
await this.requireCollabAddon();
return this.guards.tripRead(params, ctx, async (userId) => {
await this.requireCollabAddon(rowIdParam(params.tripId, 'tripId'), userId);
return (await this.collab.listNotes(rowIdParam(params.tripId, 'tripId'))) as unknown[];
});
}
@PluginMethod('collab.listPolls', { permission: 'db:read:collab' })
listPolls(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
return this.guards.tripRead(params, ctx, async () => {
await this.requireCollabAddon();
return this.guards.tripRead(params, ctx, async (userId) => {
await this.requireCollabAddon(rowIdParam(params.tripId, 'tripId'), userId);
return (await this.collab.listPolls(rowIdParam(params.tripId, 'tripId'))) as unknown[];
});
}
@PluginMethod('collab.listMessages', { permission: 'db:read:collab' })
listMessages(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
return this.guards.tripRead(params, ctx, async () => {
await this.requireCollabAddon();
return this.guards.tripRead(params, ctx, async (userId) => {
await this.requireCollabAddon(rowIdParam(params.tripId, 'tripId'), userId);
const before = params.before != null ? rowIdParam(params.before, 'before') : undefined;
return (await this.collab.listMessages(rowIdParam(params.tripId, 'tripId'), before)) as unknown[];
});
@@ -61,7 +61,7 @@ export class CollabRpc {
const input = asPayload(params.input);
if (typeof input.title !== 'string' || input.title.trim() === '') throw new BadParams('note title is required');
await this.guards.requireTripEdit(tripId, actor, COLLAB_EDIT_ACTION);
await this.requireCollabAddon();
await this.requireCollabAddon(tripId, actor);
const note = await this.collab.createNote(String(tripId), actor, input as never);
this.realtime.broadcast(tripId, 'collab:note:created', { note }, undefined);
return note;
@@ -77,7 +77,7 @@ export class CollabRpc {
if (!Array.isArray(input.options) || input.options.length < 2)
throw new BadParams('a poll needs at least two options');
await this.guards.requireTripEdit(tripId, actor, COLLAB_EDIT_ACTION);
await this.requireCollabAddon();
await this.requireCollabAddon(tripId, actor);
const poll = await this.collab.createPoll(String(tripId), actor, input as never);
this.realtime.broadcast(tripId, 'collab:poll:created', { poll }, undefined);
return poll;
@@ -88,7 +88,7 @@ export class CollabRpc {
const tripId = rowIdParam(params.tripId, 'tripId');
const actor = this.guards.requireActor(ctx, 'collab poll');
await this.guards.requireTripEdit(tripId, actor, COLLAB_EDIT_ACTION);
await this.requireCollabAddon();
await this.requireCollabAddon(tripId, actor);
const result = await this.collab.votePoll(
String(tripId),
String(rowIdParam(params.pollId, 'pollId')),
@@ -109,7 +109,7 @@ export class CollabRpc {
throw new BadParams('message text is required (max 4000 chars)');
}
await this.guards.requireTripEdit(tripId, actor, COLLAB_EDIT_ACTION);
await this.requireCollabAddon();
await this.requireCollabAddon(tripId, actor);
// Left out or null is no reply; anything else must be a message id.
const replyTo = optionalRowIdParam(params.replyTo ?? undefined, 'replyTo') ?? null;
const result = await this.collab.createMessage(String(tripId), actor, params.text, replyTo);
@@ -118,7 +118,8 @@ export class CollabRpc {
return result.message;
}
private async requireCollabAddon(): Promise<void> {
await this.guards.requireAddon(ADDON_IDS.COLLAB, 'collab');
/** Collab has to be on for the trip, the owner's choice or else the instance switch. */
private async requireCollabAddon(tripId: number, userId: number): Promise<void> {
await this.guards.requireAddon(ADDON_IDS.COLLAB, 'collab', { tripId, userId });
}
}