diff options
author | Puyodead1 <puyodead@proton.me> | 2022-12-22 10:05:51 -0500 |
---|---|---|
committer | Puyodead1 <puyodead@proton.me> | 2023-03-18 19:09:51 -0400 |
commit | 21bfda32e452c05b8906bf318df7415d6cd5acd0 (patch) | |
tree | 997f6ff0dd5ec6969cfea270776e4d96f1d82820 /src/api/routes/connections/#connection_name/callback.ts | |
parent | Merge pull request #1005 from Xanderplayz18/patch-1 (diff) | |
download | server-21bfda32e452c05b8906bf318df7415d6cd5acd0.tar.xz |
add connections
Diffstat (limited to 'src/api/routes/connections/#connection_name/callback.ts')
-rw-r--r-- | src/api/routes/connections/#connection_name/callback.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/api/routes/connections/#connection_name/callback.ts b/src/api/routes/connections/#connection_name/callback.ts new file mode 100644 index 00000000..f158a037 --- /dev/null +++ b/src/api/routes/connections/#connection_name/callback.ts @@ -0,0 +1,52 @@ +import { Request, Response, Router } from "express"; +import { + ConnectionCallbackSchema, + emitEvent, + FieldErrors, +} from "../../../../util"; +import { ConnectionStore } from "../../../../util/connections"; +import { route } from "../../../util"; + +const router = Router(); + +router.post( + "/", + route({ body: "ConnectionCallbackSchema" }), + async (req: Request, res: Response) => { + const { connection_id: connection_name } = req.params; + const connection = ConnectionStore.connections.get(connection_name); + if (!connection) + throw FieldErrors({ + provider_id: { + code: "BASE_TYPE_CHOICES", + message: req.t("common:field.BASE_TYPE_CHOICES", { + types: Array.from( + ConnectionStore.connections.keys(), + ).join(", "), + }), + }, + }); + + if (!connection.settings.enabled) + throw FieldErrors({ + provider_id: { + message: "This connection has been disabled server-side.", + }, + }); + + const body = req.body as ConnectionCallbackSchema; + const userId = connection.getUserId(body.state); + const emit = await connection.handleCallback(body); + + // whether we should emit a connections update event, only used when a connection doesnt already exist + if (emit) + emitEvent({ + event: "USER_CONNECTIONS_UPDATE", + data: {}, + user_id: userId, + }); + res.sendStatus(204); + }, +); + +export default router; |