Replaces the earlier 404-for-private model with Mastodon-style locked accounts. A private profile now returns 200 with a stub layout and gates content behind follow approval. Default for new users flips from 'public' to 'private' to align with trails.cool's privacy-first content defaults. Schema: - users.profile_visibility default flipped to 'private'. Existing rows remain 'public' (backfill on first migration handled them). Follow API (follow.server.ts): - followUser now creates Pending (accepted_at = NULL) against private targets and Accepted against public targets — no more refusal. - New: countPendingFollowRequests, listPendingFollowRequests, approveFollowRequest, rejectFollowRequest. Approve/reject are owner-bound: only the followed user can act on their own incoming requests. - countFollowers / countFollowing / listFollowers / listFollowing now filter to accepted-only relations. Loader (users.$username.tsx): - Drops the 404 paths. New canSeeContent flag = isOwn || profile_visibility='public' || (followState.following === true). - When canSeeContent=false, render a stub: header + 🔒 badge + body copy + Request-to-follow / sign-in CTA. Routes/activities sections are not rendered. UI: - FollowButton gains a "Request to follow" / "Requested" state for private targets via a new isPrivateTarget prop. Cancel-request reuses the unfollow endpoint. - New /follows/requests page lists incoming Pending requests with Approve / Reject buttons. - New API routes: POST /api/follows/:id/approve and /reject. - Navbar shows a count badge linking to /follows/requests when pending > 0. Privacy manifest already documents the follows relation; no changes needed (the locked-account semantics don't add new data — same row, different lifecycle). Specs / design (social-feed change): - public-profiles delta rewritten around the four-mode locked model (public, private+anon, private+pending, private+accepted) with scenarios for each. - social-follows delta gains Pending lifecycle requirements (auto vs. manual accept, approve/reject endpoints, pending request management, Pending follows do not contribute to feed). - design.md decision section reflects the new model and rationale for default-private; non-goal "locked-local-accounts as a follow-up" is removed since this change ships it. Tests: - follow.integration.test.ts: pending-against-private, approve flips to accepted, reject deletes, owner-bound enforcement. - e2e/social.test.ts: full Request → Pending → Approve → full-view flow, plus stub-for-anonymous and /follows/requests auth gate. Supersedes PR #309 (closed): the empty-public-profile 200 is now a side-effect of the new render path. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
233 lines
7.9 KiB
TypeScript
233 lines
7.9 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}`);
|
|
}
|
|
}
|
|
|
|
// Pending follow-request count for the navbar badge. Cheap (a single
|
|
// `count(*) WHERE accepted_at IS NULL`); only computed for signed-in
|
|
// users. Hidden behind a dynamic import so the root layout doesn't
|
|
// pull in the follow module on anonymous renders.
|
|
let pendingFollowRequests = 0;
|
|
if (user) {
|
|
const { countPendingFollowRequests } = await import("./lib/follow.server.ts");
|
|
pendingFollowRequests = await countPendingFollowRequests(user.id);
|
|
}
|
|
|
|
return {
|
|
user: user ? { id: user.id, username: user.username } : null,
|
|
locale,
|
|
pendingFollowRequests,
|
|
};
|
|
}
|
|
|
|
function NavBar({
|
|
user,
|
|
pendingFollowRequests,
|
|
}: {
|
|
user: { id: string; username: string } | null;
|
|
pendingFollowRequests: number;
|
|
}) {
|
|
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="/follows/requests"
|
|
className={`relative ${linkClass("/follows/requests")}`}
|
|
title={t("social.requests.title")}
|
|
>
|
|
{t("social.requests.title")}
|
|
{pendingFollowRequests > 0 && (
|
|
<span className="ml-1 inline-flex h-5 min-w-[1.25rem] items-center justify-center rounded-full bg-red-500 px-1.5 text-xs font-semibold text-white">
|
|
{pendingFollowRequests}
|
|
</span>
|
|
)}
|
|
</Link>
|
|
<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";
|
|
const pendingFollowRequests = loaderData?.pendingFollowRequests ?? 0;
|
|
useEffect(() => {
|
|
if (user) {
|
|
initSentryClient();
|
|
Sentry.setUser({ id: user.id });
|
|
} else {
|
|
Sentry.setUser(null);
|
|
stopSentryClient();
|
|
}
|
|
}, [user]);
|
|
|
|
return (
|
|
<LocaleProvider locale={locale}>
|
|
<AlphaBanner />
|
|
<NavBar user={user ?? null} pendingFollowRequests={pendingFollowRequests} />
|
|
<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>
|
|
);
|
|
}
|