mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
Give GPX tracks their own colour (#776)
Imported tracks all render in the same blue: the GPX/KML importers never assign a category, so category_color is null for every one of them and several walks in the same area are impossible to tell apart. Adds a nullable route_color on places, kept out of the COALESCE group in updatePlace so an explicit null can reset a track back to inheriting its category colour. The hex is validated in the Nest controller rather than in the shared contract: the update body is an open record on purpose, and tightening it would mean removing PlacesController.update from the ratchet-only body-contract allow-list. The importers now hand each new track a colour of its own, picked from the palette entries the trip is not already using. That happens in the Nest service over the injected DatabaseService — the colour assignment is new logic, and new logic does not belong in the legacy place service (see server/src/nest/README.md). The field itself is only threaded through the existing update/create statements, which the proportionality rule covers. Also fixes duplicateTrip, whose explicit column list has been silently dropping route_geometry all along, so a duplicated trip lost its tracks.
This commit is contained in:
@@ -3822,6 +3822,14 @@ function runMigrations(db: Database.Database): void {
|
||||
);
|
||||
`);
|
||||
},
|
||||
// Manual GPX track colour (#776): imported tracks all render in the same blue
|
||||
// because the importer never assigns a category, so several walks in the same
|
||||
// area are indistinguishable. NULL keeps the old behaviour (category colour,
|
||||
// then the #3b82f6 fallback) for every existing row.
|
||||
() => {
|
||||
const hasRouteColor = db.prepare("SELECT 1 FROM pragma_table_info('places') WHERE name = 'route_color'").get();
|
||||
if (!hasRouteColor) db.exec('ALTER TABLE places ADD COLUMN route_color TEXT');
|
||||
},
|
||||
];
|
||||
|
||||
if (currentVersion < migrations.length) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { memoryStorage } from 'multer';
|
||||
import { hexColorSchema } from '@trek/shared';
|
||||
import { readEnv } from '../../app-config';
|
||||
import type { User } from '../../types';
|
||||
import { PlacesService } from './places.service';
|
||||
@@ -39,6 +40,16 @@ function validateLengths(body: Record<string, unknown>): void {
|
||||
}
|
||||
}
|
||||
|
||||
// A bad hex makes MapLibre fail hard when it parses the paint property, and the
|
||||
// update body is an open record that Zod does not police — so check it here.
|
||||
function validateRouteColor(body: Record<string, unknown>): void {
|
||||
const value = body.route_color;
|
||||
if (value === undefined || value === null) return;
|
||||
if (typeof value !== 'string' || !hexColorSchema.safeParse(value).success) {
|
||||
throw new HttpException({ error: 'route_color must be a hex colour like #4f46e5' }, 400);
|
||||
}
|
||||
}
|
||||
|
||||
function parseBool(v: unknown, defaultVal: boolean): boolean {
|
||||
return v === undefined || v === null ? defaultVal : String(v) === 'true';
|
||||
}
|
||||
@@ -93,6 +104,7 @@ export class PlacesController {
|
||||
) {
|
||||
const trip = this.requireTrip(tripId, user);
|
||||
validateLengths(body);
|
||||
validateRouteColor(body);
|
||||
this.requireEdit(trip, user);
|
||||
if (!body.name) {
|
||||
throw new HttpException({ error: 'Place name is required' }, 400);
|
||||
@@ -358,6 +370,7 @@ export class PlacesController {
|
||||
) {
|
||||
const trip = this.requireTrip(tripId, user);
|
||||
validateLengths(body);
|
||||
validateRouteColor(body);
|
||||
this.requireEdit(trip, user);
|
||||
const result = this.places.update(tripId, id, body as never, ifMatch);
|
||||
if (!result) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { TRACK_COLORS } from '@trek/shared';
|
||||
import { broadcast } from '../../websocket';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { PermissionsService } from '../permissions/permissions.service';
|
||||
@@ -8,6 +9,8 @@ import { onPlaceCreated, onPlaceUpdated, onPlaceDeleted } from '../../services/j
|
||||
|
||||
type Trip = { user_id: number };
|
||||
|
||||
type ImportedPlace = { id: number; route_geometry?: string | null; route_color?: string | null };
|
||||
|
||||
/**
|
||||
* Thin Nest wrapper around the existing place service. Trip access mirrors the
|
||||
* requireTripAccess middleware (canAccessTrip); mutations use 'place_edit'. The
|
||||
@@ -66,11 +69,53 @@ export class PlacesService {
|
||||
buffer: Buffer,
|
||||
opts: { importWaypoints: boolean; importRoutes: boolean; importTracks: boolean; defaultName?: string },
|
||||
) {
|
||||
return svc.importGpx(tripId, buffer, opts);
|
||||
return this.colorizeImportedTracks(tripId, svc.importGpx(tripId, buffer, opts));
|
||||
}
|
||||
|
||||
importMapFile(tripId: string, buffer: Buffer, filename: string, opts: svc.KmlImportOptions) {
|
||||
return svc.importMapFile(tripId, buffer, filename, opts);
|
||||
async importMapFile(tripId: string, buffer: Buffer, filename: string, opts: svc.KmlImportOptions) {
|
||||
return this.colorizeImportedTracks(tripId, await svc.importMapFile(tripId, buffer, filename, opts));
|
||||
}
|
||||
|
||||
/**
|
||||
* Hand every freshly imported track its own colour (#776).
|
||||
*
|
||||
* The importers never assign a category, so without this every track in a
|
||||
* trip renders in the same #3b82f6 — which is the actual complaint behind
|
||||
* the request: several walks in one area are impossible to tell apart.
|
||||
*
|
||||
* Picks the palette entries the trip is not already using rather than
|
||||
* counting rows: counting collides as soon as somebody recolours a track by
|
||||
* hand or deletes one, which is exactly when distinguishability matters.
|
||||
* Once all ten are taken it wraps around — ten walks in one trip is already
|
||||
* past what a colour alone can separate.
|
||||
*
|
||||
* Only rows that carry geometry are touched, and only ones that have no
|
||||
* colour yet; plain waypoints and existing places stay untouched.
|
||||
*/
|
||||
private colorizeImportedTracks<T extends { places: ImportedPlace[] } | null>(tripId: string, result: T): T {
|
||||
const tracks = result?.places?.filter((p) => p.route_geometry && !p.route_color) ?? [];
|
||||
if (tracks.length === 0) return result;
|
||||
|
||||
// Read and write in one transaction so two concurrent imports cannot both
|
||||
// read the same set of free colours.
|
||||
this.dbs.transaction((conn) => {
|
||||
const taken = new Set(
|
||||
(conn
|
||||
.prepare('SELECT DISTINCT route_color AS c FROM places WHERE trip_id = ? AND route_color IS NOT NULL')
|
||||
.all(tripId) as { c: string }[]).map((r) => r.c),
|
||||
);
|
||||
const free = TRACK_COLORS.filter((c) => !taken.has(c));
|
||||
const stmt = conn.prepare('UPDATE places SET route_color = ? WHERE id = ?');
|
||||
tracks.forEach((track, i) => {
|
||||
// Free ones first, then wrap through the whole palette — never reuse a
|
||||
// free colour twice within the same import.
|
||||
const color = i < free.length ? free[i] : TRACK_COLORS[(i - free.length) % TRACK_COLORS.length];
|
||||
stmt.run(color, track.id);
|
||||
track.route_color = color;
|
||||
});
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
importGoogleList(tripId: string, url: string, opts?: Parameters<typeof svc.importGoogleList>[2]) {
|
||||
|
||||
@@ -125,26 +125,28 @@ export function createPlace(
|
||||
place_time?: string; end_time?: string;
|
||||
duration_minutes?: number; notes?: string; image_url?: string;
|
||||
google_place_id?: string; google_ftid?: string; osm_id?: string; website?: string; phone?: string;
|
||||
transport_mode?: string; tags?: number[];
|
||||
transport_mode?: string; route_geometry?: string; route_color?: string; tags?: number[];
|
||||
},
|
||||
) {
|
||||
const {
|
||||
name, description, lat, lng, address, category_id, price, currency,
|
||||
place_time, end_time,
|
||||
duration_minutes, notes, image_url, google_place_id, google_ftid, osm_id, website, phone,
|
||||
transport_mode, tags = [],
|
||||
transport_mode, route_geometry, route_color, tags = [],
|
||||
} = body;
|
||||
|
||||
const result = db.prepare(`
|
||||
INSERT INTO places (trip_id, name, description, lat, lng, address, category_id, price, currency,
|
||||
place_time, end_time,
|
||||
duration_minutes, notes, image_url, google_place_id, google_ftid, osm_id, website, phone, transport_mode)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
duration_minutes, notes, image_url, google_place_id, google_ftid, osm_id, website, phone, transport_mode,
|
||||
route_geometry, route_color)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
tripId, name, description || null, lat || null, lng || null, address || null,
|
||||
category_id || null, price || null, currency || null,
|
||||
place_time || null, end_time || null, duration_minutes || 60, notes || null, image_url || null,
|
||||
google_place_id || null, google_ftid || null, osm_id || null, website || null, phone || null, transport_mode || 'walking',
|
||||
route_geometry || null, route_color || null,
|
||||
);
|
||||
|
||||
const placeId = result.lastInsertRowid;
|
||||
@@ -182,7 +184,7 @@ export function updatePlace(
|
||||
place_time?: string; end_time?: string;
|
||||
duration_minutes?: number; notes?: string; image_url?: string;
|
||||
google_place_id?: string; google_ftid?: string; osm_id?: string; website?: string; phone?: string;
|
||||
transport_mode?: string; tags?: number[];
|
||||
transport_mode?: string; route_color?: string | null; tags?: number[];
|
||||
},
|
||||
ifMatch?: string,
|
||||
): ReturnType<typeof getPlaceWithTags> | UpdateConflict | null {
|
||||
@@ -200,7 +202,7 @@ export function updatePlace(
|
||||
name, description, lat, lng, address, category_id, price, currency,
|
||||
place_time, end_time,
|
||||
duration_minutes, notes, image_url, google_place_id, google_ftid, osm_id, website, phone,
|
||||
transport_mode, tags,
|
||||
transport_mode, route_color, tags,
|
||||
} = body;
|
||||
|
||||
db.prepare(`
|
||||
@@ -224,6 +226,7 @@ export function updatePlace(
|
||||
website = ?,
|
||||
phone = ?,
|
||||
transport_mode = COALESCE(?, transport_mode),
|
||||
route_color = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
`).run(
|
||||
@@ -246,6 +249,9 @@ export function updatePlace(
|
||||
website !== undefined ? website : existingPlace.website,
|
||||
phone !== undefined ? phone : existingPlace.phone,
|
||||
transport_mode || null,
|
||||
// Deliberately not COALESCE: an explicit null is how the picker resets a
|
||||
// track back to its category colour (#776).
|
||||
route_color !== undefined ? route_color : existingPlace.route_color,
|
||||
placeId,
|
||||
);
|
||||
|
||||
|
||||
@@ -917,14 +917,15 @@ export function copyTripById(sourceTripId: string | number, newOwnerId: number,
|
||||
const insertPlace = db.prepare(`
|
||||
INSERT INTO places (trip_id, name, description, lat, lng, address, category_id, price, currency,
|
||||
reservation_status, reservation_notes, reservation_datetime, place_time, end_time,
|
||||
duration_minutes, notes, image_url, google_place_id, google_ftid, website, phone, transport_mode, osm_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
duration_minutes, notes, image_url, google_place_id, google_ftid, website, phone, transport_mode, osm_id,
|
||||
route_geometry, route_color)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
for (const p of oldPlaces) {
|
||||
const r = insertPlace.run(newTripId, p.name, p.description, p.lat, p.lng, p.address, p.category_id,
|
||||
p.price, p.currency, p.reservation_status, p.reservation_notes, p.reservation_datetime,
|
||||
p.place_time, p.end_time, p.duration_minutes, p.notes, p.image_url, p.google_place_id,
|
||||
p.google_ftid, p.website, p.phone, p.transport_mode, p.osm_id);
|
||||
p.google_ftid, p.website, p.phone, p.transport_mode, p.osm_id, p.route_geometry, p.route_color);
|
||||
placeMap.set(p.id, r.lastInsertRowid);
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,8 @@ export interface Place {
|
||||
google_place_id?: string | null;
|
||||
google_ftid?: string | null;
|
||||
osm_id?: string | null;
|
||||
route_geometry?: string | null;
|
||||
route_color?: string | null;
|
||||
website?: string | null;
|
||||
phone?: string | null;
|
||||
transport_mode?: string;
|
||||
|
||||
Reference in New Issue
Block a user