From 76c85f7181cf5116b8f4ccd9015d1df371eb9c01 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sun, 5 Feb 2023 12:35:27 +1100 Subject: Fix being unable to unset nicknames Remove unwanted guild profile handler in cdn Fix rights check for bio/avatars of your own user --- .../guilds/#guild_id/members/#member_id/index.ts | 12 +++++++++- src/cdn/routes/guilds.ts | 28 ---------------------- 2 files changed, 11 insertions(+), 29 deletions(-) delete mode 100644 src/cdn/routes/guilds.ts (limited to 'src') 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/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 . -*/ - -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; -- cgit 1.5.1 From 3c11ffe17a45b4f6a9e1cb500fe22e26bc764f4b Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:31:36 +1100 Subject: Fix a bug prevent connections using erlpack (#977) --- src/gateway/events/Connection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') 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; -- cgit 1.5.1 From b4db451babd756f06c5511a90f361ddc3f066fe4 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 17 Feb 2023 18:00:16 -0500 Subject: Implement default avatars --- assets/default-avatars/1.png | Bin 0 -> 4397 bytes assets/default-avatars/2.png | Bin 0 -> 4544 bytes assets/default-avatars/3.png | Bin 0 -> 2831 bytes assets/default-avatars/4.png | Bin 0 -> 2855 bytes assets/default-avatars/5.png | Bin 0 -> 4276 bytes src/cdn/routes/embed.ts | 56 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+) create mode 100644 assets/default-avatars/1.png create mode 100644 assets/default-avatars/2.png create mode 100644 assets/default-avatars/3.png create mode 100644 assets/default-avatars/4.png create mode 100644 assets/default-avatars/5.png create mode 100644 src/cdn/routes/embed.ts (limited to 'src') diff --git a/assets/default-avatars/1.png b/assets/default-avatars/1.png new file mode 100644 index 00000000..61b3f9cf Binary files /dev/null and b/assets/default-avatars/1.png differ diff --git a/assets/default-avatars/2.png b/assets/default-avatars/2.png new file mode 100644 index 00000000..8caebf90 Binary files /dev/null and b/assets/default-avatars/2.png differ diff --git a/assets/default-avatars/3.png b/assets/default-avatars/3.png new file mode 100644 index 00000000..3fadcedf Binary files /dev/null and b/assets/default-avatars/3.png differ diff --git a/assets/default-avatars/4.png b/assets/default-avatars/4.png new file mode 100644 index 00000000..bcf0c183 Binary files /dev/null and b/assets/default-avatars/4.png differ diff --git a/assets/default-avatars/5.png b/assets/default-avatars/5.png new file mode 100644 index 00000000..3cc5589c Binary files /dev/null and b/assets/default-avatars/5.png differ diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts new file mode 100644 index 00000000..86268189 --- /dev/null +++ b/src/cdn/routes/embed.ts @@ -0,0 +1,56 @@ +/* + 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 . +*/ + +import { Request, Response, Router } from "express"; +import FileType from "file-type"; +import fs from "fs"; +import { HTTPError } from "lambert-server"; +import { join } from "path"; + +const router = Router(); + +function getFile(path: string) { + try { + return fs.readFileSync(path); + } catch (error) { + try { + const files = fs.readdirSync(path); + if (!files.length) return null; + return fs.readFileSync(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 path = join(process.cwd(), "assets", "default-avatars", `${id}.png`); + + const file = 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; -- cgit 1.5.1 From 3aae202e19cd75be7189dd670a7e580f4cd8f908 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 17 Feb 2023 18:10:05 -0500 Subject: async --- src/cdn/routes/embed.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts index 86268189..c38046ed 100644 --- a/src/cdn/routes/embed.ts +++ b/src/cdn/routes/embed.ts @@ -18,20 +18,20 @@ import { Request, Response, Router } from "express"; import FileType from "file-type"; -import fs from "fs"; +import fs from "fs/promises"; import { HTTPError } from "lambert-server"; import { join } from "path"; const router = Router(); -function getFile(path: string) { +async function getFile(path: string) { try { - return fs.readFileSync(path); + return fs.readFile(path); } catch (error) { try { - const files = fs.readdirSync(path); + const files = await fs.readdir(path); if (!files.length) return null; - return fs.readFileSync(join(path, files[0])); + return fs.readFile(join(path, files[0])); } catch (error) { return null; } @@ -43,7 +43,7 @@ router.get("/avatars/:id", async (req: Request, res: Response) => { id = id.split(".")[0]; // remove .file extension const path = join(process.cwd(), "assets", "default-avatars", `${id}.png`); - const file = getFile(path); + const file = await getFile(path); if (!file) throw new HTTPError("not found", 404); const type = await FileType.fromBuffer(file); -- cgit 1.5.1 From fb52929599f91c5cc74062d0fed50b731385ee27 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 17 Feb 2023 19:59:12 -0500 Subject: add support for desktop/web --- assets/default-avatars/1.png | Bin 4397 -> 0 bytes assets/default-avatars/2.png | Bin 4544 -> 0 bytes assets/default-avatars/3.png | Bin 2831 -> 0 bytes assets/default-avatars/4.png | Bin 2855 -> 0 bytes assets/default-avatars/5.png | Bin 4276 -> 0 bytes assets/public/1f0bfc0865d324c2587920a7d80c609b.png | Bin 0 -> 4397 bytes assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png | Bin 0 -> 4276 bytes assets/public/6f26ddd1bf59740c536d2274bb834a05.png | Bin 0 -> 2855 bytes assets/public/7c8f476123d28d103efe381543274c25.png | Bin 0 -> 2831 bytes assets/public/c09a43a372ba81e3018c3151d4ed4773.png | Bin 0 -> 4544 bytes package-lock.json | 1 - src/cdn/routes/embed.ts | 12 +++++++++++- 12 files changed, 11 insertions(+), 2 deletions(-) delete mode 100644 assets/default-avatars/1.png delete mode 100644 assets/default-avatars/2.png delete mode 100644 assets/default-avatars/3.png delete mode 100644 assets/default-avatars/4.png delete mode 100644 assets/default-avatars/5.png create mode 100644 assets/public/1f0bfc0865d324c2587920a7d80c609b.png create mode 100644 assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png create mode 100644 assets/public/6f26ddd1bf59740c536d2274bb834a05.png create mode 100644 assets/public/7c8f476123d28d103efe381543274c25.png create mode 100644 assets/public/c09a43a372ba81e3018c3151d4ed4773.png (limited to 'src') diff --git a/assets/default-avatars/1.png b/assets/default-avatars/1.png deleted file mode 100644 index 61b3f9cf..00000000 Binary files a/assets/default-avatars/1.png and /dev/null differ diff --git a/assets/default-avatars/2.png b/assets/default-avatars/2.png deleted file mode 100644 index 8caebf90..00000000 Binary files a/assets/default-avatars/2.png and /dev/null differ diff --git a/assets/default-avatars/3.png b/assets/default-avatars/3.png deleted file mode 100644 index 3fadcedf..00000000 Binary files a/assets/default-avatars/3.png and /dev/null differ diff --git a/assets/default-avatars/4.png b/assets/default-avatars/4.png deleted file mode 100644 index bcf0c183..00000000 Binary files a/assets/default-avatars/4.png and /dev/null differ diff --git a/assets/default-avatars/5.png b/assets/default-avatars/5.png deleted file mode 100644 index 3cc5589c..00000000 Binary files a/assets/default-avatars/5.png and /dev/null differ diff --git a/assets/public/1f0bfc0865d324c2587920a7d80c609b.png b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png new file mode 100644 index 00000000..61b3f9cf Binary files /dev/null and b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png differ diff --git a/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png new file mode 100644 index 00000000..3cc5589c Binary files /dev/null and b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png differ diff --git a/assets/public/6f26ddd1bf59740c536d2274bb834a05.png b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png new file mode 100644 index 00000000..bcf0c183 Binary files /dev/null and b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png differ diff --git a/assets/public/7c8f476123d28d103efe381543274c25.png b/assets/public/7c8f476123d28d103efe381543274c25.png new file mode 100644 index 00000000..3fadcedf Binary files /dev/null and b/assets/public/7c8f476123d28d103efe381543274c25.png differ diff --git a/assets/public/c09a43a372ba81e3018c3151d4ed4773.png b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png new file mode 100644 index 00000000..8caebf90 Binary files /dev/null and b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png differ diff --git a/package-lock.json b/package-lock.json index 165a2b23..ecd455b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -45,7 +45,6 @@ "probe-image-size": "^7.2.3", "proxy-agent": "^5.0.0", "reflect-metadata": "^0.1.13", - "sqlite3": "*", "ts-node": "^10.9.1", "tslib": "^2.4.1", "typeorm": "^0.3.10", diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts index c38046ed..5e5ff76d 100644 --- a/src/cdn/routes/embed.ts +++ b/src/cdn/routes/embed.ts @@ -22,6 +22,14 @@ import fs from "fs/promises"; import { HTTPError } from "lambert-server"; import { join } from "path"; +const defaultAvatarHashMap = new Map([ + ["1", "1f0bfc0865d324c2587920a7d80c609b"], + ["2", "c09a43a372ba81e3018c3151d4ed4773"], + ["3", "7c8f476123d28d103efe381543274c25"], + ["4", "6f26ddd1bf59740c536d2274bb834a05"], + ["5", "3c6ccb83716d1e4fb91d3082f6b21d77"], +]); + const router = Router(); async function getFile(path: string) { @@ -41,7 +49,9 @@ async function getFile(path: string) { router.get("/avatars/:id", async (req: Request, res: Response) => { let { id } = req.params; id = id.split(".")[0]; // remove .file extension - const path = join(process.cwd(), "assets", "default-avatars", `${id}.png`); + 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); -- cgit 1.5.1 From db10c1d387ed33455a51c7f6eeae94cc1519b366 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 18 Feb 2023 12:52:32 +1100 Subject: Update default avatar colour scheme and fix ordering --- assets/public/1f0bfc0865d324c2587920a7d80c609b.png | Bin 4397 -> 4380 bytes assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png | Bin 4276 -> 4511 bytes assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png | Bin 0 -> 4251 bytes assets/public/6f26ddd1bf59740c536d2274bb834a05.png | Bin 2855 -> 4494 bytes assets/public/7c8f476123d28d103efe381543274c25.png | Bin 2831 -> 4657 bytes assets/public/c09a43a372ba81e3018c3151d4ed4773.png | Bin 4544 -> 4158 bytes src/cdn/routes/embed.ts | 11 ++++++----- 7 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png (limited to 'src') diff --git a/assets/public/1f0bfc0865d324c2587920a7d80c609b.png b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png index 61b3f9cf..9b92bd2f 100644 Binary files a/assets/public/1f0bfc0865d324c2587920a7d80c609b.png and b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png differ diff --git a/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png index 3cc5589c..62c599a7 100644 Binary files a/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png and b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png differ diff --git a/assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png b/assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png new file mode 100644 index 00000000..bd7afef2 Binary files /dev/null and b/assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png differ diff --git a/assets/public/6f26ddd1bf59740c536d2274bb834a05.png b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png index bcf0c183..9f137906 100644 Binary files a/assets/public/6f26ddd1bf59740c536d2274bb834a05.png and b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png differ diff --git a/assets/public/7c8f476123d28d103efe381543274c25.png b/assets/public/7c8f476123d28d103efe381543274c25.png index 3fadcedf..3e8eeae9 100644 Binary files a/assets/public/7c8f476123d28d103efe381543274c25.png and b/assets/public/7c8f476123d28d103efe381543274c25.png differ diff --git a/assets/public/c09a43a372ba81e3018c3151d4ed4773.png b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png index 8caebf90..67ee7bbd 100644 Binary files a/assets/public/c09a43a372ba81e3018c3151d4ed4773.png and b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png differ diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts index 5e5ff76d..fd13d215 100644 --- a/src/cdn/routes/embed.ts +++ b/src/cdn/routes/embed.ts @@ -23,11 +23,12 @@ import { HTTPError } from "lambert-server"; import { join } from "path"; const defaultAvatarHashMap = new Map([ - ["1", "1f0bfc0865d324c2587920a7d80c609b"], - ["2", "c09a43a372ba81e3018c3151d4ed4773"], - ["3", "7c8f476123d28d103efe381543274c25"], - ["4", "6f26ddd1bf59740c536d2274bb834a05"], - ["5", "3c6ccb83716d1e4fb91d3082f6b21d77"], + ["0", "1f0bfc0865d324c2587920a7d80c609b"], + ["1", "c09a43a372ba81e3018c3151d4ed4773"], + ["2", "7c8f476123d28d103efe381543274c25"], + ["3", "6f26ddd1bf59740c536d2274bb834a05"], + ["4", "3c6ccb83716d1e4fb91d3082f6b21d77"], + ["5", "4c1b599b1ef5b9f1874fdb9933f3e03b"], ]); const router = Router(); -- cgit 1.5.1 From ecbfe2efadfa15ffcc7dea74858fe54c941ef5bf Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 20 Feb 2023 05:09:57 +0100 Subject: Remove channel mentions from messages, in order to fix them in test client. Signed-off-by: TheArcaneBrony --- src/api/util/handlers/Message.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src') 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 { } 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 { // 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.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; -- cgit 1.5.1 From ee62d47efd7a4b36ea821d4527f5ab5f8cb848c4 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Tue, 21 Feb 2023 11:35:06 +1100 Subject: Close #954 --- src/api/Server.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src') diff --git a/src/api/Server.ts b/src/api/Server.ts index 01c60f23..78870669 100644 --- a/src/api/Server.ts +++ b/src/api/Server.ts @@ -84,6 +84,25 @@ export class FosscordServer extends Server { ); } + // Discord.com sends ISO strings with +00:00 extension, not Z + // This causes issues with Python bot libs + this.app.set( + "json replacer", + 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; + }, + ); + this.app.use(CORS); this.app.use(BodyParser({ inflate: true, limit: "10mb" })); -- cgit 1.5.1 From 0be17e9496656c5b2c7b374b9e98b97dfbe5c7d9 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Tue, 21 Feb 2023 11:56:56 +1100 Subject: Fix gateway encoding Date objects as {} when using erlpack. Fixes NaN/NaN/NaN timestamps in desktop client --- src/api/Server.ts | 20 ++------------------ src/gateway/util/Send.ts | 26 +++++++++++++++++++++++--- src/util/util/JSON.ts | 15 +++++++++++++++ src/util/util/index.ts | 1 + 4 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 src/util/util/JSON.ts (limited to 'src') diff --git a/src/api/Server.ts b/src/api/Server.ts index 78870669..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,24 +85,7 @@ export class FosscordServer extends Server { ); } - // Discord.com sends ISO strings with +00:00 extension, not Z - // This causes issues with Python bot libs - this.app.set( - "json replacer", - 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; - }, - ); + this.app.set("json replacer", JSONReplacer); this.app.use(CORS); this.app.use(BodyParser({ inflate: true, limit: "10mb" })); 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"; -- cgit 1.5.1