diff --git a/index.ts b/index.ts index 6a9c79a..d97fd77 100644 --- a/index.ts +++ b/index.ts @@ -155,55 +155,47 @@ function ensureExistence(thing?: T | null): T { return thing; } -app.get('/talks/:slug', (req: PotentiallyAuthenticatedRequest, res, next) => { +app.get('/talks/:id', (req: PotentiallyAuthenticatedRequest, res, next) => { const { uploadCount, commentCount, nothingReceived } = req.query; const { isAuthorized } = req; - return ( - Talk.findBySlug(req.params.slug) - .then(ensureExistence) - .then(async (talk) => { - if (req.isAuthorized) { - const comments = await talk.getComments(); - // FIXME: This destroys the getter - return { talk, comments }; - } - return { talk }; - }) - .then(({ talk, comments }) => { - res.render('talk', { - talk, - comments, - uploadCount, - commentCount, - nothingReceived, - isAuthorized, - }); - }) - // If that failed, try looking the talk up by ID instead - .catch(() => - Talk.findById(req.params.slug) - .then(ensureExistence) - .then((talk) => res.redirect(`/talks/${talk.slug}/`)) - ) - .catch(next) - ); + return Talk.findById(req.params.id) + .then(ensureExistence) + .then(async (talk) => { + if (req.isAuthorized) { + const comments = await talk.getComments(); + // FIXME: This destroys the getter + return { talk, comments }; + } + return { talk }; + }) + .then(({ talk, comments }) => { + res.render('talk', { + talk, + comments, + uploadCount, + commentCount, + nothingReceived, + isAuthorized, + }); + }) + .catch(next); }); app.get('/sign-in', forceAuth, (req, res) => { res.redirect('/'); }); -app.post('/talks/:slug/files/', upload.any() as any, (req, res, next) => { +app.post('/talks/:id/files/', upload.any() as any, (req, res, next) => { let requestTalk; const { body } = req; const files = req.files as Express.Multer.File[]; if (!files.length && !body.comment) { log.info('Form submitted, but no files and no comment received'); - res.redirect(`/talks/${req.params.slug}/?nothingReceived=true`); + res.redirect(`/talks/${req.params.id}/?nothingReceived=true`); return; } log.info({ files, body }, 'Files received'); - return Talk.findBySlug(req.params.slug) + return Talk.findById(req.params.id) .then(ensureExistence) .then((talk) => { requestTalk = talk; @@ -214,7 +206,7 @@ app.post('/talks/:slug/files/', upload.any() as any, (req, res, next) => { }) .then((talk) => { res.redirect( - `/talks/${talk.slug}/?uploadCount=${files.length}&commentCount=${body.comment ? '1' : '0'}` + `/talks/${talk.id}/?uploadCount=${files.length}&commentCount=${body.comment ? '1' : '0'}` ); }) .catch((err) => { @@ -223,8 +215,8 @@ app.post('/talks/:slug/files/', upload.any() as any, (req, res, next) => { }); }); -app.get('/talks/:slug/files.zip', forceAuth, (req, res, next) => { - return Talk.findBySlug(req.params.slug) +app.get('/talks/:id/files.zip', forceAuth, (req, res, next) => { + return Talk.findById(req.params.id) .then(ensureExistence) .then((talk) => { const archive = archiver('zip'); @@ -240,8 +232,8 @@ app.get('/talks/:slug/files.zip', forceAuth, (req, res, next) => { .catch(next); }); -app.get('/talks/:slug/files/:filename', forceAuth, (req, res, next) => { - return Talk.findBySlug(req.params.slug) +app.get('/talks/:id/files/:filename', forceAuth, (req, res, next) => { + return Talk.findById(req.params.id) .then(ensureExistence) .then((talk) => { const file = _.find(talk.files, { name: req.params.filename }) as TalkFile; @@ -254,16 +246,10 @@ app.get('/talks/:slug/files/:filename', forceAuth, (req, res, next) => { .catch(next); }); -app.get('/talks/:slug/files/', (req, res) => { - res.redirect(`/${req.params.slug}/`); +app.get('/talks/:id/files/', (req, res) => { + res.redirect(`/talks/${req.params.id}/`); }); -// app.get('/:slug/files/:file', (req, res, next) => { -// return Talk.findBySlug(req.params.slug) -// .then(talk => { talk.readFile(req.params.file).pipe(res) }) -// .catch(next) -// }) - app.use((req, res, next) => { log.info(`%s %s Request didn't match a route`, req.method, req.url); res.status(404).render('error', { status: 404 }); diff --git a/views/index.pug b/views/index.pug index 19a87b8..0ab3619 100644 --- a/views/index.pug +++ b/views/index.pug @@ -19,7 +19,7 @@ block content input#filter.form-control(type='text', placeholder=__('Start typing to find talks by name or speaker'), autocomplete='off', autocorrect='off', autocapitalize='off', spellcheck='false') .list-group each talk in talks - a(href=`/talks/${talk.slug}/`, data-filter-string=`${talk.title} ${talk.speakers.join(' ')}`).list-group-item + a(href=`/talks/${talk.id}/`, data-filter-string=`${talk.title} ${talk.speakers.join(' ')}`).list-group-item h5.list-group-item-heading = talk.title p.list-group-item-text