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
@@ -53,7 +53,7 @@ function build(
const guards = new PluginGuards(
db,
{ checkPermission: vi.fn(() => true) } as unknown as PermissionsService,
{ isAddonEnabled: vi.fn(() => true) } as unknown as AddonsService,
{ isAddonEnabled: vi.fn(() => true), isEnabledForTrip: vi.fn(async () => true) } as unknown as AddonsService,
{ getRole: vi.fn(async () => 'user') } as unknown as UsersRepository,
);
const realtime = { broadcast: vi.fn() } as never;
@@ -74,7 +74,7 @@ function build(addonOn: boolean) {
const guards = new PluginGuards(
db,
{ checkPermission: vi.fn(() => true) } as unknown as PermissionsService,
{ isAddonEnabled: vi.fn(() => addonOn) } as unknown as AddonsService,
{ isAddonEnabled: vi.fn(() => addonOn), isEnabledForTrip: vi.fn(async () => addonOn) } as unknown as AddonsService,
{ getRole: vi.fn(async () => 'user') } as unknown as UsersRepository,
);
const registry = createTestPluginRegistry([
@@ -168,8 +168,10 @@ describe('every addon-gated plugin method refuses when its addon is off', () =>
expect(res.ok).toBe(false);
expect(res.error.code).toBe('RESOURCE_FORBIDDEN');
expect(res.error.message).toBe(`the ${addon} addon is disabled`);
// The gate has to come before the service, not after it.
expect(f.calls).toEqual([]);
// The gate has to come before the service, not after it. listMine first reads which trips the
// user has, since a trip of theirs may have switched Costs on while the instance has it off.
const reads = method === 'costs.listMine' ? ['membership.listAccessibleTripIds'] : [];
expect(f.calls).toEqual(reads);
});
}
@@ -0,0 +1,61 @@
/**
* PEVADDON-001..004: the plugin event sink with the trip's addons in front.
*
* A trip that has an addon off shows none of its rows, so a subscribed plugin
* gets none of that family's events either; every other event goes out at once.
*/
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import { tripAddonOfEvent, tripEventSink } from '../../../src/nest/plugins/plugin-event-addons';
import { describe, it, expect, vi } from 'vitest';
/** Lets the sink's pending addon read settle. */
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
describe('the plugin event sink per trip', () => {
it('PEVADDON-001: an event names its trip addon by family; a trip event names none', () => {
expect(tripAddonOfEvent('budget:created')).toBe('budget');
expect(tripAddonOfEvent('packing:updated')).toBe('packing');
expect(tripAddonOfEvent('todo:deleted')).toBe('packing');
expect(tripAddonOfEvent('file:created')).toBe('documents');
expect(tripAddonOfEvent('collab:message:created')).toBe('collab');
expect(tripAddonOfEvent('roadtripVia:created')).toBe('roadtrip');
expect(tripAddonOfEvent('tours:updated')).toBe('tours');
expect(tripAddonOfEvent('place:created')).toBeNull();
expect(tripAddonOfEvent('toString:x')).toBeNull();
});
it('PEVADDON-002: an event of no addon goes out at once, without asking', () => {
const deliver = vi.fn();
const addons = { isEnabledForTrip: vi.fn() } as unknown as AddonsService;
tripEventSink(addons, undefined, deliver)(5, 'place:created', undefined);
expect(deliver).toHaveBeenCalledWith(5, 'place:created', undefined);
expect(addons.isEnabledForTrip).not.toHaveBeenCalled();
});
it('PEVADDON-003: an addon event goes out only for a trip that has the addon on', async () => {
const deliver = vi.fn();
const addons = {
isEnabledForTrip: vi.fn(async (tripId: number, id: string) => !(tripId === 5 && id === 'budget')),
} as unknown as AddonsService;
const sink = tripEventSink(addons, undefined, deliver);
sink(5, 'budget:created', undefined);
sink(6, 'budget:created', undefined);
sink(5, 'packing:created', undefined);
await settle();
expect(deliver.mock.calls.map(([tripId, event]) => `${tripId} ${event}`)).toEqual([
'6 budget:created',
'5 packing:created',
]);
});
it('PEVADDON-004: an addon that cannot be read sends nothing', async () => {
const deliver = vi.fn();
const addons = {
isEnabledForTrip: vi.fn(async () => Promise.reject(new Error('db gone'))),
} as unknown as AddonsService;
tripEventSink(addons, undefined, deliver)(5, 'file:created', undefined);
await settle();
expect(deliver).not.toHaveBeenCalled();
});
});
@@ -83,7 +83,7 @@ function guardsFor() {
guards: new PluginGuards(
db,
{ checkPermission: vi.fn(() => true) } as unknown as PermissionsService,
{ isAddonEnabled: vi.fn(() => true) } as unknown as AddonsService,
{ isAddonEnabled: vi.fn(() => true), isEnabledForTrip: vi.fn(async () => true) } as unknown as AddonsService,
{ getRole: vi.fn(async () => 'user') } as unknown as UsersRepository,
),
};
@@ -6,6 +6,7 @@
* length/count caps.
*/
import type { TripsRepository } from '../../../src/db/repositories/Trips.repository';
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import { ViewContributionsController } from '../../../src/nest/plugins/contributions/view-contributions.controller';
import type { PluginHooks } from '../../../src/nest/plugins/plugin-hooks.service';
import { TripAccessService } from '../../../src/nest/trip-membership/trip-access.service';
@@ -23,7 +24,7 @@ vi.mock('../../../src/nest/plugins/kill-switch', () => ({ pluginsEnabled }));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const req = (id?: number) => ({ user: id === undefined ? undefined : { id } }) as any;
function controller(invoke: (id: string) => unknown, providers = ['p1']) {
function controller(invoke: (id: string) => unknown, providers = ['p1'], offForTrip: readonly string[] = []) {
const runtime = {
providersOf: vi.fn(() => providers),
tableContributions: vi.fn(async (id: string) => invoke(id)),
@@ -32,6 +33,9 @@ function controller(invoke: (id: string) => unknown, providers = ['p1']) {
c: new ViewContributionsController(
runtime,
new TripAccessService({ findAccessible: canAccessTrip } as unknown as TripsRepository),
{
isEnabledForTrip: vi.fn(async (_tripId: number, id: string) => !offForTrip.includes(id)),
} as unknown as AddonsService,
),
runtime,
};
@@ -152,4 +156,13 @@ describe('ViewContributionsController', () => {
expect(out.map((o) => o.id)).toEqual(['from-p1']);
expect(runtime.tableContributions).toHaveBeenCalledWith('p1', 'reservations', 1, 5);
});
it('VIEWCONTRIB-ADDON: a view whose addon is off for the trip gets nothing, the other views still do', async () => {
const { c, runtime } = controller(() => [col()], ['p1'], ['budget', 'packing']);
const off = await Promise.all(['costs', 'packing', 'todos'].map((view) => c.get(view, '1', req(5))));
expect(off.map((answer) => answer.contributions)).toEqual([[], [], []]);
expect(runtime.tableContributions).not.toHaveBeenCalled();
expect((await c.get('files', '1', req(5))).contributions).toHaveLength(1);
expect((await c.get('reservations', '1', req(5))).contributions).toHaveLength(1);
});
});