mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
fix(places): say where a result came from, and stop asking questions in the form
Six changes to the place picker and the card behind it, all of them about a panel saying less and meaning more. The TREK place index has no switch in the admin panel any more. It decides whether a search leaves the instance at all, which is a property of the deployment rather than a taste: an operator pins TREK_PLACES_ENABLED=false in their compose file, and it cannot be turned off from a browser by whoever holds an admin account that day. The switch never shipped, so nothing to migrate. The standing "Using OpenStreetMap" notice above the search box is gone. It was a sentence about configuration in the middle of a form about a place, and it answered a question nobody asked while leaving the real one open. Each result now carries a small mark instead: TREK, OpenStreetMap or Google. It sits on the row rather than above the list, because a list can be two indexes interleaved and a heading could only name one of them. The saved-places filters open across the full width of the row. Two of them sit side by side, only one can be open at a time, and list names were being cut at about ten characters for no reason. Three empty states use the mascot the rest of TREK uses. The place details panel no longer tells the reader to go and ask an administrator for a Google key, the saved-places list and the road trip rail no longer explain themselves in two lines of grey text. That needed one new mood, `sad`: `sleepy` reads as falling asleep, which is a different thing from finding nothing. The Kind of stop field is out of the place form. It is a road trip concept, and the road trip view already sets it from the stop's own number, where the choice is next to what it changes.
This commit is contained in:
@@ -140,6 +140,18 @@ export function deriveMaps(raw: RawEnv) {
|
||||
* who wants nothing to leave their network points at their own machine.
|
||||
*/
|
||||
trekPlacesUrl: raw.TREK_PLACES_URL || undefined,
|
||||
/**
|
||||
* Whether the index answers at all. On unless an operator says otherwise,
|
||||
* because it is the path we want people on and an upgrade must not quietly
|
||||
* drop back to Nominatim, whose usage policy forbids what TREK was doing
|
||||
* with it.
|
||||
*
|
||||
* Deliberately an environment variable and not an admin switch: it decides
|
||||
* whether searches leave the instance, and a setting that reaches for the
|
||||
* network is one an operator wants pinned in their compose file, not one a
|
||||
* second admin can flip in a browser.
|
||||
*/
|
||||
trekPlacesEnabled: raw.TREK_PLACES_ENABLED !== 'false',
|
||||
placesApiKey: raw.PLACES_API_KEY || undefined,
|
||||
/** Public pk.* token shipped with a managed instance; reaches the browser by design. */
|
||||
mapboxToken: raw.MAPBOX_ACCESS_TOKEN || undefined,
|
||||
|
||||
@@ -144,6 +144,7 @@ export const envSchema = z.object({
|
||||
TREK_MANAGED: boolStr,
|
||||
PLACES_API_BASE: url,
|
||||
TREK_PLACES_URL: url,
|
||||
TREK_PLACES_ENABLED: boolStr,
|
||||
PLACES_API_KEY: anyString,
|
||||
MAPBOX_ACCESS_TOKEN: anyString,
|
||||
CARTO_API_KEY: anyString,
|
||||
|
||||
@@ -218,28 +218,6 @@ export class AddonsService {
|
||||
|
||||
updatePlacesEnrich(enabled: boolean) { return this.writeFlag('places_enrich_enabled', enabled); }
|
||||
|
||||
/**
|
||||
* The TREK place index, read fail-OPEN for the same reason enrichment is.
|
||||
*
|
||||
* This is the one switch on the card that is about egress rather than about
|
||||
* spending: with it on, every search and every keystroke in the place box
|
||||
* leaves the instance for places.liketrek.com. An operator who does not want
|
||||
* that needs somewhere to say so, and until this pair existed the key was
|
||||
* read by six call paths and written by nobody — the only way to turn the
|
||||
* index off was an INSERT against app_settings by hand.
|
||||
*
|
||||
* It has to agree with MapsService.trekPlacesEnabled(), which reads the same
|
||||
* key the same way. If the two ever disagree, the panel shows "off" while the
|
||||
* queries keep leaving, which is the worst of both.
|
||||
*/
|
||||
getTrekPlaces() {
|
||||
const row = this.db.prepare("SELECT value FROM app_settings WHERE key = 'trek_places_enabled'").get() as
|
||||
| { value: string }
|
||||
| undefined;
|
||||
return { enabled: row?.value !== 'false' };
|
||||
}
|
||||
updateTrekPlaces(enabled: boolean) { return this.writeFlag('trek_places_enabled', enabled); }
|
||||
|
||||
// ── Transit backend (#1699) ────────────────────────────────────────────────
|
||||
// Not a flag: two named backends, so it stores the name rather than a
|
||||
// boolean. The read/write pair lives in transit/transit-provider.ts because
|
||||
|
||||
@@ -253,20 +253,6 @@ export class AdminController {
|
||||
return result;
|
||||
}
|
||||
|
||||
// The one switch here that is about egress rather than about spending: with
|
||||
// the index on, every search and every keystroke in the place box leaves the
|
||||
// instance. Six call paths read the key; until this pair existed nothing
|
||||
// wrote it, so an operator who wanted it off had no way to say so.
|
||||
@Get('trek-places')
|
||||
getTrekPlaces() { return this.addons.getTrekPlaces(); }
|
||||
|
||||
@Put('trek-places')
|
||||
updateTrekPlaces(@CurrentUser() user: User, @Body() body: AdminFeatureToggleDto, @Req() req: Request) {
|
||||
const result = this.addons.updateTrekPlaces(body.enabled);
|
||||
this.audit.writeAudit({ userId: user.id, action: 'admin.trek_places', ip: getClientIp(req), details: { enabled: result.enabled } });
|
||||
return result;
|
||||
}
|
||||
|
||||
@Get('collab-features')
|
||||
getCollabFeatures() { return this.addons.getCollabFeatures(); }
|
||||
|
||||
|
||||
@@ -662,16 +662,19 @@ export class MapsService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fail-OPEN, unlike the three Google switches: the index is the path we want
|
||||
* people on, and an install that upgrades without visiting the admin panel
|
||||
* should land on it rather than stay on Nominatim, whose usage policy forbids
|
||||
* what TREK was doing with it.
|
||||
* Whether the index answers on this instance.
|
||||
*
|
||||
* On unless TREK_PLACES_ENABLED says otherwise, because the index is the path
|
||||
* we want people on and an upgrade must not quietly drop back to Nominatim,
|
||||
* whose usage policy forbids what TREK was doing with it.
|
||||
*
|
||||
* An environment variable rather than an admin switch on purpose. This decides
|
||||
* whether a search leaves the instance at all, which is a property of the
|
||||
* deployment: an operator pins it in their compose file, and it cannot be
|
||||
* turned off from a browser by whoever holds an admin account that day.
|
||||
*/
|
||||
trekPlacesEnabled(): boolean {
|
||||
const row = this.database.get<{ value: string }>(
|
||||
"SELECT value FROM app_settings WHERE key = 'trek_places_enabled'",
|
||||
);
|
||||
return row?.value !== 'false';
|
||||
return readEnv().maps.trekPlacesEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
deriveHttp,
|
||||
deriveSession,
|
||||
deriveDemo,
|
||||
deriveMaps,
|
||||
deriveOidc,
|
||||
deriveSmtp,
|
||||
deriveMcp,
|
||||
@@ -270,3 +271,16 @@ describe('deriveAll', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('deriveMaps', () => {
|
||||
it('TREK_PLACES_ENABLED is on unless it says the literal "false"', () => {
|
||||
// Fail-open, and deliberately not the boolean-like family the switches
|
||||
// above accept: a typo must not silently drop a whole install back to
|
||||
// Nominatim, whose usage policy forbids what TREK was doing with it.
|
||||
expect(deriveMaps({} as never).trekPlacesEnabled).toBe(true);
|
||||
expect(deriveMaps({ TREK_PLACES_ENABLED: 'false' } as never).trekPlacesEnabled).toBe(false);
|
||||
for (const value of ['true', 'FALSE', '0', 'no', '']) {
|
||||
expect(deriveMaps({ TREK_PLACES_ENABLED: value } as never).trekPlacesEnabled, value).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -495,91 +495,6 @@ describe('AddonsService transit provider', () => {
|
||||
* PlaceShadowService.enabled(), which reads the same key itself — if the two
|
||||
* diverge the admin panel shows "off" while the log keeps collecting picks.
|
||||
*/
|
||||
/**
|
||||
* The index switch reads fail-OPEN, like places_enrich_enabled and unlike the
|
||||
* three Google flags: nothing backfills a row on upgrade, so an absent row has
|
||||
* to mean on, or every existing install would silently lose the index.
|
||||
*
|
||||
* It has to keep agreeing with MapsService.trekPlacesEnabled(), which reads the
|
||||
* same key itself. If the two diverge, the admin panel shows "off" while every
|
||||
* search and keystroke keeps leaving the instance.
|
||||
*/
|
||||
describe('AddonsService trek places flag', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('ADDONS-SVC-094 an unset flag reads as ON, and only the literal "false" turns it off', () => {
|
||||
dbMock._stmt.get.mockReturnValueOnce(undefined);
|
||||
expect(svc().getTrekPlaces()).toEqual({ enabled: true });
|
||||
expect(dbMock.prepare).toHaveBeenLastCalledWith(
|
||||
"SELECT value FROM app_settings WHERE key = 'trek_places_enabled'",
|
||||
);
|
||||
|
||||
dbMock._stmt.get.mockReturnValueOnce({ value: 'false' });
|
||||
expect(svc().getTrekPlaces()).toEqual({ enabled: false });
|
||||
|
||||
// Anything else is on. A garbled row must not silently disable the default
|
||||
// search path for a whole install.
|
||||
for (const value of ['true', 'FALSE', '0', '']) {
|
||||
dbMock._stmt.get.mockReturnValueOnce({ value });
|
||||
expect(svc().getTrekPlaces(), value).toEqual({ enabled: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('ADDONS-SVC-095 the setter round-trips through the getter under its own key', () => {
|
||||
// Keyed store rather than an echo assertion: the write has to produce the
|
||||
// exact string the read compares against, so a setter persisting '0' fails
|
||||
// here instead of silently reading back ON in production.
|
||||
//
|
||||
// The reader inlines its key in the SQL and binds nothing, the way the
|
||||
// other fail-open flag does, so the statement text is what identifies the
|
||||
// row here — not the bound parameter the writer uses.
|
||||
const stored = new Map<string, string>();
|
||||
let lastSql = '';
|
||||
dbMock.prepare.mockImplementation((sql: string) => {
|
||||
lastSql = sql;
|
||||
return dbMock._stmt;
|
||||
});
|
||||
dbMock._stmt.run.mockImplementation((key: string, value: string) => {
|
||||
stored.set(key, value);
|
||||
});
|
||||
dbMock._stmt.get.mockImplementation((key?: string) => {
|
||||
const wanted = key ?? (lastSql.match(/key = '([^']+)'/)?.[1] ?? '');
|
||||
const value = stored.get(wanted);
|
||||
return value === undefined ? undefined : { value };
|
||||
});
|
||||
|
||||
expect(svc().updateTrekPlaces(false)).toEqual({ enabled: false });
|
||||
expect(dbMock._stmt.run).toHaveBeenLastCalledWith('trek_places_enabled', 'false');
|
||||
expect(svc().getTrekPlaces()).toEqual({ enabled: false });
|
||||
|
||||
expect(svc().updateTrekPlaces(true)).toEqual({ enabled: true });
|
||||
expect(dbMock._stmt.run).toHaveBeenLastCalledWith('trek_places_enabled', 'true');
|
||||
expect(svc().getTrekPlaces()).toEqual({ enabled: true });
|
||||
expect([...stored.keys()]).toEqual(['trek_places_enabled']);
|
||||
});
|
||||
|
||||
it('ADDONS-SVC-096 answers the same as MapsService.trekPlacesEnabled() for every stored value', () => {
|
||||
// The admin panel and the six call paths that gate on this must never
|
||||
// disagree: showing "off" while queries keep leaving is worse than either
|
||||
// default on its own.
|
||||
const maps = new MapsService(new DatabaseService(dbConn), {} as never);
|
||||
const rows: Array<[{ value: string } | undefined, boolean]> = [
|
||||
[undefined, true],
|
||||
[{ value: 'false' }, false],
|
||||
[{ value: 'true' }, true],
|
||||
[{ value: 'garbage' }, true],
|
||||
];
|
||||
|
||||
for (const [row, expected] of rows) {
|
||||
dbMock._stmt.get.mockReturnValueOnce(row).mockReturnValueOnce(row);
|
||||
expect(svc().getTrekPlaces()).toEqual({ enabled: expected });
|
||||
expect(maps.trekPlacesEnabled()).toBe(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('AddonsService place shadow flag', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
@@ -49,8 +49,6 @@ const addonsStub = () => ({
|
||||
getPlacesEnrich: vi.fn(() => ({ enabled: true })),
|
||||
updatePlacesEnrich: vi.fn((enabled: boolean) => ({ enabled })),
|
||||
// Fail-open too, and the only switch here about egress rather than spending.
|
||||
getTrekPlaces: vi.fn(() => ({ enabled: true })),
|
||||
updateTrekPlaces: vi.fn((enabled: boolean) => ({ enabled })),
|
||||
getCollabFeatures: vi.fn(() => ({ chat: false })),
|
||||
updateCollabFeatures: vi.fn(() => ({ features: { chat: true }, changed: true })),
|
||||
getTransitProvider: vi.fn(() => ({ provider: 'transitous', googleKeySource: null })),
|
||||
@@ -302,26 +300,6 @@ describe('AdminController feature toggles', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('ADMIN-TOGGLE-005 trek-places forwards the body value and audits the stored one', () => {
|
||||
// The audit row carries what the service stored, not what the request
|
||||
// asked for: this switch decides whether every search leaves the instance,
|
||||
// so a row claiming it was turned off when it was not would be worse than
|
||||
// no row at all.
|
||||
const updateTrekPlaces = vi.fn(() => ({ enabled: true }));
|
||||
const c = adminCtl(svc(), undefined, { ...addonsStub(), updateTrekPlaces } as unknown as AddonsService);
|
||||
expect(c.updateTrekPlaces(user, { enabled: false }, req)).toEqual({ enabled: true });
|
||||
expect(updateTrekPlaces).toHaveBeenCalledWith(false);
|
||||
expect(writeAudit).toHaveBeenCalledWith({ userId: user.id, action: 'admin.trek_places', ip: '1.2.3.4', details: { enabled: true } });
|
||||
});
|
||||
|
||||
it('ADMIN-TOGGLE-005b trek-places reads through to AddonsService instead of a controller-side default', () => {
|
||||
const getTrekPlaces = vi.fn(() => ({ enabled: false }));
|
||||
const c = adminCtl(svc(), undefined, { ...addonsStub(), getTrekPlaces } as unknown as AddonsService);
|
||||
expect(c.getTrekPlaces()).toEqual({ enabled: false });
|
||||
expect(getTrekPlaces).toHaveBeenCalledTimes(1);
|
||||
expect(writeAudit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ADMIN-TOGGLE-004 place-shadow forwards the body value and audits the stored one', () => {
|
||||
// The handler audits result.enabled. A service that declines the flip must
|
||||
// not leave behind an audit row claiming the shadow log was switched on.
|
||||
|
||||
@@ -27,6 +27,22 @@ import { MapsService } from '../../../src/nest/maps/maps.service';
|
||||
import type { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
import type { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
|
||||
|
||||
// The index switch is an environment variable now, not an admin row: it decides
|
||||
// whether a search leaves the instance at all, so it is pinned by the operator
|
||||
// rather than flippable from a browser. `setTrekPlaces()` below drives it.
|
||||
const trekPlaces = vi.hoisted(() => ({ on: true }));
|
||||
vi.mock('../../../src/app-config', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('../../../src/app-config')>();
|
||||
return {
|
||||
...actual,
|
||||
readEnv: () => {
|
||||
const env = actual.readEnv();
|
||||
return { ...env, maps: { ...env.maps, trekPlacesEnabled: trekPlaces.on } };
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
const INPUT = 'Café Kröpel';
|
||||
|
||||
const hit = (over: Record<string, unknown> = {}) => ({
|
||||
@@ -47,11 +63,8 @@ const hit = (over: Record<string, unknown> = {}) => ({
|
||||
* `'false'` to the key resolver and send the fallback at Google for real.
|
||||
*/
|
||||
function make(enabled = true) {
|
||||
const database = {
|
||||
get: vi.fn((sql: string) =>
|
||||
sql.includes('trek_places_enabled') && !enabled ? { value: 'false' } : undefined,
|
||||
),
|
||||
} as unknown as DatabaseService;
|
||||
trekPlaces.on = enabled;
|
||||
const database = { get: vi.fn(() => undefined) } as unknown as DatabaseService;
|
||||
return new MapsService(database, {} as PlacePhotoCacheService);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,22 @@ import { MapsService } from '../../../src/nest/maps/maps.service';
|
||||
import type { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
import type { PlacePhotoCacheService } from '../../../src/nest/place-photos/place-photo-cache.service';
|
||||
|
||||
// The index switch is an environment variable now, not an admin row: it decides
|
||||
// whether a search leaves the instance at all, so it is pinned by the operator
|
||||
// rather than flippable from a browser. `setTrekPlaces()` below drives it.
|
||||
const trekPlaces = vi.hoisted(() => ({ on: true }));
|
||||
vi.mock('../../../src/app-config', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('../../../src/app-config')>();
|
||||
return {
|
||||
...actual,
|
||||
readEnv: () => {
|
||||
const env = actual.readEnv();
|
||||
return { ...env, maps: { ...env.maps, trekPlacesEnabled: trekPlaces.on } };
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
/** An index row, far enough from the OSM one below not to be deduped. */
|
||||
const indexHit = (name: string, lat = 35.31, lng = 139.53) => ({
|
||||
gers: name.toLowerCase().replace(/\W/g, ''),
|
||||
@@ -62,11 +78,8 @@ const osmAnswer = (rows: unknown[]) => ({ ok: true, json: async () => rows });
|
||||
* `'false'` to the API-key resolver, which would send the fallback at Google.
|
||||
*/
|
||||
function make(enabled = true) {
|
||||
const database = {
|
||||
get: vi.fn((sql: string) =>
|
||||
sql.includes('trek_places_enabled') && !enabled ? { value: 'false' } : undefined,
|
||||
),
|
||||
} as unknown as DatabaseService;
|
||||
trekPlaces.on = enabled;
|
||||
const database = { get: vi.fn(() => undefined) } as unknown as DatabaseService;
|
||||
return new MapsService(database, {} as PlacePhotoCacheService);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user