trails/apps/journal/app/lib/routes.server.ts
Ullrich Schäfer 5905cdadea
Apply public-content-visibility: visibility flag + public profile
Implements the public-content-visibility OpenSpec change. Adds the
smallest social surface that lets us demo the product to logged-out
visitors without user signup.

Schema:
- `visibility text NOT NULL DEFAULT 'private'` on routes + activities.
- Shared Visibility type exported from the schema module.

Access:
- New canView(content, viewer, { asDirectLink }) helper in auth.server.ts
  centralises the rule: public → anyone; unlisted → anyone on direct
  link; private → owner only.
- routes.$id and activities.$id loaders return 404 (not 403) when
  canView rejects, so existence of private content isn't leaked.
- Detail pages emit Open Graph + Twitter Card meta on public/unlisted
  content only.

Editing:
- Visibility <select> on routes/:id/edit with owner-only access.
- Activity detail page gets a small visibility form + set-visibility
  action intent (no separate activity-edit page needed).
- EN + DE i18n under routes.visibility.* and activities.visibility.*.

Listings:
- Listing helpers listPublicRoutesForOwner / listPublicActivitiesForOwner
  for cross-user queries. Existing owner-scoped listRoutes/listActivities
  stay — owners see their own content regardless of visibility.

Public profile:
- /users/:username is now truly public. Renders the user's public
  routes + activities, 404s when no public content exists AND viewer
  isn't the owner (prevents account enumeration).
- Owner sees a short "this is your profile" note linking to settings.
- Open Graph meta (og:type=profile) for shareable preview.

Privacy manifest:
- Added a bullet noting public content is world-visible on profile
  and indexable by search engines.
- Bumped PRIVACY_LAST_UPDATED to 2026-04-20 + rendered legal-archive
  snapshot.

Tests:
- 13 unit tests for canView covering the full matrix.
- 6 E2E tests in e2e/public-content.test.ts covering:
  - Private route → 404 for logged-out visitor
  - Public route → reachable + OG tags present (og:title, og:type,
    og:site_name)
  - Owner still sees own private content
  - Profile 404 when no public content
  - Profile renders when at least one public route exists
  - Unlisted route reachable via direct URL but hidden from profile
- Test file runs serially (describe.configure mode=serial) to avoid
  WebAuthn virtual-authenticator races under Playwright's default
  parallel workers.
- New public-content Playwright project added to config.

Rollout safety: every existing row in prod keeps visibility='private'
by default — nothing becomes visible to outsiders until an owner
explicitly opts in.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 09:11:39 +02:00

243 lines
7.3 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { eq, desc, and } from "drizzle-orm";
import { getDb } from "./db.ts";
import { routes, routeVersions } from "@trails-cool/db/schema/journal";
import type { Visibility } from "@trails-cool/db/schema/journal";
import { parseGpxAsync } from "@trails-cool/gpx";
import { sql } from "drizzle-orm";
export interface RouteInput {
name: string;
description?: string;
gpx?: string;
routingProfile?: string;
visibility?: Visibility;
}
export async function createRoute(ownerId: string, input: RouteInput) {
const db = getDb();
const id = randomUUID();
let distance: number | null = null;
let elevationGain: number | null = null;
let elevationLoss: number | null = null;
let dayBreaks: number[] = [];
if (input.gpx) {
const stats = await computeRouteStats(input.gpx);
distance = stats.distance;
elevationGain = stats.elevationGain;
elevationLoss = stats.elevationLoss;
dayBreaks = stats.dayBreaks;
}
await db.insert(routes).values({
id,
ownerId,
name: input.name,
description: input.description ?? "",
gpx: input.gpx,
routingProfile: input.routingProfile,
distance,
elevationGain,
elevationLoss,
dayBreaks,
});
if (input.gpx) {
await setGeomFromGpx(id, "routes", input.gpx);
}
// Create initial version if GPX provided
if (input.gpx) {
await db.insert(routeVersions).values({
id: randomUUID(),
routeId: id,
version: 1,
gpx: input.gpx,
createdBy: ownerId,
changeDescription: "Initial version",
});
}
return id;
}
export async function getRoute(id: string) {
const db = getDb();
const [route] = await db.select().from(routes).where(eq(routes.id, id));
if (!route) return null;
const geojson = await getGeojson("routes", id);
return { ...route, geojson };
}
export async function getRouteWithVersions(id: string) {
const db = getDb();
const [route] = await db.select().from(routes).where(eq(routes.id, id));
if (!route) return null;
const versions = await db
.select()
.from(routeVersions)
.where(eq(routeVersions.routeId, id))
.orderBy(desc(routeVersions.version));
return { ...route, versions };
}
export async function listRoutes(ownerId: string) {
const db = getDb();
const rows = await db
.select()
.from(routes)
.where(eq(routes.ownerId, ownerId))
.orderBy(desc(routes.updatedAt));
// Batch-fetch simplified GeoJSON for list thumbnails
const ids = rows.map((r) => r.id);
const geojsonMap = ids.length > 0 ? await getSimplifiedGeojsonBatch(ids) : new Map();
return rows.map((r) => ({ ...r, geojson: geojsonMap.get(r.id) ?? null }));
}
/**
* List the *public* routes of a given owner. Used for cross-user listings
* (the public profile page); never includes `unlisted` or `private` content.
*/
export async function listPublicRoutesForOwner(ownerId: string) {
const db = getDb();
const rows = await db
.select()
.from(routes)
.where(and(eq(routes.ownerId, ownerId), eq(routes.visibility, "public")))
.orderBy(desc(routes.updatedAt));
const ids = rows.map((r) => r.id);
const geojsonMap = ids.length > 0 ? await getSimplifiedGeojsonBatch(ids) : new Map();
return rows.map((r) => ({ ...r, geojson: geojsonMap.get(r.id) ?? null }));
}
export async function updateRoute(
id: string,
ownerId: string,
input: Partial<RouteInput>,
) {
const db = getDb();
const updateData: Record<string, unknown> = { updatedAt: new Date() };
if (input.name !== undefined) updateData.name = input.name;
if (input.description !== undefined) updateData.description = input.description;
if (input.visibility !== undefined) updateData.visibility = input.visibility;
if (input.gpx) {
updateData.gpx = input.gpx;
const stats = await computeRouteStats(input.gpx);
updateData.distance = stats.distance;
updateData.elevationGain = stats.elevationGain;
updateData.elevationLoss = stats.elevationLoss;
updateData.dayBreaks = stats.dayBreaks;
if (stats.description && input.description === undefined) {
updateData.description = stats.description;
}
// Get next version number
const existingVersions = await db
.select()
.from(routeVersions)
.where(eq(routeVersions.routeId, id))
.orderBy(desc(routeVersions.version));
const nextVersion = (existingVersions[0]?.version ?? 0) + 1;
await db.insert(routeVersions).values({
id: randomUUID(),
routeId: id,
version: nextVersion,
gpx: input.gpx,
createdBy: ownerId,
});
}
await db
.update(routes)
.set(updateData)
.where(and(eq(routes.id, id), eq(routes.ownerId, ownerId)));
if (input.gpx) {
await setGeomFromGpx(id, "routes", input.gpx);
}
}
export async function deleteRoute(id: string, ownerId: string) {
const db = getDb();
const result = await db
.delete(routes)
.where(and(eq(routes.id, id), eq(routes.ownerId, ownerId)))
.returning({ id: routes.id });
return result.length > 0;
}
async function computeRouteStats(gpxString: string) {
try {
const gpxData = await parseGpxAsync(gpxString);
const dayBreaks = gpxData.waypoints
.map((w, i) => (w.isDayBreak ? i : -1))
.filter((i) => i >= 0);
return {
distance: gpxData.distance,
elevationGain: gpxData.elevation.gain,
elevationLoss: gpxData.elevation.loss,
dayBreaks,
description: gpxData.description,
};
} catch {
return { distance: null, elevationGain: null, elevationLoss: null, dayBreaks: [] as number[], description: undefined };
}
}
async function setGeomFromGpx(id: string, table: "routes" | "activities", gpxString: string) {
try {
const gpxData = await parseGpxAsync(gpxString);
const coords = gpxData.tracks.flat().map((p) => [p.lon, p.lat] as [number, number]);
if (coords.length < 2) return;
const geojson = JSON.stringify({ type: "LineString", coordinates: coords });
const db = getDb();
await db.execute(
sql`UPDATE ${sql.identifier("journal")}.${sql.identifier(table)} SET geom = ST_GeomFromGeoJSON(${geojson}) WHERE id = ${id}`,
);
} catch (e) {
console.error(`Failed to set geom for ${table}/${id}:`, e);
}
}
export { setGeomFromGpx };
async function getGeojson(table: "routes" | "activities", id: string): Promise<string | null> {
try {
const db = getDb();
const result = await db.execute(
sql`SELECT ST_AsGeoJSON(geom) as geojson FROM ${sql.identifier("journal")}.${sql.identifier(table)} WHERE id = ${id} AND geom IS NOT NULL`,
);
const row = (result as unknown as Array<{ geojson: string }>)[0];
return row?.geojson ?? null;
} catch {
return null;
}
}
async function getSimplifiedGeojsonBatch(ids: string[]): Promise<Map<string, string>> {
const map = new Map<string, string>();
if (ids.length === 0) return map;
try {
const db = getDb();
// Fetch individually — Drizzle's sql template doesn't handle array params well with ANY()
await Promise.all(ids.map(async (id) => {
const result = await db.execute(
sql`SELECT ST_AsGeoJSON(ST_Simplify(geom, 0.001)) as geojson FROM journal.routes WHERE id = ${id} AND geom IS NOT NULL`,
);
const row = (result as unknown as Array<{ geojson: string }>)[0];
if (row?.geojson) map.set(id, row.geojson);
}));
} catch {
// Fallback: no geojson
}
return map;
}