diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-03-31 15:26:15 +1100 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-03-31 15:26:15 +1100 |
commit | 698ad90d3e82a15b85ceb021ad5667109ac0bcdb (patch) | |
tree | 7b79fc6d65e79af82b11d89cb1a5502a9f7bff08 /src/api | |
parent | fix: disable cache if multi threaded (diff) | |
download | server-698ad90d3e82a15b85ceb021ad5667109ac0bcdb.tar.xz |
Revert "Merge pull request #1008 from spacebarchat/dev/samuel"
This reverts commit 69ea71aa9e0bd2e5a98904a66fba0ad3745707cb, reversing changes made to 8b2faf0b18336e5dff1eeff4e849bcfd96b09e88.
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/Server.ts | 4 | ||||
-rw-r--r-- | src/api/middlewares/Authentication.ts | 17 | ||||
-rw-r--r-- | src/api/middlewares/Translation.ts | 59 | ||||
-rw-r--r-- | src/api/routes/guilds/#guild_id/templates.ts | 1 | ||||
-rw-r--r-- | src/api/routes/users/@me/notes.ts | 6 | ||||
-rw-r--r-- | src/api/util/handlers/Message.ts | 2 |
6 files changed, 29 insertions, 60 deletions
diff --git a/src/api/Server.ts b/src/api/Server.ts index f88f94c0..032e923e 100644 --- a/src/api/Server.ts +++ b/src/api/Server.ts @@ -32,7 +32,7 @@ import "missing-native-js-functions"; import morgan from "morgan"; import path from "path"; import { red } from "picocolors"; -import { CORS, initAuthentication } from "./middlewares/"; +import { Authentication, CORS } 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; - initAuthentication(api); + api.use(Authentication); await initRateLimits(api); await initTranslation(api); diff --git a/src/api/middlewares/Authentication.ts b/src/api/middlewares/Authentication.ts index 0aa585e5..400a16f4 100644 --- a/src/api/middlewares/Authentication.ts +++ b/src/api/middlewares/Authentication.ts @@ -18,9 +18,8 @@ import { checkToken, Config, Rights } from "@fosscord/util"; import * as Sentry from "@sentry/node"; -import { NextFunction, Request, Response, Router } from "express"; +import { NextFunction, Request, Response } from "express"; import { HTTPError } from "lambert-server"; -import { createSecretKey, KeyObject } from "crypto"; export const NO_AUTHORIZATION_ROUTES = [ // Authentication routes @@ -70,16 +69,6 @@ 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, @@ -101,9 +90,11 @@ export async function Authentication( Sentry.setUser({ id: req.user_id }); try { + const { jwtSecret } = Config.get().security; + const { decoded, user } = await checkToken( req.headers.authorization, - jwtPublicKey, + jwtSecret, ); req.token = decoded; diff --git a/src/api/middlewares/Translation.ts b/src/api/middlewares/Translation.ts index 0ddc56bb..60ff4ad7 100644 --- a/src/api/middlewares/Translation.ts +++ b/src/api/middlewares/Translation.ts @@ -18,20 +18,11 @@ import fs from "fs"; import path from "path"; -import i18next, { TFunction } from "i18next"; -import i18nextBackend from "i18next-fs-backend"; +import i18next from "i18next"; +import i18nextMiddleware from "i18next-http-middleware"; +import i18nextBackend from "i18next-node-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) { @@ -43,33 +34,21 @@ export async function initTranslation(router: Router) { .filter((x) => x.endsWith(".json")) .map((x) => x.slice(0, x.length - 5)); - 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((req, res, next) => { - let lng = "en"; - if (req.headers["accept-language"]) { - lng = req.headers["accept-language"].split(",")[0]; - } - req.language = lng; + 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", + }); - // eslint-disable-next-line @typescript-eslint/no-explicit-any - req.t = (key: string | string[], options?: any) => { - return i18next.t(key, { - ...options, - lng, - }); - }; - next(); - }); + router.use(i18nextMiddleware.handle(i18next, {})); } diff --git a/src/api/routes/guilds/#guild_id/templates.ts b/src/api/routes/guilds/#guild_id/templates.ts index 707675e5..8a8c53fe 100644 --- a/src/api/routes/guilds/#guild_id/templates.ts +++ b/src/api/routes/guilds/#guild_id/templates.ts @@ -39,7 +39,6 @@ const TemplateGuildProjection: (keyof Guild)[] = [ "system_channel_id", "system_channel_flags", "icon", - "id", ]; router.get("/", route({}), async (req: Request, res: Response) => { diff --git a/src/api/routes/users/@me/notes.ts b/src/api/routes/users/@me/notes.ts index 07c07d72..87d45277 100644 --- a/src/api/routes/users/@me/notes.ts +++ b/src/api/routes/users/@me/notes.ts @@ -52,17 +52,17 @@ router.put("/:id", route({}), async (req: Request, res: Response) => { where: { owner: { id: owner.id }, target: { id: target.id } }, }) ) { - await Note.update( + Note.update( { owner: { id: owner.id }, target: { id: target.id } }, { owner, target, content: note }, ); } else { - await Note.create({ + Note.insert({ id: Snowflake.generate(), owner, target, content: note, - }).save(); + }); } } else { await Note.delete({ diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts index 8818904e..65dbcdfe 100644 --- a/src/api/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts @@ -274,7 +274,7 @@ export async function sendMessage(opts: MessageOptions) { const message = await handleMessage({ ...opts, timestamp: new Date() }); await Promise.all([ - message.save(), + Message.insert(message), emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, |