Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert server "object exists" functions to async/await. #2344

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions server/controllers/collection.controller/collectionForUserExists.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import Collection from '../../models/collection';
import User from '../../models/user';

export default function collectionForUserExists(
username,
collectionId,
callback
) {
function sendFailure() {
callback(false);
}

function sendSuccess(collection) {
callback(collection != null);
}

function findUser() {
return User.findByUsername(username);
}

function findCollection(owner) {
if (owner == null) {
throw new Error('User not found');
}

return Collection.findOne({ _id: collectionId, owner });
}

return findUser().then(findCollection).then(sendSuccess).catch(sendFailure);
/**
* @param {string} username
* @param {string} collectionId
* @return {Promise<boolean>}
*/
export default async function collectionForUserExists(username, collectionId) {
const user = await User.findByUsername(username);
if (!user) return false;
const collection = await Collection.findOne({
_id: collectionId,
owner: user
}).exec();
return collection != null;
}
39 changes: 19 additions & 20 deletions server/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,28 @@ export function getProjects(req, res) {
}
}

export function projectExists(projectId, callback) {
Project.findById(projectId, (err, project) =>
project ? callback(true) : callback(false)
);
/**
* @param {string} projectId
* @return {Promise<boolean>}
*/
export async function projectExists(projectId) {
const project = await Project.findById(projectId);
return project != null;
}

export function projectForUserExists(username, projectId, callback) {
User.findByUsername(username, (err, user) => {
if (!user) {
callback(false);
return;
}
Project.findOne(
{ user: user._id, $or: [{ _id: projectId }, { slug: projectId }] },
(innerErr, project) => {
if (!project) {
callback(false);
return;
}
callback(true);
}
);
/**
* @param {string} username
* @param {string} projectId
* @return {Promise<boolean>}
*/
export async function projectForUserExists(username, projectId) {
const user = await User.findByUsername(username);
if (!user) return false;
const project = await Project.findOne({
user: user._id,
$or: [{ _id: projectId }, { slug: projectId }]
});
return project != null;
}

function bundleExternalLibs(project) {
Expand Down
11 changes: 7 additions & 4 deletions server/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,13 @@ export function updatePassword(req, res) {
// eventually send email that the password has been reset
}

export function userExists(username, callback) {
User.findByUsername(username, (err, user) =>
user ? callback(true) : callback(false)
);
/**
* @param {string} username
* @return {Promise<boolean>}
*/
export async function userExists(username) {
const user = await User.findByUsername(username);
return user != null;
}

export function saveUser(res, user) {
Expand Down
84 changes: 43 additions & 41 deletions server/routes/server.routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Router } from 'express';
import { renderIndex } from '../views/index';
import { get404Sketch } from '../views/404Page';
import sendHtml, { renderIndex } from '../views/index';
import { userExists } from '../controllers/user.controller';
import {
projectExists,
Expand All @@ -27,40 +26,46 @@ router.get('/signup', (req, res) => {
return res.send(renderIndex());
});

router.get('/projects/:project_id', (req, res) => {
projectExists(req.params.project_id, (exists) =>
exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html))
);
router.get('/projects/:project_id', async (req, res) => {
const exists = await projectExists(req.params.project_id);
sendHtml(req, res, exists);
});

router.get('/:username/sketches/:project_id/add-to-collection', (req, res) => {
projectForUserExists(req.params.username, req.params.project_id, (exists) =>
exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html))
);
});
router.get(
'/:username/sketches/:project_id/add-to-collection',
async (req, res) => {
const exists = await projectForUserExists(
req.params.username,
req.params.project_id
);
sendHtml(req, res, exists);
}
);

router.get('/:username/sketches/:project_id', (req, res) => {
projectForUserExists(req.params.username, req.params.project_id, (exists) =>
exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html))
router.get('/:username/sketches/:project_id', async (req, res) => {
const exists = await projectForUserExists(
req.params.username,
req.params.project_id
);
sendHtml(req, res, exists);
});

router.get('/:username/sketches', (req, res) => {
userExists(req.params.username, (exists) =>
exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html))
);
router.get('/:username/sketches', async (req, res) => {
const exists = await userExists(req.params.username);
sendHtml(req, res, exists);
});

router.get('/:username/full/:project_id', (req, res) => {
projectForUserExists(req.params.username, req.params.project_id, (exists) =>
exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html))
router.get('/:username/full/:project_id', async (req, res) => {
const exists = await projectForUserExists(
req.params.username,
req.params.project_id
);
sendHtml(req, res, exists);
});

router.get('/full/:project_id', (req, res) => {
projectExists(req.params.project_id, (exists) =>
exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html))
);
router.get('/full/:project_id', async (req, res) => {
const exists = await projectExists(req.params.project_id);
sendHtml(req, res, exists);
});

router.get('/login', (req, res) => {
Expand Down Expand Up @@ -98,15 +103,11 @@ router.get('/assets', (req, res) => {
}
});

router.get('/:username/assets', (req, res) => {
userExists(req.params.username, (exists) => {
const isLoggedInUser =
req.user && req.user.username === req.params.username;
const canAccess = exists && isLoggedInUser;
return canAccess
? res.send(renderIndex())
: get404Sketch((html) => res.send(html));
});
router.get('/:username/assets', async (req, res) => {
const exists = await userExists(req.params.username);
const isLoggedInUser = req.user && req.user.username === req.params.username;
const canAccess = exists && isLoggedInUser;
sendHtml(req, res, canAccess);
});

router.get('/account', (req, res) => {
Expand All @@ -121,16 +122,17 @@ router.get('/about', (req, res) => {
res.send(renderIndex());
});

router.get('/:username/collections/:id', (req, res) => {
collectionForUserExists(req.params.username, req.params.id, (exists) =>
exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html))
router.get('/:username/collections/:id', async (req, res) => {
const exists = await collectionForUserExists(
req.params.username,
req.params.id
);
sendHtml(req, res, exists);
});

router.get('/:username/collections', (req, res) => {
userExists(req.params.username, (exists) =>
exists ? res.send(renderIndex()) : get404Sketch((html) => res.send(html))
);
router.get('/:username/collections', async (req, res) => {
const exists = await userExists(req.params.username);
sendHtml(req, res, exists);
});

router.get('/privacy-policy', (req, res) => {
Expand Down
17 changes: 17 additions & 0 deletions server/views/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import get404Sketch from './404Page';

export function renderIndex() {
const assetsManifest = process.env.webpackAssets && JSON.parse(process.env.webpackAssets);
return `
Expand Down Expand Up @@ -45,3 +47,18 @@ export function renderIndex() {
</html>
`;
}

/**
* Send a 404 page if `exists` is false.
* @param {import('express').e.Request} req
* @param {import('express').e.Response} res
* @param {boolean} [exists]
*/
export default function sendHtml(req, res, exists = true) {
if (!exists) {
res.status(404);
get404Sketch((html) => res.send(html));
} else {
res.send(renderIndex());
}
};
Loading