diff --git a/src/api/routes/users/@me/connections/#connection_name/#connection_id/access-token.ts b/src/api/routes/users/@me/connections/#connection_name/#connection_id/access-token.ts
index a422050ff..fcab43219 100644
--- a/src/api/routes/users/@me/connections/#connection_name/#connection_id/access-token.ts
+++ b/src/api/routes/users/@me/connections/#connection_name/#connection_id/access-token.ts
@@ -17,14 +17,7 @@
*/
import { route } from "@spacebar/api";
-import {
- ApiError,
- ConnectedAccount,
- ConnectionStore,
- DiscordApiErrors,
- FieldErrors,
- RefreshableConnection,
-} from "@spacebar/util";
+import { ApiError, ConnectedAccount, ConnectionStore, DiscordApiErrors, FieldErrors, RefreshableConnection } from "@spacebar/util";
import { Request, Response, Router } from "express";
const router = Router({ mergeParams: true });
@@ -62,33 +55,17 @@ router.get("/", route({}), async (req: Request, res: Response) => {
external_id: connection_id,
user_id: req.user_id,
},
- select: [
- "external_id",
- "type",
- "name",
- "verified",
- "visibility",
- "show_activity",
- "revoked",
- "token_data",
- "friend_sync",
- "integrations",
- ],
+ select: ["external_id", "type", "name", "verified", "visibility", "show_activity", "revoked", "token_data", "friend_sync", "integrations"],
});
if (!connectedAccount) throw DiscordApiErrors.UNKNOWN_CONNECTION;
if (connectedAccount.revoked) throw DiscordApiErrors.CONNECTION_REVOKED;
- if (!connectedAccount.token_data)
- throw new ApiError("No token data", 0, 400);
+ if (!connectedAccount.token_data) throw new ApiError("No token data", 0, 400);
let access_token = connectedAccount.token_data.access_token;
const { expires_at, expires_in, fetched_at } = connectedAccount.token_data;
- 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);
+ 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);
access_token = tokenData.access_token;
}
diff --git a/src/api/routes/users/@me/connections/#connection_name/#connection_id/index.ts b/src/api/routes/users/@me/connections/#connection_name/#connection_id/index.ts
index f4b6ab14d..37deee389 100644
--- a/src/api/routes/users/@me/connections/#connection_name/#connection_id/index.ts
+++ b/src/api/routes/users/@me/connections/#connection_name/#connection_id/index.ts
@@ -19,62 +19,48 @@
import { route } from "@spacebar/api";
import { ConnectedAccount, DiscordApiErrors, emitEvent } from "@spacebar/util";
import { Request, Response, Router } from "express";
-import { ConnectionUpdateSchema } from "@spacebar/schemas"
+import { ConnectionUpdateSchema } from "@spacebar/schemas";
const router = Router({ mergeParams: true });
// TODO: connection update schema
-router.patch(
- "/",
- route({ requestBody: "ConnectionUpdateSchema" }),
- async (req: Request, res: Response) => {
- const { connection_name, connection_id } = req.params;
- const body = req.body as ConnectionUpdateSchema;
+router.patch("/", route({ requestBody: "ConnectionUpdateSchema" }), async (req: Request, res: Response) => {
+ const { connection_name, connection_id } = req.params;
+ const body = req.body as ConnectionUpdateSchema;
- const connection = await ConnectedAccount.findOne({
- where: {
- user_id: req.user_id,
- external_id: connection_id,
- type: connection_name,
- },
- select: [
- "external_id",
- "type",
- "name",
- "verified",
- "visibility",
- "show_activity",
- "revoked",
- "friend_sync",
- "integrations",
- ],
- });
+ const connection = await ConnectedAccount.findOne({
+ where: {
+ user_id: req.user_id,
+ external_id: connection_id,
+ type: connection_name,
+ },
+ select: ["external_id", "type", "name", "verified", "visibility", "show_activity", "revoked", "friend_sync", "integrations"],
+ });
- if (!connection) return DiscordApiErrors.UNKNOWN_CONNECTION;
- // TODO: do we need to do anything if the connection is revoked?
+ if (!connection) return DiscordApiErrors.UNKNOWN_CONNECTION;
+ // TODO: do we need to do anything if the connection is revoked?
- if (typeof body.visibility === "boolean")
- //@ts-expect-error For some reason the client sends this as a boolean, even tho docs say its a number?
- body.visibility = body.visibility ? 1 : 0;
- if (typeof body.show_activity === "boolean")
- //@ts-expect-error For some reason the client sends this as a boolean, even tho docs say its a number?
- body.show_activity = body.show_activity ? 1 : 0;
- if (typeof body.metadata_visibility === "boolean")
- //@ts-expect-error For some reason the client sends this as a boolean, even tho docs say its a number?
- body.metadata_visibility = body.metadata_visibility ? 1 : 0;
+ if (typeof body.visibility === "boolean")
+ //@ts-expect-error For some reason the client sends this as a boolean, even tho docs say its a number?
+ body.visibility = body.visibility ? 1 : 0;
+ if (typeof body.show_activity === "boolean")
+ //@ts-expect-error For some reason the client sends this as a boolean, even tho docs say its a number?
+ body.show_activity = body.show_activity ? 1 : 0;
+ if (typeof body.metadata_visibility === "boolean")
+ //@ts-expect-error For some reason the client sends this as a boolean, even tho docs say its a number?
+ body.metadata_visibility = body.metadata_visibility ? 1 : 0;
- connection.assign(req.body);
+ connection.assign(req.body);
- await ConnectedAccount.update(
- {
- user_id: req.user_id,
- external_id: connection_id,
- type: connection_name,
- },
- connection,
- );
- res.json(connection.toJSON());
- },
-);
+ await ConnectedAccount.update(
+ {
+ user_id: req.user_id,
+ external_id: connection_id,
+ type: connection_name,
+ },
+ connection,
+ );
+ res.json(connection.toJSON());
+});
router.delete("/", route({}), async (req: Request, res: Response) => {
const { connection_name, connection_id } = req.params;
diff --git a/src/api/util/handlers/route.ts b/src/api/util/handlers/route.ts
index 15cc0a196..bb38c5303 100644
--- a/src/api/util/handlers/route.ts
+++ b/src/api/util/handlers/route.ts
@@ -16,26 +16,15 @@
along with this program. If not, see .
*/
-import {
- DiscordApiErrors,
- EVENT,
- FieldErrors,
- PermissionResolvable,
- Permissions,
- RightResolvable,
- Rights,
- SpacebarApiErrors,
- getPermission,
- getRights,
-} from "@spacebar/util";
+import { DiscordApiErrors, EVENT, FieldErrors, PermissionResolvable, Permissions, RightResolvable, Rights, SpacebarApiErrors, getPermission, getRights } from "@spacebar/util";
import { AnyValidateFunction } from "ajv/dist/core";
import { NextFunction, Request, Response } from "express";
-import { ajv } from "@spacebar/schemas"
+import { ajv } from "@spacebar/schemas";
const ignoredRequestSchemas = [
// skip validation for settings proto JSON updates - TODO: figure out if this even possible to fix?
- "SettingsProtoUpdateJsonSchema"
-]
+ "SettingsProtoUpdateJsonSchema",
+];
declare global {
// TODO: fix this
@@ -94,27 +83,18 @@ export function route(opts: RouteOptions) {
throw e;
}
- if (!validate)
- throw new Error(`Body schema ${opts.requestBody} not found`);
+ if (!validate) throw new Error(`Body schema ${opts.requestBody} not found`);
}
return async (req: Request, res: Response, next: NextFunction) => {
if (opts.permission) {
- req.permission = await getPermission(
- req.user_id,
- req.params.guild_id,
- req.params.channel_id,
- );
+ req.permission = await getPermission(req.user_id, req.params.guild_id, req.params.channel_id);
- const requiredPerms = Array.isArray(opts.permission)
- ? opts.permission
- : [opts.permission];
+ const requiredPerms = Array.isArray(opts.permission) ? opts.permission : [opts.permission];
requiredPerms.forEach((perm) => {
// bitfield comparison: check if user lacks certain permission
if (!req.permission!.has(new Permissions(perm))) {
- throw DiscordApiErrors.MISSING_PERMISSIONS.withParams(
- perm as string,
- );
+ throw DiscordApiErrors.MISSING_PERMISSIONS.withParams(perm as string);
}
});
}
@@ -124,20 +104,14 @@ export function route(opts: RouteOptions) {
req.rights = await getRights(req.user_id);
if (!req.rights || !req.rights.has(required)) {
- throw SpacebarApiErrors.MISSING_RIGHTS.withParams(
- opts.right as string,
- );
+ throw SpacebarApiErrors.MISSING_RIGHTS.withParams(opts.right as string);
}
}
-
if (validate && !ignoredRequestSchemas.includes(opts.requestBody!)) {
const valid = validate(req.body);
if (!valid) {
- const fields: Record<
- string,
- { code?: string; message: string }
- > = {};
+ const fields: Record = {};
validate.errors?.forEach(
(x) =>
(fields[x.instancePath.slice(1)] = {
@@ -145,11 +119,7 @@ export function route(opts: RouteOptions) {
message: x.message || "",
}),
);
- if (process.env.LOG_VALIDATION_ERRORS)
- console.log(
- `[VALIDATION ERROR] ${req.method} ${req.originalUrl} - SCHEMA='${opts.requestBody}' -`,
- validate?.errors,
- );
+ if (process.env.LOG_VALIDATION_ERRORS) console.log(`[VALIDATION ERROR] ${req.method} ${req.originalUrl} - SCHEMA='${opts.requestBody}' -`, validate?.errors);
throw FieldErrors(fields, validate.errors!);
}
}
diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts
index 96fbe1c95..8bbd62834 100644
--- a/src/api/util/utility/EmbedHandlers.ts
+++ b/src/api/util/utility/EmbedHandlers.ts
@@ -16,7 +16,7 @@
along with this program. If not, see .
*/
-import { Config }from "@spacebar/util";
+import { Config } from "@spacebar/util";
import { Embed, EmbedImage, EmbedType } from "@spacebar/schemas";
import * as cheerio from "cheerio";
import crypto from "crypto";
@@ -26,18 +26,13 @@ import probe from "probe-image-size";
export const DEFAULT_FETCH_OPTIONS: RequestInit = {
redirect: "follow",
headers: {
- "user-agent":
- "Mozilla/5.0 (compatible; Spacebar/1.0; +https://github.com/spacebarchat/server)",
+ "user-agent": "Mozilla/5.0 (compatible; Spacebar/1.0; +https://github.com/spacebarchat/server)",
},
// size: 1024 * 1024 * 5, // grabbed from config later
method: "GET",
};
-const makeEmbedImage = (
- url: string | undefined,
- width: number | undefined,
- height: number | undefined,
-): Required | undefined => {
+const makeEmbedImage = (url: string | undefined, width: number | undefined, height: number | undefined): Required | undefined => {
if (!url || !width || !height) return undefined;
return {
url,
@@ -49,13 +44,8 @@ const makeEmbedImage = (
let hasWarnedAboutImagor = false;
-export const getProxyUrl = (
- url: URL,
- width: number,
- height: number,
-): string => {
- const { resizeWidthMax, resizeHeightMax, imagorServerUrl } =
- Config.get().cdn;
+export const getProxyUrl = (url: URL, width: number, height: number): string => {
+ const { resizeWidthMax, resizeHeightMax, imagorServerUrl } = Config.get().cdn;
const secret = Config.get().security.requestSignature;
width = Math.min(width || 500, resizeWidthMax || width);
height = Math.min(height || 500, resizeHeightMax || width);
@@ -64,24 +54,14 @@ export const getProxyUrl = (
if (imagorServerUrl) {
const path = `${width}x${height}/${url.host}${url.pathname}`;
- const hash = crypto
- .createHmac("sha1", secret)
- .update(path)
- .digest("base64")
- .replace(/\+/g, "-")
- .replace(/\//g, "_");
+ const hash = crypto.createHmac("sha1", secret).update(path).digest("base64").replace(/\+/g, "-").replace(/\//g, "_");
return `${imagorServerUrl}/${hash}/${path}`;
}
if (!hasWarnedAboutImagor) {
hasWarnedAboutImagor = true;
- console.log(
- "[Embeds]",
- yellow(
- "Imagor has not been set up correctly. https://docs.spacebar.chat/setup/server/configuration/imagor/",
- ),
- );
+ console.log("[Embeds]", yellow("Imagor has not been set up correctly. https://docs.spacebar.chat/setup/server/configuration/imagor/"));
}
return url.toString();
@@ -161,11 +141,7 @@ const genericImageHandler = async (url: URL): Promise