summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2022-12-24 16:24:58 -0500
committerPuyodead1 <puyodead@proton.me>2023-03-18 19:28:45 -0400
commit6d6944cfee4af656c6386c7a44efc6b99bdfd6ed (patch)
tree72ed39ba329e7cbd894ddcc0fabdcbdf636ef4a6 /src/api
parentFix connection update visibilty dying when given boolean (diff)
downloadserver-6d6944cfee4af656c6386c7a44efc6b99bdfd6ed.tar.xz
Add Twitch, error handling, revokation changes, etc
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/users/@me/connections/#connection_name/#connection_id/access-token.ts16
-rw-r--r--src/api/routes/users/@me/connections/#connection_name/#connection_id/index.ts13
2 files changed, 17 insertions, 12 deletions
diff --git a/src/api/routes/users/@me/connections/#connection_name/#connection_id/access-token.ts b/src/api/routes/users/@me/connections/#connection_name/#connection_id/access-token.ts
index 760f8135..1ad1c7a7 100644
--- a/src/api/routes/users/@me/connections/#connection_name/#connection_id/access-token.ts
+++ b/src/api/routes/users/@me/connections/#connection_name/#connection_id/access-token.ts
@@ -19,7 +19,7 @@ const ALLOWED_CONNECTIONS = ["twitch", "youtube"];
 router.get("/", route({}), async (req: Request, res: Response) => {
 	const { connection_name, connection_id } = req.params;
 
-	const connection = ConnectionStore.connections.get(connection_id);
+	const connection = ConnectionStore.connections.get(connection_name);
 
 	if (!ALLOWED_CONNECTIONS.includes(connection_name) || !connection)
 		throw FieldErrors({
@@ -41,7 +41,7 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 	const connectedAccount = await ConnectedAccount.findOne({
 		where: {
 			type: connection_name,
-			id: connection_id,
+			external_id: connection_id,
 			user_id: req.user_id,
 		},
 		select: [
@@ -64,14 +64,12 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 		throw new ApiError("No token data", 0, 400);
 
 	let access_token = connectedAccount.token_data.access_token;
-	const { expires_at, expires_in } = connectedAccount.token_data;
+	const { expires_at, expires_in, fetched_at } = connectedAccount.token_data;
 
-	if (expires_at && expires_at < Date.now()) {
-		if (!(connection instanceof RefreshableConnection))
-			throw new ApiError("Access token expired", 0, 400);
-		const tokenData = await connection.refresh(connectedAccount);
-		access_token = tokenData.access_token;
-	} else if (expires_in && expires_in < Date.now()) {
+	if (
+		(expires_at && expires_at < Date.now()) ||
+		(expires_in && fetched_at + expires_in * 1000 < Date.now())
+	) {
 		if (!(connection instanceof RefreshableConnection))
 			throw new ApiError("Access token expired", 0, 400);
 		const tokenData = await connection.refresh(connectedAccount);
diff --git a/src/api/routes/users/@me/connections/#connection_name/#connection_id/index.ts b/src/api/routes/users/@me/connections/#connection_name/#connection_id/index.ts
index 9d5f517d..07440eac 100644
--- a/src/api/routes/users/@me/connections/#connection_name/#connection_id/index.ts
+++ b/src/api/routes/users/@me/connections/#connection_name/#connection_id/index.ts
@@ -1,5 +1,10 @@
 import { route } from "@fosscord/api";
-import { ConnectedAccount, DiscordApiErrors, emitEvent, ConnectionUpdateSchema } from "@fosscord/util";
+import {
+	ConnectedAccount,
+	ConnectionUpdateSchema,
+	DiscordApiErrors,
+	emitEvent
+} from "@fosscord/util";
 import { Request, Response, Router } from "express";
 const router = Router();
 
@@ -35,6 +40,8 @@ router.patch(
 
 		//@ts-ignore For some reason the client sends this as a boolean, even tho docs say its a number?
 		if (typeof body.visibility === "boolean") body.visibility = body.visibility ? 1 : 0;
+		//@ts-ignore For some reason the client sends this as a boolean, even tho docs say its a number?
+		if (typeof body.show_activity === "boolean") body.show_activity = body.show_activity ? 1 : 0;
 
 		connection.assign(req.body);
 
@@ -58,7 +65,7 @@ router.delete("/", route({}), async (req: Request, res: Response) => {
 			user_id: req.user_id,
 			external_id: connection_id,
 			type: connection_name,
-		}
+		},
 	});
 
 	await Promise.all([
@@ -67,7 +74,7 @@ router.delete("/", route({}), async (req: Request, res: Response) => {
 			event: "USER_CONNECTIONS_UPDATE",
 			data: account,
 			user_id: req.user_id,
-		})
+		}),
 	]);
 
 	return res.sendStatus(200);