summary refs log tree commit diff
path: root/src/util/connections/Connection.ts
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2022-12-23 18:34:36 -0500
committerPuyodead1 <puyodead@proton.me>2023-03-18 19:27:39 -0400
commit0db1fa5f0b2b9b357c1f96178c0e5df7858a99ab (patch)
treeb045eee5f984a40ae47413bad2458cf65eff3c8e /src/util/connections/Connection.ts
parentDon't try to upload entire config for each connection loaded (diff)
downloadserver-0db1fa5f0b2b9b357c1f96178c0e5df7858a99ab.tar.xz
Refreshable connections, refactoring, access-token endpoint
- Aded /users/@me/connections/:connection_name/:connection_id/access-token
- Replaced `access_token` property on ConnectedAccount with `token_data` object for refreshing tokens
- Made a common interface for connection things like ComonOAuthTokenResponse
- Added `RefreshableConnection` class
- Added token refresh to Spotify connection (disabled)
Diffstat (limited to '')
-rw-r--r--src/util/connections/Connection.ts25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/util/connections/Connection.ts b/src/util/connections/Connection.ts

index 164cfac7..8b60b0d2 100644 --- a/src/util/connections/Connection.ts +++ b/src/util/connections/Connection.ts
@@ -1,9 +1,11 @@ import crypto from "crypto"; import { ConnectedAccount } from "../entities"; -import { OrmUtils } from "../imports"; import { ConnectedAccountSchema, ConnectionCallbackSchema } from "../schemas"; import { DiscordApiErrors } from "../util"; +/** + * A connection that can be used to connect to an external service. + */ export default abstract class Connection { id: string; settings: { enabled: boolean }; @@ -21,7 +23,9 @@ export default abstract class Connection { * Processes the callback * @param args Callback arguments */ - abstract handleCallback(params: ConnectionCallbackSchema): Promise<ConnectedAccount | null>; + abstract handleCallback( + params: ConnectionCallbackSchema, + ): Promise<ConnectedAccount | null>; /** * Gets a user id from state @@ -54,12 +58,25 @@ export default abstract class Connection { this.states.delete(state); } - async createConnection(data: ConnectedAccountSchema): Promise<ConnectedAccount> { - const ca = OrmUtils.mergeDeep(new ConnectedAccount(), data) as ConnectedAccount; + /** + * Creates a Connected Account in the database. + * @param data connected account data + * @returns the new connected account + */ + async createConnection( + data: ConnectedAccountSchema, + ): Promise<ConnectedAccount> { + const ca = ConnectedAccount.create({ ...data }); await ca.save(); return ca; } + /** + * Checks if a user has an exist connected account for the given extenal id. + * @param userId the user id + * @param externalId the connection id to find + * @returns + */ async hasConnection(userId: string, externalId: string): Promise<boolean> { const existing = await ConnectedAccount.findOne({ where: {