summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-12-23 12:44:04 +1100
committerPuyodead1 <puyodead@proton.me>2023-03-18 19:23:20 -0400
commita4961800d7b6b37864b7b7c44893c734ef1b05ae (patch)
treec633c3de4d901e3ac5e4742220d06874a5702810 /src
parentimplement PATCH connection (diff)
downloadserver-a4961800d7b6b37864b7b7c44893c734ef1b05ae.tar.xz
`handleCallback` returns connection if created for `USER_CONNECTIONS_UPDATE`
Diffstat (limited to 'src')
-rw-r--r--src/api/routes/connections/#connection_name/callback.ts15
-rw-r--r--src/connections/BattleNet/index.ts18
-rw-r--r--src/connections/GitHub/index.ts10
-rw-r--r--src/util/connections/Connection.ts7
4 files changed, 25 insertions, 25 deletions
diff --git a/src/api/routes/connections/#connection_name/callback.ts b/src/api/routes/connections/#connection_name/callback.ts
index 80a5b784..250d4710 100644
--- a/src/api/routes/connections/#connection_name/callback.ts
+++ b/src/api/routes/connections/#connection_name/callback.ts
@@ -1,11 +1,11 @@
-import { Request, Response, Router } from "express";
+import { route } from "@fosscord/api";
 import {
 	ConnectionCallbackSchema,
+	ConnectionStore,
 	emitEvent,
 	FieldErrors,
-} from "../../../../util";
-import { ConnectionStore } from "../../../../util/connections";
-import { route } from "../../../util";
+} from "@fosscord/util";
+import { Request, Response, Router } from "express";
 
 const router = Router();
 
@@ -36,15 +36,16 @@ router.post(
 
 		const body = req.body as ConnectionCallbackSchema;
 		const userId = connection.getUserId(body.state);
-		const emit = await connection.handleCallback(body);
+		const connectedAccnt = await connection.handleCallback(body);
 
 		// whether we should emit a connections update event, only used when a connection doesnt already exist
-		if (emit)
+		if (connectedAccnt)
 			emitEvent({
 				event: "USER_CONNECTIONS_UPDATE",
-				data: {},
+				data: connectedAccnt,
 				user_id: userId,
 			});
+
 		res.sendStatus(204);
 	},
 );
diff --git a/src/connections/BattleNet/index.ts b/src/connections/BattleNet/index.ts
index 1b725afd..5bbfefe9 100644
--- a/src/connections/BattleNet/index.ts
+++ b/src/connections/BattleNet/index.ts
@@ -1,5 +1,5 @@
 import fetch from "node-fetch";
-import { Config, ConnectionCallbackSchema, DiscordApiErrors } from "../../util";
+import { Config, ConnectedAccount, ConnectionCallbackSchema, DiscordApiErrors } from "../../util";
 import Connection from "../../util/connections/Connection";
 import { ConnectionLoader } from "../../util/connections/ConnectionLoader";
 import { BattleNetSettings } from "./BattleNetSettings";
@@ -46,8 +46,7 @@ export default class BattleNetConnection extends Connection {
 		// TODO: probably shouldn't rely on cdn as this could be different from what we actually want. we should have an api endpoint setting.
 		url.searchParams.append(
 			"redirect_uri",
-			`${
-				Config.get().cdn.endpointPrivate || "http://localhost:3001"
+			`${Config.get().cdn.endpointPrivate || "http://localhost:3001"
 			}/connections/${this.id}/callback`,
 		);
 		url.searchParams.append("scope", this.scopes.join(" "));
@@ -75,9 +74,8 @@ export default class BattleNetConnection extends Connection {
 				code: code,
 				client_id: this.settings.clientId!,
 				client_secret: this.settings.clientSecret!,
-				redirect_uri: `${
-					Config.get().cdn.endpointPrivate || "http://localhost:3001"
-				}/connections/${this.id}/callback`,
+				redirect_uri: `${Config.get().cdn.endpointPrivate || "http://localhost:3001"
+					}/connections/${this.id}/callback`,
 			}),
 		})
 			.then((res) => res.json())
@@ -108,21 +106,21 @@ export default class BattleNetConnection extends Connection {
 			});
 	}
 
-	async handleCallback(params: ConnectionCallbackSchema): Promise<boolean> {
+	async handleCallback(params: ConnectionCallbackSchema): Promise<ConnectedAccount | null> {
 		const userId = this.getUserId(params.state);
 		const token = await this.exchangeCode(params.state, params.code!);
 		const userInfo = await this.getUser(token);
 
 		const exists = await this.hasConnection(userId, userInfo.id.toString());
 
-		if (exists) return false;
-		await this.createConnection({
+		if (exists) return null;
+		
+		return await this.createConnection({
 			user_id: userId,
 			external_id: userInfo.id.toString(),
 			friend_sync: params.friend_sync,
 			name: userInfo.battletag,
 			type: this.id,
 		});
-		return true;
 	}
 }
diff --git a/src/connections/GitHub/index.ts b/src/connections/GitHub/index.ts
index 41806a67..fc44ff9d 100644
--- a/src/connections/GitHub/index.ts
+++ b/src/connections/GitHub/index.ts
@@ -1,5 +1,5 @@
 import fetch from "node-fetch";
-import { Config, ConnectionCallbackSchema, DiscordApiErrors } from "../../util";
+import { Config, ConnectedAccount, ConnectionCallbackSchema, DiscordApiErrors } from "../../util";
 import Connection from "../../util/connections/Connection";
 import { ConnectionLoader } from "../../util/connections/ConnectionLoader";
 import { GitHubSettings } from "./GitHubSettings";
@@ -89,21 +89,21 @@ export default class GitHubConnection extends Connection {
 		}).then((res) => res.json());
 	}
 
-	async handleCallback(params: ConnectionCallbackSchema): Promise<boolean> {
+	async handleCallback(params: ConnectionCallbackSchema): Promise<ConnectedAccount | null> {
 		const userId = this.getUserId(params.state);
 		const token = await this.exchangeCode(params.state, params.code!);
 		const userInfo = await this.getUser(token);
 
 		const exists = await this.hasConnection(userId, userInfo.id.toString());
 
-		if (exists) return false;
-		await this.createConnection({
+		if (exists) return null;
+
+		return await this.createConnection({
 			user_id: userId,
 			external_id: userInfo.id.toString(),
 			friend_sync: params.friend_sync,
 			name: userInfo.name,
 			type: this.id,
 		});
-		return true;
 	}
 }
diff --git a/src/util/connections/Connection.ts b/src/util/connections/Connection.ts
index e8d41c36..164cfac7 100644
--- a/src/util/connections/Connection.ts
+++ b/src/util/connections/Connection.ts
@@ -21,7 +21,7 @@ export default abstract class Connection {
 	 * Processes the callback
 	 * @param args Callback arguments
 	 */
-	abstract handleCallback(params: ConnectionCallbackSchema): Promise<boolean>;
+	abstract handleCallback(params: ConnectionCallbackSchema): Promise<ConnectedAccount | null>;
 
 	/**
 	 * Gets a user id from state
@@ -54,9 +54,10 @@ export default abstract class Connection {
 		this.states.delete(state);
 	}
 
-	async createConnection(data: ConnectedAccountSchema): Promise<void> {
-		const ca = OrmUtils.mergeDeep(new ConnectedAccount(), data);
+	async createConnection(data: ConnectedAccountSchema): Promise<ConnectedAccount> {
+		const ca = OrmUtils.mergeDeep(new ConnectedAccount(), data) as ConnectedAccount;
 		await ca.save();
+		return ca;
 	}
 
 	async hasConnection(userId: string, externalId: string): Promise<boolean> {