summary refs log tree commit diff
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-02-21 15:30:49 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-02-21 15:30:49 +1100
commitfda13104adb343fd9ffcab395f83ed5d956cb2ed (patch)
tree6d885a3ff1829359e2912d30f6a6081bcab2f6e5
parentcrap (diff)
parentFix gateway encoding Date objects as {} when using erlpack. Fixes NaN/NaN/NaN... (diff)
downloadserver-fda13104adb343fd9ffcab395f83ed5d956cb2ed.tar.xz
Merge branch 'master' into feat/webrtc
-rw-r--r--assets/client_test/index.html14
-rw-r--r--assets/public/1f0bfc0865d324c2587920a7d80c609b.pngbin0 -> 4380 bytes
-rw-r--r--assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.pngbin0 -> 4511 bytes
-rw-r--r--assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.pngbin0 -> 4251 bytes
-rw-r--r--assets/public/6f26ddd1bf59740c536d2274bb834a05.pngbin0 -> 4494 bytes
-rw-r--r--assets/public/7c8f476123d28d103efe381543274c25.pngbin0 -> 4657 bytes
-rw-r--r--assets/public/c09a43a372ba81e3018c3151d4ed4773.pngbin0 -> 4158 bytes
-rw-r--r--src/api/Server.ts3
-rw-r--r--src/api/routes/guilds/#guild_id/members/#member_id/index.ts12
-rw-r--r--src/api/util/handlers/Message.ts16
-rw-r--r--src/cdn/routes/embed.ts67
-rw-r--r--src/cdn/routes/guilds.ts28
-rw-r--r--src/gateway/events/Connection.ts2
-rw-r--r--src/gateway/util/Send.ts26
-rw-r--r--src/util/util/JSON.ts15
-rw-r--r--src/util/util/index.ts1
16 files changed, 138 insertions, 46 deletions
diff --git a/assets/client_test/index.html b/assets/client_test/index.html
index c6833930..a2e40392 100644
--- a/assets/client_test/index.html
+++ b/assets/client_test/index.html
@@ -17,25 +17,25 @@
 			API_VERSION: 9,
 			GATEWAY_ENDPOINT: `${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}`,
 			WEBAPP_ENDPOINT: "",
-			CDN_HOST: `${location.hostname}`,
+			CDN_HOST: `${location.host}`,
 			ASSET_ENDPOINT: "",
-			MEDIA_PROXY_ENDPOINT: "https://media.discordapp.net",
+			MEDIA_PROXY_ENDPOINT: `${location.host}/media`,
 			WIDGET_ENDPOINT: `//${location.host}/widget`,
-			INVITE_HOST: `${location.hostname}/invite`,
+			INVITE_HOST: `${location.host}/invite`,
 			GUILD_TEMPLATE_HOST: "${location.host}/template",
-			GIFT_CODE_HOST: "${location.hostname}/gift",
+			GIFT_CODE_HOST: `${location.host}/gift`,
 			RELEASE_CHANNEL: "canary",
 			MARKETING_ENDPOINT: "//discord.com",
 			BRAINTREE_KEY: "production_5st77rrc_49pp2rp4phym7387",
 			STRIPE_KEY: "pk_live_CUQtlpQUF0vufWpnpUmQvcdi",
 			NETWORKING_ENDPOINT: "//router.discordapp.net",
-			RTC_LATENCY_ENDPOINT: "//${location.hostname}/rtc",
+			RTC_LATENCY_ENDPOINT: `//${location.host}/rtc`,
 			ACTIVITY_APPLICATION_HOST: "discordsays.com",
 			PROJECT_ENV: "production",
 			REMOTE_AUTH_ENDPOINT: "//localhost:3020",
 			SENTRY_TAGS: { buildId: "d5b97e42230075cb9634c419c0cf4d2f8f9ada53", buildType: "normal" },
-			MIGRATION_SOURCE_ORIGIN: "https://${location.hostname}",
-			MIGRATION_DESTINATION_ORIGIN: "https://${location.hostname}",
+			MIGRATION_SOURCE_ORIGIN: `https://${location.host}`,
+			MIGRATION_DESTINATION_ORIGIN: `https://${location.host}`,
 			HTML_TIMESTAMP: Date.now(),
 			ALGOLIA_KEY: "aca0d7082e4e63af5ba5917d5e96bed0"
 		};
diff --git a/assets/public/1f0bfc0865d324c2587920a7d80c609b.png b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png
new file mode 100644
index 00000000..9b92bd2f
--- /dev/null
+++ b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png
Binary files differdiff --git a/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png
new file mode 100644
index 00000000..62c599a7
--- /dev/null
+++ b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png
Binary files differdiff --git a/assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png b/assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png
new file mode 100644
index 00000000..bd7afef2
--- /dev/null
+++ b/assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png
Binary files differdiff --git a/assets/public/6f26ddd1bf59740c536d2274bb834a05.png b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png
new file mode 100644
index 00000000..9f137906
--- /dev/null
+++ b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png
Binary files differdiff --git a/assets/public/7c8f476123d28d103efe381543274c25.png b/assets/public/7c8f476123d28d103efe381543274c25.png
new file mode 100644
index 00000000..3e8eeae9
--- /dev/null
+++ b/assets/public/7c8f476123d28d103efe381543274c25.png
Binary files differdiff --git a/assets/public/c09a43a372ba81e3018c3151d4ed4773.png b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png
new file mode 100644
index 00000000..67ee7bbd
--- /dev/null
+++ b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png
Binary files differdiff --git a/src/api/Server.ts b/src/api/Server.ts
index 01c60f23..7eb4e6f1 100644
--- a/src/api/Server.ts
+++ b/src/api/Server.ts
@@ -23,6 +23,7 @@ import {
 	Config,
 	initDatabase,
 	initEvent,
+	JSONReplacer,
 	Sentry,
 	WebAuthn,
 } from "@fosscord/util";
@@ -84,6 +85,8 @@ export class FosscordServer extends Server {
 			);
 		}
 
+		this.app.set("json replacer", JSONReplacer);
+
 		this.app.use(CORS);
 		this.app.use(BodyParser({ inflate: true, limit: "10mb" }));
 
diff --git a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts
index d40f2772..6ac0dd3b 100644
--- a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts
+++ b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts
@@ -65,9 +65,19 @@ router.patch(
 
 		if ("nick" in body) {
 			permission.hasThrow("MANAGE_NICKNAMES");
+
+			if (!body.nick) {
+				delete body.nick;
+				// eslint-disable-next-line @typescript-eslint/ban-ts-comment
+				//@ts-ignore shut up
+				member.nick = null; // remove the nickname
+			}
 		}
 
-		if (("bio" in body || "avatar" in body) && member_id != "@me") {
+		if (
+			("bio" in body || "avatar" in body) &&
+			req.params.member_id != "@me"
+		) {
 			const rights = await getRights(req.user_id);
 			rights.hasThrow("MANAGE_USERS");
 		}
diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts
index 42325681..e514d400 100644
--- a/src/api/util/handlers/Message.ts
+++ b/src/api/util/handlers/Message.ts
@@ -26,7 +26,7 @@ import {
 	MessageUpdateEvent,
 	getPermission,
 	getRights,
-	CHANNEL_MENTION,
+	//CHANNEL_MENTION,
 	USER_MENTION,
 	ROLE_MENTION,
 	Role,
@@ -144,7 +144,9 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
 	}
 
 	let content = opts.content;
-	const mention_channel_ids = [] as string[];
+
+	// root@Rory - 20/02/2023 - This breaks channel mentions in test client. We're not sure this was used in older clients.
+	//const mention_channel_ids = [] as string[];
 	const mention_role_ids = [] as string[];
 	const mention_user_ids = [] as string[];
 	let mention_everyone = false;
@@ -153,10 +155,11 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
 		// TODO: explicit-only mentions
 		message.content = content.trim();
 		content = content.replace(/ *`[^)]*` */g, ""); // remove codeblocks
-		for (const [, mention] of content.matchAll(CHANNEL_MENTION)) {
+		// root@Rory - 20/02/2023 - This breaks channel mentions in test client. We're not sure this was used in older clients.
+		/*for (const [, mention] of content.matchAll(CHANNEL_MENTION)) {
 			if (!mention_channel_ids.includes(mention))
 				mention_channel_ids.push(mention);
-		}
+		}*/
 
 		for (const [, mention] of content.matchAll(USER_MENTION)) {
 			if (!mention_user_ids.includes(mention))
@@ -183,9 +186,10 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
 		}
 	}
 
-	message.mention_channels = mention_channel_ids.map((x) =>
+	// root@Rory - 20/02/2023 - This breaks channel mentions in test client. We're not sure this was used in older clients.
+	/*message.mention_channels = mention_channel_ids.map((x) =>
 		Channel.create({ id: x }),
-	);
+	);*/
 	message.mention_roles = mention_role_ids.map((x) => Role.create({ id: x }));
 	message.mentions = mention_user_ids.map((x) => User.create({ id: x }));
 	message.mention_everyone = mention_everyone;
diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts
new file mode 100644
index 00000000..fd13d215
--- /dev/null
+++ b/src/cdn/routes/embed.ts
@@ -0,0 +1,67 @@
+/*
+	Fosscord: A FOSS re-implementation and extension of the Discord.com backend.
+	Copyright (C) 2023 Fosscord and Fosscord Contributors
+	
+	This program is free software: you can redistribute it and/or modify
+	it under the terms of the GNU Affero General Public License as published
+	by the Free Software Foundation, either version 3 of the License, or
+	(at your option) any later version.
+	
+	This program is distributed in the hope that it will be useful,
+	but WITHOUT ANY WARRANTY; without even the implied warranty of
+	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	GNU Affero General Public License for more details.
+	
+	You should have received a copy of the GNU Affero General Public License
+	along with this program.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
+import { Request, Response, Router } from "express";
+import FileType from "file-type";
+import fs from "fs/promises";
+import { HTTPError } from "lambert-server";
+import { join } from "path";
+
+const defaultAvatarHashMap = new Map([
+	["0", "1f0bfc0865d324c2587920a7d80c609b"],
+	["1", "c09a43a372ba81e3018c3151d4ed4773"],
+	["2", "7c8f476123d28d103efe381543274c25"],
+	["3", "6f26ddd1bf59740c536d2274bb834a05"],
+	["4", "3c6ccb83716d1e4fb91d3082f6b21d77"],
+	["5", "4c1b599b1ef5b9f1874fdb9933f3e03b"],
+]);
+
+const router = Router();
+
+async function getFile(path: string) {
+	try {
+		return fs.readFile(path);
+	} catch (error) {
+		try {
+			const files = await fs.readdir(path);
+			if (!files.length) return null;
+			return fs.readFile(join(path, files[0]));
+		} catch (error) {
+			return null;
+		}
+	}
+}
+
+router.get("/avatars/:id", async (req: Request, res: Response) => {
+	let { id } = req.params;
+	id = id.split(".")[0]; // remove .file extension
+	const hash = defaultAvatarHashMap.get(id);
+	if (!hash) throw new HTTPError("not found", 404);
+	const path = join(process.cwd(), "assets", "public", `${hash}.png`);
+
+	const file = await getFile(path);
+	if (!file) throw new HTTPError("not found", 404);
+	const type = await FileType.fromBuffer(file);
+
+	res.set("Content-Type", type?.mime);
+	res.set("Cache-Control", "public, max-age=31536000");
+
+	return res.send(file);
+});
+
+export default router;
diff --git a/src/cdn/routes/guilds.ts b/src/cdn/routes/guilds.ts
deleted file mode 100644
index e36fd363..00000000
--- a/src/cdn/routes/guilds.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
-	Fosscord: A FOSS re-implementation and extension of the Discord.com backend.
-	Copyright (C) 2023 Fosscord and Fosscord Contributors
-	
-	This program is free software: you can redistribute it and/or modify
-	it under the terms of the GNU Affero General Public License as published
-	by the Free Software Foundation, either version 3 of the License, or
-	(at your option) any later version.
-	
-	This program is distributed in the hope that it will be useful,
-	but WITHOUT ANY WARRANTY; without even the implied warranty of
-	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-	GNU Affero General Public License for more details.
-	
-	You should have received a copy of the GNU Affero General Public License
-	along with this program.  If not, see <https://www.gnu.org/licenses/>.
-*/
-
-import { Router } from "express";
-import { getAvatar } from "./avatars";
-
-const router = Router();
-
-// TODO: handle guild profiles
-router.get("/:guild_id/users/:user_id/avatars/:hash", getAvatar);
-router.get("/:guild_id/users/:user_id/banners/:hash", getAvatar);
-
-export default router;
diff --git a/src/gateway/events/Connection.ts b/src/gateway/events/Connection.ts
index 5ba68ef7..1c902de0 100644
--- a/src/gateway/events/Connection.ts
+++ b/src/gateway/events/Connection.ts
@@ -88,7 +88,7 @@ export async function Connection(
 		if (!["json", "etf"].includes(socket.encoding))
 			return socket.close(CLOSECODES.Decode_error);
 
-		if (socket.encoding === "etf" && erlpack)
+		if (socket.encoding === "etf" && !erlpack)
 			throw new Error("Erlpack is not installed: 'npm i erlpack'");
 
 		socket.version = Number(searchParams.get("version")) || 8;
diff --git a/src/gateway/util/Send.ts b/src/gateway/util/Send.ts
index 38e5ab4a..e39554a4 100644
--- a/src/gateway/util/Send.ts
+++ b/src/gateway/util/Send.ts
@@ -20,7 +20,7 @@ import { Payload, WebSocket } from "@fosscord/gateway";
 import fs from "fs/promises";
 import path from "path";
 
-import type { ErlpackType } from "@fosscord/util";
+import { ErlpackType, JSONReplacer } from "@fosscord/util";
 let erlpack: ErlpackType | null = null;
 try {
 	erlpack = require("erlpack") as ErlpackType;
@@ -28,6 +28,21 @@ try {
 	// empty
 }
 
+// don't care
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+const recurseJsonReplace = (json: any) => {
+	for (const key in json) {
+		// eslint-disable-next-line no-prototype-builtins
+		if (!json.hasOwnProperty(key)) continue;
+
+		json[key] = JSONReplacer.call(json, key, json[key]);
+
+		if (typeof json[key] == "object" && json[key] !== null)
+			json[key] = recurseJsonReplace(json[key]);
+	}
+	return json;
+};
+
 export function Send(socket: WebSocket, data: Payload) {
 	if (process.env.WS_VERBOSE)
 		console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`);
@@ -47,9 +62,14 @@ export function Send(socket: WebSocket, data: Payload) {
 	}
 
 	let buffer: Buffer | string;
-	if (socket.encoding === "etf" && erlpack) buffer = erlpack.pack(data);
+	if (socket.encoding === "etf" && erlpack) {
+		// Erlpack doesn't like Date objects, encodes them as {}
+		data = recurseJsonReplace(data);
+		buffer = erlpack.pack(data);
+	}
 	// TODO: encode circular object
-	else if (socket.encoding === "json") buffer = JSON.stringify(data);
+	else if (socket.encoding === "json")
+		buffer = JSON.stringify(data, JSONReplacer);
 	else return;
 	// TODO: compression
 	if (socket.deflate) {
diff --git a/src/util/util/JSON.ts b/src/util/util/JSON.ts
new file mode 100644
index 00000000..b092eb88
--- /dev/null
+++ b/src/util/util/JSON.ts
@@ -0,0 +1,15 @@
+// Discord.com sends ISO strings with +00:00 extension, not Z
+// This causes issues with Python bot libs
+const JSONReplacer = function (
+	this: { [key: string]: unknown },
+	key: string,
+	value: unknown,
+) {
+	if (this[key] instanceof Date) {
+		return (this[key] as Date).toISOString().replace("Z", "+00:00");
+	}
+
+	return value;
+};
+
+export { JSONReplacer };
diff --git a/src/util/util/index.ts b/src/util/util/index.ts
index 543a49a9..6eb686d0 100644
--- a/src/util/util/index.ts
+++ b/src/util/util/index.ts
@@ -40,3 +40,4 @@ export * from "./TraverseDirectory";
 export * from "./InvisibleCharacters";
 export * from "./Sentry";
 export * from "./WebAuthn";
+export * from "./JSON";