Files
TREK/server/src/nest/plugins/plugin-event-addons.ts
T
Maurice 3f38621024 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.
2026-11-21 13:23:09 +01:00

52 lines
2.1 KiB
TypeScript

import { ADDON_IDS } from '../../addons';
import type { PluginEventMeta } from '../../plugin-event-sink';
import type { AddonsService } from '../addons/addons.service';
import { withRequestContext } from '../database/request-context';
import type { MikroORM } from '@mikro-orm/core';
/**
* The trip addon a core event belongs to, read from its family (the part of
* the name before the first colon), or null for an event of the trip itself.
* A trip that has the addon off sends a plugin none of that family's events,
* as it shows none of their rows: costs, lists, files, collab, road trip and
* tours stay between the trip's members while the owner keeps them off.
*/
const FAMILY_ADDON: Readonly<Record<string, string>> = {
budget: ADDON_IDS.BUDGET,
packing: ADDON_IDS.PACKING,
todo: ADDON_IDS.PACKING,
file: ADDON_IDS.DOCUMENTS,
collab: ADDON_IDS.COLLAB,
roadtripBoundary: ADDON_IDS.ROADTRIP,
roadtripPreferences: ADDON_IDS.ROADTRIP,
roadtripTrack: ADDON_IDS.ROADTRIP,
roadtripVia: ADDON_IDS.ROADTRIP,
tours: ADDON_IDS.TOURS,
};
export function tripAddonOfEvent(event: string): string | null {
const family = event.split(':', 1)[0];
return Object.prototype.hasOwnProperty.call(FAMILY_ADDON, family) ? FAMILY_ADDON[family] : null;
}
type Deliver = (tripId: number, event: string, meta?: PluginEventMeta) => void;
/**
* The plugin event sink with the trip's addons in front of it: an event of a
* trip addon's family is handed on only once the addon is read as on for the
* trip, any other event at once. An addon that cannot be read sends nothing,
* since events are best-effort and a family the owner switched off must not
* leak through a failed read.
*/
export function tripEventSink(addons: AddonsService, orm: MikroORM | undefined, deliver: Deliver): Deliver {
return (tripId, event, meta) => {
const addonId = tripAddonOfEvent(event);
if (!addonId) return deliver(tripId, event, meta);
const ask = () => addons.isEnabledForTrip(tripId, addonId);
void (orm ? withRequestContext(orm, ask) : ask()).then(
(on) => on && deliver(tripId, event, meta),
() => undefined,
);
};
}