feat(collections): a list as a file, so it can leave the instance (#2198)

Sharing a list works between people on the same TREK. This is the other
case: giving one to somebody who runs their own, or keeping a copy. Export
sits beside Edit and Share in the list header and downloads the open list;
Import sits beside New list in the rail, reads a file, shows what is in it
and makes a new list of it. Desktop only, which the route already decides.

What travels is what somebody wrote down: the list's name, colour and
description, its labels, and each place with its address, coordinates,
notes, status, contact details, category name and labels. A file is dull
JSON on purpose, so it can be read and edited by a person.

What does not travel is everything that only means something on the instance
it came from. Ids of every kind, since the receiver issues its own and a
category crosses over by name. Star ratings and members, because a rating is
an opinion somebody gave the people on that list. Provenance into trips,
which points at rows the receiver cannot see. And an image path under
/uploads, which resolves on the sender's server: only an absolute https URL
survives.

A file is a stranger's JSON and is read as one. It is parsed in the browser
that chose it and never uploaded; the parsed list goes through the same
contract on both sides; and a link that is not http(s) arrives as nothing
rather than as an anchor. Strict where it must refuse, forgiving where it
can drop: a file that does not say what it is never reaches the importer,
while one unreadable place inside it is skipped and counted, and one bad
field on a good place costs that field alone.

An import is always a new list. Importing into an existing one would mean
deciding what happens to a place that is already there, and every answer to
that is a merge nobody asked for.
This commit is contained in:
Maurice
2026-09-11 14:06:05 +02:00
committed by Maurice
parent a8e4c45576
commit 2c06c9904b
43 changed files with 1803 additions and 16 deletions
@@ -51,6 +51,7 @@ import {
CollectionLabelCreateDto,
CollectionLabelUpdateDto,
CollectionLabelAssignDto,
CollectionImportDto,
} from './collections.dto';
import { PlaceRatingDto } from '../places/places.dto';
@@ -199,6 +200,17 @@ export class CollectionsController {
}
// ── Copy to trip ────────────────────────────────────────────────────────────
/**
* Read a list file back as a new list of the caller's own (#2198).
*
* 201, like POST / above, because it creates a list. The other POSTs here
* are @HttpCode(200) because they act on one that already exists.
*/
@Post('import')
importCollection(@CurrentUser() user: User, @Body() body: CollectionImportDto) {
return this.collections.importCollection(user.id, body);
}
@Post('copy-to-trip')
@HttpCode(200)
copyToTrip(@CurrentUser() user: User, @Body() body: CollectionCopyToTripDto) {
@@ -370,6 +382,16 @@ export class CollectionsController {
/** Preview for the bulk trip import: the trip's places plus, per place, the same
* duplicate verdict the import itself applies. Read-only, so the dialog can grey out
* what would be skipped instead of reporting it afterwards. */
/**
* The list as a file (#2198). A plain JSON body: the browser turns it into a
* download, so there is no Content-Disposition to get wrong here and no
* filename decided by the server.
*/
@Get(':id/export')
exportCollection(@CurrentUser() user: User, @Param('id') id: string) {
return this.collections.exportCollection(user.id, Number(id));
}
@Get(':id/importable/:tripId')
importable(@CurrentUser() user: User, @Param('id') id: string, @Param('tripId') tripId: string) {
return this.collections.importablePlaces(user.id, Number(id), Number(tripId));
@@ -20,6 +20,7 @@ import {
collectionLabelCreateRequestSchema,
collectionLabelUpdateRequestSchema,
collectionLabelAssignRequestSchema,
collectionImportRequestSchema,
} from '@trek/shared';
/**
@@ -49,3 +50,4 @@ export class CollectionSetMemberRoleDto extends createZodDto(collectionSetMember
export class CollectionLabelCreateDto extends createZodDto(collectionLabelCreateRequestSchema) {}
export class CollectionLabelUpdateDto extends createZodDto(collectionLabelUpdateRequestSchema) {}
export class CollectionLabelAssignDto extends createZodDto(collectionLabelAssignRequestSchema) {}
export class CollectionImportDto extends createZodDto(collectionImportRequestSchema) {}
@@ -12,7 +12,19 @@ import {
trackInsertedInDedupSet,
type DedupSet,
} from '../places/places.helpers';
import { placeMatchStrategies, type PlaceMatchCandidate } from '@trek/shared';
import {
placeMatchStrategies,
collectionFilePlaceSchema,
COLLECTION_FILE_FORMAT,
COLLECTION_FILE_VERSION,
MAX_COLLECTION_FILE_LABELS,
type PlaceMatchCandidate,
type CollectionFile,
type CollectionFileLabel,
type CollectionFilePlace,
type CollectionImportRequest,
type CollectionImportResult,
} from '@trek/shared';
import type {
Collection,
CollectionDetailResponse,
@@ -339,6 +351,170 @@ export class CollectionsService {
};
}
// -------------------------------------------------------------------------
// Export / import as a file (#2198)
// -------------------------------------------------------------------------
/**
* A list as a portable file.
*
* Built here rather than in the browser from what the page happens to hold,
* for two reasons: the read is access-checked like every other read, and the
* decision about what may leave the instance is one decision in one place
* instead of whatever the client forgot to strip. The contract in
* `collection-file.schema.ts` says what travels and why.
*
* Any member may export. A list is shared with somebody so they can use it,
* and a viewer who can read all of this on screen loses nothing by having it
* as a file; what a viewer must not do is write, which no export does.
*/
exportCollection(userId: number, id: number): CollectionFile {
this.assertAccess(userId, id);
const collection = this.getCollectionRow(id);
const labels = this.loadLabelsByCollection(id);
const labelNameById = new Map(labels.map(l => [l.id, l.name]));
const rows = this.db.all<PlaceRow>(`
SELECT cp.*, c.name AS category_name, c.color AS category_color, c.icon AS category_icon
FROM collection_places cp
LEFT JOIN categories c ON cp.category_id = c.id
WHERE cp.collection_id = ?
ORDER BY cp.sort_order, cp.created_at
`, id);
const labelIdsByPlace = this.loadLabelIdsByPlaceIds(rows.map(r => r.id));
const places: CollectionFilePlace[] = rows.map(row => ({
name: row.name,
description: row.description ?? null,
lat: row.lat ?? null,
lng: row.lng ?? null,
address: row.address ?? null,
notes: row.notes ?? null,
price: row.price ?? null,
currency: row.currency ?? null,
website: row.website ?? null,
phone: row.phone ?? null,
// Only an absolute https URL survives: a /uploads path resolves on this
// server, not the reader's. See the note in the file contract.
image_url: typeof row.image_url === 'string' && /^https:\/\//i.test(row.image_url) ? row.image_url : null,
google_place_id: row.google_place_id ?? null,
google_ftid: row.google_ftid ?? null,
osm_id: row.osm_id ?? null,
status: row.status,
links: parseLinks((row as { links?: unknown }).links),
category: row.category_name ?? null,
labels: (labelIdsByPlace[row.id] || []).map(lid => labelNameById.get(lid)).filter((n): n is string => !!n),
}));
return {
format: COLLECTION_FILE_FORMAT,
version: COLLECTION_FILE_VERSION,
name: collection.name,
description: collection.description ?? null,
color: collection.color ?? null,
icon: collection.icon ?? null,
exported_at: new Date().toISOString(),
labels: labels.map(l => ({ name: l.name, color: l.color ?? null })),
places,
};
}
/**
* Read a file back as a new list of the caller's own.
*
* Always a new list. Importing into an existing one would mean deciding what
* happens to a place that is already there, and every answer to that is a
* merge somebody did not ask for; a fresh list is a thing they can look at
* and then move places out of, which the list UI already does well.
*
* The places go in through the same INSERT the rest of the service uses,
* inside one transaction, so a file that fails halfway leaves nothing
* behind. Each place is re-validated against the file contract on the way
* in — the body was validated once at the pipe, and this is the second pass
* that lets one bad row be dropped instead of failing the whole import.
*/
importCollection(userId: number, body: CollectionImportRequest): CollectionImportResult {
const file = body.file;
const name = (body.name ?? file.name).trim().slice(0, 120) || file.name;
// The palette is instance-wide and read-only here: a file names a category,
// it does not get to create one.
const categoryIdByName = new Map<string, number>();
for (const c of this.db.all<{ id: number; name: string }>('SELECT id, name FROM categories')) {
categoryIdByName.set(c.name.trim().toLowerCase(), c.id);
}
const result = this.db.transaction(() => {
const collection = this.createCollection(userId, {
name,
description: file.description ?? null,
color: file.color ?? undefined,
icon: file.icon ?? undefined,
});
const labelIdByName = new Map<string, number>();
for (const label of (file.labels ?? []).slice(0, MAX_COLLECTION_FILE_LABELS)) {
const key = label.name.trim().toLowerCase();
if (!key || labelIdByName.has(key)) continue;
labelIdByName.set(key, this.insertImportedLabel(collection.id, label, labelIdByName.size));
}
const insertPlace = this.db.prepare(`
INSERT INTO collection_places (
collection_id, owner_id, saved_by, name, description, lat, lng, address,
category_id, price, currency, notes, image_url, google_place_id, google_ftid,
osm_id, website, phone, status, links, sort_order
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
const assignLabel = this.db.prepare('INSERT OR IGNORE INTO collection_place_labels (collection_place_id, label_id) VALUES (?, ?)');
let imported = 0;
let skipped = 0;
for (const raw of file.places) {
const parsed = collectionFilePlaceSchema.safeParse(raw);
if (!parsed.success) { skipped += 1; continue; }
const place = parsed.data;
const res = insertPlace.run(
collection.id, userId, userId,
place.name, place.description ?? null, place.lat ?? null, place.lng ?? null, place.address ?? null,
place.category ? (categoryIdByName.get(place.category.trim().toLowerCase()) ?? null) : null,
place.price ?? null, place.currency ?? null, place.notes ?? null,
place.image_url ?? null, place.google_place_id ?? null, place.google_ftid ?? null,
place.osm_id ?? null, place.website ?? null, place.phone ?? null,
place.status ?? 'idea', serializeLinks(place.links), imported,
);
const placeId = Number(res.lastInsertRowid);
for (const labelName of place.labels ?? []) {
const labelId = labelIdByName.get(labelName.trim().toLowerCase());
if (labelId) assignLabel.run(placeId, labelId);
}
imported += 1;
}
return { collectionId: collection.id, imported, skipped };
});
const collection = this.getCollectionRow(result.collectionId);
return {
collection: { ...collection, is_owner: true, labels: this.loadLabelsByCollection(result.collectionId) },
imported: result.imported,
skipped: result.skipped,
};
}
/**
* A label straight from a file, without createLabel's duplicate check.
*
* The caller already de-duplicates by name, the list is empty, and
* createLabel would notify the (nonexistent) members once per label.
*/
private insertImportedLabel(collectionId: number, label: CollectionFileLabel, sortOrder: number): number {
const res = this.db.run(
'INSERT INTO collection_labels (collection_id, name, color, sort_order) VALUES (?, ?, ?, ?)',
collectionId, label.name.trim(), label.color ?? '#6366f1', sortOrder,
);
return Number(res.lastInsertRowid);
}
createCollection(userId: number, body: CollectionCreateRequest): Collection {
const max = this.db.get<{ m: number }>('SELECT COALESCE(MAX(sort_order), -1) AS m FROM collections WHERE owner_id = ?', userId)!.m;
const result = this.db.run(`