fix(wanderer): offer the connection and run the watch where a trip switched Tours on

With Tours off for the instance, a trip that switched it on opened the wanderer
import, but Settings hid the connection and the watch cron never ran. The addon
feed now tells each user whether wanderer applies to them, and the watch looks
for every user who has Tours on somewhere; the others only take their turn.
This commit is contained in:
Maurice
2026-11-23 15:13:35 +01:00
parent c289541c43
commit 8b9de04960
6 changed files with 53 additions and 8 deletions
+4 -2
View File
@@ -1,3 +1,5 @@
import type { User } from '../../types';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { AddonsService } from './addons.service';
import { Controller, Get, UseGuards } from '@nestjs/common';
@@ -16,7 +18,7 @@ export class AddonsController {
constructor(private readonly addons: AddonsService) {}
@Get()
async list() {
return this.addons.list();
async list(@CurrentUser() user: User) {
return this.addons.list(user.id);
}
}
+6 -2
View File
@@ -258,7 +258,8 @@ export class AddonsService {
return { features: after, changed };
}
async list() {
/** `userId` decides the wanderer flag for that user: allowed, and Tours on for them somewhere. */
async list(userId?: number) {
const addonRows = await this.addons.listEnabled();
// Photo providers surface only inside journeys, so with the journey addon
// off they are unavailable no matter what their own rows say. Deriving that
@@ -280,7 +281,10 @@ export class AddonsService {
return {
collabFeatures: await this.getCollabFeatures(),
bagTracking: (await this.getBagTracking()).enabled,
toursWanderer: await this.isWandererAvailable(),
toursWanderer:
userId === undefined
? await this.isWandererAvailable()
: (await this.isWandererAllowed()) && (await this.isEnabledForUserAnywhere(userId, ADDON_IDS.TOURS)),
toursHikingMap: (await this.isAddonEnabled(ADDON_IDS.TOURS)) && (await this.getToursHikingMap()).enabled,
toursCycleMap: (await this.isAddonEnabled(ADDON_IDS.TOURS)) && (await this.getToursCycleMap()).enabled,
// The same three options without the instance Tours switch: a trip may switch Tours on
@@ -1,3 +1,4 @@
import { ADDON_IDS } from '../../addons';
import { TourSourceHints } from '../../db/entities/TourSourceHints.entity';
import { TOUR_SOURCE_HINT_CHANGES } from '../../db/json-columns';
import type {
@@ -92,7 +93,7 @@ export class WandererWatchService {
* one user that fails keeps no other from theirs.
*/
async run(now: Date = new Date()): Promise<void> {
if (this.running || !(await this.addons.isWandererAvailable())) return;
if (this.running || !(await this.addons.isWandererAllowed())) return;
this.running = true;
try {
const userIds = await this.wanderer.watchableUserIds(dbNow(now), WATCH_CONNECTIONS_PER_RUN);
@@ -113,6 +114,16 @@ export class WandererWatchService {
* after each failure in a row; the hints already there stay as they are.
*/
private async watchUser(userId: number, now: Date): Promise<void> {
// Tours off for the instance and in every trip of the user: the turn passes without a look.
if (!(await this.addons.isEnabledForUserAnywhere(userId, ADDON_IDS.TOURS))) {
const state = await this.wanderer.watchState(userId);
await this.wanderer.noteWatchRun(userId, {
lastRunAt: dbNow(now),
failures: state.failures,
pausedUntil: state.pausedUntil,
});
return;
}
const instance = await this.wanderer.instanceOf(userId);
const checkedBefore = dbNow(new Date(now.getTime() - WATCH_RECHECK_MS));
const due = await this.hints.listDue(userId, instance, checkedBefore, WATCH_SOURCES_PER_RUN);
@@ -1,5 +1,6 @@
import { AddonsController } from '../../../src/nest/addons/addons.controller';
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import type { User } from '../../../src/types';
import { describe, it, expect, vi } from 'vitest';
@@ -20,8 +21,8 @@ describe('AddonsController (parity with the legacy GET /api/addons route)', () =
const list = vi.fn().mockReturnValue(feed);
const svc = makeService({ list } as Partial<AddonsService>);
expect(await new AddonsController(svc).list()).toBe(feed);
expect(await new AddonsController(svc).list({ id: 4 } as User)).toBe(feed);
expect(list).toHaveBeenCalledTimes(1);
expect(list).toHaveBeenCalledWith();
expect(list).toHaveBeenCalledWith(4);
});
});
@@ -145,6 +145,18 @@ describe('TripAddonsService: routes without a trip', () => {
expect(await addons.isEnabledAnywhere('roadtrip')).toBe(true);
});
it('TRIPADDON-SVC-016: the feed offers wanderer to a user once a trip of theirs switched Tours on', async () => {
const { owner, trip } = await ownedTrip();
const { user: stranger } = await makeUser(orm());
await setAddonEnabled(orm(), 'tours', false);
await addons.updateToursWanderer(true);
expect((await addons.list(owner.id)).toursWanderer).toBe(false);
await svc.setTripOverride(trip.id, owner.id, 'tours', true);
expect((await addons.list(owner.id)).toursWanderer).toBe(true);
expect((await addons.list(stranger.id)).toursWanderer).toBe(false);
expect((await addons.list()).toursWanderer).toBe(false);
});
it('TRIPADDON-SVC-013: tripsWithAddon keeps the trips the addon applies to, in their order, each as isEnabledForTrip says', async () => {
const { owner, trip: off } = await ownedTrip();
const on = await makeTrip(orm(), owner.id);
@@ -18,7 +18,7 @@ import { WandererClient, WandererError } from '../../../src/nest/wanderer/wander
import { createSnapshotTestDb } from '../../helpers/db-mock';
import { makePlace } from '../../helpers/factories/places';
import { countRows, deleteRows, findRow, insertRow, updateRows } from '../../helpers/factories/rows';
import { setAddonEnabled, setAppSetting } from '../../helpers/factories/settings';
import { setAddonEnabled, setAppSetting, setTripAddon } from '../../helpers/factories/settings';
import { makeTour, makeTourSource } from '../../helpers/factories/tours';
import { makeTrip } from '../../helpers/factories/trips';
import { makeUser } from '../../helpers/factories/users';
@@ -129,6 +129,21 @@ describe('WandererWatchService.run', () => {
expect(await connection(user.id)).toMatchObject({ watch_last_run_at: null });
});
it('WWATCH-014: with Tours off for the instance, a user whose trip switched it on is watched, the others only take their turn', async () => {
await setAddonEnabled(orm(), 'tours', false);
const opened = await fixture();
const closed = await fixture();
await setTripAddon(orm(), opened.trip.id, 'tours', true);
await watch.run(NOW);
expect(client.trail).toHaveBeenCalledTimes(1);
expect(await hintRow(opened.placeId)).toMatchObject({ checked_at: '2026-10-20 10:00:00' });
expect(await hintRow(closed.placeId)).toBeNull();
expect(await connection(closed.user.id)).toMatchObject({
watch_last_run_at: '2026-10-20 10:00:00',
watch_failures: 0,
});
});
it('WWATCH-002: a record as imported writes a quiet look, tells nobody and never fetches a GPX', async () => {
const { user, placeId } = await fixture();
const before = await countRows(orm(), Places);