From 70892870161edad2a44ae36bdf9092961ef830bb Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Tue, 6 Apr 2021 18:02:10 +0200 Subject: [PATCH] :art: Convert id bigint to string --- src/middlewares/Authentication.ts | 2 +- src/routes/auth/login.ts | 17 +++++++++----- src/routes/auth/register.ts | 22 +++++++++++++++---- src/routes/channels/#channel_id/invites.ts | 4 ++-- .../#channel_id/messages/bulk-delete.ts | 6 ++--- .../channels/#channel_id/messages/index.ts | 10 ++++----- src/routes/guilds/#id/bans.ts | 14 ++++++------ src/routes/guilds/#id/channels.ts | 4 ++-- src/routes/guilds/#id/index.ts | 6 ++--- src/routes/guilds/#id/members.ts | 10 ++++----- src/routes/users/@me/guilds.ts | 2 +- src/schema/Channel.ts | 10 ++++----- src/schema/Guild.ts | 14 ++++++------ src/schema/Message.ts | 12 +++++----- src/test/mongo_test.ts | 5 ++--- src/util/Config.ts | 2 +- src/util/Member.ts | 4 ++-- src/util/User.ts | 2 +- 18 files changed, 83 insertions(+), 63 deletions(-) diff --git a/src/middlewares/Authentication.ts b/src/middlewares/Authentication.ts index 595bcf73..f2cd438c 100644 --- a/src/middlewares/Authentication.ts +++ b/src/middlewares/Authentication.ts @@ -29,7 +29,7 @@ export async function Authentication(req: Request, res: Response, next: NextFunc const decoded: any = await checkToken(req.headers.authorization); req.token = decoded; - req.user_id = BigInt(decoded.id); + req.user_id = decoded.id; return next(); } catch (error) { return next(new HTTPError(error.toString(), 400)); diff --git a/src/routes/auth/login.ts b/src/routes/auth/login.ts index b0936104..3c279319 100644 --- a/src/routes/auth/login.ts +++ b/src/routes/auth/login.ts @@ -25,12 +25,20 @@ router.post( const query: any[] = [{ phone: login }]; if (email) query.push({ email }); - // * MongoDB Specific query for user with same email or phone number const user = await UserModel.findOne( { $or: query, }, - `user_data.hash id user_settings.locale user_settings.theme` + { + id: true, + user_settings: { + locale: true, + theme: true, + }, + user_data: { + hash: true, + }, + } ).exec(); if (!user) { @@ -57,13 +65,13 @@ router.post( } ); -export async function generateToken(id: bigint) { +export async function generateToken(id: string) { const iat = Math.floor(Date.now() / 1000); const algorithm = "HS256"; return new Promise((res, rej) => { jwt.sign( - { id: `${id}`, iat }, + { id: id, iat }, Config.get().security.jwtSecret, { algorithm, @@ -80,7 +88,6 @@ export async function generateToken(id: bigint) { * POST /auth/login * @argument { login: "email@gmail.com", password: "cleartextpassword", undelete: false, captcha_key: null, login_source: null, gift_code_sku_id: null, } - * MFA required: * @returns {"token": null, "mfa": true, "sms": true, "ticket": "SOME TICKET JWT TOKEN"} diff --git a/src/routes/auth/register.ts b/src/routes/auth/register.ts index 9543e814..5fbe610e 100644 --- a/src/routes/auth/register.ts +++ b/src/routes/auth/register.ts @@ -166,10 +166,9 @@ router.post( } // TODO: save date_of_birth + // appearently discord doesn't save the date of birth and just calculate if nsfw is allowed + // if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false - // constructing final user object - // TODO fix: - // @ts-ignore const user: User = { id: Snowflake.generate(), created_at: new Date(), @@ -178,10 +177,25 @@ router.post( avatar: null, bot: false, system: false, + desktop: false, + mobile: false, + premium: false, + premium_type: 0, + phone: undefined, mfa_enabled: false, verified: false, + presence: { + activities: [], + client_status: { + desktop: undefined, + mobile: undefined, + web: undefined, + }, + status: "offline", + }, email: adjusted_email, nsfw_allowed: true, // TODO: depending on age + public_flags: 0n, flags: 0n, // TODO: generate default flags guilds: [], user_data: { @@ -233,7 +247,7 @@ router.post( }; // insert user into database - await new UserModel(user).save({}); + await new UserModel(user).save(); return res.json({ token: await generateToken(user.id) }); } diff --git a/src/routes/channels/#channel_id/invites.ts b/src/routes/channels/#channel_id/invites.ts index c9910ae2..adfb0688 100644 --- a/src/routes/channels/#channel_id/invites.ts +++ b/src/routes/channels/#channel_id/invites.ts @@ -13,7 +13,7 @@ const router: Router = Router(); router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => { const usID = req.user_id; - const chID = BigInt(req.params.channel_id); + const chID = req.params.channel_id; const channel = await ChannelModel.findOne({ id: chID }).exec(); if (!channel || !channel.guild_id) { @@ -47,7 +47,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) router.get("/", async (req: Request, res: Response) => { const usID = req.user_id; - const chID = BigInt(req.params.channel_id); + const chID = req.params.channel_id; const channel = await ChannelModel.findOne({ id: chID }).exec(); if (!channel || !channel.guild_id) { diff --git a/src/routes/channels/#channel_id/messages/bulk-delete.ts b/src/routes/channels/#channel_id/messages/bulk-delete.ts index f5c9afc7..89e9d720 100644 --- a/src/routes/channels/#channel_id/messages/bulk-delete.ts +++ b/src/routes/channels/#channel_id/messages/bulk-delete.ts @@ -12,8 +12,8 @@ export default router; // TODO: should users be able to bulk delete messages or only bots? // TODO: should this request fail, if you provide messages older than 14 days/invalid ids? // https://discord.com/developers/docs/resources/channel#bulk-delete-messages -router.post("/", check({ messages: [BigInt] }), async (req, res) => { - const channel_id = BigInt(req.params.channel_id); +router.post("/", check({ messages: [String] }), async (req, res) => { + const channel_id = req.params.channel_id const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec(); if (!channel?.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400); @@ -22,7 +22,7 @@ router.post("/", check({ messages: [BigInt] }), async (req, res) => { const { maxBulkDelete } = Config.get().limits.message; - const { messages } = req.body as { messages: bigint[] }; + const { messages } = req.body as { messages: string[] }; if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete"); if (messages.length > maxBulkDelete) throw new HTTPError(`You cannot delete more than ${maxBulkDelete} messages`); diff --git a/src/routes/channels/#channel_id/messages/index.ts b/src/routes/channels/#channel_id/messages/index.ts index 13c819b4..0b91a977 100644 --- a/src/routes/channels/#channel_id/messages/index.ts +++ b/src/routes/channels/#channel_id/messages/index.ts @@ -27,23 +27,23 @@ function isTextChannel(type: ChannelType): boolean { // https://discord.com/developers/docs/resources/channel#create-message // get messages router.get("/", async (req, res) => { - const channel_id = BigInt(req.params.channel_id); + const channel_id = req.params.channel_id; const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); if (!channel) throw new HTTPError("Channel not found", 404); isTextChannel(channel.type); try { - instanceOf({ $around: BigInt, $after: BigInt, $before: BigInt, $limit: new Length(Number, 1, 100) }, req.query, { + instanceOf({ $around: String, $after: String, $before: String, $limit: new Length(Number, 1, 100) }, req.query, { path: "query", req, }); } catch (error) { return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error }); } - var { around, after, before, limit }: { around?: bigint; after?: bigint; before?: bigint; limit?: number } = req.query; + var { around, after, before, limit }: { around?: string; after?: string; before?: string; limit?: number } = req.query; if (!limit) limit = 50; - var halfLimit = BigInt(Math.floor(limit / 2)); + var halfLimit = Math.floor(limit / 2); if ([ChannelType.GUILD_VOICE, ChannelType.GUILD_CATEGORY, ChannelType.GUILD_STORE].includes(channel.type)) throw new HTTPError("Not a text channel"); @@ -89,7 +89,7 @@ const messageUpload = multer({ limits: { fieldSize: 1024 * 1024 * 1024 * 50 } }) // TODO: trim and replace message content and every embed field // Send message router.post("/", check(MessageCreateSchema), async (req, res) => { - const channel_id = BigInt(req.params.channel_id); + const channel_id = req.params.channel_id; const body = req.body as MessageCreateSchema; const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); diff --git a/src/routes/guilds/#id/bans.ts b/src/routes/guilds/#id/bans.ts index ba062a44..e66ae3d4 100644 --- a/src/routes/guilds/#id/bans.ts +++ b/src/routes/guilds/#id/bans.ts @@ -11,7 +11,7 @@ import { getPublicUser } from "../../../util/User"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const guild_id = BigInt(req.params.id); + const guild_id = req.params.id; const guild = await GuildModel.findOne({ id: guild_id }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); @@ -21,8 +21,8 @@ router.get("/", async (req: Request, res: Response) => { }); router.get("/:user", async (req: Request, res: Response) => { - const guild_id = BigInt(req.params.id); - const user_id = BigInt(req.params.ban); + const guild_id = req.params.id; + const user_id = req.params.ban; var ban = await BanModel.findOne({ guild_id: guild_id, user_id: user_id }).exec(); if (!ban) throw new HTTPError("Ban not found", 404); @@ -30,8 +30,8 @@ router.get("/:user", async (req: Request, res: Response) => { }); router.post("/:user_id", check(BanCreateSchema), async (req: Request, res: Response) => { - const guild_id = BigInt(req.params.id); - const banned_user_id = BigInt(req.params.user_id); + const guild_id = req.params.id; + const banned_user_id = req.params.user_id; const banned_user = await getPublicUser(banned_user_id); const perms = await getPermission(req.user_id, guild_id); @@ -61,8 +61,8 @@ router.post("/:user_id", check(BanCreateSchema), async (req: Request, res: Respo }); router.delete("/:user_id", async (req: Request, res: Response) => { - var guild_id = BigInt(req.params.id); - var banned_user_id = BigInt(req.params.user_id); + var guild_id = req.params.id; + var banned_user_id = req.params.user_id; const banned_user = await getPublicUser(banned_user_id); const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); diff --git a/src/routes/guilds/#id/channels.ts b/src/routes/guilds/#id/channels.ts index df41ec41..cd03fdbb 100644 --- a/src/routes/guilds/#id/channels.ts +++ b/src/routes/guilds/#id/channels.ts @@ -6,14 +6,14 @@ import { check } from "../../../util/instanceOf"; const router = Router(); router.get("/", async (req, res) => { - const guild_id = BigInt(req.params.id); + const guild_id = (req.params.id); const channels = await ChannelModel.find({ guild_id }).lean().exec(); res.json(channels); }); router.post("/", check(ChannelModifySchema), async (req, res) => { - const guild_id = BigInt(req.params.id); + const guild_id = (req.params.id); const body = req.body as ChannelModifySchema; if (!body.permission_overwrites) body.permission_overwrites = []; if (!body.topic) body.topic = ""; diff --git a/src/routes/guilds/#id/index.ts b/src/routes/guilds/#id/index.ts index 9b5ce313..385904f6 100644 --- a/src/routes/guilds/#id/index.ts +++ b/src/routes/guilds/#id/index.ts @@ -19,7 +19,7 @@ import { check } from "../../../util/instanceOf"; const router = Router(); router.get("/", async (req: Request, res: Response) => { - const guild_id = BigInt(req.params.id); + const guild_id = req.params.id; const guild = await GuildModel.findOne({ id: guild_id }).exec(); if (!guild) throw new HTTPError("Guild does not exist", 404); @@ -32,7 +32,7 @@ router.get("/", async (req: Request, res: Response) => { router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) => { const body = req.body as GuildUpdateSchema; - const guild_id = BigInt(req.params.id); + const guild_id = req.params.id; const guild = await GuildModel.findOne({ id: guild_id }).exec(); if (!guild) throw new HTTPError("This guild does not exist", 404); @@ -45,7 +45,7 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) }); router.delete("/", async (req: Request, res: Response) => { - var guild_id = BigInt(req.params.id); + var guild_id = req.params.id; const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec(); if (!guild) throw new HTTPError("This guild does not exist", 404); diff --git a/src/routes/guilds/#id/members.ts b/src/routes/guilds/#id/members.ts index 25889e2e..f95bd313 100644 --- a/src/routes/guilds/#id/members.ts +++ b/src/routes/guilds/#id/members.ts @@ -10,12 +10,12 @@ const router = Router(); // TODO: not allowed for user -> only allowed for bots with privileged intents // TODO: send over websocket router.get("/", async (req: Request, res: Response) => { - const guild_id = BigInt(req.params.id); + const guild_id = req.params.id; const guild = await GuildModel.findOne({ id: guild_id }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); try { - instanceOf({ $limit: new Length(Number, 1, 1000), $after: BigInt }, req.query, { + instanceOf({ $limit: new Length(Number, 1, 1000), $after: String }, req.query, { path: "query", req, ref: { obj: null, key: "" }, @@ -26,7 +26,7 @@ router.get("/", async (req: Request, res: Response) => { // @ts-ignore if (!req.query.limit) req.query.limit = 1; - const { limit, after } = (req.query) as { limit: number; after: bigint }; + const { limit, after } = (req.query) as { limit: number; after: string }; const query = after ? { id: { $gt: after } } : {}; var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection) @@ -39,8 +39,8 @@ router.get("/", async (req: Request, res: Response) => { }); router.get("/:member", async (req: Request, res: Response) => { - const guild_id = BigInt(req.params.id); - const user_id = BigInt(req.params.member); + const guild_id = req.params.id; + const user_id = req.params.member; const member = await MemberModel.findOne({ id: user_id, guild_id }).populate({ path: "user", select: PublicUserProjection }).exec(); if (!member) throw new HTTPError("Member not found", 404); diff --git a/src/routes/users/@me/guilds.ts b/src/routes/users/@me/guilds.ts index 1b87b51d..6b9243ac 100644 --- a/src/routes/users/@me/guilds.ts +++ b/src/routes/users/@me/guilds.ts @@ -19,7 +19,7 @@ router.get("/", async (req: Request, res: Response) => { // user send to leave a certain guild router.delete("/:id", async (req: Request, res: Response) => { - const guildID = BigInt(req.params.id); + const guildID = (req.params.id); const guild = await GuildModel.findOne({ id: guildID }).exec(); if (!guild) throw new HTTPError("Guild doesn't exist", 404); diff --git a/src/schema/Channel.ts b/src/schema/Channel.ts index 2e7d1214..0fafc54d 100644 --- a/src/schema/Channel.ts +++ b/src/schema/Channel.ts @@ -10,7 +10,7 @@ export const ChannelModifySchema = { $position: Number, $permission_overwrites: [ { - id: BigInt, + id: String, type: new Length(Number, 0, 1), // either 0 (role) or 1 (member) allow: BigInt, deny: BigInt, @@ -29,23 +29,23 @@ export interface ChannelModifySchema { rate_limit_per_user?: Number; position?: number; permission_overwrites?: { - id: bigint; + id: string; type: number; allow: bigint; deny: bigint; }[]; - parent_id?: bigint; + parent_id?: string; nsfw?: boolean; } export const ChannelGuildPositionUpdateSchema = [ { - id: BigInt, + id: String, $position: Number, }, ]; export type ChannelGuildPositionUpdateSchema = { - id: bigint; + id: string; position?: number; }[]; diff --git a/src/schema/Guild.ts b/src/schema/Guild.ts index d8c3431d..6527f35d 100644 --- a/src/schema/Guild.ts +++ b/src/schema/Guild.ts @@ -7,7 +7,7 @@ export const GuildCreateSchema = { $icon: String, $channels: [Object], $guild_template_code: String, - $system_channel_id: BigInt, + $system_channel_id: String, }; export interface GuildCreateSchema { @@ -16,7 +16,7 @@ export interface GuildCreateSchema { icon?: string; channels?: GuildChannel[]; guild_template_code?: string; - system_channel_id?: bigint; + system_channel_id?: string; } export const GuildUpdateSchema = { @@ -29,11 +29,11 @@ export const GuildUpdateSchema = { $verification_level: Number, $default_message_notifications: Number, $system_channel_flags: Number, - $system_channel_id: BigInt, + $system_channel_id: String, $explicit_content_filter: Number, - $public_updates_channel_id: BigInt, + $public_updates_channel_id: String, $afk_timeout: Number, - $afk_channel_id: BigInt, + $afk_channel_id: String, }; // @ts-ignore delete GuildUpdateSchema.$channels; @@ -47,9 +47,9 @@ export interface GuildUpdateSchema extends Omit { default_message_notifications?: number; system_channel_flags?: number; explicit_content_filter?: number; - public_updates_channel_id?: bigint; + public_updates_channel_id?: string; afk_timeout?: number; - afk_channel_id?: bigint; + afk_channel_id?: string; } export const GuildGetSchema = { diff --git a/src/schema/Message.ts b/src/schema/Message.ts index 4ae6c136..0c682a00 100644 --- a/src/schema/Message.ts +++ b/src/schema/Message.ts @@ -44,9 +44,9 @@ export const MessageCreateSchema = { }, $allowed_mentions: [], $message_reference: { - message_id: BigInt, - channel_id: BigInt, - $guild_id: BigInt, + message_id: String, + channel_id: String, + $guild_id: String, $fail_if_not_exists: Boolean, }, $payload_json: String, @@ -60,9 +60,9 @@ export interface MessageCreateSchema { embed?: Embed & { timestamp: string }; allowed_mentions?: []; message_reference?: { - message_id: bigint; - channel_id: bigint; - guild_id?: bigint; + message_id: string; + channel_id: string; + guild_id?: string; fail_if_not_exists: boolean; }; payload_json?: string; diff --git a/src/test/mongo_test.ts b/src/test/mongo_test.ts index f1a7f3f6..44b04c5b 100644 --- a/src/test/mongo_test.ts +++ b/src/test/mongo_test.ts @@ -1,13 +1,12 @@ import mongoose, { Schema, Types } from "mongoose"; -import { Long as MongoTypeLong } from "mongodb"; require("mongoose-long")(mongoose); const userSchema = new Schema({ - id: MongoTypeLong, + id: String, }); const messageSchema = new Schema({ - id: MongoTypeLong, + id: String, content: String, }); const message = mongoose.model("message", messageSchema, "messages"); diff --git a/src/util/Config.ts b/src/util/Config.ts index a9cbddde..60d83e1a 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts @@ -23,7 +23,7 @@ export interface RateLimit { export interface DefaultOptions { general: { - instance_id: bigint; + instance_id: string; }; permissions: { user: { diff --git a/src/util/Member.ts b/src/util/Member.ts index 2b268597..3ce06962 100644 --- a/src/util/Member.ts +++ b/src/util/Member.ts @@ -25,7 +25,7 @@ export const PublicMemberProjection = { premium_since: true, }; -export async function addMember(user_id: bigint, guild_id: bigint, cache?: { guild?: Guild }) { +export async function addMember(user_id: string, guild_id: string, cache?: { guild?: Guild }) { const user = await getPublicUser(user_id, { guilds: true }); const guildSize = user.guilds.length; @@ -83,7 +83,7 @@ export async function addMember(user_id: bigint, guild_id: bigint, cache?: { gui ]); } -export async function removeMember(user_id: bigint, guild_id: bigint) { +export async function removeMember(user_id: string, guild_id: string) { const user = await getPublicUser(user_id); const guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec(); diff --git a/src/util/User.ts b/src/util/User.ts index 3658e46d..70098bb5 100644 --- a/src/util/User.ts +++ b/src/util/User.ts @@ -9,7 +9,7 @@ export const PublicUserProjection = { avatar: true, }; -export async function getPublicUser(user_id: bigint, additional_fields?: any) { +export async function getPublicUser(user_id: string, additional_fields?: any) { const user = await UserModel.findOne( { id: user_id }, {