diff --git a/app/routes/rfd.$slug.discussion.tsx b/app/routes/rfd.$slug.discussion.tsx new file mode 100644 index 0000000..757a41e --- /dev/null +++ b/app/routes/rfd.$slug.discussion.tsx @@ -0,0 +1,34 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at https://mozilla.org/MPL/2.0/. + * + * Copyright Oxide Computer Company + */ + +import { redirect, type LoaderArgs } from '@remix-run/node' + +import { isAuthenticated } from '~/services/authn.server' +import { fetchRfd } from '~/services/rfd.server' +import { parseRfdNum } from '~/utils/parseRfdNum' + +import { resp404 } from './rfd.$slug' + +export async function loader({ request, params: { slug } }: LoaderArgs) { + const num = parseRfdNum(slug) + if (!num) throw resp404() + + const user = await isAuthenticated(request) + const rfd = await fetchRfd(num, user) + + // !rfd covers both non-existent and private RFDs for the logged-out user. In + // both cases, once they log in, if they have permission to read it, they'll + // get the redirect, otherwise they will get 404. + if (!rfd && !user) throw redirect(`/login?returnTo=/rfd/${num}/discussion`) + + // If you don't see an RFD but you are logged in, you can't tell whether you + // don't have access or it doesn't exist. That's fine. + if (!rfd || !rfd.discussion_link) throw resp404() + + return redirect(rfd.discussion_link) +} diff --git a/app/routes/rfd.$slug.tsx b/app/routes/rfd.$slug.tsx index b59ab82..5d265e9 100644 --- a/app/routes/rfd.$slug.tsx +++ b/app/routes/rfd.$slug.tsx @@ -67,7 +67,7 @@ ad.ConverterFactory.register(new InlineConverter(), ['html5']) export const links = () => [{ rel: 'stylesheet', href: styles }] -const resp404 = () => new Response('Not Found', { status: 404 }) +export const resp404 = () => new Response('Not Found', { status: 404 }) /** * Fetch RFD, accounting for the possibility of the RFD being public. diff --git a/test/e2e/everything.spec.ts b/test/e2e/everything.spec.ts index 4dbe3d5..e97039b 100644 --- a/test/e2e/everything.spec.ts +++ b/test/e2e/everything.spec.ts @@ -49,7 +49,7 @@ test('Filter by author', async ({ page }) => { // don't know how many public RFDs there are but there are a bunch expect(await rfdLinks.count()).toBeGreaterThan(10) - await page.getByPlaceholder('Filter by').fill('crespo') + await page.getByPlaceholder('Filter by').fill('davidcrespo') // but after you filter there are fewer expect(await rfdLinks.count()).toEqual(2)