trails/apps/journal/app/root.tsx
Ullrich Schäfer 811d5f62f5 Implement social-feed: local follows + /feed + profile visibility
Implements the social-feed change end-to-end. Local-only follows
between users on the same instance, an aggregated /feed of public
activities from people you follow, and an explicit profile_visibility
setting so the question "can someone follow me?" has a deterministic
answer.

Schema (additive, drizzle-kit push --force in cd-apps handles it):
- journal.follows table keyed by `followed_actor_iri TEXT` for
  federation forward-compat. Local IRIs look like
  `${ORIGIN}/users/${username}`. `accepted_at` is nullable so the
  Pending state from social-federation slots in without migration.
- journal.users.profile_visibility ('public' | 'private', default
  'public'). Existing users land 'public' via the default; current
  effective behavior is unchanged.

Server (apps/journal/app/lib):
- actor-iri.ts: localActorIri(username) helper — single source of
  truth for IRI construction.
- follow.server.ts: followUser / unfollowUser / getFollowState /
  countFollowers / countFollowing / listFollowers / listFollowing.
  Refuses self-follow + private targets. Idempotent.
- activities.server.ts: listSocialFeed(followerId, limit) joining
  follows → activities WHERE visibility='public', reverse-chrono.

Routes:
- POST /api/users/:username/follow + /unfollow (session-bound)
- /feed (signed-in only; redirects anon to /auth/login)
- /users/:username/followers + /users/:username/following (paginated)
- /users/:username gates on profile_visibility AND has-public-content
  for visitors; owners on private get an amber explainer banner.
- /settings adds a Public/Private radio with explainer text.

UI:
- FollowButton component on profile page (hidden for owner + anon).
- Follower/following counts on profile linking to collection pages.
- "Feed" link in nav (signed-in) + on personal dashboard alongside
  "New Activity".

Privacy manifest updated to document the new follows relation and
profile_visibility setting.

Tests: follow.integration.test.ts (FOLLOW_INTEGRATION=1) for the
follow lifecycle; e2e/social.test.ts for /feed redirect, follow
button + count transitions, and the profile_visibility 404 toggle.

Local development: run `pnpm db:push` after pulling to apply the
schema additions. Production migrates automatically via cd-apps.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 22:51:18 +02:00

200 lines
6.6 KiB
TypeScript

import { useEffect } from "react";
import { Links, Meta, Outlet, Scripts, ScrollRestoration, isRouteErrorResponse, useLocation, Form, Link, redirect } from "react-router";
import type { LinksFunction } from "react-router";
import type { Route } from "./+types/root";
import * as Sentry from "@sentry/react";
import { useTranslation } from "react-i18next";
import { detectLocale } from "@trails-cool/i18n";
import { getSessionUser } from "~/lib/auth.server";
import { LocaleProvider } from "~/components/LocaleContext";
import { AlphaBanner } from "~/components/AlphaBanner";
import { Footer } from "~/components/Footer";
import { initSentryClient, stopSentryClient } from "~/lib/sentry.client";
import { TERMS_VERSION } from "~/lib/legal";
import stylesheet from "@trails-cool/ui/styles.css?url";
// Paths that must stay reachable even when the user has a stale
// terms_version, so they can read the Terms, accept them, or log out.
const TERMS_GATE_ALLOWLIST = [
"/auth/accept-terms",
"/auth/logout",
"/legal/",
];
export const links: LinksFunction = () => [{ rel: "stylesheet", href: stylesheet }];
export function Layout({ children }: { children: React.ReactNode }) {
const { i18n } = useTranslation();
return (
<html lang={i18n.language}>
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<Meta />
<Links />
</head>
<body className="min-h-screen bg-gray-50">
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
);
}
export async function loader({ request }: Route.LoaderArgs) {
const user = await getSessionUser(request);
const locale = detectLocale(request);
// Gate logged-in users with stale / missing terms_version: send them to the
// re-accept page on any request that isn't already on an allow-listed path.
if (user && user.termsVersion !== TERMS_VERSION) {
const pathname = new URL(request.url).pathname;
const onAllowlistedPath = TERMS_GATE_ALLOWLIST.some((p) =>
p.endsWith("/") ? pathname.startsWith(p) : pathname === p,
);
if (!onAllowlistedPath) {
const returnTo = encodeURIComponent(pathname + new URL(request.url).search);
throw redirect(`/auth/accept-terms?returnTo=${returnTo}`);
}
}
return { user: user ? { id: user.id, username: user.username } : null, locale };
}
function NavBar({ user }: { user: { id: string; username: string } | null }) {
const { t } = useTranslation("journal");
const location = useLocation();
const isActive = (path: string) =>
location.pathname === path || location.pathname.startsWith(path + "/");
const linkClass = (path: string) =>
`text-sm font-medium ${
isActive(path)
? "text-blue-600"
: "text-gray-600 hover:text-gray-900"
}`;
return (
<nav className="border-b border-gray-200 bg-white px-4 py-2">
<div className="mx-auto flex max-w-6xl items-center justify-between">
<div className="flex items-center gap-6">
<Link to="/" className="text-lg font-semibold text-gray-900">
{t("title")}
</Link>
{user && (
<>
<Link to="/feed" className={linkClass("/feed")}>
{t("social.feed.title")}
</Link>
<Link to="/routes" className={linkClass("/routes")}>
{t("nav.routes")}
</Link>
<Link to="/activities" className={linkClass("/activities")}>
{t("nav.activities")}
</Link>
</>
)}
</div>
<div className="flex items-center gap-4">
{user ? (
<>
<Link
to={`/users/${user.username}`}
className={linkClass(`/users/${user.username}`)}
>
{user.username}
</Link>
<Link to="/settings" className={linkClass("/settings")}>
{t("nav.settings")}
</Link>
<Form method="post" action="/auth/logout">
<button
type="submit"
className="text-sm font-medium text-gray-600 hover:text-gray-900"
>
{t("nav.logout")}
</button>
</Form>
</>
) : (
<>
<Link to="/auth/login" className={linkClass("/auth/login")}>
{t("nav.login")}
</Link>
<Link
to="/auth/register"
className="rounded-md bg-blue-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-blue-700"
>
{t("nav.register")}
</Link>
</>
)}
</div>
</div>
</nav>
);
}
export default function App({ loaderData }: Route.ComponentProps) {
const user = loaderData?.user;
const locale = loaderData?.locale ?? "en";
useEffect(() => {
if (user) {
initSentryClient();
Sentry.setUser({ id: user.id });
} else {
Sentry.setUser(null);
stopSentryClient();
}
}, [user]);
return (
<LocaleProvider locale={locale}>
<AlphaBanner />
<NavBar user={user ?? null} />
<Outlet />
<Footer />
</LocaleProvider>
);
}
export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
// Don't report expected HTTP errors (404, 403) to Sentry
if (!(isRouteErrorResponse(error) && error.status < 500)) {
Sentry.captureException(error);
}
const { t } = useTranslation();
if (isRouteErrorResponse(error)) {
return (
<div className="mx-auto max-w-md px-4 py-16 text-center">
<h1 className="text-4xl font-bold text-gray-900">{error.status}</h1>
<p className="mt-2 text-gray-600">
{error.status === 404 && t("pageNotFound")}
{error.status === 503 && t("serviceUnavailable")}
{error.status !== 404 && error.status !== 503 && (error.statusText || t("error"))}
</p>
<a href="/" className="mt-6 inline-block text-blue-600 hover:underline">
{t("goHome")}
</a>
</div>
);
}
return (
<div className="mx-auto max-w-md px-4 py-16 text-center">
<h1 className="text-4xl font-bold text-gray-900">{t("error")}</h1>
<p className="mt-2 text-gray-600">
{error instanceof Error ? error.message : t("error")}
</p>
<a href="/" className="mt-6 inline-block text-blue-600 hover:underline">
{t("goHome")}
</a>
</div>
);
}