mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
3f38621024
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.
188 lines
10 KiB
TypeScript
188 lines
10 KiB
TypeScript
import { ADDON_IDS } from '../../addons';
|
|
import { PluginGuards, type RpcAddon } from '../../nest-rpc/plugin-guards.service';
|
|
import { BadParams, ForbiddenResource } from '../../nest-rpc/rpc-errors';
|
|
import { PluginController, PluginMethod } from '../../nest-rpc/rpc-kit/decorators';
|
|
import type { PluginRpcContext } from '../../nest-rpc/rpc-kit/types';
|
|
import { asPayload, rowIdParam, schemaMessage } from '../../nest-rpc/rpc-params';
|
|
import { isUpdateConflict } from '../common/conflictResult';
|
|
import { RealtimeService } from '../realtime/realtime.service';
|
|
import { PackingService, isInvalidBagRef } from './packing.service';
|
|
import { packingCreateItemRequestSchema, packingUpdateItemRequestSchema } from '@trek/shared';
|
|
|
|
/** Packing rides on the app's own 'packing_edit' permission, exactly like the REST path. */
|
|
const PACKING_EDIT_ACTION = 'packing_edit';
|
|
|
|
type PrivacyItem = { is_private?: number; owner_id?: number | null; recipients?: { user_id: number }[] };
|
|
|
|
/** Every read and write here belongs to the packing addon and follows it per trip. */
|
|
const PACKING_ADDON: RpcAddon = { addonId: ADDON_IDS.PACKING, noun: 'packing' };
|
|
|
|
/**
|
|
* The packing surface a plugin may reach (#plugins).
|
|
*
|
|
* The privacy-scoped broadcasts (#858) go through PackingService, the same methods
|
|
* the REST controller calls. They used to be duplicated inside the plugin deps
|
|
* factory, with a comment asking for both copies to be kept in lockstep by hand;
|
|
* that copy is gone. Getting this wrong leaks a private item to the whole trip room,
|
|
* which is why `wasPrivate` is read BEFORE the write and why the four transitions
|
|
* live in exactly one place.
|
|
*
|
|
* Bags carry no privacy, so they broadcast to the room unfiltered.
|
|
*/
|
|
@PluginController()
|
|
export class PackingRpc {
|
|
constructor(
|
|
private readonly packing: PackingService,
|
|
private readonly realtime: RealtimeService,
|
|
private readonly guards: PluginGuards,
|
|
) {}
|
|
|
|
@PluginMethod('packing.list', { permission: 'db:read:packing' })
|
|
list(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
|
// Scoped to the acting user so the #858 visibility filter applies: a plugin must
|
|
// not see another member's private items.
|
|
return this.guards.tripRead(
|
|
params,
|
|
ctx,
|
|
(userId) => this.packing.listItems(rowIdParam(params.tripId, 'tripId'), userId),
|
|
PACKING_ADDON,
|
|
);
|
|
}
|
|
|
|
@PluginMethod('packing.create', { permission: 'db:write:packing' })
|
|
async create(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
|
|
const tripId = rowIdParam(params.tripId, 'tripId');
|
|
const actor = this.guards.requireActor(ctx, 'packing item');
|
|
const parsed = packingCreateItemRequestSchema.safeParse(params.input);
|
|
if (!parsed.success) throw new BadParams(`invalid packing item: ${schemaMessage(parsed.error)}`);
|
|
await this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION, PACKING_ADDON);
|
|
const item = (await this.packing.createItem(String(tripId), parsed.data as never, actor)) as PrivacyItem;
|
|
// A referenced bag must exist on this trip (#2154), as on the REST route.
|
|
if (isInvalidBagRef(item)) throw new BadParams(`no packing bag ${parsed.data.bag_id} on trip ${tripId}`);
|
|
this.packing.emitToViewers(String(tripId), 'packing:created', { item }, item, undefined);
|
|
this.packing.broadcastBagTotals(String(tripId));
|
|
return item;
|
|
}
|
|
|
|
@PluginMethod('packing.update', { permission: 'db:write:packing' })
|
|
async update(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
|
|
const tripId = rowIdParam(params.tripId, 'tripId');
|
|
const itemId = rowIdParam(params.itemId, 'itemId');
|
|
const actor = this.guards.requireActor(ctx, 'packing item');
|
|
const parsed = packingUpdateItemRequestSchema.safeParse(params.input);
|
|
if (!parsed.success) throw new BadParams(`invalid packing item: ${schemaMessage(parsed.error)}`);
|
|
await this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION, PACKING_ADDON);
|
|
// Read the privacy BEFORE the write, so a public/private toggle routes correctly.
|
|
const before = await this.packing.getItemPrivacy(tripId, itemId);
|
|
const input = parsed.data as Record<string, unknown>;
|
|
// Plan 4 Task 8b (U6) — itemId is already a real row id (rowIdParam() above);
|
|
// PackingService.updateItem's id param no longer needs the String() wrapper.
|
|
const updated = await this.packing.updateItem(
|
|
String(tripId),
|
|
itemId,
|
|
input as never,
|
|
Object.keys(input),
|
|
undefined,
|
|
actor,
|
|
);
|
|
if (!updated) throw new ForbiddenResource(`no packing item ${itemId} on trip ${tripId}`);
|
|
if (isUpdateConflict(updated)) throw new BadParams('packing item was modified concurrently');
|
|
// A referenced bag must exist on this trip (#2154), as on the REST route.
|
|
if (isInvalidBagRef(updated)) throw new BadParams(`no packing bag ${parsed.data.bag_id} on trip ${tripId}`);
|
|
this.packing.broadcastUpdate(String(tripId), itemId, updated as PrivacyItem, !!before?.is_private, undefined);
|
|
if (['weight_grams', 'quantity', 'bag_id'].some((k) => Object.keys(input).includes(k))) {
|
|
this.packing.broadcastBagTotals(String(tripId));
|
|
}
|
|
return updated;
|
|
}
|
|
|
|
@PluginMethod('packing.delete', { permission: 'db:write:packing' })
|
|
async delete(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
|
|
const tripId = rowIdParam(params.tripId, 'tripId');
|
|
const itemId = rowIdParam(params.itemId, 'itemId');
|
|
const actor = this.guards.requireActor(ctx, 'packing item');
|
|
await this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION, PACKING_ADDON);
|
|
// Plan 4 Task 8b (U6) — same drop of itemId's String() wrapper as update above.
|
|
const deleted = (await this.packing.deleteItem(String(tripId), itemId, actor)) as PrivacyItem | null;
|
|
if (!deleted) throw new ForbiddenResource(`no packing item ${itemId} on trip ${tripId}`);
|
|
this.packing.emitToViewers(String(tripId), 'packing:deleted', { itemId }, deleted, undefined);
|
|
this.packing.broadcastBagTotals(String(tripId));
|
|
return { deleted: true };
|
|
}
|
|
|
|
@PluginMethod('packing.listBags', { permission: 'db:write:packing' })
|
|
async listBags(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown[]> {
|
|
// Note the permission: the envelope really does gate this READ on the write
|
|
// grant. The decorator makes the oddity visible instead of burying it.
|
|
return await this.guards.tripRead(
|
|
params,
|
|
ctx,
|
|
async () => (await this.packing.listBags(String(rowIdParam(params.tripId, 'tripId')))) as unknown[],
|
|
PACKING_ADDON,
|
|
);
|
|
}
|
|
|
|
@PluginMethod('packing.createBag', { permission: 'db:write:packing' })
|
|
async createBag(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
|
|
const tripId = rowIdParam(params.tripId, 'tripId');
|
|
const actor = this.guards.requireActor(ctx, 'packing bag');
|
|
const input = asPayload(params.input);
|
|
if (typeof input.name !== 'string' || input.name.trim() === '') throw new BadParams('bag name is required');
|
|
await this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION, PACKING_ADDON);
|
|
const bag = await this.packing.createBag(String(tripId), {
|
|
name: input.name,
|
|
color: typeof input.color === 'string' ? input.color : undefined,
|
|
weight_limit_grams: typeof input.weight_limit_grams === 'number' ? input.weight_limit_grams : undefined,
|
|
});
|
|
this.realtime.broadcast(tripId, 'packing:bag-created', { bag }, undefined);
|
|
return bag;
|
|
}
|
|
|
|
@PluginMethod('packing.updateBag', { permission: 'db:write:packing' })
|
|
async updateBag(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
|
|
const tripId = rowIdParam(params.tripId, 'tripId');
|
|
const bagId = rowIdParam(params.bagId, 'bagId');
|
|
const actor = this.guards.requireActor(ctx, 'packing bag');
|
|
await this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION, PACKING_ADDON);
|
|
const input = asPayload(params.input);
|
|
// Plan 4 Task 8b (U6) — bagId is already a real row id (rowIdParam() above);
|
|
// PackingService.updateBag's bagId param no longer needs the String() wrapper.
|
|
const bag = await this.packing.updateBag(String(tripId), bagId, input as never, Object.keys(input));
|
|
if (!bag) throw new ForbiddenResource(`no packing bag ${bagId} on trip ${tripId}`);
|
|
this.realtime.broadcast(tripId, 'packing:bag-updated', { bag }, undefined);
|
|
return bag;
|
|
}
|
|
|
|
@PluginMethod('packing.deleteBag', { permission: 'db:write:packing' })
|
|
async deleteBag(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
|
|
const tripId = rowIdParam(params.tripId, 'tripId');
|
|
const bagId = rowIdParam(params.bagId, 'bagId');
|
|
const actor = this.guards.requireActor(ctx, 'packing bag');
|
|
await this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION, PACKING_ADDON);
|
|
// Plan 4 Task 8b (U6) — same drop of bagId's String() wrapper as updateBag above.
|
|
if (!(await this.packing.deleteBag(String(tripId), bagId))) {
|
|
throw new ForbiddenResource(`no packing bag ${bagId} on trip ${tripId}`);
|
|
}
|
|
this.realtime.broadcast(tripId, 'packing:bag-deleted', { bagId }, undefined);
|
|
// bag_id is ON DELETE SET NULL — its items just moved to the unassigned pile.
|
|
this.packing.broadcastBagTotals(String(tripId));
|
|
return { deleted: true };
|
|
}
|
|
|
|
@PluginMethod('packing.setBagMembers', { permission: 'db:write:packing' })
|
|
async setBagMembers(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
|
|
const tripId = rowIdParam(params.tripId, 'tripId');
|
|
const bagId = rowIdParam(params.bagId, 'bagId');
|
|
const actor = this.guards.requireActor(ctx, 'packing bag');
|
|
await this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION, PACKING_ADDON);
|
|
// userIds sits on the params object itself, not under `input`.
|
|
const raw = asPayload(params).userIds;
|
|
const userIds = Array.isArray(raw) ? raw.filter((x): x is number => typeof x === 'number') : [];
|
|
// Plan 4 Task 8b (U6) — same drop of bagId's String() wrapper as updateBag above.
|
|
const members = await this.packing.setBagMembers(String(tripId), bagId, userIds);
|
|
if (!members) throw new ForbiddenResource(`no packing bag ${bagId} on trip ${tripId}`);
|
|
this.realtime.broadcast(tripId, 'packing:bag-members-updated', { bagId, members }, undefined);
|
|
return members;
|
|
}
|
|
}
|