From bc7a7a8c2465a7fbe21c60e66cbf3fb6506a4a5d Mon Sep 17 00:00:00 2001 From: diced Date: Thu, 12 Nov 2020 09:42:09 -0800 Subject: [PATCH] enforcing single line if statements --- .eslintrc.js | 3 +- package.json | 1 + src/index.ts | 16 +++--- src/lib/Config.ts | 3 +- src/lib/Util.ts | 3 +- src/lib/controllers/ImagesController.ts | 13 ++--- src/lib/controllers/MultiFactorController.ts | 10 ++-- src/lib/controllers/RootController.ts | 18 +++--- src/lib/controllers/URLSController.ts | 29 +++++----- src/lib/controllers/UserController.ts | 58 +++++++++----------- src/pages/dash/users.tsx | 2 +- src/pages/user/manage.tsx | 3 +- 12 files changed, 69 insertions(+), 90 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 8a8fcc21..9ff752e4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -29,6 +29,7 @@ module.exports = { 'linebreak-style': ['error', 'unix'], quotes: ['error', 'single'], semi: ['error', 'always'], - 'comma-dangle': ['error', 'never'] + 'comma-dangle': ['error', 'never'], + 'nonblock-statement-body-position': ['error', 'beside'] } }; diff --git a/package.json b/package.json index 7a63e87d..972d9cdd 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "scripts": { "format": "prettier --write .", "lint": "eslint .", + "lint:fix": "eslint . --fix", "dev": "ts-node src", "dev:verbose": "VERBOSE=true ts-node src", "build": "next build && tsc -p .", diff --git a/src/index.ts b/src/index.ts index 7fcabc26..ed0f0b70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,10 +27,9 @@ import { MultiFactorController } from './lib/controllers/MultiFactorController'; const dev = process.env.NODE_ENV !== 'production'; (async () => { - if (await checkVersion()) - Console.logger('Zipline').info( - 'running an outdated version of zipline, please update soon!' - ); + if (await checkVersion()) Console.logger('Zipline').info( + 'running an outdated version of zipline, please update soon!' + ); })(); console.log(` @@ -85,11 +84,10 @@ server.register(fastifyRateLimit, { global: false }); -if (dev) - server.get('/_next/*', async (req, reply) => { - await handle(req.raw, reply.raw); - return (reply.sent = true); - }); +if (dev) server.get('/_next/*', async (req, reply) => { + await handle(req.raw, reply.raw); + return (reply.sent = true); +}); server.all('/*', async (req, reply) => { await handle(req.raw, reply.raw); diff --git a/src/lib/Config.ts b/src/lib/Config.ts index e964119e..62d2a6d5 100644 --- a/src/lib/Config.ts +++ b/src/lib/Config.ts @@ -79,8 +79,7 @@ export class Configuration { try { const data = readFileSync(resolve(process.cwd(), 'Zipline.toml'), 'utf8'); const parsed = parse(data); - if (parsed.webhooks) - parsed.webhooks.events = Webhooks.convert(parsed.webhooks.events); + if (parsed.webhooks) parsed.webhooks.events = Webhooks.convert(parsed.webhooks.events); return parsed; } catch (e) { return null; diff --git a/src/lib/Util.ts b/src/lib/Util.ts index 05e24d11..19678b33 100644 --- a/src/lib/Util.ts +++ b/src/lib/Util.ts @@ -15,8 +15,7 @@ export function createRandomId( charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ) { let result = ''; - for (let i = 0; i < length; i++) - result += charset.charAt(Math.floor(Math.random() * charset.length)); + for (let i = 0; i < length; i++) result += charset.charAt(Math.floor(Math.random() * charset.length)); return result; } diff --git a/src/lib/controllers/ImagesController.ts b/src/lib/controllers/ImagesController.ts index f3d0766f..72dc1641 100644 --- a/src/lib/controllers/ImagesController.ts +++ b/src/lib/controllers/ImagesController.ts @@ -70,13 +70,12 @@ export class ImagesController { unlinkSync(path); Console.logger(Image).info(`image ${image.id} was deleted`); - if (this.webhooks.events.includes(WebhookType.DELETE_IMAGE)) - Webhooks.sendWebhook(this.webhooks.upload.content, { - image, - host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${ - config.uploader.route - }/` - }); + if (this.webhooks.events.includes(WebhookType.DELETE_IMAGE)) Webhooks.sendWebhook(this.webhooks.upload.content, { + image, + host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${ + config.uploader.route + }/` + }); return reply.send(image); } catch (e) { diff --git a/src/lib/controllers/MultiFactorController.ts b/src/lib/controllers/MultiFactorController.ts index b543ef42..46f42389 100644 --- a/src/lib/controllers/MultiFactorController.ts +++ b/src/lib/controllers/MultiFactorController.ts @@ -99,8 +99,7 @@ export class MultiFactorController { } }); - if (!user) - return sendError(reply, `User "${req.body.username}" was not found.`); + if (!user) return sendError(reply, `User "${req.body.username}" was not found.`); if (!checkPassword(req.body.password, user.password)) { this.logger.error( `${user.username} (${user.id}) tried to login but failed with mfa` @@ -122,10 +121,9 @@ export class MultiFactorController { }); this.logger.info(`${user.username} (${user.id}) logged in with mfa`); - if (this.webhooks.events.includes(WebhookType.LOGIN)) - Webhooks.sendWebhook(this.webhooks.login.content, { - user - }); + if (this.webhooks.events.includes(WebhookType.LOGIN)) Webhooks.sendWebhook(this.webhooks.login.content, { + user + }); return reply.send({ user, passed }); } diff --git a/src/lib/controllers/RootController.ts b/src/lib/controllers/RootController.ts index 84d90eb2..249065f2 100644 --- a/src/lib/controllers/RootController.ts +++ b/src/lib/controllers/RootController.ts @@ -109,8 +109,7 @@ export class RootController { @POST('/upload', rateLimiterConfig) async loginStatus(req: FastifyRequest, reply: FastifyReply) { - if (!req.headers.authorization) - return sendError(reply, 'No authorization header!'); + if (!req.headers.authorization) return sendError(reply, 'No authorization header!'); const user = await this.users.findOne({ where: { @@ -123,12 +122,10 @@ export class RootController { //@ts-ignore stupid multipart types smh const data: Multipart = await req.file(); - if (!existsSync(config.uploader.directory)) - mkdirSync(config.uploader.directory); + if (!existsSync(config.uploader.directory)) mkdirSync(config.uploader.directory); const ext = data.filename.split('.')[1]; - if (config.uploader.blacklisted.includes(ext)) - return sendError(reply, 'Blacklisted file extension!'); + if (config.uploader.blacklisted.includes(ext)) return sendError(reply, 'Blacklisted file extension!'); const fileName = config.uploader.original ? data.filename.split('.')[0] @@ -153,11 +150,10 @@ export class RootController { : config.uploader.route }/${fileName}.${ext}`; - if (this.webhooks.events.includes(WebhookType.UPLOAD)) - Webhooks.sendWebhook(this.webhooks.upload.content, { - image, - host - }); + if (this.webhooks.events.includes(WebhookType.UPLOAD)) Webhooks.sendWebhook(this.webhooks.upload.content, { + image, + host + }); reply.send(host); } diff --git a/src/lib/controllers/URLSController.ts b/src/lib/controllers/URLSController.ts index b14ca9a5..ca8c8ffa 100644 --- a/src/lib/controllers/URLSController.ts +++ b/src/lib/controllers/URLSController.ts @@ -62,13 +62,12 @@ export class URLSController { }); this.logger.info(`url ${url.id} was deleted`); - if (this.webhooks.events.includes(WebhookType.DELETE_URL)) - Webhooks.sendWebhook(this.webhooks.delete_url.content, { - url, - host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${ - config.urls.route - }/` - }); + if (this.webhooks.events.includes(WebhookType.DELETE_URL)) Webhooks.sendWebhook(this.webhooks.delete_url.content, { + url, + host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${ + config.urls.route + }/` + }); return reply.send(url); } @@ -86,8 +85,7 @@ export class URLSController { vanity: req.body.vanity } }); - if (existingVanity) - return sendError(reply, 'There is an existing vanity!'); + if (existingVanity) return sendError(reply, 'There is an existing vanity!'); } const user = await this.users.findOne({ @@ -106,13 +104,12 @@ export class URLSController { ); this.logger.info(`saved url ${url.id}`); - if (this.webhooks.events.includes(WebhookType.SHORTEN)) - Webhooks.sendWebhook(this.webhooks.shorten.content, { - url, - host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${ - config.urls.route - }/` - }); + if (this.webhooks.events.includes(WebhookType.SHORTEN)) Webhooks.sendWebhook(this.webhooks.shorten.content, { + url, + host: `${config.core.secure ? 'https' : 'http'}://${req.hostname}${ + config.urls.route + }/` + }); return reply.send(url); } diff --git a/src/lib/controllers/UserController.ts b/src/lib/controllers/UserController.ts index fe284af8..16e488cf 100644 --- a/src/lib/controllers/UserController.ts +++ b/src/lib/controllers/UserController.ts @@ -80,10 +80,9 @@ export class UserController { await this.users.save(user); this.logger.info(`saved ${user.username} (${user.id})`); - if (this.webhooks.events.includes(WebhookType.USER_EDIT)) - Webhooks.sendWebhook(this.webhooks.user_edit.content, { - user - }); + if (this.webhooks.events.includes(WebhookType.USER_EDIT)) Webhooks.sendWebhook(this.webhooks.user_edit.content, { + user + }); delete user.password; return reply.send(user); @@ -104,8 +103,7 @@ export class UserController { } }); - if (!user) - return sendError(reply, `User "${req.body.username}" was not found.`); + if (!user) return sendError(reply, `User "${req.body.username}" was not found.`); if (!checkPassword(req.body.password, user.password)) { this.logger.error( `${user.username} (${user.id}) tried to verify their credentials but failed` @@ -133,8 +131,7 @@ export class UserController { } }); - if (!user) - return sendError(reply, `User "${req.body.username}" was not found.`); + if (!user) return sendError(reply, `User "${req.body.username}" was not found.`); if (!checkPassword(req.body.password, user.password)) { this.logger.error( `${user.username} (${user.id}) tried to login but failed` @@ -150,10 +147,9 @@ export class UserController { }); this.logger.info(`${user.username} (${user.id}) logged in`); - if (this.webhooks.events.includes(WebhookType.LOGIN)) - Webhooks.sendWebhook(this.webhooks.login.content, { - user - }); + if (this.webhooks.events.includes(WebhookType.LOGIN)) Webhooks.sendWebhook(this.webhooks.login.content, { + user + }); return reply.send(user); } @@ -188,10 +184,9 @@ export class UserController { await this.users.save(user); this.logger.info(`reset token ${user.username} (${user.id})`); - if (this.webhooks.events.includes(WebhookType.TOKEN_RESET)) - Webhooks.sendWebhook(this.webhooks.token_reset.content, { - user - }); + if (this.webhooks.events.includes(WebhookType.TOKEN_RESET)) Webhooks.sendWebhook(this.webhooks.token_reset.content, { + user + }); return reply.send({ updated: true }); } @@ -222,21 +217,19 @@ export class UserController { ) ); this.logger.info(`created user ${user.username} (${user.id})`); - if (this.webhooks.events.includes(WebhookType.CREATE_USER)) - Webhooks.sendWebhook(this.webhooks.create_user.content, { - user - }); + if (this.webhooks.events.includes(WebhookType.CREATE_USER)) Webhooks.sendWebhook(this.webhooks.create_user.content, { + user + }); const firstSetup = await getFirst(this.instance.orm); - if (firstSetup) - await this.instance.orm.getRepository(Zipline).update( - { - id: 'zipline' - }, - { - first: false - } - ); + if (firstSetup) await this.instance.orm.getRepository(Zipline).update( + { + id: 'zipline' + }, + { + first: false + } + ); delete user.password; return reply.send(user); @@ -266,10 +259,9 @@ export class UserController { await this.users.remove(existing); this.logger.info(`deleted ${existing.username} (${existing.id})`); - if (this.webhooks.events.includes(WebhookType.USER_DELETE)) - Webhooks.sendWebhook(this.webhooks.user_delete.content, { - user: existing - }); + if (this.webhooks.events.includes(WebhookType.USER_DELETE)) Webhooks.sendWebhook(this.webhooks.user_delete.content, { + user: existing + }); return reply.send({ ok: true }); } catch (e) { diff --git a/src/pages/dash/users.tsx b/src/pages/dash/users.tsx index 55d6bffe..b756b100 100644 --- a/src/pages/dash/users.tsx +++ b/src/pages/dash/users.tsx @@ -219,7 +219,7 @@ export default function Users() { } title={`${u.username} (${u.id})`} subheader={`${u.administrator ? 'Administrator' : 'User' - }`} + }`} /> diff --git a/src/pages/user/manage.tsx b/src/pages/user/manage.tsx index 1c3aa538..e1916adf 100644 --- a/src/pages/user/manage.tsx +++ b/src/pages/user/manage.tsx @@ -10,8 +10,7 @@ export default function Manage({ config }) { const router = useRouter(); const state = store.getState(); - if (typeof window !== 'undefined' && !state.loggedIn) - router.push('/user/login'); + if (typeof window !== 'undefined' && !state.loggedIn) router.push('/user/login'); else { return (