fix(addons): give a member who joins a trip with an opened addon a new MCP session

A trip that switched on an addon the instance has off ended the MCP sessions
of the members it had then, but somebody added later, or joining through the
invite link, kept a session without that addon's tools. Joining now ends it too.
This commit is contained in:
Maurice
2026-11-23 15:08:16 +01:00
parent 8709f67ff4
commit 146f46c5e3
7 changed files with 49 additions and 1 deletions
@@ -110,6 +110,15 @@ export class TripAddonsService {
for (const userId of new Set([...(ownerId === null ? [] : [ownerId]), ...memberIds])) revokeUserSessions(userId);
}
/**
* Somebody joined the trip. While it has an addon on that the instance has off, their MCP
* session attached its tools without that addon's, so it is ended the same way.
*/
async reattachJoinedMember(tripId: number, userId: number): Promise<void> {
const opened = (await this.effectiveTripAddons(tripId)).some((addon) => addon.enabled && !addon.instanceEnabled);
if (opened) revokeUserSessions(userId);
}
/** A copied trip keeps the choices of the trip it came from. Runs inside the copy's transaction. */
async copyOverrides(fromTripId: number, toTripId: number): Promise<void> {
await this.overrides.copyForTrip(fromTripId, toTripId);
@@ -1,5 +1,6 @@
import { TripInviteTokens } from '../../db/entities/TripInviteTokens.entity';
import { Trips } from '../../db/entities/Trips.entity';
import { AddonsModule } from '../addons/addons.module';
import { AppConfigModule } from '../app-config/app-config.module';
import { AuditModule } from '../audit/audit.module';
import { DemoModule } from '../common/demo.module';
@@ -32,6 +33,7 @@ import { Module } from '@nestjs/common';
AppConfigModule,
RealtimeModule,
DemoModule,
AddonsModule,
MikroOrmModule.forFeature([TripInviteTokens, Trips]),
],
controllers: [TripInviteLinkController, TripInviteController],
@@ -2,6 +2,7 @@ import { TripInviteTokens } from '../../db/entities/TripInviteTokens.entity';
import type { TripInviteTokensRepository } from '../../db/repositories/TripInviteTokens.repository';
import type { TripAccess } from '../../db/repositories/Trips.repository';
import type { User } from '../../types';
import { TripAddonsService } from '../addons/trip-addons.service';
import { forbidden } from '../common/domain-error';
import { UnitOfWork } from '../database/unit-of-work';
import { PermissionsService } from '../permissions/permissions.service';
@@ -49,6 +50,7 @@ export class TripInviteService {
private readonly membership: TripMembershipService,
private readonly uow: UnitOfWork,
@InjectRepository(TripInviteTokens) private readonly tripInviteTokens: TripInviteTokensRepository,
private readonly tripAddons: TripAddonsService,
) {}
async verifyTripAccess(tripId: string, userId: number) {
@@ -137,6 +139,8 @@ export class TripInviteService {
/** Join the resolved trip as the current (authenticated, non-guest) user.
* invited_by is null — they joined via a link, not a personal invite. */
async join(tripId: number, userId: number) {
return await this.membership.joinTripAsMember(tripId, userId, null);
const result = await this.membership.joinTripAsMember(tripId, userId, null);
if (result.joined) await this.tripAddons.reattachJoinedMember(tripId, userId);
return result;
}
}
@@ -3,6 +3,7 @@ import { Users } from '../../db/entities/Users.entity';
import type { UsersRepository } from '../../db/repositories/Users.repository';
import type { User } from '../../types';
import { AddonsService } from '../addons/addons.service';
import { TripAddonsService } from '../addons/trip-addons.service';
import { BudgetService } from '../budget/budget.service';
import { restTripWriter, type TripWriter } from '../common/trip-writer';
import { RealtimeService } from '../realtime/realtime.service';
@@ -30,6 +31,7 @@ export class TripMemberWritesService {
private readonly realtime: RealtimeService,
@InjectRepository(Users) private readonly users: UsersRepository,
private readonly addons: AddonsService,
private readonly tripAddons: TripAddonsService,
) {}
/** REST's writer for a roster change: the room minus the tab that sent the request. */
@@ -41,6 +43,7 @@ export class TripMemberWritesService {
async addMember(tripId: string | number, identifier: string, tripOwnerId: number, writer: TripWriter) {
const result = await this.roster.addMember(tripId, identifier, tripOwnerId, writer.userId);
writer.events.emit('member:added', { member: result.member });
await this.tripAddons.reattachJoinedMember(Number(tripId), result.targetUserId);
// The invitation names who sent it by name, as the route does with its session user.
const actor = { id: writer.userId, username: (await this.users.findById(writer.userId))?.username ?? '' };
this.roster.notifyInvite(String(tripId), actor, result.targetUserId, result.tripTitle, result.member.email);
@@ -260,4 +260,18 @@ describe('TripAddonsService: open MCP sessions', () => {
await svc.setTripOverride(trip.id, owner.id, 'roadtrip', false);
expect(own.closed).toBe(false);
});
it('TRIPADDON-SVC-032: a member who joins a trip that switched on an addon the instance has off gets a new session', async () => {
const { owner, trip } = await ownedTrip();
const { user: joiner } = await makeUser(orm());
await setAddonEnabled(orm(), 'roadtrip', false);
const before = liveSession('before', joiner.id);
await svc.reattachJoinedMember(trip.id, joiner.id);
expect(before.closed).toBe(false);
await svc.setTripOverride(trip.id, owner.id, 'roadtrip', true);
const joined = liveSession('joined', joiner.id);
await svc.reattachJoinedMember(trip.id, joiner.id);
expect(joined.closed).toBe(true);
});
});
@@ -6,6 +6,7 @@
*/
import { db as testDb } from '../../../src/db/database';
import { TripInviteTokens } from '../../../src/db/entities/TripInviteTokens.entity';
import type { TripAddonsService } from '../../../src/nest/addons/trip-addons.service';
import { PermissionsService } from '../../../src/nest/permissions/permissions.service';
import { TripInviteService } from '../../../src/nest/trip-invite/trip-invite.service';
import { TripAccessService } from '../../../src/nest/trip-membership/trip-access.service';
@@ -40,6 +41,7 @@ vi.mock('../../../src/db/database', async () => {
});
let svc: TripInviteService;
const tripAddons = { reattachJoinedMember: vi.fn() };
beforeAll(async () => {
const uow = await createTestUnitOfWork(testDb);
svc = new TripInviteService(
@@ -50,6 +52,7 @@ beforeAll(async () => {
new TripMembershipService(await createTestTripsRepo(testDb), await createTestTripMembersRepo(testDb)),
uow,
await createTestTripInviteTokensRepo(testDb),
tripAddons as unknown as TripAddonsService,
);
});
@@ -136,4 +139,13 @@ describe('TripInviteService', () => {
expect((await svc.createOrRotate(trip.id, owner.id, 0)).expires_at).toBeNull();
expect((await svc.createOrRotate(trip.id, owner.id, -3)).expires_at).toBeNull();
});
it('TRIP-INVITE-008: joining through the link asks for a new MCP session, a second join does not', async () => {
const { trip } = setup();
const { user: joiner } = createUser(testDb);
tripAddons.reattachJoinedMember.mockClear();
expect(await svc.join(trip.id, joiner.id)).toEqual({ joined: true, tripId: trip.id });
expect(await svc.join(trip.id, joiner.id)).toEqual({ joined: false, tripId: trip.id });
expect(tripAddons.reattachJoinedMember.mock.calls).toEqual([[trip.id, joiner.id]]);
});
});
@@ -7,6 +7,7 @@
* deleted guest's split is read back again is left out instead of announced.
*/
import { AddonsService } from '../../../src/nest/addons/addons.service';
import { TripAddonsService } from '../../../src/nest/addons/trip-addons.service';
import { BudgetService } from '../../../src/nest/budget/budget.service';
import type { TripWriter } from '../../../src/nest/common/trip-writer';
import { TripMemberWritesService } from '../../../src/nest/trip-members/trip-member-writes.service';
@@ -31,6 +32,7 @@ const budget = {
getBudgetItem: vi.fn(),
};
const addons = { isEnabledForTrip: vi.fn() };
const tripAddons = { reattachJoinedMember: vi.fn() };
/** A writer whose user has no row, so nothing about them can be read back. */
function writerFor(userId: number): TripWriter {
@@ -45,6 +47,7 @@ beforeAll(async () => {
{ provide: TripMembersService, useValue: roster },
{ provide: BudgetService, useValue: budget },
{ provide: AddonsService, useValue: addons },
{ provide: TripAddonsService, useValue: tripAddons },
],
});
writes = t.get(TripMemberWritesService);
@@ -78,6 +81,7 @@ describe('TripMemberWritesService', () => {
'Lisbon',
'invitee@example.test',
);
expect(tripAddons.reattachJoinedMember).toHaveBeenCalledWith(7, 21);
});
it('MEMBER-WRITES-002: deleting a guest announces only the expenses that still read back after the re-split', async () => {