summary refs log tree commit diff
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2022-12-22 10:47:32 -0500
committerPuyodead1 <puyodead@proton.me>2023-03-18 19:15:40 -0400
commit6a52e65e27d514273a4210dadafbee9692f23354 (patch)
treec2eb70b2b7c4be1a482392ca8982459885e1865c
parentadd connections (diff)
downloadserver-6a52e65e27d514273a4210dadafbee9692f23354.tar.xz
adding connection now works
-rw-r--r--src/api/Server.ts5
-rw-r--r--src/api/routes/connections/#connection_name/authorize.ts2
-rw-r--r--src/api/routes/connections/#connection_name/callback.ts2
-rw-r--r--src/connections/BattleNet/index.ts7
-rw-r--r--src/connections/GitHub/index.ts7
-rw-r--r--src/util/connections/Connection.ts4
-rw-r--r--src/util/connections/ConnectionConfig.ts1
-rw-r--r--src/util/connections/ConnectionLoader.ts7
-rw-r--r--src/util/dtos/ConnectedAccountDTO.ts18
-rw-r--r--src/util/entities/ConnectedAccount.ts24
-rw-r--r--src/util/index.ts1
-rw-r--r--src/util/schemas/ConnectedAccountSchema.ts16
-rw-r--r--src/util/schemas/index.ts1
13 files changed, 54 insertions, 41 deletions
diff --git a/src/api/Server.ts b/src/api/Server.ts
index 49229494..dc3b66ef 100644
--- a/src/api/Server.ts
+++ b/src/api/Server.ts
@@ -25,6 +25,8 @@ import {
 	registerRoutes,
 	Sentry,
 	WebAuthn,
+	ConnectionConfig,
+	ConnectionLoader
 } from "@fosscord/util";
 import { Request, Response, Router } from "express";
 import { Server, ServerOptions } from "lambert-server";
@@ -64,6 +66,7 @@ export class FosscordServer extends Server {
 		await Config.init();
 		await initEvent();
 		await Email.init();
+		await ConnectionConfig.init();
 		await initInstance();
 		await Sentry.init(this.app);
 		WebAuthn.init();
@@ -130,6 +133,8 @@ export class FosscordServer extends Server {
 
 		Sentry.errorHandler(this.app);
 
+		ConnectionLoader.loadConnections();
+
 		if (logRequests)
 			console.log(
 				red(
diff --git a/src/api/routes/connections/#connection_name/authorize.ts b/src/api/routes/connections/#connection_name/authorize.ts
index 8e640a69..5ce420cf 100644
--- a/src/api/routes/connections/#connection_name/authorize.ts
+++ b/src/api/routes/connections/#connection_name/authorize.ts
@@ -6,7 +6,7 @@ import { route } from "../../../util";
 const router = Router();
 
 router.get("/", route({}), async (req: Request, res: Response) => {
-	const { connection_id: connection_name } = req.params;
+	const { connection_name } = req.params;
 	const connection = ConnectionStore.connections.get(connection_name);
 	if (!connection)
 		throw FieldErrors({
diff --git a/src/api/routes/connections/#connection_name/callback.ts b/src/api/routes/connections/#connection_name/callback.ts
index f158a037..80a5b784 100644
--- a/src/api/routes/connections/#connection_name/callback.ts
+++ b/src/api/routes/connections/#connection_name/callback.ts
@@ -13,7 +13,7 @@ router.post(
 	"/",
 	route({ body: "ConnectionCallbackSchema" }),
 	async (req: Request, res: Response) => {
-		const { connection_id: connection_name } = req.params;
+		const { connection_name } = req.params;
 		const connection = ConnectionStore.connections.get(connection_name);
 		if (!connection)
 			throw FieldErrors({
diff --git a/src/connections/BattleNet/index.ts b/src/connections/BattleNet/index.ts
index 0fd0aa18..1b725afd 100644
--- a/src/connections/BattleNet/index.ts
+++ b/src/connections/BattleNet/index.ts
@@ -118,15 +118,10 @@ export default class BattleNetConnection extends Connection {
 		if (exists) return false;
 		await this.createConnection({
 			user_id: userId,
-			external_id: userInfo.id,
+			external_id: userInfo.id.toString(),
 			friend_sync: params.friend_sync,
 			name: userInfo.battletag,
-			revoked: false,
-			show_activity: false,
 			type: this.id,
-			verified: true,
-			visibility: 0,
-			integrations: [],
 		});
 		return true;
 	}
diff --git a/src/connections/GitHub/index.ts b/src/connections/GitHub/index.ts
index a96ac68e..41806a67 100644
--- a/src/connections/GitHub/index.ts
+++ b/src/connections/GitHub/index.ts
@@ -99,15 +99,10 @@ export default class GitHubConnection extends Connection {
 		if (exists) return false;
 		await this.createConnection({
 			user_id: userId,
-			external_id: userInfo.id,
+			external_id: userInfo.id.toString(),
 			friend_sync: params.friend_sync,
 			name: userInfo.name,
-			revoked: false,
-			show_activity: false,
 			type: this.id,
-			verified: true,
-			visibility: 0,
-			integrations: [],
 		});
 		return true;
 	}
diff --git a/src/util/connections/Connection.ts b/src/util/connections/Connection.ts
index 02104d39..e8d41c36 100644
--- a/src/util/connections/Connection.ts
+++ b/src/util/connections/Connection.ts
@@ -1,7 +1,7 @@
 import crypto from "crypto";
 import { ConnectedAccount } from "../entities";
 import { OrmUtils } from "../imports";
-import { ConnectionCallbackSchema } from "../schemas";
+import { ConnectedAccountSchema, ConnectionCallbackSchema } from "../schemas";
 import { DiscordApiErrors } from "../util";
 
 export default abstract class Connection {
@@ -54,7 +54,7 @@ export default abstract class Connection {
 		this.states.delete(state);
 	}
 
-	async createConnection(data: any): Promise<void> {
+	async createConnection(data: ConnectedAccountSchema): Promise<void> {
 		const ca = OrmUtils.mergeDeep(new ConnectedAccount(), data);
 		await ca.save();
 	}
diff --git a/src/util/connections/ConnectionConfig.ts b/src/util/connections/ConnectionConfig.ts
index 9b120c93..dd372ff7 100644
--- a/src/util/connections/ConnectionConfig.ts
+++ b/src/util/connections/ConnectionConfig.ts
@@ -21,7 +21,6 @@ export const ConnectionConfig = {
 	set: function set(val: Partial<any>) {
 		if (!config || !val) return;
 		config = val.merge(config);
-		console.debug("config", config); // TODO: if no more issues with sql, remove this or find the reason why it's happening
 
 		return applyConfig(config);
 	},
diff --git a/src/util/connections/ConnectionLoader.ts b/src/util/connections/ConnectionLoader.ts
index d06a6446..7467739c 100644
--- a/src/util/connections/ConnectionLoader.ts
+++ b/src/util/connections/ConnectionLoader.ts
@@ -23,7 +23,6 @@ export class ConnectionLoader {
 
 		dirs.forEach(async (x) => {
 			let modPath = path.resolve(path.join(root, x));
-			console.log(`Loading connection: ${modPath}`);
 			const mod = new (require(modPath).default)() as Connection;
 			ConnectionStore.connections.set(mod.id, mod);
 
@@ -55,11 +54,13 @@ export class ConnectionLoader {
 			console.log(
 				`[ConnectionConfig/WARN] ${id} tried to set config=null!`,
 			);
-		await ConnectionConfig.set({
+
+		const a = {
 			[id]: OrmUtils.mergeDeep(
 				ConnectionLoader.getConnectionConfig(id) || {},
 				config,
 			),
-		});
+		};
+		await ConnectionConfig.set(a);
 	}
 }
diff --git a/src/util/dtos/ConnectedAccountDTO.ts b/src/util/dtos/ConnectedAccountDTO.ts
index a0287086..36a4e6b3 100644
--- a/src/util/dtos/ConnectedAccountDTO.ts
+++ b/src/util/dtos/ConnectedAccountDTO.ts
@@ -4,17 +4,17 @@ export class ConnectedAccountDTO {
 	id: string;
 	user_id: string;
 	access_token?: string;
-	friend_sync: boolean;
+	friend_sync?: boolean;
 	name: string;
-	revoked: boolean;
-	show_activity: boolean;
+	revoked?: boolean;
+	show_activity?: boolean;
 	type: string;
-	verified: boolean;
-	visibility: boolean;
-	integrations: string[];
-	metadata_: any;
-	metadata_visibility: boolean;
-	two_way_link: boolean;
+	verified?: boolean;
+	visibility?: number;
+	integrations?: string[];
+	metadata_?: any;
+	metadata_visibility?: number;
+	two_way_link?: boolean;
 
 	constructor(
 		connectedAccount: ConnectedAccount,
diff --git a/src/util/entities/ConnectedAccount.ts b/src/util/entities/ConnectedAccount.ts
index 70923d2c..25d5a0c7 100644
--- a/src/util/entities/ConnectedAccount.ts
+++ b/src/util/entities/ConnectedAccount.ts
@@ -40,39 +40,39 @@ export class ConnectedAccount extends BaseClass {
 	})
 	user: User;
 
-	@Column({ select: false })
-	access_token: string;
+	@Column({ select: false, nullable: true })
+	access_token?: string;
 
 	@Column({ select: false })
-	friend_sync: boolean = false;
+	friend_sync?: boolean = false;
 
 	@Column()
 	name: string;
 
 	@Column({ select: false })
-	revoked: boolean = false;
+	revoked?: boolean = false;
 
 	@Column({ select: false })
-	show_activity: boolean = true;
+	show_activity?: boolean = true;
 
 	@Column()
 	type: string;
 
 	@Column()
-	verified: boolean;
+	verified?: boolean = true;
 
 	@Column({ select: false })
-	visibility: boolean = true;
+	visibility?: number = 0;
 
 	@Column({ type: "simple-array" })
-	integrations: string[];
+	integrations?: string[] = [];
 
-	@Column({ type: "simple-json", name: "metadata" })
-	metadata_: any;
+	@Column({ type: "simple-json", name: "metadata", nullable: true })
+	metadata_?: any;
 
 	@Column()
-	metadata_visibility: boolean = true;
+	metadata_visibility?: number = 0;
 
 	@Column()
-	two_way_link: boolean = false;
+	two_way_link?: boolean = false;
 }
diff --git a/src/util/index.ts b/src/util/index.ts
index a3495a0c..cb180ee8 100644
--- a/src/util/index.ts
+++ b/src/util/index.ts
@@ -25,3 +25,4 @@ export * from "./dtos/index";
 export * from "./schemas";
 export * from "./imports";
 export * from "./config";
+export * from "./connections"
\ No newline at end of file
diff --git a/src/util/schemas/ConnectedAccountSchema.ts b/src/util/schemas/ConnectedAccountSchema.ts
new file mode 100644
index 00000000..e00e4fa1
--- /dev/null
+++ b/src/util/schemas/ConnectedAccountSchema.ts
@@ -0,0 +1,16 @@
+export interface ConnectedAccountSchema {
+	external_id: string;
+	user_id: string;
+	access_token?: string;
+	friend_sync?: boolean;
+	name: string;
+	revoked?: boolean;
+	show_activity?: boolean;
+	type: string;
+	verified?: boolean;
+	visibility?: number;
+	integrations?: string[];
+	metadata_?: any;
+	metadata_visibility?: number;
+	two_way_link?: boolean;
+}
diff --git a/src/util/schemas/index.ts b/src/util/schemas/index.ts
index 2831d42a..71b4c2ce 100644
--- a/src/util/schemas/index.ts
+++ b/src/util/schemas/index.ts
@@ -30,6 +30,7 @@ export * from "./ChannelModifySchema";
 export * from "./ChannelPermissionOverwriteSchema";
 export * from "./ChannelReorderSchema";
 export * from "./CodesVerificationSchema";
+export * from "./ConnectedAccountSchema";
 export * from "./ConnectionCallbackSchema";
 export * from "./DmChannelCreateSchema";
 export * from "./EmojiCreateSchema";