From 6a1e0594dc55e35c0846370ff6bc7a8bd55947de Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 8 Mar 2021 18:40:37 +0100 Subject: [PATCH] rename req.userid -> req.user_id --- package-lock.json | Bin 389864 -> 396416 bytes package.json | 2 + src/middlewares/Authentication.ts | 4 +- src/middlewares/RateLimit.ts | 2 +- src/routes/api/v8/auth/register.ts | 4 +- .../api/v8/channels/#channel_id/invites.ts | 4 +- src/routes/api/v8/guilds/#id/bans.ts | 16 ++-- src/routes/api/v8/guilds/#id/index.ts | 6 +- src/routes/api/v8/guilds/index.ts | 6 +- src/routes/api/v8/users/@me/guilds.ts | 12 +-- src/routes/api/v8/users/@me/index.ts | 2 +- src/schema/Message.ts | 72 ++++++++++++++---- src/util/Constants.ts | 29 +++---- src/util/Member.ts | 2 +- src/util/instanceOf.ts | 2 +- 15 files changed, 98 insertions(+), 65 deletions(-) diff --git a/package-lock.json b/package-lock.json index cdbef42d1edac1b2d93f30c5b2cb4cd630ddcdfa..70a846c85b8e451398418aed0427a32bbc10d0fe 100644 GIT binary patch delta 2270 zcmc(fS!@$$5XZIq?IwYg98hxL#EzXpseqdXv$9)c)Rw}+5Gy`-ppptO&blD1Hwl?c74s!DY2gsT#%sG#-B zdS`cLcE0)lf0r*-9ynTc`YKrx9g3uU3B~$4#V%IG;~Das<@bhETE#l7Ub1p(8ugbV z@fn`DT21iy^v?_&SdWU9=L@Kn>iIOKSOq3uCgk=ekD77o+cSx7CZQSV3Yr_a_HMl{ z=IJpvdSd~9FRO761=5j3+8g(VT4L&8IK3?y7}7H?-l_>`jIG-py{!p-+};*q9l=3U zyWZ}H5uRl5Twx(Lqhk0Ff z$$Cj^f44!`-Mc4gH8izxR*TQV^$vM@n9hh3%MVpeDfufV`0fT#fwyD|q39@jAJ4f; zaPi(BsiCAN2IUg6nqV<|LcP+H3uMGD^M90#N~2=o{02fQ@wCK4=5&_vn|zJwAU`-@ z^rfCo`m7F92X_A{TNd)pAAK%G3x=IYLbEDXHH;@)pTe)r$_f{1!&?W5wXn6B!#%-tAfePgshfT`a5kVTiVse^V{|W zJDeKM+N|Y!JcdAD7|O4ZGKeNg6?7g%OX2fZ3G=O*N1fy+xgJx$?fp`Lv+I|F9~0$M zzm88Epz9b_i4VWPKbD(99KSDj#n0TVU)AIC7=nppw#}JoHXFib&fDT;tPRQ39z5AA zw%SRqkml!9tXsN7{3(*DgwGvK`rHX`FegO@&Xp1kcx<+w%GF+Ud^+Fvp-#V0^49PN z&&&#EW{E8oL|*%_^E%0v&a3^l>~YtJs~y8meZ05P(U9?E>~W8?V>s2{(X_)5GB$;V zQvt2IJ?z_Iw;6TAp@`p@X42WL#pG>?^X7gJYqxVLtEGc;*j%X{F&$@#W-W>BA+9BW z<#yRU3SKZPqhfBI-@F3y@x1z2_+SLp;N3OFu;c<&OfxF2sEhp@2`Sh@bd_wEM#bI9 zPG#agWhl8&Rww$`{U%)jFFuEs;j%F)Tq`B&ggRXC4Js^4|(l7n-jII-6ilq66cAl0;zFmVj~C=^ybpKvRl|&#pke$tT_P@$2E!|q zof5sygW+`~lPrdzkqDB)*G=@Z@cd!w3=EplWV`f3P3XPy7cnUHS!m{L??6#P%pq0wYnn_WvM>Lra$lC%>oWKK*w9sa*sh Xn-s!Jr|6psVeAV!Q7cI2=xOpVWm)to delta 228 zcmV;XL%x4i@c$mW-8W&$6VaLEPMmkZzm z7Po-y0$C`N^ARSu?-c{BB9n^>5SMUw0UfuwI0Hfvw=GKp(;T { - const usID = req.userid; + const usID = req.user_id; const chID = BigInt(req.params.channel_id); const channel = await ChannelModel.findOne({ id: chID }).exec(); @@ -33,7 +33,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) uses: 0, max_uses: req.body.max_uses, max_age: req.body.max_age, - created_at: Date.now(), + created_at: new Date(), guild_id: guID, channel_id: chID, inviter_id: usID, diff --git a/src/routes/api/v8/guilds/#id/bans.ts b/src/routes/api/v8/guilds/#id/bans.ts index aaae39cb..5133ee3c 100644 --- a/src/routes/api/v8/guilds/#id/bans.ts +++ b/src/routes/api/v8/guilds/#id/bans.ts @@ -29,14 +29,14 @@ router.get("/:user", async (req: Request, res: Response) => { return res.json(ban); }); -router.post("/:userid", check(BanCreateSchema), 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.userid); + const banned_user_id = BigInt(req.params.user_id); const banned_user = await getPublicUser(banned_user_id); - const perms = await getPermission(req.userid, guild_id); + const perms = await getPermission(req.user_id, guild_id); if (!perms.has("BAN_MEMBERS")) throw new HTTPError("You don't have the permission to ban members", 403); - if (req.userid === banned_user_id) throw new HTTPError("You can't ban yourself", 400); + if (req.user_id === banned_user_id) throw new HTTPError("You can't ban yourself", 400); await removeMember(banned_user_id, guild_id); @@ -44,7 +44,7 @@ router.post("/:userid", check(BanCreateSchema), async (req: Request, res: Respon user_id: banned_user_id, guild_id: guild_id, ip: getIpAdress(req), - executor_id: req.userid, + executor_id: req.user_id, reason: req.body.reason, // || otherwise empty }).save(); @@ -60,15 +60,15 @@ router.post("/:userid", check(BanCreateSchema), async (req: Request, res: Respon return res.json(ban).send(); }); -router.delete("/:userid", async (req: Request, res: Response) => { +router.delete("/:user_id", async (req: Request, res: Response) => { var guild_id = BigInt(req.params.id); - var banned_user_id = BigInt(req.params.userid); + var banned_user_id = BigInt(req.params.user_id); const banned_user = await getPublicUser(banned_user_id); const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); - const perms = await getPermission(req.userid, guild.id); + const perms = await getPermission(req.user_id, guild.id); if (!perms.has("BAN_MEMBERS")) { throw new HTTPError("No permissions", 403); } diff --git a/src/routes/api/v8/guilds/#id/index.ts b/src/routes/api/v8/guilds/#id/index.ts index 09f2a5c2..e86d9416 100644 --- a/src/routes/api/v8/guilds/#id/index.ts +++ b/src/routes/api/v8/guilds/#id/index.ts @@ -24,7 +24,7 @@ router.get("/", async (req: Request, res: Response) => { const guild = await GuildModel.findOne({ id: guild_id }).exec(); if (!guild) throw new HTTPError("Guild does not exist", 404); - const member = await MemberModel.findOne({ guild_id: guild_id, id: req.userid }, "id").exec(); + const member = await MemberModel.findOne({ guild_id: guild_id, id: req.user_id }, "id").exec(); if (!member) throw new HTTPError("You are not a member of the guild you are trying to access", 401); return res.json(guild); @@ -37,7 +37,7 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) const guild = await GuildModel.findOne({ id: guild_id }).exec(); if (!guild) throw new HTTPError("This guild does not exist", 404); - const perms = await getPermission(req.userid, guild_id); + const perms = await getPermission(req.user_id, guild_id); if (!perms.has("MANAGE_GUILD")) throw new HTTPError("You do not have the MANAGE_GUILD permission", 401); await GuildModel.updateOne({ id: guild_id }, body).exec(); @@ -49,7 +49,7 @@ router.delete("/", async (req: Request, res: Response) => { const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec(); if (!guild) throw new HTTPError("This guild does not exist", 404); - if (guild.owner_id !== req.userid) throw new HTTPError("You are not the owner of this guild", 401); + if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401); await emitEvent({ event: "GUILD_DELETE", diff --git a/src/routes/api/v8/guilds/index.ts b/src/routes/api/v8/guilds/index.ts index 7179ea9e..319184ad 100644 --- a/src/routes/api/v8/guilds/index.ts +++ b/src/routes/api/v8/guilds/index.ts @@ -13,7 +13,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = const body = req.body as GuildCreateSchema; const { maxGuilds } = Config.get().limits.user; - const user = await getPublicUser(req.userid, { guilds: true }); + const user = await getPublicUser(req.user_id, { guilds: true }); if (user.guilds.length >= maxGuilds) { throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403); @@ -23,7 +23,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = const guild: Guild = { name: body.name, region: body.region || "en-US", - owner_id: req.userid, + owner_id: req.user_id, icon: undefined, afk_channel_id: undefined, afk_timeout: 300, @@ -73,7 +73,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = tags: null, }).save(), ]); - await addMember(req.userid, guild_id, { guild }); + await addMember(req.user_id, guild_id, { guild }); res.status(201).json({ id: guild.id }); }); diff --git a/src/routes/api/v8/users/@me/guilds.ts b/src/routes/api/v8/users/@me/guilds.ts index 6c3b0925..a2a64ce6 100644 --- a/src/routes/api/v8/users/@me/guilds.ts +++ b/src/routes/api/v8/users/@me/guilds.ts @@ -7,7 +7,7 @@ import { getPublicUser } from "../../../../../util/User"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.userid }, { guilds: true }).exec(); + const user = await UserModel.findOne({ id: req.user_id }, { guilds: true }).exec(); if (!user) throw new HTTPError("User not found", 404); var guildIDs = user.guilds || []; @@ -21,18 +21,18 @@ router.delete("/:id", async (req: Request, res: Response) => { const guild = await GuildModel.findOne({ id: guildID }).exec(); if (!guild) throw new HTTPError("Guild doesn't exist", 404); - if (guild.owner_id === req.userid) throw new HTTPError("You can't leave your own guild", 400); + if (guild.owner_id === req.user_id) throw new HTTPError("You can't leave your own guild", 400); - await MemberModel.deleteOne({ id: req.userid, guild_id: guildID }).exec(); - await UserModel.updateOne({ id: req.userid }, { $pull: { guilds: guildID } }).exec(); - const user = await getPublicUser(req.userid); + await MemberModel.deleteOne({ id: req.user_id, guild_id: guildID }).exec(); + await UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guildID } }).exec(); + const user = await getPublicUser(req.user_id); await emitEvent({ event: "GUILD_DELETE", data: { id: guildID, }, - user_id: req.userid, + user_id: req.user_id, } as GuildDeleteEvent); await emitEvent({ diff --git a/src/routes/api/v8/users/@me/index.ts b/src/routes/api/v8/users/@me/index.ts index 4307ef76..32877dcc 100644 --- a/src/routes/api/v8/users/@me/index.ts +++ b/src/routes/api/v8/users/@me/index.ts @@ -6,7 +6,7 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { // TODO: user projection - const user = await UserModel.findOne({ id: req.userid }).exec(); + const user = await UserModel.findOne({ id: req.user_id }).exec(); if (!user) throw new HTTPError("User not found", 404); res.json(user); diff --git a/src/schema/Message.ts b/src/schema/Message.ts index 80897f9d..4ae6c136 100644 --- a/src/schema/Message.ts +++ b/src/schema/Message.ts @@ -1,27 +1,69 @@ +import { Embed, EmbedImage } from "fosscord-server-util"; +import { Length } from "../util/instanceOf"; + export const MessageCreateSchema = { - content: String, - nonce: Number, - tts: Boolean, - embed: {}, - allowed_mentions: [], - message_reference: { + $content: new Length(String, 0, 2000), + $nonce: String, + $tts: Boolean, + $embed: { + $title: new Length(String, 0, 256), //title of embed + $type: String, // type of embed (always "rich" for webhook embeds) + $description: new Length(String, 0, 2048), // description of embed + $url: String, // url of embed + $timestamp: String, // ISO8601 timestamp + $color: Number, // color code of the embed + $footer: { + text: new Length(String, 0, 2048), + icon_url: String, + proxy_icon_url: String, + }, // footer object footer information + $image: EmbedImage, // image object image information + $thumbnail: EmbedImage, // thumbnail object thumbnail information + $video: EmbedImage, // video object video information + $provider: { + name: String, + url: String, + }, // provider object provider information + $author: { + name: new Length(String, 0, 256), + url: String, + icon_url: String, + proxy_icon_url: String, + }, // author object author information + $fields: new Length( + [ + { + name: new Length(String, 0, 256), + value: new Length(String, 0, 1024), + $inline: Boolean, + }, + ], + 0, + 25 + ), + }, + $allowed_mentions: [], + $message_reference: { message_id: BigInt, channel_id: BigInt, - guild_id: BigInt, - fail_if_not_exists: Boolean, + $guild_id: BigInt, + $fail_if_not_exists: Boolean, }, + $payload_json: String, + $file: Object, }; export interface MessageCreateSchema { - content: string; - nonce: number; - tts: boolean; - embed: {}; - allowed_mentions: []; - message_reference: { + content?: string; + nonce?: string; + tts?: boolean; + embed?: Embed & { timestamp: string }; + allowed_mentions?: []; + message_reference?: { message_id: bigint; channel_id: bigint; - guild_id: bigint; + guild_id?: bigint; fail_if_not_exists: boolean; }; + payload_json?: string; } diff --git a/src/util/Constants.ts b/src/util/Constants.ts index 1b0e8dbf..f3a8dd67 100644 --- a/src/util/Constants.ts +++ b/src/util/Constants.ts @@ -32,9 +32,9 @@ export const Endpoints = { Emoji: (emojiID: string, format = "png") => `${root}/emojis/${emojiID}.${format}`, Asset: (name: string) => `${root}/assets/${name}`, DefaultAvatar: (discriminator: string) => `${root}/embed/avatars/${discriminator}.png`, - Avatar: (userID: string, hash: string, format = "webp", size: number, dynamic = false) => { + Avatar: (user_id: string, hash: string, format = "webp", size: number, dynamic = false) => { if (dynamic) format = hash.startsWith("a_") ? "gif" : format; - return makeImageUrl(`${root}/avatars/${userID}/${hash}`, { format, size }); + return makeImageUrl(`${root}/avatars/${user_id}/${hash}`, { format, size }); }, Banner: (guildID: string, hash: string, format = "webp", size: number) => makeImageUrl(`${root}/banners/${guildID}/${hash}`, { format, size }), @@ -42,27 +42,18 @@ export const Endpoints = { if (dynamic) format = hash.startsWith("a_") ? "gif" : format; return makeImageUrl(`${root}/icons/${guildID}/${hash}`, { format, size }); }, - AppIcon: ( - clientID: string, - hash: string, - { format = "webp", size }: { format?: string; size?: number } = {} - ) => makeImageUrl(`${root}/app-icons/${clientID}/${hash}`, { size, format }), - AppAsset: ( - clientID: string, - hash: string, - { format = "webp", size }: { format?: string; size?: number } = {} - ) => makeImageUrl(`${root}/app-assets/${clientID}/${hash}`, { size, format }), + AppIcon: (clientID: string, hash: string, { format = "webp", size }: { format?: string; size?: number } = {}) => + makeImageUrl(`${root}/app-icons/${clientID}/${hash}`, { size, format }), + AppAsset: (clientID: string, hash: string, { format = "webp", size }: { format?: string; size?: number } = {}) => + makeImageUrl(`${root}/app-assets/${clientID}/${hash}`, { size, format }), GDMIcon: (channelID: string, hash: string, format = "webp", size: number) => makeImageUrl(`${root}/channel-icons/${channelID}/${hash}`, { size, format }), Splash: (guildID: string, hash: string, format = "webp", size: number) => makeImageUrl(`${root}/splashes/${guildID}/${hash}`, { size, format }), DiscoverySplash: (guildID: string, hash: string, format = "webp", size: number) => makeImageUrl(`${root}/discovery-splashes/${guildID}/${hash}`, { size, format }), - TeamIcon: ( - teamID: string, - hash: string, - { format = "webp", size }: { format?: string; size?: number } = {} - ) => makeImageUrl(`${root}/team-icons/${teamID}/${hash}`, { size, format }), + TeamIcon: (teamID: string, hash: string, { format = "webp", size }: { format?: string; size?: number } = {}) => + makeImageUrl(`${root}/team-icons/${teamID}/${hash}`, { size, format }), }; }, invite: (root: string, code: string) => `${root}/${code}`, @@ -346,9 +337,7 @@ export const MessageTypes = [ * * REPLY * @typedef {string} SystemMessageType */ -export const SystemMessageTypes = MessageTypes.filter( - (type: string | null) => type && type !== "DEFAULT" && type !== "REPLY" -); +export const SystemMessageTypes = MessageTypes.filter((type: string | null) => type && type !== "DEFAULT" && type !== "REPLY"); /** * Bots cannot set a `CUSTOM_STATUS`, it is only for custom statuses received from users diff --git a/src/util/Member.ts b/src/util/Member.ts index 2df34073..319eab60 100644 --- a/src/util/Member.ts +++ b/src/util/Member.ts @@ -43,7 +43,7 @@ export async function addMember(user_id: bigint, guild_id: bigint, cache?: { gui guild_id: guild_id, nick: undefined, roles: [guild_id], // @everyone role - joined_at: Date.now(), + joined_at: new Date(), premium_since: undefined, deaf: false, mute: false, diff --git a/src/util/instanceOf.ts b/src/util/instanceOf.ts index bbb30c12..4f30bd46 100644 --- a/src/util/instanceOf.ts +++ b/src/util/instanceOf.ts @@ -51,7 +51,7 @@ export class Length { constructor(public type: any, public min: number, public max: number) {} check(value: string) { - if (typeof value === "string") return value.length >= this.min && value.length <= this.max; + if (typeof value === "string" || Array.isArray(value)) return value.length >= this.min && value.length <= this.max; if (typeof value === "number" || typeof value === "bigint") return value >= this.min && value <= this.max; return false; }