summary refs log tree commit diff
path: root/src/api/util/utility
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/util/utility')
-rw-r--r--src/api/util/utility/Base64.ts3
-rw-r--r--src/api/util/utility/EmbedHandlers.ts88
-rw-r--r--src/api/util/utility/RandomInviteID.ts6
-rw-r--r--src/api/util/utility/String.ts8
-rw-r--r--src/api/util/utility/captcha.ts2
-rw-r--r--src/api/util/utility/ipAddress.ts31
-rw-r--r--src/api/util/utility/passwordStrength.ts12
7 files changed, 35 insertions, 115 deletions
diff --git a/src/api/util/utility/Base64.ts b/src/api/util/utility/Base64.ts
index c6d1257c..7426d22b 100644
--- a/src/api/util/utility/Base64.ts
+++ b/src/api/util/utility/Base64.ts
@@ -16,8 +16,7 @@
 	along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
 
-const alphabet =
-	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+";
+const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+";
 
 // binary to string lookup table
 const b2s = alphabet.split("");
diff --git a/src/api/util/utility/EmbedHandlers.ts b/src/api/util/utility/EmbedHandlers.ts
index 0f1e88a5..78e56986 100644
--- a/src/api/util/utility/EmbedHandlers.ts
+++ b/src/api/util/utility/EmbedHandlers.ts
@@ -27,8 +27,7 @@ export const DEFAULT_FETCH_OPTIONS: RequestInit = {
 	redirect: "follow",
 	follow: 1,
 	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
 	compress: true,
@@ -38,7 +37,7 @@ export const DEFAULT_FETCH_OPTIONS: RequestInit = {
 const makeEmbedImage = (
 	url: string | undefined,
 	width: number | undefined,
-	height: number | undefined,
+	height: number | undefined
 ): Required<EmbedImage> | undefined => {
 	if (!url || !width || !height) return undefined;
 	return {
@@ -51,13 +50,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);
@@ -81,8 +75,8 @@ export const getProxyUrl = (
 		console.log(
 			"[Embeds]",
 			yellow(
-				"Imagor has not been set up correctly. https://docs.spacebar.chat/setup/server/configuration/imagor/",
-			),
+				"Imagor has not been set up correctly. https://docs.spacebar.chat/setup/server/configuration/imagor/"
+			)
 		);
 	}
 
@@ -157,11 +151,7 @@ const genericImageHandler = async (url: URL): Promise<Embed | null> => {
 		const response = await doFetch(url);
 		if (!response) return null;
 		const metas = getMetaDescriptions(await response.text());
-		image = makeEmbedImage(
-			metas.image || metas.image_fallback,
-			metas.width,
-			metas.height,
-		);
+		image = makeEmbedImage(metas.image || metas.image_fallback, metas.width, metas.height);
 	}
 
 	if (!image) return null;
@@ -182,8 +172,7 @@ export const EmbedHandlers: {
 			...DEFAULT_FETCH_OPTIONS,
 			method: "HEAD",
 		});
-		if (type.headers.get("content-type")?.indexOf("image") !== -1)
-			return await genericImageHandler(url);
+		if (type.headers.get("content-type")?.indexOf("image") !== -1) return await genericImageHandler(url);
 
 		const response = await doFetch(url);
 		if (!response) return null;
@@ -275,9 +264,7 @@ export const EmbedHandlers: {
 		const text = json.data.text;
 		const created_at = new Date(json.data.created_at);
 		const metrics = json.data.public_metrics;
-		const media = json.includes.media?.filter(
-			(x: { type: string }) => x.type == "photo",
-		);
+		const media = json.includes.media?.filter((x: { type: string }) => x.type == "photo");
 
 		const embed: Embed = {
 			type: EmbedType.rich,
@@ -286,11 +273,7 @@ export const EmbedHandlers: {
 			author: {
 				url: `https://twitter.com/${author.username}`,
 				name: `${author.name} (@${author.username})`,
-				proxy_icon_url: getProxyUrl(
-					new URL(author.profile_image_url),
-					400,
-					400,
-				),
+				proxy_icon_url: getProxyUrl(new URL(author.profile_image_url), 400, 400),
 				icon_url: author.profile_image_url,
 			},
 			timestamp: created_at,
@@ -310,14 +293,11 @@ export const EmbedHandlers: {
 			footer: {
 				text: "Twitter",
 				proxy_icon_url: getProxyUrl(
-					new URL(
-						"https://abs.twimg.com/icons/apple-touch-icon-192x192.png",
-					),
-					192,
+					new URL("https://abs.twimg.com/icons/apple-touch-icon-192x192.png"),
 					192,
+					192
 				),
-				icon_url:
-					"https://abs.twimg.com/icons/apple-touch-icon-192x192.png",
+				icon_url: "https://abs.twimg.com/icons/apple-touch-icon-192x192.png",
 			},
 			// Discord doesn't send this?
 			// provider: {
@@ -331,11 +311,7 @@ export const EmbedHandlers: {
 				width: media[0].width,
 				height: media[0].height,
 				url: media[0].url,
-				proxy_url: getProxyUrl(
-					new URL(media[0].url),
-					media[0].width,
-					media[0].height,
-				),
+				proxy_url: getProxyUrl(new URL(media[0].url), media[0].width, media[0].height),
 			};
 			media.shift();
 		}
@@ -388,11 +364,7 @@ export const EmbedHandlers: {
 			type: EmbedType.image,
 			title: metas.title,
 			description: metas.description,
-			image: makeEmbedImage(
-				metas.image || metas.image_fallback,
-				metas.width,
-				metas.height,
-			),
+			image: makeEmbedImage(metas.image || metas.image_fallback, metas.width, metas.height),
 			provider: {
 				url: "https://pixiv.net",
 				name: "Pixiv",
@@ -404,17 +376,9 @@ export const EmbedHandlers: {
 		const response = await doFetch(url);
 		if (!response) return null;
 		const metas = getMetaDescriptions(await response.text());
-		const numReviews = metas.$("#review_summary_num_reviews").val() as
-			| string
-			| undefined;
-		const price = metas
-			.$(".game_purchase_price.price")
-			.data("price-final") as number | undefined;
-		const releaseDate = metas
-			.$(".release_date")
-			.find("div.date")
-			.text()
-			.trim();
+		const numReviews = metas.$("#review_summary_num_reviews").val() as string | undefined;
+		const price = metas.$(".game_purchase_price.price").data("price-final") as number | undefined;
+		const releaseDate = metas.$(".release_date").find("div.date").text().trim();
 		const isReleased = new Date(releaseDate) < new Date();
 
 		const fields: Embed["fields"] = [];
@@ -452,9 +416,7 @@ export const EmbedHandlers: {
 				width: 460,
 				height: 215,
 				url: metas.image,
-				proxy_url: metas.image
-					? getProxyUrl(new URL(metas.image), 460, 215)
-					: undefined,
+				proxy_url: metas.image ? getProxyUrl(new URL(metas.image), 460, 215) : undefined,
 			},
 			provider: {
 				url: "https://store.steampowered.com",
@@ -485,19 +447,11 @@ export const EmbedHandlers: {
 		const metas = getMetaDescriptions(await response.text());
 
 		return {
-			video: makeEmbedImage(
-				metas.youtube_embed,
-				metas.width,
-				metas.height,
-			),
+			video: makeEmbedImage(metas.youtube_embed, metas.width, metas.height),
 			url: url.href,
 			type: metas.youtube_embed ? EmbedType.video : EmbedType.link,
 			title: metas.title,
-			thumbnail: makeEmbedImage(
-				metas.image || metas.image_fallback,
-				metas.width,
-				metas.height,
-			),
+			thumbnail: makeEmbedImage(metas.image || metas.image_fallback, metas.width, metas.height),
 			provider: {
 				url: "https://www.youtube.com",
 				name: "YouTube",
diff --git a/src/api/util/utility/RandomInviteID.ts b/src/api/util/utility/RandomInviteID.ts
index 926750d3..f3b9197b 100644
--- a/src/api/util/utility/RandomInviteID.ts
+++ b/src/api/util/utility/RandomInviteID.ts
@@ -24,8 +24,7 @@ import crypto from "crypto";
 
 export function random(length = 6) {
 	// Declare all characters
-	const chars =
-		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+	const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
 
 	// Pick characers randomly
 	let str = "";
@@ -38,8 +37,7 @@ export function random(length = 6) {
 
 export function snowflakeBasedInvite() {
 	// Declare all characters
-	const chars =
-		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+	const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
 	const base = BigInt(chars.length);
 	let snowflake = Snowflake.generateWorkerProcess();
 
diff --git a/src/api/util/utility/String.ts b/src/api/util/utility/String.ts
index eef69e39..e531b447 100644
--- a/src/api/util/utility/String.ts
+++ b/src/api/util/utility/String.ts
@@ -20,13 +20,7 @@ import { Request } from "express";
 import { ntob } from "./Base64";
 import { FieldErrors } from "@spacebar/util";
 
-export function checkLength(
-	str: string,
-	min: number,
-	max: number,
-	key: string,
-	req: Request,
-) {
+export function checkLength(str: string, min: number, max: number, key: string, req: Request) {
 	if (str.length < min || str.length > max) {
 		throw FieldErrors({
 			[key]: {
diff --git a/src/api/util/utility/captcha.ts b/src/api/util/utility/captcha.ts
index db1b7957..9326eeb8 100644
--- a/src/api/util/utility/captcha.ts
+++ b/src/api/util/utility/captcha.ts
@@ -49,7 +49,7 @@ export async function verifyCaptcha(response: string, ip?: string) {
 
 	if (!service || !secret || !sitekey)
 		throw new Error(
-			"CAPTCHA is not configured correctly. https://docs.spacebar.chat/setup/server/security/captcha/",
+			"CAPTCHA is not configured correctly. https://docs.spacebar.chat/setup/server/security/captcha/"
 		);
 
 	const res = await fetch(verifyEndpoints[service], {
diff --git a/src/api/util/utility/ipAddress.ts b/src/api/util/utility/ipAddress.ts
index c51daf6c..731812d7 100644
--- a/src/api/util/utility/ipAddress.ts
+++ b/src/api/util/utility/ipAddress.ts
@@ -83,9 +83,7 @@ export async function IPAnalysis(ip: string): Promise<typeof exampleData> {
 	const { ipdataApiKey } = Config.get().security;
 	if (!ipdataApiKey) return { ...exampleData, ip };
 
-	return (
-		await fetch(`https://api.ipdata.co/${ip}?api-key=${ipdataApiKey}`)
-	).json();
+	return (await fetch(`https://api.ipdata.co/${ip}?api-key=${ipdataApiKey}`)).json();
 }
 
 export function isProxy(data: typeof exampleData) {
@@ -102,37 +100,20 @@ export function getIpAdress(req: Request): string {
 	return (
 		// eslint-disable-next-line @typescript-eslint/ban-ts-comment
 		// @ts-ignore
-		req.headers[Config.get().security.forwardedFor] ||
-		req.socket.remoteAddress
+		req.headers[Config.get().security.forwardedFor] || req.socket.remoteAddress
 	);
 }
 
 type Location = { latitude: number; longitude: number };
-export function distanceBetweenLocations(
-	loc1: Location,
-	loc2: Location,
-): number {
-	return distanceBetweenCoords(
-		loc1.latitude,
-		loc1.longitude,
-		loc2.latitude,
-		loc2.longitude,
-	);
+export function distanceBetweenLocations(loc1: Location, loc2: Location): number {
+	return distanceBetweenCoords(loc1.latitude, loc1.longitude, loc2.latitude, loc2.longitude);
 }
 
 //Haversine function
-function distanceBetweenCoords(
-	lat1: number,
-	lon1: number,
-	lat2: number,
-	lon2: number,
-) {
+function distanceBetweenCoords(lat1: number, lon1: number, lat2: number, lon2: number) {
 	const p = 0.017453292519943295; // Math.PI / 180
 	const c = Math.cos;
-	const a =
-		0.5 -
-		c((lat2 - lat1) * p) / 2 +
-		(c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))) / 2;
+	const a = 0.5 - c((lat2 - lat1) * p) / 2 + (c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p))) / 2;
 
 	return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
 }
diff --git a/src/api/util/utility/passwordStrength.ts b/src/api/util/utility/passwordStrength.ts
index fd627fbf..2f3e8bb1 100644
--- a/src/api/util/utility/passwordStrength.ts
+++ b/src/api/util/utility/passwordStrength.ts
@@ -36,8 +36,7 @@ const reSYMBOLS = /[A-Z,a-z,0-9]/g;
  * Returns: 0 > pw > 1
  */
 export function checkPassword(password: string): number {
-	const { minLength, minNumbers, minUpperCase, minSymbols } =
-		Config.get().register.password;
+	const { minLength, minNumbers, minUpperCase, minSymbols } = Config.get().register.password;
 	let strength = 0;
 
 	// checks for total password len
@@ -61,10 +60,7 @@ export function checkPassword(password: string): number {
 	}
 
 	// checks if password only consists of numbers or only consists of chars
-	if (
-		password.length == password.count(reNUMBER) ||
-		password.length === password.count(reUPPERCASELETTER)
-	) {
+	if (password.length == password.count(reNUMBER) || password.length === password.count(reUPPERCASELETTER)) {
 		strength = 0;
 	}
 
@@ -77,8 +73,6 @@ export function checkPassword(password: string): number {
 	const entropies = Object.values(entropyMap);
 
 	entropies.map((x) => x / entropyMap.length);
-	strength +=
-		entropies.reduceRight((a: number, x: number) => a - x * Math.log2(x)) /
-		Math.log2(password.length);
+	strength += entropies.reduceRight((a: number, x: number) => a - x * Math.log2(x)) / Math.log2(password.length);
 	return strength;
 }