summary refs log tree commit diff
path: root/src/api/routes/users
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-01-20 18:10:47 +1100
committerGitHub <noreply@github.com>2023-01-20 18:10:47 +1100
commit084dc0be08555891cad4c2bb984822a62ec5ec9f (patch)
treeed2ca0fafefa2224ae32761f955f63935422a97d /src/api/routes/users
parentfix: route file regex (#956) (diff)
downloadserver-084dc0be08555891cad4c2bb984822a62ec5ec9f.tar.xz
Add ESLint (#941)
* Add eslint, switch to lint-staged for precommit

* Fix all ESLint errors

* Update GH workflow to check prettier and eslint
Diffstat (limited to 'src/api/routes/users')
-rw-r--r--src/api/routes/users/#id/delete.ts3
-rw-r--r--src/api/routes/users/#id/profile.ts9
-rw-r--r--src/api/routes/users/#id/relationships.ts4
-rw-r--r--src/api/routes/users/@me/delete.ts2
-rw-r--r--src/api/routes/users/@me/guilds/#guild_id/settings.ts2
-rw-r--r--src/api/routes/users/@me/index.ts7
-rw-r--r--src/api/routes/users/@me/mfa/codes-verification.ts10
-rw-r--r--src/api/routes/users/@me/mfa/codes.ts2
-rw-r--r--src/api/routes/users/@me/mfa/totp/disable.ts2
-rw-r--r--src/api/routes/users/@me/mfa/totp/enable.ts2
-rw-r--r--src/api/routes/users/@me/relationships.ts6
-rw-r--r--src/api/routes/users/@me/settings.ts2
12 files changed, 27 insertions, 24 deletions
diff --git a/src/api/routes/users/#id/delete.ts b/src/api/routes/users/#id/delete.ts
index e7caeb05..9bc3f9f8 100644
--- a/src/api/routes/users/#id/delete.ts
+++ b/src/api/routes/users/#id/delete.ts
@@ -23,7 +23,6 @@ import {
 	PrivateUserProjection,
 	User,
 	UserDeleteEvent,
-	UserDeleteSchema,
 } from "@fosscord/util";
 import { Request, Response, Router } from "express";
 
@@ -33,7 +32,7 @@ router.post(
 	"/",
 	route({ right: "MANAGE_USERS" }),
 	async (req: Request, res: Response) => {
-		let user = await User.findOneOrFail({
+		await User.findOneOrFail({
 			where: { id: req.params.id },
 			select: [...PrivateUserProjection, "data"],
 		});
diff --git a/src/api/routes/users/#id/profile.ts b/src/api/routes/users/#id/profile.ts
index 1103bb48..dbf95a52 100644
--- a/src/api/routes/users/#id/profile.ts
+++ b/src/api/routes/users/#id/profile.ts
@@ -19,11 +19,9 @@
 import { Router, Request, Response } from "express";
 import {
 	PublicConnectedAccount,
-	PublicUser,
 	User,
 	UserPublic,
 	Member,
-	Guild,
 	UserProfileModifySchema,
 	handleFile,
 	PrivateUserProjection,
@@ -53,8 +51,8 @@ router.get(
 			relations: ["connected_accounts"],
 		});
 
-		var mutual_guilds: object[] = [];
-		var premium_guild_since;
+		const mutual_guilds: object[] = [];
+		let premium_guild_since;
 
 		if (with_mutual_guilds == "true") {
 			const requested_member = await Member.find({
@@ -169,7 +167,7 @@ router.patch(
 				`/banners/${req.user_id}`,
 				body.banner as string,
 			);
-		let user = await User.findOneOrFail({
+		const user = await User.findOneOrFail({
 			where: { id: req.user_id },
 			select: [...PrivateUserProjection, "data"],
 		});
@@ -177,6 +175,7 @@ router.patch(
 		user.assign(body);
 		await user.save();
 
+		// eslint-disable-next-line @typescript-eslint/ban-ts-comment
 		// @ts-ignore
 		delete user.data;
 
diff --git a/src/api/routes/users/#id/relationships.ts b/src/api/routes/users/#id/relationships.ts
index aa36967a..e915e3ff 100644
--- a/src/api/routes/users/#id/relationships.ts
+++ b/src/api/routes/users/#id/relationships.ts
@@ -36,7 +36,7 @@ router.get(
 	"/",
 	route({ test: { response: { body: "UserRelationsResponse" } } }),
 	async (req: Request, res: Response) => {
-		var mutual_relations: object[] = [];
+		const mutual_relations: object[] = [];
 		const requested_relations = await User.findOneOrFail({
 			where: { id: req.params.id },
 			relations: ["relationships"],
@@ -53,7 +53,7 @@ router.get(
 					rmem.type === 1 &&
 					rmem.to_id !== req.user_id
 				) {
-					var relation_user = await User.getPublicUser(rmem.to_id);
+					const relation_user = await User.getPublicUser(rmem.to_id);
 
 					mutual_relations.push({
 						id: relation_user.id,
diff --git a/src/api/routes/users/@me/delete.ts b/src/api/routes/users/@me/delete.ts
index a6ae2d13..8043eae3 100644
--- a/src/api/routes/users/@me/delete.ts
+++ b/src/api/routes/users/@me/delete.ts
@@ -17,7 +17,7 @@
 */
 
 import { Router, Request, Response } from "express";
-import { Guild, Member, User } from "@fosscord/util";
+import { Member, User } from "@fosscord/util";
 import { route } from "@fosscord/api";
 import bcrypt from "bcrypt";
 import { HTTPError } from "lambert-server";
diff --git a/src/api/routes/users/@me/guilds/#guild_id/settings.ts b/src/api/routes/users/@me/guilds/#guild_id/settings.ts
index 0525bea2..72c95d6b 100644
--- a/src/api/routes/users/@me/guilds/#guild_id/settings.ts
+++ b/src/api/routes/users/@me/guilds/#guild_id/settings.ts
@@ -43,7 +43,7 @@ router.patch(
 		const body = req.body as UserGuildSettingsSchema;
 
 		if (body.channel_overrides) {
-			for (var channel in body.channel_overrides) {
+			for (const channel in body.channel_overrides) {
 				Channel.findOneOrFail({ where: { id: channel } });
 			}
 		}
diff --git a/src/api/routes/users/@me/index.ts b/src/api/routes/users/@me/index.ts
index 596e2575..0d3c3135 100644
--- a/src/api/routes/users/@me/index.ts
+++ b/src/api/routes/users/@me/index.ts
@@ -55,7 +55,7 @@ router.patch(
 		});
 
 		// Populated on password change
-		var newToken: string | undefined;
+		let newToken: string | undefined;
 
 		if (body.avatar)
 			body.avatar = await handleFile(
@@ -120,7 +120,7 @@ router.patch(
 		}
 
 		if (body.username) {
-			var check_username = body?.username?.replace(/\s/g, "");
+			const check_username = body?.username?.replace(/\s/g, "");
 			if (!check_username) {
 				throw FieldErrors({
 					username: {
@@ -153,7 +153,8 @@ router.patch(
 		user.validate();
 		await user.save();
 
-		// @ts-ignore
+		// eslint-disable-next-line @typescript-eslint/ban-ts-comment
+		//@ts-ignore
 		delete user.data;
 
 		// TODO: send update member list event in gateway
diff --git a/src/api/routes/users/@me/mfa/codes-verification.ts b/src/api/routes/users/@me/mfa/codes-verification.ts
index ac16f7e7..24f018c9 100644
--- a/src/api/routes/users/@me/mfa/codes-verification.ts
+++ b/src/api/routes/users/@me/mfa/codes-verification.ts
@@ -23,6 +23,7 @@ import {
 	generateMfaBackupCodes,
 	User,
 	CodesVerificationSchema,
+	DiscordApiErrors,
 } from "@fosscord/util";
 
 const router = Router();
@@ -31,14 +32,17 @@ router.post(
 	"/",
 	route({ body: "CodesVerificationSchema" }),
 	async (req: Request, res: Response) => {
-		const { key, nonce, regenerate } = req.body as CodesVerificationSchema;
+		// const { key, nonce, regenerate } = req.body as CodesVerificationSchema;
+		const { regenerate } = req.body as CodesVerificationSchema;
 
 		// TODO: We don't have email/etc etc, so can't send a verification code.
 		// Once that's done, this route can verify `key`
 
-		const user = await User.findOneOrFail({ where: { id: req.user_id } });
+		// const user = await User.findOneOrFail({ where: { id: req.user_id } });
+		if ((await User.count({ where: { id: req.user_id } })) === 0)
+			throw DiscordApiErrors.UNKNOWN_USER;
 
-		var codes: BackupCode[];
+		let codes: BackupCode[];
 		if (regenerate) {
 			await BackupCode.update(
 				{ user: { id: req.user_id } },
diff --git a/src/api/routes/users/@me/mfa/codes.ts b/src/api/routes/users/@me/mfa/codes.ts
index 09b9b329..e2600400 100644
--- a/src/api/routes/users/@me/mfa/codes.ts
+++ b/src/api/routes/users/@me/mfa/codes.ts
@@ -51,7 +51,7 @@ router.post(
 			});
 		}
 
-		var codes: BackupCode[];
+		let codes: BackupCode[];
 		if (regenerate) {
 			await BackupCode.update(
 				{ user: { id: req.user_id } },
diff --git a/src/api/routes/users/@me/mfa/totp/disable.ts b/src/api/routes/users/@me/mfa/totp/disable.ts
index c399ba33..e35691ae 100644
--- a/src/api/routes/users/@me/mfa/totp/disable.ts
+++ b/src/api/routes/users/@me/mfa/totp/disable.ts
@@ -42,7 +42,7 @@ router.post(
 
 		const backup = await BackupCode.findOne({ where: { code: body.code } });
 		if (!backup) {
-			const ret = verifyToken(user.totp_secret!, body.code);
+			const ret = verifyToken(user.totp_secret || "", body.code);
 			if (!ret || ret.delta != 0)
 				throw new HTTPError(
 					req.t("auth:login.INVALID_TOTP_CODE"),
diff --git a/src/api/routes/users/@me/mfa/totp/enable.ts b/src/api/routes/users/@me/mfa/totp/enable.ts
index a59983ac..f6519ad0 100644
--- a/src/api/routes/users/@me/mfa/totp/enable.ts
+++ b/src/api/routes/users/@me/mfa/totp/enable.ts
@@ -57,7 +57,7 @@ router.post(
 		if (verifyToken(body.secret, body.code)?.delta != 0)
 			throw new HTTPError(req.t("auth:login.INVALID_TOTP_CODE"), 60008);
 
-		let backup_codes = generateMfaBackupCodes(req.user_id);
+		const backup_codes = generateMfaBackupCodes(req.user_id);
 		await Promise.all(backup_codes.map((x) => x.save()));
 		await User.update(
 			{ id: req.user_id },
diff --git a/src/api/routes/users/@me/relationships.ts b/src/api/routes/users/@me/relationships.ts
index de684a34..4dfb4c33 100644
--- a/src/api/routes/users/@me/relationships.ts
+++ b/src/api/routes/users/@me/relationships.ts
@@ -175,7 +175,7 @@ async function updateRelationship(
 		select: userProjection,
 	});
 
-	var relationship = user.relationships.find((x) => x.to_id === id);
+	let relationship = user.relationships.find((x) => x.to_id === id);
 	const friendRequest = friend.relationships.find(
 		(x) => x.to_id === req.user_id,
 	);
@@ -219,13 +219,13 @@ async function updateRelationship(
 	if (user.relationships.length >= maxFriends)
 		throw DiscordApiErrors.MAXIMUM_FRIENDS.withParams(maxFriends);
 
-	var incoming_relationship = Relationship.create({
+	let incoming_relationship = Relationship.create({
 		nickname: undefined,
 		type: RelationshipType.incoming,
 		to: user,
 		from: friend,
 	});
-	var outgoing_relationship = Relationship.create({
+	let outgoing_relationship = Relationship.create({
 		nickname: undefined,
 		type: RelationshipType.outgoing,
 		to: friend,
diff --git a/src/api/routes/users/@me/settings.ts b/src/api/routes/users/@me/settings.ts
index ad922084..c883bb30 100644
--- a/src/api/routes/users/@me/settings.ts
+++ b/src/api/routes/users/@me/settings.ts
@@ -17,7 +17,7 @@
 */
 
 import { Router, Response, Request } from "express";
-import { OrmUtils, User, UserSettingsSchema } from "@fosscord/util";
+import { User, UserSettingsSchema } from "@fosscord/util";
 import { route } from "@fosscord/api";
 
 const router = Router();