Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/database/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPool, Pool, PoolConfig, Types } from "mariadb";
import { createPool, Pool, PoolConfig } from "mariadb";

import { TokenInformation } from "./models/auth";
import { EditionLogType } from "./models/misc-db-types";
Expand All @@ -21,7 +21,8 @@ const DB_CONFIG: PoolConfig = {
typeCast: function castField(field, useDefaultTypeCasting) {
// We only want to cast bit fields that have a single-bit in them. If the field
// has more than one bit, then we cannot assume it is supposed to be a Boolean.
if ( ( field.type === Types.BIT ) && ( field.columnLength === 1 ) ) {
// FIXME: should use Types.BIT here but mariadb broke its export (https://github.com/mariadb-corporation/mariadb-connector-nodejs/issues/347)
if ( ( field.type === "BIT" ) && ( field.columnLength === 1 ) ) {
const bytes = field.buffer();
// A Buffer in Node represents a collection of 8-bit unsigned integers.
// Therefore, our single "bit field" comes back as the bits '0000 0001',
Expand Down
2 changes: 1 addition & 1 deletion src/modules/auth/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function tokenCheckMiddleware(req: Request, res: Response, next: Ne

export async function createToken(userId: bigint | string, discordToken: string | null) {
const numericUserId = BigInt(userId);
const apiToken = sign({ result: userId.toString() }, JWT_SECRET_TOKEN, { expiresIn: JWT_TOKEN_EXPIRATION_DAYS + "d" });
const apiToken = sign({ result: userId.toString() }, JWT_SECRET_TOKEN, { expiresIn: `${JWT_TOKEN_EXPIRATION_DAYS}d` });
const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + JWT_TOKEN_EXPIRATION_DAYS);
await db.registerToken(numericUserId, apiToken, discordToken, expirationDate);
Expand Down
31 changes: 16 additions & 15 deletions src/modules/discord/controler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChannelType, GuildBasedChannel } from "discord.js";
import { NextFunction, Request, Response } from "express";
import { ParamsFlatDictionary } from "express-serve-static-core";
import { SqlError } from "mariadb";
import { equals, is } from "typia";

Expand Down Expand Up @@ -40,7 +41,7 @@ export async function getDefaultGuildConfigOptions(req: Request, res: Response)
res.send(optionsList);
}

export async function getGuildConfig(req: Request, res: Response) {
export async function getGuildConfig(req: Request<ParamsFlatDictionary>, res: Response) {
const categoriesQuery = parseCategoriesParameter(req.query.categories);
if (categoriesQuery === null) {
res.status(400).send("Invalid category");
Expand All @@ -63,7 +64,7 @@ export async function getGuildConfig(req: Request, res: Response) {
res.send(config);
}

export async function getGuildConfigEditionLogs(req: Request, res: Response) {
export async function getGuildConfigEditionLogs(req: Request<ParamsFlatDictionary>, res: Response) {
const page = parseInt(req.query.page as string) || 0;
const limit = parseInt(req.query.limit as string) || 100;
if (page < 0 || limit < 0 || limit > 500) {
Expand Down Expand Up @@ -92,7 +93,7 @@ export async function getGuildConfigEditionLogs(req: Request, res: Response) {
res.json(editionLogs);
}

export async function getGuildRoleRewards(req: Request, res: Response) {
export async function getGuildRoleRewards(req: Request<ParamsFlatDictionary>, res: Response) {
let guildId;
try {
guildId = BigInt(req.params.guildId);
Expand Down Expand Up @@ -131,7 +132,7 @@ export async function getGlobalLeaderboard(req: Request, res: Response, next: Ne
});
}

export async function getGuildLeaderboard(req: Request, res: Response, next: NextFunction) {
export async function getGuildLeaderboard(req: Request<ParamsFlatDictionary>, res: Response, next: NextFunction) {
const page = parseInt(req.query.page as string) || 0;
const limit = parseInt(req.query.limit as string) || 50;
let guildId;
Expand Down Expand Up @@ -194,7 +195,7 @@ export async function getGuildLeaderboard(req: Request, res: Response, next: Nex
});
}

export async function getGuildLeaderboardAsJson(req: Request, res: Response, next: NextFunction) {
export async function getGuildLeaderboardAsJson(req: Request<ParamsFlatDictionary>, res: Response, next: NextFunction) {
let guildId;
try {
guildId = BigInt(req.params.guildId);
Expand Down Expand Up @@ -291,7 +292,7 @@ export async function getUserGuilds(req: Request, res: Response) {
res.send(userGuilds);
}

export async function getBasicGuildInfo(req: Request, res: Response) {
export async function getBasicGuildInfo(req: Request<ParamsFlatDictionary>, res: Response) {
let guildId;
try {
guildId = BigInt(req.params.guildId);
Expand All @@ -309,7 +310,7 @@ export async function getBasicGuildInfo(req: Request, res: Response) {
res.json(await discordClient.getBasicGuildInfo({ baseGuild: guild, userId: res.locals.user!.user_id.toString() }));
}

export async function getGuildRoles(req: Request, res: Response) {
export async function getGuildRoles(req: Request<ParamsFlatDictionary>, res: Response) {
let guildId;
try {
guildId = BigInt(req.params.guildId);
Expand Down Expand Up @@ -338,7 +339,7 @@ export async function getGuildRoles(req: Request, res: Response) {
res.json(roles);
}

export async function getGuildChannels(req: Request, res: Response) {
export async function getGuildChannels(req: Request<ParamsFlatDictionary>, res: Response) {
let guildId;
try {
guildId = BigInt(req.params.guildId);
Expand Down Expand Up @@ -398,7 +399,7 @@ export async function getGuildChannels(req: Request, res: Response) {
}


export async function putGuildLeaderboard(req: Request, res: Response, next: NextFunction) {
export async function putGuildLeaderboard(req: Request<ParamsFlatDictionary>, res: Response, next: NextFunction) {
// check guild ID validity
let guildId;
try {
Expand Down Expand Up @@ -459,7 +460,7 @@ export async function putGuildLeaderboard(req: Request, res: Response, next: Nex
res.sendStatus(204);
}

export async function putRoleRewards(req: Request, res: Response) {
export async function putRoleRewards(req: Request<ParamsFlatDictionary>, res: Response) {
// check user and guild ID validity
if (res.locals.user === undefined) {
res.status(401).send("Invalid token");
Expand Down Expand Up @@ -516,7 +517,7 @@ export async function putRoleRewards(req: Request, res: Response) {
res.send(newRewards);
}

export async function editGuildConfig(req: Request, res: Response) {
export async function editGuildConfig(req: Request<ParamsFlatDictionary>, res: Response) {
// check user and guild ID validity
if (res.locals.user === undefined) {
res.status(401).send("Invalid token");
Expand Down Expand Up @@ -572,7 +573,7 @@ export async function editGuildConfig(req: Request, res: Response) {
res.send(updatedConfig);
}

export async function getGuildRssFeeds(req: Request, res: Response) {
export async function getGuildRssFeeds(req: Request<ParamsFlatDictionary>, res: Response) {
let guildId;
try {
guildId = BigInt(req.params.guildId);
Expand Down Expand Up @@ -602,7 +603,7 @@ async function registerRssFeedsEdition(guildId: bigint, userId: bigint, eventTyp
await db.addConfigEditionLog(guildId, userId, eventType, { feed: feedIdsAndName });
}

export async function toggleRssFeed(req: Request, res: Response) {
export async function toggleRssFeed(req: Request<ParamsFlatDictionary>, res: Response) {
let guildId, feedId;
// check guild ID validity
try {
Expand Down Expand Up @@ -646,7 +647,7 @@ export async function toggleRssFeed(req: Request, res: Response) {
res.json(updatedFeedWithDisplayName);
}

export async function editRssFeed(req: Request, res: Response) {
export async function editRssFeed(req: Request<ParamsFlatDictionary>, res: Response) {
let guildId, feedId;
// check guild ID validity
try {
Expand Down Expand Up @@ -700,7 +701,7 @@ export async function editRssFeed(req: Request, res: Response) {
res.json(updatedFeed);
}

export async function deleteRssFeed(req: Request, res: Response) {
export async function deleteRssFeed(req: Request<ParamsFlatDictionary>, res: Response) {
let guildId, feedId;
// check guild ID validity
try {
Expand Down
5 changes: 3 additions & 2 deletions src/modules/discord/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NextFunction, Request, Response } from "express";
import { ParamsFlatDictionary } from "express-serve-static-core";

import DiscordClient from "../../bot/client";

const discordClient = DiscordClient.getInstance();

export async function isDiscordServerMember(req: Request, res: Response, next: NextFunction) {
export async function isDiscordServerMember(req: Request<ParamsFlatDictionary>, res: Response, next: NextFunction) {
if (res.locals.user === undefined) {
res._err = "Invalid token";
res.status(401).send(res._err);
Expand All @@ -28,7 +29,7 @@ export async function isDiscordServerMember(req: Request, res: Response, next: N
next();
}

export async function isDiscordServerAdmin(req: Request, res: Response, next: NextFunction) {
export async function isDiscordServerAdmin(req: Request<ParamsFlatDictionary>, res: Response, next: NextFunction) {
if (res.locals.user === undefined) {
res._err = "Invalid token";
res.status(401).send(res._err);
Expand Down
3 changes: 2 additions & 1 deletion src/modules/discord/utils/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Guild } from "discord.js";
import { Request, Response } from "express";
import { ParamsFlatDictionary } from "express-serve-static-core";

import DiscordClient from "../../../bot/client";
import { tokenCheck } from "../../auth/tokens";
Expand Down Expand Up @@ -37,7 +38,7 @@ export async function getGuildInfo(guild: Guild): Promise<LeaderboardGuildData>
};
}

export async function checkUserAuthentificationAndPermission(req: Request, res: Response): Promise<boolean> {
export async function checkUserAuthentificationAndPermission(req: Request<ParamsFlatDictionary>, res: Response): Promise<boolean> {
const tokenCheckResult = await tokenCheck(req);
if (Array.isArray(tokenCheckResult)) { // is an error code and message
res._err = tokenCheckResult[1];
Expand Down
Loading