Add Twitch, error handling, revokation changes, etc

This commit is contained in:
Puyodead1
2022-12-24 16:24:58 -05:00
parent a60f147156
commit 6d6944cfee
19 changed files with 416 additions and 46 deletions

View File

@@ -19,7 +19,7 @@ const ALLOWED_CONNECTIONS = ["twitch", "youtube"];
router.get("/", route({}), async (req: Request, res: Response) => {
const { connection_name, connection_id } = req.params;
const connection = ConnectionStore.connections.get(connection_id);
const connection = ConnectionStore.connections.get(connection_name);
if (!ALLOWED_CONNECTIONS.includes(connection_name) || !connection)
throw FieldErrors({
@@ -41,7 +41,7 @@ router.get("/", route({}), async (req: Request, res: Response) => {
const connectedAccount = await ConnectedAccount.findOne({
where: {
type: connection_name,
id: connection_id,
external_id: connection_id,
user_id: req.user_id,
},
select: [
@@ -64,14 +64,12 @@ router.get("/", route({}), async (req: Request, res: Response) => {
throw new ApiError("No token data", 0, 400);
let access_token = connectedAccount.token_data.access_token;
const { expires_at, expires_in } = connectedAccount.token_data;
const { expires_at, expires_in, fetched_at } = connectedAccount.token_data;
if (expires_at && expires_at < Date.now()) {
if (!(connection instanceof RefreshableConnection))
throw new ApiError("Access token expired", 0, 400);
const tokenData = await connection.refresh(connectedAccount);
access_token = tokenData.access_token;
} else if (expires_in && expires_in < Date.now()) {
if (
(expires_at && expires_at < Date.now()) ||
(expires_in && fetched_at + expires_in * 1000 < Date.now())
) {
if (!(connection instanceof RefreshableConnection))
throw new ApiError("Access token expired", 0, 400);
const tokenData = await connection.refresh(connectedAccount);

View File

@@ -1,5 +1,10 @@
import { route } from "@fosscord/api";
import { ConnectedAccount, DiscordApiErrors, emitEvent, ConnectionUpdateSchema } from "@fosscord/util";
import {
ConnectedAccount,
ConnectionUpdateSchema,
DiscordApiErrors,
emitEvent
} from "@fosscord/util";
import { Request, Response, Router } from "express";
const router = Router();
@@ -35,6 +40,8 @@ router.patch(
//@ts-ignore For some reason the client sends this as a boolean, even tho docs say its a number?
if (typeof body.visibility === "boolean") body.visibility = body.visibility ? 1 : 0;
//@ts-ignore For some reason the client sends this as a boolean, even tho docs say its a number?
if (typeof body.show_activity === "boolean") body.show_activity = body.show_activity ? 1 : 0;
connection.assign(req.body);
@@ -58,7 +65,7 @@ router.delete("/", route({}), async (req: Request, res: Response) => {
user_id: req.user_id,
external_id: connection_id,
type: connection_name,
}
},
});
await Promise.all([
@@ -67,7 +74,7 @@ router.delete("/", route({}), async (req: Request, res: Response) => {
event: "USER_CONNECTIONS_UPDATE",
data: account,
user_id: req.user_id,
})
}),
]);
return res.sendStatus(200);