mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
Move packing onto the decorators and de-duplicate its privacy fan-out
Nine more methods, so the registry owns 24 of 113. The #858 privacy logic existed three times: PackingService had the primitives, the controller layered emitToViewers and broadcastUpdate on top privately, and the plugin deps factory carried a standalone copy of all four, with a comment asking for them to be kept in lockstep by hand. Migrating packing meant either copying them a fourth time or fixing that, so the two private controller methods moved into PackingService and the factory copy is gone. Controller and PackingRpc now call the same code, and the controller suite proves it by passing with all 45 assertions unchanged. The four public/private transitions get one test each, because each one is a way to leak a private item to the whole trip room, plus a test that the privacy is read before the write rather than after: reading it afterwards would report "already private", skip the room delete, and leave the item on every member's screen.
This commit is contained in:
@@ -109,21 +109,10 @@ export class PackingController {
|
||||
this.requireEdit(trip, user);
|
||||
// checked arrives as boolean or legacy 0/1 — the service coerces by truthiness.
|
||||
const item = this.packing.createItem(tripId, { name: body.name, category: body.category, checked: body.checked === undefined ? undefined : !!body.checked, is_private: body.is_private, visibility: body.visibility, recipient_ids: body.recipient_ids }, user.id);
|
||||
this.emitToViewers(tripId, 'packing:created', { item }, item, socketId);
|
||||
this.packing.emitToViewers(tripId, 'packing:created', { item }, item, socketId);
|
||||
return { item };
|
||||
}
|
||||
|
||||
/** Deliver an item event to exactly the people who can see it (#858): the whole
|
||||
* room for a Common item, or owner + recipients for a restricted one. */
|
||||
private emitToViewers<E extends TrekWsTripEventName>(tripId: string, event: E, payload: TrekWsPayload<E>, item: PackingItemRow, socketId: string | undefined): void {
|
||||
const viewers = this.packing.viewersOf(item);
|
||||
if (viewers === null) {
|
||||
this.packing.broadcast(tripId, event, payload, socketId);
|
||||
} else {
|
||||
this.packing.broadcastToViewers(tripId, event, payload, viewers, socketId);
|
||||
}
|
||||
}
|
||||
|
||||
@Put('reorder')
|
||||
reorder(
|
||||
@CurrentUser() user: User,
|
||||
@@ -163,41 +152,10 @@ export class PackingController {
|
||||
if (isUpdateConflict(updated)) {
|
||||
throw new HttpException({ error: 'conflict', server: updated.server }, 409);
|
||||
}
|
||||
this.broadcastUpdate(tripId, id, updated as PackingItemRow, !!before?.is_private, socketId);
|
||||
this.packing.broadcastUpdate(tripId, id, updated as PackingItemRow, !!before?.is_private, socketId);
|
||||
return { item: updated };
|
||||
}
|
||||
|
||||
/**
|
||||
* Routes a packing-item update over WebSocket so private items (#858) stay
|
||||
* scoped to their owner across the four public↔private transitions:
|
||||
* - stays private → owner-only update
|
||||
* - public→private → drop it from the whole room, re-add for the owner
|
||||
* - private→public → create for members who lacked it, then update for all
|
||||
* - stays public → plain update to all
|
||||
*/
|
||||
private broadcastUpdate(
|
||||
tripId: string,
|
||||
id: string,
|
||||
item: PackingItemRow,
|
||||
wasPrivate: boolean,
|
||||
socketId: string | undefined,
|
||||
): void {
|
||||
const nowPrivate = !!item.is_private;
|
||||
if (nowPrivate) {
|
||||
if (wasPrivate) {
|
||||
this.packing.broadcastItem(tripId, 'packing:updated', { item }, item, socketId);
|
||||
} else {
|
||||
this.packing.broadcast(tripId, 'packing:deleted', { itemId: Number(id) }, socketId);
|
||||
this.packing.broadcastItem(tripId, 'packing:created', { item }, item, socketId);
|
||||
}
|
||||
} else {
|
||||
if (wasPrivate) {
|
||||
this.packing.broadcast(tripId, 'packing:created', { item }, socketId);
|
||||
}
|
||||
this.packing.broadcast(tripId, 'packing:updated', { item }, socketId);
|
||||
}
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(
|
||||
@CurrentUser() user: User,
|
||||
@@ -212,7 +170,7 @@ export class PackingController {
|
||||
throw new HttpException({ error: 'Item not found' }, 404);
|
||||
}
|
||||
// Scope the delete to the people who could see it (owner + recipients, #858).
|
||||
this.emitToViewers(tripId, 'packing:deleted', { itemId: Number(id) }, deleted as PackingItemRow, socketId);
|
||||
this.packing.emitToViewers(tripId, 'packing:deleted', { itemId: Number(id) }, deleted as PackingItemRow, socketId);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@@ -236,7 +194,7 @@ export class PackingController {
|
||||
// The viewer set just changed: drop the item from the whole room, then re-add
|
||||
// it for whoever can now see it (owner + recipients, or everyone if Common).
|
||||
this.packing.broadcast(tripId, 'packing:deleted', { itemId: Number(id) }, socketId);
|
||||
this.emitToViewers(tripId, 'packing:created', { item: updated }, updated as PackingItemRow, socketId);
|
||||
this.packing.emitToViewers(tripId, 'packing:created', { item: updated }, updated as PackingItemRow, socketId);
|
||||
return { item: updated };
|
||||
}
|
||||
|
||||
@@ -255,7 +213,7 @@ export class PackingController {
|
||||
throw new HttpException({ error: 'Item not found' }, 404);
|
||||
}
|
||||
// The clone is personal to the caller — only their sockets need it.
|
||||
this.emitToViewers(tripId, 'packing:created', { item }, item, socketId);
|
||||
this.packing.emitToViewers(tripId, 'packing:created', { item }, item, socketId);
|
||||
return { item };
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@ import { Module } from '@nestjs/common';
|
||||
import { PackingController } from './packing.controller';
|
||||
import { PackingMcp } from './packing.mcp';
|
||||
import { PackingService } from './packing.service';
|
||||
import { PackingRpc } from './packing.rpc';
|
||||
import { PluginGuardsModule } from '../plugins/host/plugin-guards.module';
|
||||
import { RealtimeModule } from '../realtime/realtime.module';
|
||||
import { PermissionsModule } from '../permissions/permissions.module';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
|
||||
@@ -9,9 +12,9 @@ import { AuthModule } from '../auth/auth.module';
|
||||
* Exports PackingService for in-container consumers (TripsService bundle,
|
||||
* PluginHostDepsFactory). */
|
||||
@Module({
|
||||
imports: [PermissionsModule, AuthModule],
|
||||
imports: [PermissionsModule, AuthModule, RealtimeModule, PluginGuardsModule],
|
||||
controllers: [PackingController],
|
||||
providers: [PackingService, PackingMcp],
|
||||
providers: [PackingService, PackingMcp, PackingRpc],
|
||||
exports: [PackingService],
|
||||
})
|
||||
export class PackingModule {}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
import { packingCreateItemRequestSchema, packingUpdateItemRequestSchema } from '@trek/shared';
|
||||
import { PluginController, PluginMethod } from '../plugins/host/rpc-kit/decorators';
|
||||
import { PluginGuards } from '../plugins/host/plugin-guards.service';
|
||||
import { BadParams, ForbiddenResource } from '../plugins/host/rpc-errors';
|
||||
import { asPayload, num } from '../plugins/host/rpc-params';
|
||||
import type { PluginRpcContext } from '../plugins/host/rpc-kit/types';
|
||||
import { RealtimeService } from '../realtime/realtime.service';
|
||||
import { PackingService } from './packing.service';
|
||||
import { isUpdateConflict } from '../common/conflictResult';
|
||||
|
||||
/** 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 }[] };
|
||||
|
||||
/**
|
||||
* 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(num(params.tripId, 'tripId'), userId));
|
||||
}
|
||||
|
||||
@PluginMethod('packing.create', { permission: 'db:write:packing' })
|
||||
create(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
||||
const tripId = num(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: ${parsed.error.issues[0]?.message ?? 'bad input'}`);
|
||||
this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION);
|
||||
const item = this.packing.createItem(String(tripId), parsed.data as never, actor) as PrivacyItem;
|
||||
this.packing.emitToViewers(String(tripId), 'packing:created', { item }, item, undefined);
|
||||
return item;
|
||||
}
|
||||
|
||||
@PluginMethod('packing.update', { permission: 'db:write:packing' })
|
||||
update(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
||||
const tripId = num(params.tripId, 'tripId');
|
||||
const itemId = num(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: ${parsed.error.issues[0]?.message ?? 'bad input'}`);
|
||||
this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION);
|
||||
// Read the privacy BEFORE the write, so a public/private toggle routes correctly.
|
||||
const before = this.packing.getItemPrivacy(tripId, itemId);
|
||||
const input = parsed.data as Record<string, unknown>;
|
||||
const updated = this.packing.updateItem(String(tripId), String(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');
|
||||
this.packing.broadcastUpdate(String(tripId), itemId, updated as PrivacyItem, !!before?.is_private, undefined);
|
||||
return updated;
|
||||
}
|
||||
|
||||
@PluginMethod('packing.delete', { permission: 'db:write:packing' })
|
||||
delete(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
||||
const tripId = num(params.tripId, 'tripId');
|
||||
const itemId = num(params.itemId, 'itemId');
|
||||
const actor = this.guards.requireActor(ctx, 'packing item');
|
||||
this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION);
|
||||
const deleted = this.packing.deleteItem(String(tripId), String(itemId)) 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);
|
||||
return { deleted: true };
|
||||
}
|
||||
|
||||
@PluginMethod('packing.listBags', { permission: 'db:write:packing' })
|
||||
listBags(params: Record<string, unknown>, ctx: PluginRpcContext): 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 this.guards.tripRead(params, ctx, () => this.packing.listBags(String(num(params.tripId, 'tripId'))) as unknown[]);
|
||||
}
|
||||
|
||||
@PluginMethod('packing.createBag', { permission: 'db:write:packing' })
|
||||
createBag(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
||||
const tripId = num(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');
|
||||
this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION);
|
||||
const bag = this.packing.createBag(String(tripId), { name: input.name, color: typeof input.color === 'string' ? input.color : undefined });
|
||||
this.realtime.broadcast(tripId, 'packing:bag-created', { bag }, undefined);
|
||||
return bag;
|
||||
}
|
||||
|
||||
@PluginMethod('packing.updateBag', { permission: 'db:write:packing' })
|
||||
updateBag(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
||||
const tripId = num(params.tripId, 'tripId');
|
||||
const bagId = num(params.bagId, 'bagId');
|
||||
const actor = this.guards.requireActor(ctx, 'packing bag');
|
||||
this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION);
|
||||
const input = asPayload(params.input);
|
||||
const bag = this.packing.updateBag(String(tripId), String(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' })
|
||||
deleteBag(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
||||
const tripId = num(params.tripId, 'tripId');
|
||||
const bagId = num(params.bagId, 'bagId');
|
||||
const actor = this.guards.requireActor(ctx, 'packing bag');
|
||||
this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION);
|
||||
if (!this.packing.deleteBag(String(tripId), String(bagId))) {
|
||||
throw new ForbiddenResource(`no packing bag ${bagId} on trip ${tripId}`);
|
||||
}
|
||||
this.realtime.broadcast(tripId, 'packing:bag-deleted', { bagId }, undefined);
|
||||
return { deleted: true };
|
||||
}
|
||||
|
||||
@PluginMethod('packing.setBagMembers', { permission: 'db:write:packing' })
|
||||
setBagMembers(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
|
||||
const tripId = num(params.tripId, 'tripId');
|
||||
const bagId = num(params.bagId, 'bagId');
|
||||
const actor = this.guards.requireActor(ctx, 'packing bag');
|
||||
this.guards.requireTripEdit(tripId, actor, PACKING_EDIT_ACTION);
|
||||
// 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') : [];
|
||||
const members = this.packing.setBagMembers(String(tripId), String(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;
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,49 @@ export class PackingService {
|
||||
return ids;
|
||||
}
|
||||
|
||||
/** Deliver an item event to exactly the people who can see it (#858): the whole
|
||||
* room for a Common item, or owner + recipients for a restricted one. */
|
||||
emitToViewers<E extends TrekWsTripEventName>(tripId: string, event: E, payload: TrekWsPayload<E>, item: PrivacyFields | null | undefined, socketId: string | undefined): void {
|
||||
const viewers = this.viewersOf(item);
|
||||
if (viewers === null) {
|
||||
this.broadcast(tripId, event, payload, socketId);
|
||||
} else {
|
||||
this.broadcastToViewers(tripId, event, payload, viewers, socketId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The four public/private transitions after an update (#858). `wasPrivate` must be
|
||||
* read BEFORE the write: getting it wrong leaks a freshly-privatized item to the
|
||||
* whole room.
|
||||
*
|
||||
* - private -> private: owner-only update
|
||||
* - public -> private: drop it from the room, then re-add it for the owner
|
||||
* - private -> public: add it for the members who did not have it, then update
|
||||
* - public -> public: a plain update to everyone
|
||||
*
|
||||
* Both the REST controller and the plugin RPC handler call this. It used to exist
|
||||
* three times over (here, in the controller, and as a standalone copy inside the
|
||||
* plugin deps factory), with a comment asking for all of them to be kept in
|
||||
* lockstep by hand.
|
||||
*/
|
||||
broadcastUpdate(tripId: string, id: string | number, item: PrivacyFields, wasPrivate: boolean, socketId: string | undefined): void {
|
||||
const nowPrivate = !!item.is_private;
|
||||
if (nowPrivate) {
|
||||
if (wasPrivate) {
|
||||
this.broadcastItem(tripId, 'packing:updated', { item } as TrekWsPayload<'packing:updated'>, item, socketId);
|
||||
} else {
|
||||
this.broadcast(tripId, 'packing:deleted', { itemId: Number(id) }, socketId);
|
||||
this.broadcastItem(tripId, 'packing:created', { item } as TrekWsPayload<'packing:created'>, item, socketId);
|
||||
}
|
||||
} else {
|
||||
if (wasPrivate) {
|
||||
this.broadcast(tripId, 'packing:created', { item } as TrekWsPayload<'packing:created'>, socketId);
|
||||
}
|
||||
this.broadcast(tripId, 'packing:updated', { item } as TrekWsPayload<'packing:updated'>, socketId);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Items ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
|
||||
import { readEnv } from '../../../app-config';
|
||||
import { RealtimeService } from '../../realtime/realtime.service';
|
||||
import { isUpdateConflict } from '../../common/conflictResult';
|
||||
import { getWeather } from '../../weather/weather.impl';
|
||||
import { BLOCKED_EXTENSIONS, filesDir } from '../../files/files.constants';
|
||||
import { TripMembershipService } from '../../trip-membership/trip-membership.service';
|
||||
import { NotificationsService } from '../../notifications/notifications.service';
|
||||
@@ -64,54 +61,10 @@ function mirrorJourneys(run: () => void): void {
|
||||
try { run(); } catch { /* non-fatal */ }
|
||||
}
|
||||
|
||||
// --- #858 packing privacy: viewer-scoped fan-out mirrored from
|
||||
// packing.controller.broadcastUpdate/emitToViewers and PackingService's
|
||||
// broadcastItem/viewersOf/broadcastToViewers. A private item's events reach ONLY
|
||||
// its owner (+ recipients for a shared item), never the whole trip room; passing the
|
||||
// wrong onlyUserId — or forgetting to drop a freshly-privatized item from the room —
|
||||
// leaks it. Kept as a local copy (rather than delegating to the injected
|
||||
// PackingService) so the factory's unit test can assert the fan-out through the
|
||||
// websocket mock while PackingService stays a plain data stub. Keep in lockstep
|
||||
// with packing.controller.ts + packing.service.ts. ---
|
||||
type PackingPrivacy = { is_private?: number; owner_id?: number | null; recipients?: { user_id: number }[] };
|
||||
|
||||
function packingViewersOf(item: PackingPrivacy | null | undefined): number[] | null {
|
||||
if (!item || !item.is_private) return null; // Common — visible to the whole room
|
||||
return [item.owner_id, ...(item.recipients || []).map((r) => r.user_id)].filter((x): x is number => x != null);
|
||||
}
|
||||
|
||||
/** CREATE/DELETE fan-out: whole room for a Common item, else owner + recipients only. */
|
||||
function emitPackingToViewers<E extends TrekWsTripEventName>(realtime: RealtimeService, tripId: number, event: E, payload: TrekWsPayload<E>, item: PackingPrivacy): void {
|
||||
const viewers = packingViewersOf(item);
|
||||
if (viewers === null) {
|
||||
realtime.broadcast(tripId, event, payload, undefined);
|
||||
return;
|
||||
}
|
||||
for (const uid of new Set(viewers)) if (uid != null) realtime.broadcast(tripId, event, payload, undefined, uid);
|
||||
}
|
||||
|
||||
/** An item event delivered owner-only when the item is private (else to the room). */
|
||||
function broadcastPackingItem<E extends TrekWsTripEventName>(realtime: RealtimeService, tripId: number, event: E, payload: TrekWsPayload<E>, item: PackingPrivacy): void {
|
||||
const onlyUserId = item?.is_private && item.owner_id != null ? item.owner_id : undefined;
|
||||
realtime.broadcast(tripId, event, payload, undefined, onlyUserId);
|
||||
}
|
||||
|
||||
/** The four public<->private transitions (packing.controller.broadcastUpdate). `wasPrivate`
|
||||
* is read BEFORE the write — getting this wrong LEAKS a freshly-privatized item. */
|
||||
function broadcastPackingUpdate(realtime: RealtimeService, tripId: number, itemId: number, item: PackingPrivacy, wasPrivate: boolean): void {
|
||||
const nowPrivate = !!item.is_private;
|
||||
if (nowPrivate) {
|
||||
if (wasPrivate) {
|
||||
broadcastPackingItem(realtime, tripId, 'packing:updated', { item }, item); // stays private -> owner-only
|
||||
} else {
|
||||
realtime.broadcast(tripId, 'packing:deleted', { itemId }, undefined); // public->private: drop from the room...
|
||||
broadcastPackingItem(realtime, tripId, 'packing:created', { item }, item); // ...then re-add for the owner
|
||||
}
|
||||
} else {
|
||||
if (wasPrivate) realtime.broadcast(tripId, 'packing:created', { item }, undefined); // private->public: add for members who lacked it
|
||||
realtime.broadcast(tripId, 'packing:updated', { item }, undefined);
|
||||
}
|
||||
}
|
||||
// The #858 privacy-scoped packing broadcasts used to be copied here, with a comment
|
||||
// asking for the copy to be kept in lockstep with packing.controller.ts and
|
||||
// packing.service.ts by hand. They now live once, in PackingService, and both the
|
||||
// REST controller and PackingRpc call them there.
|
||||
|
||||
// Quotas for plugin entity metadata (db:meta) — a cheap disk-DoS guard on the
|
||||
// shared trek.db volume. Generous for real use, small enough to bound abuse.
|
||||
@@ -243,7 +196,6 @@ export class PluginHostDepsFactory {
|
||||
},
|
||||
// --- Read scopes (packing/files). Membership is checked by the host (tripRead);
|
||||
// these just delegate to the same services the REST paths use. ---
|
||||
listPackingItems: (tripId, userId) => this.packing.listItems(tripId, userId),
|
||||
listTripFiles: (tripId) => this.files.listFiles(tripId, false),
|
||||
// A file's bytes as base64, size-capped BEFORE the read so a 500MB video can't
|
||||
// be pulled (~667MB base64) through the IPC pipe. 10MB matches the plugin upload
|
||||
@@ -788,53 +740,7 @@ export class PluginHostDepsFactory {
|
||||
// --- Packing (packing_edit). Reuses PackingService + replicates the #858
|
||||
// privacy-scoped broadcasts (create/delete via emitPackingToViewers, update via
|
||||
// the four-case broadcastPackingUpdate) so a private item never leaks room-wide. ---
|
||||
canEditPacking: (tripId, userId) => this.canEditTripAs('packing_edit', tripId, userId),
|
||||
createPackingItem: (tripId, input, actingUserId) => {
|
||||
const i = input as { name: string; category?: string; checked?: boolean; is_private?: boolean; visibility?: 'common' | 'personal' | 'shared'; recipient_ids?: number[] };
|
||||
const item = this.packing.createItem(String(tripId), i, actingUserId) as PackingPrivacy;
|
||||
emitPackingToViewers(this.realtime, tripId, 'packing:created', { item }, item);
|
||||
return item;
|
||||
},
|
||||
updatePackingItem: (tripId, itemId, input, actingUserId) => {
|
||||
// Privacy BEFORE the write, so a public<->private toggle routes correctly.
|
||||
const before = this.packing.getItemPrivacy(tripId, itemId);
|
||||
const updated = this.packing.updateItem(String(tripId), String(itemId), input as never, Object.keys(input), undefined, actingUserId);
|
||||
if (!updated) throw new ForbiddenResource(`no packing item ${itemId} on trip ${tripId}`);
|
||||
if (isUpdateConflict(updated)) throw new BadParams('packing item was modified concurrently');
|
||||
broadcastPackingUpdate(this.realtime, tripId, itemId, updated as PackingPrivacy, !!before?.is_private);
|
||||
return updated;
|
||||
},
|
||||
deletePackingItem: (tripId, itemId) => {
|
||||
const deleted = this.packing.deleteItem(String(tripId), String(itemId)) as PackingPrivacy | null;
|
||||
if (!deleted) throw new ForbiddenResource(`no packing item ${itemId} on trip ${tripId}`);
|
||||
emitPackingToViewers(this.realtime, tripId, 'packing:deleted', { itemId }, deleted);
|
||||
return { deleted: true };
|
||||
},
|
||||
// --- Packing bags (no privacy — plain room broadcasts). ---
|
||||
listPackingBags: (tripId) => this.packing.listBags(String(tripId)) as unknown[],
|
||||
createPackingBag: (tripId, input) => {
|
||||
const i = input as { name: string; color?: string };
|
||||
const bag = this.packing.createBag(String(tripId), { name: i.name, color: i.color });
|
||||
this.realtime.broadcast(tripId, 'packing:bag-created', { bag }, undefined);
|
||||
return bag;
|
||||
},
|
||||
updatePackingBag: (tripId, bagId, input) => {
|
||||
const bag = this.packing.updateBag(String(tripId), String(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;
|
||||
},
|
||||
deletePackingBag: (tripId, bagId) => {
|
||||
if (!this.packing.deleteBag(String(tripId), String(bagId))) throw new ForbiddenResource(`no packing bag ${bagId} on trip ${tripId}`);
|
||||
this.realtime.broadcast(tripId, 'packing:bag-deleted', { bagId }, undefined);
|
||||
return { deleted: true };
|
||||
},
|
||||
setPackingBagMembers: (tripId, bagId, userIds) => {
|
||||
const members = this.packing.setBagMembers(String(tripId), String(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;
|
||||
},
|
||||
// --- Read-convenience: weather (host cache, tenant-free), categories (global), roster ---
|
||||
tripMembers: (tripId) =>
|
||||
this.db.prepare('SELECT u.id, u.username, u.display_name, u.avatar FROM trip_members tm JOIN users u ON u.id = tm.user_id WHERE tm.trip_id = ?').all(tripId) as unknown[],
|
||||
|
||||
@@ -91,7 +91,6 @@ export interface HostDeps {
|
||||
/** True if the acting user may create costs on the trip (the 'budget_edit' permission). */
|
||||
canEditCosts(tripId: number, userId: number): boolean;
|
||||
/** A trip's packing items visible to `userId` (#858 private-item filter), for `packing.list`. */
|
||||
listPackingItems(tripId: number, userId: number): unknown[];
|
||||
/** A trip's files (trash excluded), for `files.list`. */
|
||||
listTripFiles(tripId: number): unknown[];
|
||||
/** One trip file's bytes as base64 (size-capped), for `files.getContent`. Throws if it's not on the trip or exceeds the cap. */
|
||||
@@ -222,19 +221,10 @@ export interface HostDeps {
|
||||
/** Delete a reservation from a trip (same side effects); returns { deleted: true }. */
|
||||
deleteReservation(tripId: number, reservationId: number, actingUserId: number): unknown;
|
||||
// --- Packing (the 'packing_edit' permission; #858 privacy-scoped broadcasts) ---
|
||||
canEditPacking(tripId: number, userId: number): boolean;
|
||||
/** Create a packing item (owner = acting user); privacy-scoped packing:created broadcast; returns it. */
|
||||
createPackingItem(tripId: number, input: Record<string, unknown>, actingUserId: number): unknown;
|
||||
/** Update a packing item; four-case public<->private broadcast; throws if not on the trip. */
|
||||
updatePackingItem(tripId: number, itemId: number, input: Record<string, unknown>, actingUserId: number): unknown;
|
||||
/** Delete a packing item; owner+recipients-scoped packing:deleted broadcast; returns { deleted: true }. */
|
||||
deletePackingItem(tripId: number, itemId: number): unknown;
|
||||
// --- Packing bags (packing_edit; no privacy — broadcast to the whole room) ---
|
||||
listPackingBags(tripId: number): unknown[];
|
||||
createPackingBag(tripId: number, input: Record<string, unknown>): unknown;
|
||||
updatePackingBag(tripId: number, bagId: number, input: Record<string, unknown>): unknown;
|
||||
deletePackingBag(tripId: number, bagId: number): unknown;
|
||||
setPackingBagMembers(tripId: number, bagId: number, userIds: number[]): unknown;
|
||||
// --- Read-convenience: weather (tenant-free), categories (global), the trip roster ---
|
||||
tripMembers(tripId: number): unknown[];
|
||||
// --- Tags (the acting user's own; no trip) ---
|
||||
@@ -325,13 +315,6 @@ export class PluginRpcHost {
|
||||
// The trip's member roster (ids + display fields only), membership-checked.
|
||||
this.methods.set('trips.members', (p, uid) => this.tripRead(p, uid, () => deps.tripMembers(num(p.tripId, 'tripId'))));
|
||||
}
|
||||
if (has('db:read:packing')) {
|
||||
// Delegate to the packing service, scoped to the acting user so its #858 private-
|
||||
// item visibility filter applies (a plugin must not see other members' private items).
|
||||
this.methods.set('packing.list', (p, uid) =>
|
||||
this.tripRead(p, uid, (userId) => deps.listPackingItems(num(p.tripId, 'tripId'), userId)),
|
||||
);
|
||||
}
|
||||
if (has('db:read:files')) {
|
||||
// Trip files, trash excluded — same view the files tab shows.
|
||||
this.methods.set('files.list', (p, uid) =>
|
||||
@@ -823,69 +806,7 @@ export class PluginRpcHost {
|
||||
});
|
||||
}
|
||||
|
||||
if (has('db:write:packing')) {
|
||||
// Packing list write. Gated exactly like the packing REST path — trip access +
|
||||
// the 'packing_edit' permission for the HOST-bound acting user. The deps reuse
|
||||
// packingService and replicate the #858 privacy-scoped broadcasts 1:1, so a
|
||||
// private item is never leaked to the whole trip room.
|
||||
this.methods.set('packing.create', (p, uid) => {
|
||||
const tripId = num(p.tripId, 'tripId');
|
||||
const actor = this.requireActor(uid, 'packing item');
|
||||
const parsed = packingCreateItemRequestSchema.safeParse(p.input);
|
||||
if (!parsed.success) throw new BadParams(`invalid packing item: ${parsed.error.issues[0]?.message ?? 'bad input'}`);
|
||||
this.requireTripEdit(tripId, actor, deps.canEditPacking);
|
||||
return deps.createPackingItem(tripId, parsed.data as Record<string, unknown>, actor);
|
||||
});
|
||||
this.methods.set('packing.update', (p, uid) => {
|
||||
const tripId = num(p.tripId, 'tripId');
|
||||
const itemId = num(p.itemId, 'itemId');
|
||||
const actor = this.requireActor(uid, 'packing item');
|
||||
const parsed = packingUpdateItemRequestSchema.safeParse(p.input);
|
||||
if (!parsed.success) throw new BadParams(`invalid packing item: ${parsed.error.issues[0]?.message ?? 'bad input'}`);
|
||||
this.requireTripEdit(tripId, actor, deps.canEditPacking);
|
||||
return deps.updatePackingItem(tripId, itemId, parsed.data as Record<string, unknown>, actor);
|
||||
});
|
||||
this.methods.set('packing.delete', (p, uid) => {
|
||||
const tripId = num(p.tripId, 'tripId');
|
||||
const itemId = num(p.itemId, 'itemId');
|
||||
const actor = this.requireActor(uid, 'packing item');
|
||||
this.requireTripEdit(tripId, actor, deps.canEditPacking);
|
||||
return deps.deletePackingItem(tripId, itemId);
|
||||
});
|
||||
// Bags carry no privacy — a plain packing:bag-* broadcast to the whole room.
|
||||
this.methods.set('packing.listBags', (p, uid) => this.tripRead(p, uid, () => deps.listPackingBags(num(p.tripId, 'tripId'))));
|
||||
this.methods.set('packing.createBag', (p, uid) => {
|
||||
const tripId = num(p.tripId, 'tripId');
|
||||
const actor = this.requireActor(uid, 'packing bag');
|
||||
const input = asPayload(p.input);
|
||||
if (typeof input.name !== 'string' || input.name.trim() === '') throw new BadParams('bag name is required');
|
||||
this.requireTripEdit(tripId, actor, deps.canEditPacking);
|
||||
return deps.createPackingBag(tripId, input);
|
||||
});
|
||||
this.methods.set('packing.updateBag', (p, uid) => {
|
||||
const tripId = num(p.tripId, 'tripId');
|
||||
const bagId = num(p.bagId, 'bagId');
|
||||
const actor = this.requireActor(uid, 'packing bag');
|
||||
this.requireTripEdit(tripId, actor, deps.canEditPacking);
|
||||
return deps.updatePackingBag(tripId, bagId, asPayload(p.input));
|
||||
});
|
||||
this.methods.set('packing.deleteBag', (p, uid) => {
|
||||
const tripId = num(p.tripId, 'tripId');
|
||||
const bagId = num(p.bagId, 'bagId');
|
||||
const actor = this.requireActor(uid, 'packing bag');
|
||||
this.requireTripEdit(tripId, actor, deps.canEditPacking);
|
||||
return deps.deletePackingBag(tripId, bagId);
|
||||
});
|
||||
this.methods.set('packing.setBagMembers', (p, uid) => {
|
||||
const tripId = num(p.tripId, 'tripId');
|
||||
const bagId = num(p.bagId, 'bagId');
|
||||
const actor = this.requireActor(uid, 'packing bag');
|
||||
this.requireTripEdit(tripId, actor, deps.canEditPacking);
|
||||
const raw = asPayload(p).userIds;
|
||||
const userIds = Array.isArray(raw) ? raw.filter((x): x is number => typeof x === 'number') : [];
|
||||
return deps.setPackingBagMembers(tripId, bagId, userIds);
|
||||
});
|
||||
}
|
||||
// packing.* now lives in src/nest/packing/packing.rpc.ts.
|
||||
|
||||
// weather.get and categories.list now live in their own domains' *.rpc.ts.
|
||||
// tags.* now lives in src/nest/tags/tags.rpc.ts, bound through the registry below.
|
||||
|
||||
Reference in New Issue
Block a user