summary refs log tree commit diff
path: root/src/routes
diff options
context:
space:
mode:
authorDiego Magdaleno <diegomagdaleno@protonmail.com>2021-05-19 20:39:31 -0500
committerDiego Magdaleno <diegomagdaleno@protonmail.com>2021-05-19 20:39:31 -0500
commite3f6a29df79865ae9a0d842ba5d59a2851894081 (patch)
tree079b93be825cae82a66912c61d38a5fbb28f87be /src/routes
parentConfig: Start working on the config refactor (diff)
downloadserver-e3f6a29df79865ae9a0d842ba5d59a2851894081.tar.xz
Config: First rewrite of config and working implementation of getting values
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/auth/login.ts9
-rw-r--r--src/routes/auth/register.ts9
-rw-r--r--src/routes/channels/#channel_id/messages/bulk-delete.ts5
-rw-r--r--src/routes/channels/#channel_id/pins.ts5
-rw-r--r--src/routes/gateway.ts7
-rw-r--r--src/routes/guilds/index.ts5
-rw-r--r--src/routes/guilds/templates/index.ts5
7 files changed, 27 insertions, 18 deletions
diff --git a/src/routes/auth/login.ts b/src/routes/auth/login.ts

index a0fc1190..218a56ae 100644 --- a/src/routes/auth/login.ts +++ b/src/routes/auth/login.ts
@@ -3,7 +3,7 @@ import { check, FieldErrors, Length } from "../../util/instanceOf"; import bcrypt from "bcrypt"; import jwt from "jsonwebtoken"; import { UserModel } from "@fosscord/server-util"; -import Config from "../../util/Config"; +import * as Config from "../../util/Config"; import { adjustEmail } from "./register"; const router: Router = Router(); @@ -25,7 +25,9 @@ router.post( const query: any[] = [{ phone: login }]; if (email) query.push({ email }); - const config = Config.get(); + // TODO: Rewrite this to have the proper config syntax on the new method + + const config = Config.apiConfig.store as unknown as Config.DefaultOptions; if (config.login.requireCaptcha && config.security.captcha.enabled) { if (!captcha_key) { @@ -67,9 +69,10 @@ export async function generateToken(id: string) { const algorithm = "HS256"; return new Promise((res, rej) => { + const securityPropertiesSecret = Config.apiConfig.get('security.jwtSecret') as Config.DefaultOptions; jwt.sign( { id: id, iat }, - Config.get().security.jwtSecret, + securityPropertiesSecret.security.jwtSecret, { algorithm, }, diff --git a/src/routes/auth/register.ts b/src/routes/auth/register.ts
index 265516d7..6389fb22 100644 --- a/src/routes/auth/register.ts +++ b/src/routes/auth/register.ts
@@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import Config from "../../util/Config"; +import * as Config from "../../util/Config"; import { trimSpecial, User, Snowflake, UserModel } from "@fosscord/server-util"; import bcrypt from "bcrypt"; import { check, Email, EMAIL_REGEX, FieldErrors, Length } from "../../util/instanceOf"; @@ -52,7 +52,8 @@ router.post( let discriminator = ""; // get register Config - const { register, security } = Config.get(); + const securityProperties = Config.apiConfig.store as unknown as Config.DefaultOptions; + const { register, security } = securityProperties; // check if registration is allowed if (!register.allowNewRegistration) { @@ -90,13 +91,13 @@ router.post( }, }); } - } else if (register.email.required) { + } else if (register.email.necessary) { throw FieldErrors({ email: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }, }); } - if (register.dateOfBirth.required && !date_of_birth) { + if (register.dateOfBirth.necessary && !date_of_birth) { throw FieldErrors({ date_of_birth: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }, }); diff --git a/src/routes/channels/#channel_id/messages/bulk-delete.ts b/src/routes/channels/#channel_id/messages/bulk-delete.ts
index 6ac4d8de..c469e495 100644 --- a/src/routes/channels/#channel_id/messages/bulk-delete.ts +++ b/src/routes/channels/#channel_id/messages/bulk-delete.ts
@@ -1,7 +1,7 @@ import { Router } from "express"; import { ChannelModel, getPermission, MessageDeleteBulkEvent, MessageModel } from "@fosscord/server-util"; import { HTTPError } from "lambert-server"; -import Config from "../../../../util/Config"; +import * as Config from "../../../../util/Config"; import { emitEvent } from "../../../../util/Event"; import { check } from "../../../../util/instanceOf"; @@ -20,7 +20,8 @@ router.post("/", check({ messages: [String] }), async (req, res) => { const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); permission.hasThrow("MANAGE_MESSAGES"); - const { maxBulkDelete } = Config.get().limits.message; + const limitsProperties = Config.apiConfig.get('limits.message') as Config.DefaultOptions; + const { maxBulkDelete } = limitsProperties.limits.message; const { messages } = req.body as { messages: string[] }; if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete"); diff --git a/src/routes/channels/#channel_id/pins.ts b/src/routes/channels/#channel_id/pins.ts
index 7dde15d0..d8e2be9b 100644 --- a/src/routes/channels/#channel_id/pins.ts +++ b/src/routes/channels/#channel_id/pins.ts
@@ -1,6 +1,6 @@ import { ChannelModel, getPermission, MessageModel, toObject } from "@fosscord/server-util"; import { Router, Request, Response } from "express"; -import Config from "../../../util/Config"; +import * as Config from "../../../util/Config"; import { HTTPError } from "lambert-server"; const router: Router = Router(); @@ -18,7 +18,8 @@ router.put("/:message_id", async (req: Request, res: Response) => { if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES"); const pinned_count = await MessageModel.count({ channel_id, pinned: true }).exec(); - const { maxPins } = Config.get().limits.channel; + const limitsProperties = Config.apiConfig.get('limits.channel') as Config.DefaultOptions; + const { maxPins } = limitsProperties.limits.channel; if (pinned_count >= maxPins) throw new HTTPError("Max pin count reached: " + maxPins); await MessageModel.updateOne({ id: message_id }, { pinned: true }).exec(); diff --git a/src/routes/gateway.ts b/src/routes/gateway.ts
index b6c8f49f..f92053e5 100644 --- a/src/routes/gateway.ts +++ b/src/routes/gateway.ts
@@ -1,11 +1,12 @@ import { Router } from "express"; -import Config from "../util/Config" +import * as Config from "../util/Config" const router = Router(); router.get("/", (req, res) => { - const { endpoint } = Config.getAll().gateway; - res.send({ url: endpoint || "ws://localhost:3002" }); + const generalConfig = Config.apiConfig.get('gateway', 'ws://localhost:3002') as Config.DefaultOptions; + const { gateway } = generalConfig; + res.send({ url: gateway || "ws://localhost:3002" }); }); export default router; diff --git a/src/routes/guilds/index.ts b/src/routes/guilds/index.ts
index 1ed9d0ff..89f60ab2 100644 --- a/src/routes/guilds/index.ts +++ b/src/routes/guilds/index.ts
@@ -3,7 +3,7 @@ import { RoleModel, GuildModel, Snowflake, Guild, RoleDocument } from "@fosscord import { HTTPError } from "lambert-server"; import { check } from "./../../util/instanceOf"; import { GuildCreateSchema } from "../../schema/Guild"; -import Config from "../../util/Config"; +import * as Config from "../../util/Config"; import { getPublicUser } from "../../util/User"; import { addMember } from "../../util/Member"; @@ -14,7 +14,8 @@ const router: Router = Router(); router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) => { const body = req.body as GuildCreateSchema; - const { maxGuilds } = Config.get().limits.user; + const limitsProperties = Config.apiConfig.get('limits.user') as Config.DefaultOptions; + const { maxGuilds } = limitsProperties.limits.user; const user = await getPublicUser(req.user_id, { guilds: true }); if (user.guilds.length >= maxGuilds) { diff --git a/src/routes/guilds/templates/index.ts b/src/routes/guilds/templates/index.ts
index 7e32e94c..c314728d 100644 --- a/src/routes/guilds/templates/index.ts +++ b/src/routes/guilds/templates/index.ts
@@ -5,7 +5,7 @@ import { HTTPError } from "lambert-server"; import { GuildTemplateCreateSchema } from "../../../schema/Guild"; import { getPublicUser } from "../../../util/User"; import { check } from "../../../util/instanceOf"; -import Config from "../../../util/Config"; +import * as Config from "../../../util/Config"; import { addMember } from "../../../util/Member"; router.get("/:code", async (req: Request, res: Response) => { @@ -21,7 +21,8 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res const { code } = req.params; const body = req.body as GuildTemplateCreateSchema; - const { maxGuilds } = Config.get().limits.user; + const limitsProperties = Config.apiConfig.get('limits.user') as Config.DefaultOptions; + const { maxGuilds } = limitsProperties.limits.user; const user = await getPublicUser(req.user_id, { guilds: true }); if (user.guilds.length >= maxGuilds) {