Skip to content

Commit

Permalink
Add discussion redirect (#61)
Browse files Browse the repository at this point in the history
* Add discussion redirect

* Add missing license

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* add /discussion to login redirect path

* update comment

* fix e2e tests

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: David Crespo <[email protected]>
  • Loading branch information
3 people authored Aug 9, 2024
1 parent c42214b commit 615a076
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
34 changes: 34 additions & 0 deletions app/routes/rfd.$slug.discussion.tsx
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion app/routes/rfd.$slug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/everything.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 615a076

Please sign in to comment.