feat(studio): filter photos by entry, upload from Studio, and switch stops off

The content browser gets a filter over the journey's pictures: all of them,
the ones no entry holds, or one entry at a time, with the day and the place
under each name. An entry card in the Entries tab reaches its own pictures
the same way. Pictures can now come in without leaving Studio, into the
entry the filter names or into the gallery, from the panel or dropped
straight onto the page, and a photo element can fill its page or the whole
spread in one press.

Stops can be taken out of the arithmetic. A journal entry carries a
stats_excluded flag; an entry that has it is off the route, out of the
distance, out of the countries and not a step, while staying in the
journal. Studio lists every stop in the travel panel with a switch, the
entry editor carries the same switch on desktop and phone, and the entry
card says when a day is off the route.
This commit is contained in:
Maurice
2026-09-03 09:46:57 +02:00
committed by Maurice
parent 23401fcdb9
commit 6cbefd23e8
62 changed files with 2933 additions and 116 deletions
+21
View File
@@ -4236,6 +4236,27 @@ function runMigrations(db: Database.Database): void {
db.exec("ALTER TABLE plugin_actions ADD COLUMN scope TEXT NOT NULL DEFAULT 'user'");
}
},
/**
* A journal entry that is not a stop (discussion #2064).
*
* Studio draws its route and prints its distance from every entry that
* carries coordinates, and that is right until the journal starts at the
* home airport: the night before the flight, the stopover, the place the
* trip was planned from all become stops, and the distance counts the legs
* to and from them. The traveller knows which of those are the journey and
* which are the way there, so the switch sits on the entry. The entry stays
* in the journal; it is only left out of the arithmetic.
*
* DEFAULT 0: every existing entry keeps counting, which is what it did.
*
* Appended LAST: the array is index-addressed against schema_version.
*/
() => {
const cols = db.prepare("SELECT name FROM pragma_table_info('journey_entries')").all() as Array<{ name: string }>;
if (!cols.some((c) => c.name === 'stats_excluded')) {
db.exec('ALTER TABLE journey_entries ADD COLUMN stats_excluded INTEGER NOT NULL DEFAULT 0');
}
},
];
if (currentVersion < migrations.length) {
@@ -888,19 +888,27 @@ export class JourneyDomainService {
* `getCountryFromCoords`, which is the same answer computed rather than
* remembered. That import is a plain function from a module built to have no
* DI and no Nest edges, so it costs this domain no coupling to Atlas.
*
* ── Stops switched off ─────────────────────────────────────────────────
*
* An entry marked `stats_excluded` is in the journal and in nothing here:
* not on the route, not in the distance, not a step, not a country
* (discussion #2064). It is still named, under `excluded`, because the only
* way to switch a stop back on is to be able to see it.
*/
journeyStats(journeyId: number, userId: number): JourneyStats | null {
if (!this.canAccessJourney(journeyId, userId)) return null;
const entryRows = this.db.prepare(`
SELECT id, title, location_name, location_lat, location_lng, entry_date, source_trip_id
SELECT id, title, location_name, location_lat, location_lng, entry_date, source_trip_id,
source_place_id, stats_excluded
FROM journey_entries
WHERE journey_id = ?
ORDER BY entry_date ASC, sort_order ASC, id ASC
`).all(journeyId) as {
id: number; title: string | null; location_name: string | null;
location_lat: number | null; location_lng: number | null; entry_date: string | null;
source_trip_id: number | null;
source_trip_id: number | null; source_place_id: number | null; stats_excluded: number;
}[];
/*
@@ -1006,8 +1014,33 @@ export class JourneyDomainService {
return getCountryFromCoords(lat, lng);
};
const fromEntries: StatsInputPoint[] = entryRows
.filter(e => Number.isFinite(e.location_lat) && Number.isFinite(e.location_lng))
/*
* The switch is honoured before anything is measured, on both routes.
*
* Only an entry with coordinates is listed under `excluded`: one without
* a place was never a stop, and offering to switch it back on would offer
* a stop that does not exist. It still stops being a step, because the
* traveller said it does not count.
*
* The fallback has to honour it too. A skeleton is the entry TREK derives
* from a trip place, so switching the skeleton off and then drawing the
* place it came from would put the same stop straight back on the map
* under its other name. Hence the set of places to leave out.
*/
const hasCoords = (e: { location_lat: number | null; location_lng: number | null }) =>
Number.isFinite(e.location_lat) && Number.isFinite(e.location_lng);
const counting = entryRows.filter(e => !e.stats_excluded);
const excluded = entryRows
.filter(e => e.stats_excluded && hasCoords(e))
.map(e => ({ entryId: e.id, label: e.title || e.location_name || '', date: e.entry_date ?? null }));
const excludedPlaceIds = new Set(
entryRows
.filter(e => e.stats_excluded && e.source_place_id != null)
.map(e => e.source_place_id as number),
);
const fromEntries: StatsInputPoint[] = counting
.filter(hasCoords)
.map(e => ({
lat: e.location_lat as number,
lng: e.location_lng as number,
@@ -1016,12 +1049,24 @@ export class JourneyDomainService {
country: countryAt(e.location_lat as number, e.location_lng as number),
tripId: e.source_trip_id ?? null,
photoId: photoByEntry.get(e.id) ?? null,
entryId: e.id,
}));
const points: StatsInputPoint[] = fromEntries.length
/*
* Which source wins is decided before the switches, not after.
*
* `fromEntries.length` was the test, and with a switch in the world it
* answers the wrong question: a journey whose every stop has been switched
* off has no entry points left, so the fallback would draw the trip's
* places instead and put the whole route back under other names. The
* journal is the source as soon as one entry carries a point, whether or
* not it still counts.
*/
const journalHasPoints = entryRows.some(hasCoords);
const points: StatsInputPoint[] = journalHasPoints
? fromEntries
: placeRows
.filter(p => Number.isFinite(p.lat) && Number.isFinite(p.lng))
.filter(p => !excludedPlaceIds.has(p.id) && Number.isFinite(p.lat) && Number.isFinite(p.lng))
.map(p => ({
lat: p.lat as number,
lng: p.lng as number,
@@ -1034,6 +1079,8 @@ export class JourneyDomainService {
// smuggled into a field the book will print as one of the journey's
// own pictures.
photoId: null,
// No entry to switch off, so a panel lists it and offers no switch.
entryId: null,
}));
/*
@@ -1051,9 +1098,11 @@ export class JourneyDomainService {
return computeJourneyStats({
journeyId,
points,
entries: entryRows.length,
entries: counting.length,
photos: photoCount?.n ?? 0,
places: placeCount?.n ?? 0,
// A place whose skeleton was switched off is not a place the journey
// went to, whichever route drew it.
places: (placeCount?.n ?? 0) - placeRows.filter(p => excludedPlaceIds.has(p.id)).length,
tripDates,
countryNames: countryNamesFor(points),
trips: tripRows.map(t => ({
@@ -1063,6 +1112,7 @@ export class JourneyDomainService {
end: t.end,
points: perTrip.get(t.id) ?? 0,
})),
excluded,
});
}
@@ -1182,6 +1232,7 @@ export class JourneyDomainService {
pros_cons: { pros: string[]; cons: string[] };
visibility: string;
sort_order: number;
stats_excluded: boolean;
}>,
sid?: string,
): JourneyEntryWire | null {
@@ -1210,6 +1261,7 @@ export class JourneyDomainService {
'pros_cons',
'visibility',
'sort_order',
'stats_excluded',
]);
for (const [key, val] of Object.entries(data)) {
@@ -1221,6 +1273,10 @@ export class JourneyDomainService {
} else if (key === 'pros_cons') {
fields.push('pros_cons = ?');
values.push(val && typeof val === 'object' ? JSON.stringify(val) : val);
} else if (key === 'stats_excluded') {
// INTEGER column, and better-sqlite3 refuses to bind a boolean.
fields.push('stats_excluded = ?');
values.push(val ? 1 : 0);
} else {
fields.push(`${key} = ?`);
values.push(val);
+10 -3
View File
@@ -20,18 +20,25 @@ import type { JourneyEntry } from '../../types';
* breaks on it today — the journey page reloads on any event and ignores the
* payload — but a payload that contradicts what the read paths answer is a trap
* for the first listener that decides to trust it.
*
* `stats_excluded` is the same gap one column over: an INTEGER holding 0 or 1
* that the wire carries as a boolean (discussion #2064). It crosses here for
* the reason the JSON does, so that a client comparing it with `=== true` is
* comparing against something that can be true on every path.
*/
export interface JourneyEntryWire extends Omit<JourneyEntry, 'tags' | 'pros_cons'> {
export interface JourneyEntryWire extends Omit<JourneyEntry, 'tags' | 'pros_cons' | 'stats_excluded'> {
tags: string[];
pros_cons: { pros: string[]; cons: string[] } | null;
stats_excluded: boolean;
}
/** Decode a row's JSON columns. The one place that knows they are JSON. */
/** Decode a row's JSON columns and its flag. The one place that knows how they are stored. */
export function decodeEntryRow(row: JourneyEntry): JourneyEntryWire {
const { tags, pros_cons, ...rest } = row;
const { tags, pros_cons, stats_excluded, ...rest } = row;
return {
...rest,
tags: tags ? JSON.parse(tags) : [],
pros_cons: pros_cons ? JSON.parse(pros_cons) : null,
stats_excluded: !!stats_excluded,
};
}
+16 -1
View File
@@ -1,4 +1,6 @@
import type { JourneyStats, JourneyStatsCountry, JourneyStatsPoint, JourneyStatsTrip } from '@trek/shared';
import type {
JourneyStats, JourneyStatsCountry, JourneyStatsExcludedStop, JourneyStatsPoint, JourneyStatsTrip,
} from '@trek/shared';
/**
* What a journey adds up to — the arithmetic, with no database and no Nest.
@@ -27,6 +29,8 @@ export interface StatsInputPoint {
tripId?: number | null;
/** One photograph from this stop, for a map that marks it with a picture. */
photoId?: number | null;
/** The journal entry this stop was read from; null or absent for a trip place. */
entryId?: number | null;
}
const rad = (deg: number) => (deg * Math.PI) / 180;
@@ -166,6 +170,15 @@ export interface StatsInput {
countryNames: Record<string, string>;
/** The trips themselves, so a book can print a map of one of them. */
trips?: JourneyStatsTrip[];
/**
* Stops switched off by hand (discussion #2064).
*
* Not in `points`, and that is the whole arrangement: the service takes
* them out before anything here is measured, so nothing in this module has
* to know which stop is which. The list only rides along so that one
* answer holds both halves, the route and what was left off it.
*/
excluded?: JourneyStatsExcludedStop[];
}
/**
@@ -206,6 +219,7 @@ export function computeJourneyStats(input: StatsInput): JourneyStats {
country: p.country,
tripId: p.tripId ?? null,
photoId: p.photoId ?? null,
entryId: p.entryId ?? null,
})),
/*
* Counted after thinning, not before.
@@ -219,6 +233,7 @@ export function computeJourneyStats(input: StatsInput): JourneyStats {
...t,
points: points.filter(p => p.tripId === t.id).length,
})),
excluded: input.excluded ?? [],
start,
end,
};
+4 -3
View File
@@ -143,7 +143,7 @@ export class JourneyMcp {
@Tool({
name: 'get_journey_stats',
description: 'What a journey adds up to: distance travelled in metres, calendar days spanned, countries in visit order, the furthest point reached, and entry, photo and place counts. Prefer this over get_journey whenever the question is about totals, since the stats get_journey carries are three counts and nothing else.',
description: 'What a journey adds up to: distance travelled in metres, calendar days spanned, countries in visit order, the furthest point reached, and entry, photo and place counts. Stops the traveller switched off with update_journey_entry count towards none of those and are listed under excluded instead. Prefer this over get_journey whenever the question is about totals, since the stats get_journey carries are three counts and nothing else.',
inputSchema: {
journeyId: z.number().int().positive(),
include_route: z.boolean().optional().describe('Also return the route itself, up to 400 stops with coordinates. Off by default: the totals and the country list do not need it.'),
@@ -348,7 +348,7 @@ export class JourneyMcp {
@Tool({
name: 'update_journey_entry',
description: 'Update an existing journey entry: its text, date, place, coordinates, weather, tags, verdict or visibility. Fields left out keep their value; pass null to clear one. To move an entry within its day use reorder_journey_entries rather than setting sort_order here.',
description: 'Update an existing journey entry: its text, date, place, coordinates, weather, tags, verdict, visibility, or whether it counts as a stop. Fields left out keep their value; pass null to clear one. To move an entry within its day use reorder_journey_entries rather than setting sort_order here.',
inputSchema: {
entryId: z.number().int().positive(),
title: z.string().max(300).nullable().optional(),
@@ -365,6 +365,7 @@ export class JourneyMcp {
visibility: ENTRY_VISIBILITY.optional(),
type: ENTRY_TYPE.optional().describe('Promote a trip-derived "skeleton" to a real "entry" once it has been written up'),
sort_order: z.number().int().min(0).optional(),
stats_excluded: z.boolean().optional().describe('True leaves the entry in the journal but takes it off the journey route and out of its distance, countries and step count (see get_journey_stats); false puts it back. For the home airport, a stopover, the place the trip was planned from'),
},
annotations: TOOL_ANNOTATIONS_WRITE,
when: journeyAddonOn,
@@ -376,7 +377,7 @@ export class JourneyMcp {
entry_time?: string | null; location_name?: string | null; location_lat?: number | null;
location_lng?: number | null; mood?: string | null; weather?: string | null;
tags?: string[] | null; pros_cons?: { pros: string[]; cons: string[] } | null;
visibility?: EntryVisibility; type?: EntryType; sort_order?: number;
visibility?: EntryVisibility; type?: EntryType; sort_order?: number; stats_excluded?: boolean;
},
ctx: McpContext,
) {
+6
View File
@@ -382,6 +382,12 @@ export interface JourneyEntry {
pros_cons?: string | null;
visibility: 'private' | 'shared' | 'public';
sort_order: number;
/**
* 0/1 as the row holds it. Switched on, the entry stays in the journal but
* is left out of the route and the figures Studio prints (discussion #2064).
* The wire carries a boolean; journey-entry-row.ts is where the two meet.
*/
stats_excluded: number;
created_at: number;
updated_at: number;
}