Merge pull request #1008 from spacebarchat/dev/samuel

This commit is contained in:
Samuel (Flam3rboy)
2023-03-30 18:13:34 +02:00
committed by GitHub
51 changed files with 556 additions and 149 deletions
+2 -2
View File
@@ -32,7 +32,7 @@ import "missing-native-js-functions";
import morgan from "morgan";
import path from "path";
import { red } from "picocolors";
import { Authentication, CORS } from "./middlewares/";
import { CORS, initAuthentication } from "./middlewares/";
import { BodyParser } from "./middlewares/BodyParser";
import { ErrorHandler } from "./middlewares/ErrorHandler";
import { initRateLimits } from "./middlewares/RateLimit";
@@ -106,7 +106,7 @@ export class FosscordServer extends Server {
// @ts-ignore
this.app = api;
api.use(Authentication);
initAuthentication(api);
await initRateLimits(api);
await initTranslation(api);
+13 -4
View File
@@ -18,8 +18,9 @@
import { checkToken, Config, Rights } from "@fosscord/util";
import * as Sentry from "@sentry/node";
import { NextFunction, Request, Response } from "express";
import { NextFunction, Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
import { createSecretKey, KeyObject } from "crypto";
export const NO_AUTHORIZATION_ROUTES = [
// Authentication routes
@@ -69,6 +70,16 @@ declare global {
}
}
let jwtPublicKey: KeyObject;
// Initialize the jwt secret as a key object so it does not need to be regenerated for each request.
export function initAuthentication(api: Router) {
jwtPublicKey = createSecretKey(
Buffer.from(Config.get().security.jwtSecret),
);
api.use(Authentication);
}
export async function Authentication(
req: Request,
res: Response,
@@ -90,11 +101,9 @@ export async function Authentication(
Sentry.setUser({ id: req.user_id });
try {
const { jwtSecret } = Config.get().security;
const { decoded, user } = await checkToken(
req.headers.authorization,
jwtSecret,
jwtPublicKey,
);
req.token = decoded;
+40 -19
View File
@@ -18,11 +18,20 @@
import fs from "fs";
import path from "path";
import i18next from "i18next";
import i18nextMiddleware from "i18next-http-middleware";
import i18nextBackend from "i18next-node-fs-backend";
import i18next, { TFunction } from "i18next";
import i18nextBackend from "i18next-fs-backend";
import { Router } from "express";
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Express {
interface Request {
t: TFunction;
language?: string;
}
}
}
const ASSET_FOLDER_PATH = path.join(__dirname, "..", "..", "..", "assets");
export async function initTranslation(router: Router) {
@@ -34,21 +43,33 @@ export async function initTranslation(router: Router) {
.filter((x) => x.endsWith(".json"))
.map((x) => x.slice(0, x.length - 5));
await i18next
.use(i18nextBackend)
.use(i18nextMiddleware.LanguageDetector)
.init({
preload: languages,
// debug: true,
fallbackLng: "en",
ns,
backend: {
loadPath:
path.join(ASSET_FOLDER_PATH, "locales") +
"/{{lng}}/{{ns}}.json",
},
load: "all",
});
await i18next.use(i18nextBackend).init({
preload: languages,
// debug: true,
fallbackLng: "en",
ns,
backend: {
loadPath:
path.join(ASSET_FOLDER_PATH, "locales") +
"/{{lng}}/{{ns}}.json",
},
load: "all",
});
router.use(i18nextMiddleware.handle(i18next, {}));
router.use((req, res, next) => {
let lng = "en";
if (req.headers["accept-language"]) {
lng = req.headers["accept-language"].split(",")[0];
}
req.language = lng;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
req.t = (key: string | string[], options?: any) => {
return i18next.t(key, {
...options,
lng,
});
};
next();
});
}
@@ -39,6 +39,7 @@ const TemplateGuildProjection: (keyof Guild)[] = [
"system_channel_id",
"system_channel_flags",
"icon",
"id",
];
router.get("/", route({}), async (req: Request, res: Response) => {
+3 -3
View File
@@ -52,17 +52,17 @@ router.put("/:id", route({}), async (req: Request, res: Response) => {
where: { owner: { id: owner.id }, target: { id: target.id } },
})
) {
Note.update(
await Note.update(
{ owner: { id: owner.id }, target: { id: target.id } },
{ owner, target, content: note },
);
} else {
Note.insert({
await Note.create({
id: Snowflake.generate(),
owner,
target,
content: note,
});
}).save();
}
} else {
await Note.delete({
+1 -1
View File
@@ -274,7 +274,7 @@ export async function sendMessage(opts: MessageOptions) {
const message = await handleMessage({ ...opts, timestamp: new Date() });
await Promise.all([
Message.insert(message),
message.save(),
emitEvent({
event: "MESSAGE_CREATE",
channel_id: opts.channel_id,