Merge branch 'master' into feat/webrtc

This commit is contained in:
Madeline
2023-02-21 15:30:49 +11:00
16 changed files with 138 additions and 46 deletions
+7 -7
View File
@@ -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"
};
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

+3
View File
@@ -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" }));
@@ -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");
}
+10 -6
View File
@@ -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;
+67
View File
@@ -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;
-28
View File
@@ -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;
+1 -1
View File
@@ -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;
+23 -3
View File
@@ -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) {
+15
View File
@@ -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 };
+1
View File
@@ -40,3 +40,4 @@ export * from "./TraverseDirectory";
export * from "./InvisibleCharacters";
export * from "./Sentry";
export * from "./WebAuthn";
export * from "./JSON";