fix(atlas): removing the last region keeps a country the user marked visited itself

Marking a region wrote its country with source 'manual', the same as ticking
the country off by hand, and removing the last region then took the country
away whatever its origin, Dawarich confirmations included. A country that
only comes in through a region is now stored as 'region', marking it visited
afterwards turns it into the user's own mark, and the cascade in unmarkRegion
only runs for a country without such a mark. Readers still see 'manual' for
those rows, so the country detail, the MCP resource and the plugin RPC answer
as before. Rows written before this change stay 'manual' and keep their
country.

REST, MCP and plugin RPC share the service. The atlas popup on desktop and
phone now asks the server whether the country is still marked before it
drops it from the map instead of guessing.

Refs #2597
This commit is contained in:
Maurice
2026-10-12 09:55:52 +02:00
parent b3fe593a1e
commit 36b8baf259
11 changed files with 215 additions and 26 deletions
@@ -20,6 +20,15 @@ export interface VisitedCountryRow {
type VisitedCountriesReadKyselyDB = Pick<DB, 'visited_countries'>;
type VisitedCountriesWriteKyselyDB = Pick<DB, 'visited_countries'>;
/**
* The source as callers see it. 'region' marks a country that only came onto
* the map through one of its regions and goes again with the last of them
* (#2597); to everyone else it reads as a mark made by hand, as it always did.
*/
function publicSource(source: string): string {
return source === 'region' ? 'manual' : source;
}
/**
* `visited_countries` — the user's own explicit "I've been here" marks
* (Plan 3f Task 1, atlas). `markVisited` (AT12) needs the `changes > 0`
@@ -43,12 +52,13 @@ export class VisitedCountriesRepository extends TrekRepository<VisitedCountries>
/** AT10 (`listVisitedCountries`) — `SELECT country_code, created_at, source FROM visited_countries WHERE user_id = ? ORDER BY created_at DESC`. */
async listForUser(userId: number): Promise<VisitedCountryRow[]> {
return await this.readDb()
const rows = await this.readDb()
.selectFrom('visited_countries')
.select(['country_code', 'created_at', 'source'])
.where('user_id', '=', userId)
.orderBy('created_at', 'desc')
.execute();
return rows.map((row) => ({ ...row, source: publicSource(row.source) }));
}
/**
@@ -69,12 +79,29 @@ export class VisitedCountriesRepository extends TrekRepository<VisitedCountries>
/** AT8/AT9 (`countryPlaces`'s no-trip early return, `countryPlaces`'s mark lookup — identical text) — `SELECT source FROM visited_countries WHERE user_id = ? AND country_code = ?`. */
async findSource(userId: number, countryCode: string): Promise<{ source: string } | undefined> {
return await this.writeDb()
const row = await this.writeDb()
.selectFrom('visited_countries')
.select('source')
.where('user_id', '=', userId)
.where('country_code', '=', countryCode)
.executeTakeFirst();
return row && { source: publicSource(row.source) };
}
/**
* `unmarkRegion`'s cascade check: whether the user marked the country visited
* themselves (by hand or from Dawarich), rather than it only coming in
* through one of its regions (#2597).
*/
async hasOwnMark(userId: number, countryCode: string): Promise<boolean> {
const row = await this.writeDb()
.selectFrom('visited_countries')
.select('country_code')
.where('user_id', '=', userId)
.where('country_code', '=', countryCode)
.where('source', '!=', 'region')
.executeTakeFirst();
return !!row;
}
/**
@@ -103,20 +130,34 @@ export class VisitedCountriesRepository extends TrekRepository<VisitedCountries>
/**
* AT19 (`markRegion`, inside its transaction) — `INSERT OR IGNORE INTO
* visited_countries (user_id, country_code) VALUES (?, ?)`, a DISTINCT
* 2-column variant of {@link markVisited}'s 3-column insert. The legacy
* statement's column list omits `source`, letting the schema's own
* `DEFAULT 'manual'` fill it — reproduced here by passing that literal
* explicitly (`VisitedCountries.entity.ts`'s `source: p.text()
* .default('manual')`, the exact same value the omitted column would
* have taken). No `changes` signal here; the legacy statement never
* checked it either.
* visited_countries (user_id, country_code, source) VALUES (?, ?, 'region')`.
* A country that only came onto the map through one of its regions is
* written as 'region', not 'manual', so removing its last region can tell
* it apart from a country the user ticked off itself (#2597). The conflict
* is ignored, so a country already marked keeps its own source. Rows written
* before this change carry 'manual' and are left as they are. No `changes`
* signal here; the legacy statement never checked it either.
*/
async markFromRegion(userId: number, countryCode: string): Promise<void> {
await this.writeDb()
.insertInto('visited_countries')
.values({ user_id: userId, country_code: countryCode, source: 'manual' })
.values({ user_id: userId, country_code: countryCode, source: 'region' })
.onConflict((oc) => oc.doNothing())
.execute();
}
/**
* `markCountry`, inside its transaction, right after {@link markVisited}: a
* country that only came in through a region becomes the user's own mark
* once they mark it visited themselves. Any other row is left alone.
*/
async promoteFromRegion(userId: number, countryCode: string, source: 'manual' | 'dawarich'): Promise<void> {
await this.writeDb()
.updateTable('visited_countries')
.set({ source })
.where('user_id', '=', userId)
.where('country_code', '=', countryCode)
.where('source', '=', 'region')
.execute();
}
}
+2 -1
View File
@@ -283,7 +283,8 @@ export class AtlasMcp {
@Tool({
name: 'unmark_region_visited',
description: 'Remove a region from the visited list.',
description:
'Remove a region from the visited list. Removing the last region of a country also removes the country, unless it was marked visited itself.',
inputSchema: {
regionCode: z.string(),
},
+5 -5
View File
@@ -685,14 +685,13 @@ export class AtlasService {
* every mark was until an integration could make one, and `INSERT OR IGNORE`
* means a country already ticked by hand keeps that provenance — confirming
* it again from a recording does not relabel somebody's own work as imported.
*/
/**
* Returns whether this actually added the country, so a caller reporting
* "3 countries added" is not counting the ones that were already there.
*/
async markCountry(userId: number, code: string, source: 'manual' | 'dawarich' = 'manual'): Promise<boolean> {
return await this.uow.transactional(async () => {
const inserted = await this.visitedCountries.markVisited(userId, code, source); // AT12
await this.visitedCountries.promoteFromRegion(userId, code, source); // #2597
// Marking it visited again lifts a previous removal.
await this.hiddenCountries.unhide(userId, code); // AT13
return inserted;
@@ -763,10 +762,11 @@ export class AtlasService {
if (countryCode) {
await this.hiddenRegions.hide(userId, code, countryCode); // AT27
// If that was the country's last visible region, hide the country too — otherwise it
// keeps showing "visited" on the world map with nothing left to drill into.
// If that was the country's last visible region, hide the country too, or it keeps showing
// "visited" with nothing left to drill into. One the user marked itself stays (#2597).
const ownMark = await this.visitedCountries.hasOwnMark(userId, countryCode);
const hidden = await this.getHiddenRegions(userId);
if (!(await this.hasVisibleRegionForCountry(userId, countryCode, hidden))) {
if (!ownMark && !(await this.hasVisibleRegionForCountry(userId, countryCode, hidden))) {
await this.unmarkCountry(userId, countryCode);
}
}