Skip to content

Commit

Permalink
Use UUID instead of slug to look up talks
Browse files Browse the repository at this point in the history
  • Loading branch information
pbock committed Dec 14, 2023
1 parent 3cacab6 commit 04cb153
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 47 deletions.
78 changes: 32 additions & 46 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,55 +155,47 @@ function ensureExistence<T>(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;
Expand All @@ -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) => {
Expand All @@ -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');
Expand All @@ -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;
Expand All @@ -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 });
Expand Down
2 changes: 1 addition & 1 deletion views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 04cb153

Please sign in to comment.