From 21bfda32e452c05b8906bf318df7415d6cd5acd0 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Thu, 22 Dec 2022 10:05:51 -0500 Subject: add connections --- src/util/connections/ConnectionConfig.ts | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/util/connections/ConnectionConfig.ts (limited to 'src/util/connections/ConnectionConfig.ts') diff --git a/src/util/connections/ConnectionConfig.ts b/src/util/connections/ConnectionConfig.ts new file mode 100644 index 00000000..9b120c93 --- /dev/null +++ b/src/util/connections/ConnectionConfig.ts @@ -0,0 +1,79 @@ +import { ConnectionConfigEntity } from "../entities/ConnectionConfigEntity"; + +let config: any; +let pairs: ConnectionConfigEntity[]; + +export const ConnectionConfig = { + init: async function init() { + if (config) return config; + console.log("[ConnectionConfig] Loading configuration..."); + pairs = await ConnectionConfigEntity.find(); + config = pairsToConfig(pairs); + + return this.set(config); + }, + get: function get() { + if (!config) { + return {}; + } + return config; + }, + set: function set(val: Partial) { + 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); + }, +}; + +function applyConfig(val: any) { + async function apply(obj: any, key = ""): Promise { + if (typeof obj === "object" && obj !== null && !(obj instanceof Date)) + return Promise.all( + Object.keys(obj).map((k) => + apply(obj[k], key ? `${key}_${k}` : k), + ), + ); + + let pair = pairs.find((x) => x.key === key); + if (!pair) pair = new ConnectionConfigEntity(); + + pair.key = key; + + if (pair.value !== obj) { + pair.value = obj; + if (!pair.key || pair.key == null) { + console.log(`[ConnectionConfig] WARN: Empty key`); + console.log(pair); + } else return pair.save(); + } + } + + return apply(val); +} + +function pairsToConfig(pairs: ConnectionConfigEntity[]) { + let value: any = {}; + + pairs.forEach((p) => { + const keys = p.key.split("_"); + let obj = value; + let prev = ""; + let prevObj = obj; + let i = 0; + + for (const key of keys) { + if (!isNaN(Number(key)) && !prevObj[prev]?.length) + prevObj[prev] = obj = []; + if (i++ === keys.length - 1) obj[key] = p.value; + else if (!obj[key]) obj[key] = {}; + + prev = key; + prevObj = obj; + obj = obj[key]; + } + }); + + return value; +} -- cgit 1.5.1 From 6a52e65e27d514273a4210dadafbee9692f23354 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Thu, 22 Dec 2022 10:47:32 -0500 Subject: adding connection now works --- src/api/Server.ts | 5 +++++ .../connections/#connection_name/authorize.ts | 2 +- .../connections/#connection_name/callback.ts | 2 +- src/connections/BattleNet/index.ts | 7 +------ src/connections/GitHub/index.ts | 7 +------ src/util/connections/Connection.ts | 4 ++-- src/util/connections/ConnectionConfig.ts | 1 - src/util/connections/ConnectionLoader.ts | 7 ++++--- src/util/dtos/ConnectedAccountDTO.ts | 18 ++++++++-------- src/util/entities/ConnectedAccount.ts | 24 +++++++++++----------- src/util/index.ts | 1 + src/util/schemas/ConnectedAccountSchema.ts | 16 +++++++++++++++ src/util/schemas/index.ts | 1 + 13 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 src/util/schemas/ConnectedAccountSchema.ts (limited to 'src/util/connections/ConnectionConfig.ts') 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 { + async createConnection(data: ConnectedAccountSchema): Promise { 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) { 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"; -- cgit 1.5.1 From 50f068400d4690930153486de3c03410e877338a Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Fri, 23 Dec 2022 20:32:19 +1100 Subject: Don't try to upload entire config for each connection loaded --- src/util/connections/ConnectionConfig.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/util/connections/ConnectionConfig.ts') diff --git a/src/util/connections/ConnectionConfig.ts b/src/util/connections/ConnectionConfig.ts index dd372ff7..acbca026 100644 --- a/src/util/connections/ConnectionConfig.ts +++ b/src/util/connections/ConnectionConfig.ts @@ -22,7 +22,8 @@ export const ConnectionConfig = { if (!config || !val) return; config = val.merge(config); - return applyConfig(config); + // return applyConfig(config); + return applyConfig(val); }, }; -- cgit 1.5.1 From 7116a9a1b0ee782d10330d5272ed340a36b5ddb6 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sat, 18 Mar 2023 19:48:44 -0400 Subject: fix a few linting errors --- src/util/connections/ConnectionConfig.ts | 2 +- src/util/connections/ConnectionLoader.ts | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'src/util/connections/ConnectionConfig.ts') diff --git a/src/util/connections/ConnectionConfig.ts b/src/util/connections/ConnectionConfig.ts index acbca026..4950ae90 100644 --- a/src/util/connections/ConnectionConfig.ts +++ b/src/util/connections/ConnectionConfig.ts @@ -54,7 +54,7 @@ function applyConfig(val: any) { } function pairsToConfig(pairs: ConnectionConfigEntity[]) { - let value: any = {}; + const value: any = {}; pairs.forEach((p) => { const keys = p.key.split("_"); diff --git a/src/util/connections/ConnectionLoader.ts b/src/util/connections/ConnectionLoader.ts index 04d7752a..0ca5b9e7 100644 --- a/src/util/connections/ConnectionLoader.ts +++ b/src/util/connections/ConnectionLoader.ts @@ -6,7 +6,7 @@ import { ConnectionConfig } from "./ConnectionConfig"; import { ConnectionStore } from "./ConnectionStore"; const root = "dist/connections"; -let connectionsLoaded = false; +const connectionsLoaded = false; export class ConnectionLoader { public static async loadConnections() { @@ -22,7 +22,7 @@ export class ConnectionLoader { }); dirs.forEach(async (x) => { - let modPath = path.resolve(path.join(root, x)); + const modPath = path.resolve(path.join(root, x)); const mod = new (require(modPath).default)() as Connection; ConnectionStore.connections.set(mod.id, mod); @@ -58,7 +58,10 @@ export class ConnectionLoader { ); await ConnectionConfig.set({ - [id]: Object.assign(config, (ConnectionLoader.getConnectionConfig(id) || {})) + [id]: Object.assign( + config, + ConnectionLoader.getConnectionConfig(id) || {}, + ), }); } } -- cgit 1.5.1 From 2e27281e2319c6a4240972bdfead6eaac43935e0 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sun, 2 Apr 2023 11:26:24 +1000 Subject: Less spammy user connection logs --- src/util/connections/ConnectionConfig.ts | 5 +++-- src/util/connections/ConnectionLoader.ts | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'src/util/connections/ConnectionConfig.ts') diff --git a/src/util/connections/ConnectionConfig.ts b/src/util/connections/ConnectionConfig.ts index 4950ae90..7d1f9857 100644 --- a/src/util/connections/ConnectionConfig.ts +++ b/src/util/connections/ConnectionConfig.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import { ConnectionConfigEntity } from "../entities/ConnectionConfigEntity"; let config: any; @@ -6,7 +7,7 @@ let pairs: ConnectionConfigEntity[]; export const ConnectionConfig = { init: async function init() { if (config) return config; - console.log("[ConnectionConfig] Loading configuration..."); + console.log("[Connections] Loading configuration..."); pairs = await ConnectionConfigEntity.find(); config = pairsToConfig(pairs); @@ -44,7 +45,7 @@ function applyConfig(val: any) { if (pair.value !== obj) { pair.value = obj; if (!pair.key || pair.key == null) { - console.log(`[ConnectionConfig] WARN: Empty key`); + console.log(`[Connections] WARN: Empty config key`); console.log(pair); } else return pair.save(); } diff --git a/src/util/connections/ConnectionLoader.ts b/src/util/connections/ConnectionLoader.ts index 0ca5b9e7..b32f77cd 100644 --- a/src/util/connections/ConnectionLoader.ts +++ b/src/util/connections/ConnectionLoader.ts @@ -1,6 +1,5 @@ import fs from "fs"; import path from "path"; -import { OrmUtils } from "../imports"; import Connection from "./Connection"; import { ConnectionConfig } from "./ConnectionConfig"; import { ConnectionStore } from "./ConnectionStore"; @@ -27,10 +26,11 @@ export class ConnectionLoader { ConnectionStore.connections.set(mod.id, mod); mod.init(); - console.log(`[Connections] Loaded connection '${mod.id}'`); + // console.log(`[Connections] Loaded connection '${mod.id}'`); }); } + // eslint-disable-next-line @typescript-eslint/no-explicit-any public static getConnectionConfig(id: string, defaults?: any): any { let cfg = ConnectionConfig.get()[id]; if (defaults) { @@ -41,21 +41,22 @@ export class ConnectionLoader { } } - if (!cfg) - console.log( - `[ConnectionConfig/WARN] Getting connection settings for '${id}' returned null! (Did you forget to add settings?)`, - ); + if (cfg?.enabled) console.log(`[Connections] ${id} enabled`); + + // if (!cfg) + // console.log( + // `[ConnectionConfig/WARN] Getting connection settings for '${id}' returned null! (Did you forget to add settings?)`, + // ); return cfg; } public static async setConnectionConfig( id: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any config: Partial, ): Promise { if (!config) - console.log( - `[ConnectionConfig/WARN] ${id} tried to set config=null!`, - ); + console.warn(`[Connections/WARN] ${id} tried to set config=null!`); await ConnectionConfig.set({ [id]: Object.assign( -- cgit 1.5.1