mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
fix(packing): a bag weighs what everyone put in it, not just what you can see
A bag's weight was summed in the browser over the item list the viewer is allowed to see. Personal items belonging to other members are filtered out of that list by design (#858), so their weight vanished with them: two people looking at the same shared bag saw two different totals, and neither saw the real one. That number is then measured against `weight_limit_grams`, an absolute airline limit. A per-viewer subtotal there is not merely incomplete but wrong — a shared bag could sit over its limit and warn nobody. The sum now happens in SQL over every row and ships as `total_weight_grams` on the bag, with `unassigned_weight_grams` alongside so the "no bag" pile and the grand total follow the same rule; a screen mixing true totals with per-viewer ones would be worse than either. Only integers cross the wire: a member learns that a bag is heavier than the items they can see, never a name, category, quantity or owner. That disclosure is deliberate and bounded, and it is what the issue asks for. The item lists stay viewer-scoped, so #1767 ("someone shared it with you, but they are the one bringing it") is unchanged — it just no longer decides what a bag weighs. Totals also had to stay live, because the reported repro is two people looking at the same screen. A private item's event reaches only its owner, which is the very rule that made the totals wrong, so item writes now also ping the room with a content-free `packing:bag-totals` and everyone re-reads the numbers. All three surfaces — the bag sidebar, the bag modal and the mobile sheet — read the server figure and fall back to the local sum only for a bag cached offline before this change. Closes #2191 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S5VwmiBQ9sPCJwfmpv64r3
This commit is contained in:
@@ -85,6 +85,7 @@ export class PackingController {
|
||||
for (const item of created) {
|
||||
this.packing.broadcastItem(tripId, 'packing:created', { item }, item, socketId);
|
||||
}
|
||||
this.packing.broadcastBagTotals(tripId);
|
||||
return { items: created, count: created.length };
|
||||
}
|
||||
|
||||
@@ -104,6 +105,7 @@ export class PackingController {
|
||||
throw new HttpException({ error: 'Bag not found' }, 400);
|
||||
}
|
||||
this.packing.emitToViewers(tripId, 'packing:created', { item }, item, socketId);
|
||||
this.packing.broadcastBagTotals(tripId);
|
||||
return { item };
|
||||
}
|
||||
|
||||
@@ -149,6 +151,7 @@ export class PackingController {
|
||||
throw new HttpException({ error: 'Bag not found' }, 400);
|
||||
}
|
||||
this.packing.broadcastUpdate(tripId, id, updated as PackingItemRow, !!before?.is_private, socketId);
|
||||
this.packing.broadcastBagTotals(tripId);
|
||||
return { item: updated };
|
||||
}
|
||||
|
||||
@@ -166,6 +169,7 @@ export class PackingController {
|
||||
}
|
||||
// Scope the delete to the people who could see it (owner + recipients, #858).
|
||||
this.packing.emitToViewers(tripId, 'packing:deleted', { itemId: Number(id) }, deleted as PackingItemRow, socketId);
|
||||
this.packing.broadcastBagTotals(tripId);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@@ -207,6 +211,7 @@ export class PackingController {
|
||||
}
|
||||
// The clone is personal to the caller — only their sockets need it.
|
||||
this.packing.emitToViewers(tripId, 'packing:created', { item }, item, socketId);
|
||||
this.packing.broadcastBagTotals(tripId);
|
||||
return { item };
|
||||
}
|
||||
|
||||
@@ -248,7 +253,13 @@ export class PackingController {
|
||||
|
||||
@Get('bags')
|
||||
listBags(@CurrentUser() user: User, @Param('tripId') tripId: string) {
|
||||
return { bags: this.packing.listBags(tripId) };
|
||||
// unassigned_weight_grams rides along so the "no bag" pile and the grand
|
||||
// total follow the same rule as the bags themselves (#2191) — a screen
|
||||
// mixing true totals with per-viewer ones would be worse than either.
|
||||
return {
|
||||
bags: this.packing.listBags(tripId),
|
||||
unassigned_weight_grams: this.packing.unassignedWeightGrams(tripId),
|
||||
};
|
||||
}
|
||||
|
||||
@RequirePermission('packing_edit')
|
||||
@@ -324,6 +335,7 @@ export class PackingController {
|
||||
throw new HttpException({ error: 'Template not found or empty' }, 404);
|
||||
}
|
||||
this.packing.broadcastItem(tripId, 'packing:template-applied', { items: added }, added[0], socketId);
|
||||
this.packing.broadcastBagTotals(tripId);
|
||||
return { items: added, count: added.length };
|
||||
}
|
||||
|
||||
|
||||
@@ -112,6 +112,21 @@ export class PackingService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell the whole room its bag weights moved (#2191).
|
||||
*
|
||||
* Deliberately NOT excluding the originating socket, which every other
|
||||
* broadcast here does: the payload carries nothing to echo, and the sender's
|
||||
* own client cannot recompute a server-side total from the item it just
|
||||
* wrote either. Everyone refetches, everyone gets numbers.
|
||||
*
|
||||
* Called for item writes, not bag writes — a bag's own create/update already
|
||||
* broadcasts the row, and neither changes what is inside it.
|
||||
*/
|
||||
broadcastBagTotals(tripId: string): void {
|
||||
this.broadcast(tripId, 'packing:bag-totals', {}, undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -489,8 +504,42 @@ export class PackingService {
|
||||
|
||||
// ── Bags ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* What each bag actually weighs (#2191).
|
||||
*
|
||||
* Every weight TREK showed used to be a client-side sum over `listItems`,
|
||||
* which is privacy-filtered — so a bag's "total" silently omitted the private
|
||||
* items of every other member, and no one but their owner could ever see the
|
||||
* real figure. That number is then measured against `weight_limit_grams`, an
|
||||
* absolute airline limit, which makes a per-viewer subtotal not merely
|
||||
* incomplete but wrong: a shared bag could sit over its limit and warn nobody.
|
||||
*
|
||||
* So the sum is computed here, over EVERY row, and only integers cross the
|
||||
* wire. A member learns that a bag is heavier than the items they can see —
|
||||
* never a name, category, quantity or owner. That is a deliberate, bounded
|
||||
* disclosure and the point of the issue.
|
||||
*
|
||||
* Keyed by bag id, with the unassigned pile under `null` — the same shape the
|
||||
* "no bag" row on every packing surface needs.
|
||||
*/
|
||||
private bagWeightTotals(tripId: string | number): Map<number | null, number> {
|
||||
const rows = this.db.all<{ bag_id: number | null; total: number | null }>(`
|
||||
SELECT bag_id, SUM(COALESCE(weight_grams, 0) * COALESCE(quantity, 1)) AS total
|
||||
FROM packing_items
|
||||
WHERE trip_id = ?
|
||||
GROUP BY bag_id
|
||||
`, tripId);
|
||||
return new Map(rows.map(r => [r.bag_id, r.total ?? 0]));
|
||||
}
|
||||
|
||||
/** The weight of everything in the trip that is in no bag (#2191). */
|
||||
unassignedWeightGrams(tripId: string | number): number {
|
||||
return this.bagWeightTotals(tripId).get(null) ?? 0;
|
||||
}
|
||||
|
||||
listBags(tripId: string | number) {
|
||||
const bags = this.db.all<any>('SELECT * FROM packing_bags WHERE trip_id = ? ORDER BY sort_order, id', tripId);
|
||||
const totals = this.bagWeightTotals(tripId);
|
||||
const members = this.db.all<{ bag_id: number; user_id: number; username: string; avatar: string | null }>(`
|
||||
SELECT bm.bag_id, bm.user_id, COALESCE(u.display_name, u.username) AS username, u.avatar
|
||||
FROM packing_bag_members bm
|
||||
@@ -506,6 +555,7 @@ export class PackingService {
|
||||
return bags.map(b => ({
|
||||
...b,
|
||||
members: (membersByBag.get(b.id) || []).map(m => ({ ...m, avatar: avatarUrl(m) })),
|
||||
total_weight_grams: totals.get(b.id) ?? 0,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user