From 2a952755028789506f35e33a9f7477e8531c5382 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 7 Apr 2021 20:25:37 +0200 Subject: [PATCH] :zap: use mongoose autopopulate --- .../channels/#channel_id/messages/index.ts | 24 +++---------- src/routes/guilds/{#id => #guild_id}/bans.ts | 14 ++++---- .../guilds/{#id => #guild_id}/channels.ts | 4 +-- src/routes/guilds/{#id => #guild_id}/index.ts | 36 ++++++++++++------- .../guilds/{#id => #guild_id}/members.ts | 7 ++-- src/routes/users/@me/guilds.ts | 35 ++++++++++-------- src/util/Event.ts | 1 + src/util/Member.ts | 1 + 8 files changed, 63 insertions(+), 59 deletions(-) rename src/routes/guilds/{#id => #guild_id}/bans.ts (89%) rename src/routes/guilds/{#id => #guild_id}/channels.ts (96%) rename src/routes/guilds/{#id => #guild_id}/index.ts (65%) rename src/routes/guilds/{#id => #guild_id}/members.ts (89%) diff --git a/src/routes/channels/#channel_id/messages/index.ts b/src/routes/channels/#channel_id/messages/index.ts index 99f5fdcd..4978b98a 100644 --- a/src/routes/channels/#channel_id/messages/index.ts +++ b/src/routes/channels/#channel_id/messages/index.ts @@ -17,6 +17,7 @@ import { PublicUserProjection } from "../../../../util/User"; import multer from "multer"; import { emitEvent } from "../../../../util/Event"; import { Query } from "mongoose"; +import { PublicMemberProjection } from "../../../../util/Member"; const router: Router = Router(); export default router; @@ -80,14 +81,7 @@ router.get("/", async (req, res) => { query = MessageModel.find({ channel_id }).sort({ id: -1 }); } - const messages = await query - .limit(limit) - .populate({ path: "author", select: PublicUserProjection }) - .populate({ path: "mentions", select: PublicUserProjection }) - .populate({ path: "mention_channels", select: { id: true, guild_id: true, type: true, name: true } }) - .populate("mention_roles") - // .populate({ path: "member", select: PublicMemberProjection }) - .exec(); + const messages = await query.limit(limit).exec(); return res.json(toObject(messages)); }); @@ -147,19 +141,11 @@ router.post("/", check(MessageCreateSchema), async (req, res) => { type: 0, tts: body.tts, nonce: body.nonce, + pinned: false, }; - const doc = new MessageModel(message); - await doc.save(); - - const data = toObject( - await MessageModel.populate(doc, [ - { path: "author", select: PublicUserProjection }, - { path: "mentions", select: PublicUserProjection }, - { path: "mention_roles" }, - { path: "mention_channels", select: { id: true, guild_id: true, type: true, name: true } }, - ]) - ); + const doc = await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save(); + const data = toObject(doc); await emitEvent({ event: "MESSAGE_CREATE", channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent); diff --git a/src/routes/guilds/#id/bans.ts b/src/routes/guilds/#guild_id/bans.ts similarity index 89% rename from src/routes/guilds/#id/bans.ts rename to src/routes/guilds/#guild_id/bans.ts index 5ac92154..3de80a32 100644 --- a/src/routes/guilds/#id/bans.ts +++ b/src/routes/guilds/#guild_id/bans.ts @@ -13,7 +13,7 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const guild_id = req.params.id; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await GuildModel.exists({ id: guild_id }); if (!guild) throw new HTTPError("Guild not found", 404); var bans = await BanModel.find({ guild_id: guild_id }).exec(); @@ -61,30 +61,30 @@ router.post("/:user_id", check(BanCreateSchema), async (req: Request, res: Respo }); router.delete("/:user_id", async (req: Request, res: Response) => { - var guild_id = req.params.id; + var { guild_id } = req.params; 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(); + const guild = await GuildModel.exists({ id: guild_id }); if (!guild) throw new HTTPError("Guild not found", 404); - const perms = await getPermission(req.user_id, guild.id); + const perms = await getPermission(req.user_id, guild_id); if (!perms.has("BAN_MEMBERS")) { throw new HTTPError("No permissions", 403); } await BanModel.deleteOne({ user_id: banned_user_id, - guild_id: guild.id, + guild_id, }).exec(); await emitEvent({ event: "GUILD_BAN_REMOVE", data: { - guild_id: guild.id, + guild_id, user: banned_user, }, - guild_id: guild.id, + guild_id, } as GuildBanRemoveEvent); return res.status(204).send(); diff --git a/src/routes/guilds/#id/channels.ts b/src/routes/guilds/#guild_id/channels.ts similarity index 96% rename from src/routes/guilds/#id/channels.ts rename to src/routes/guilds/#guild_id/channels.ts index b949def0..e0d8f3ac 100644 --- a/src/routes/guilds/#id/channels.ts +++ b/src/routes/guilds/#guild_id/channels.ts @@ -7,14 +7,14 @@ import { check } from "../../../util/instanceOf"; const router = Router(); router.get("/", async (req, res) => { - const guild_id = req.params.id; + const { guild_id } = req.params; const channels = await ChannelModel.find({ guild_id }).exec(); res.json(toObject(channels)); }); router.post("/", check(ChannelModifySchema), async (req, res) => { - const guild_id = req.params.id; + const { guild_id } = req.params; 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/#guild_id/index.ts similarity index 65% rename from src/routes/guilds/#id/index.ts rename to src/routes/guilds/#guild_id/index.ts index 385904f6..7e5f49d3 100644 --- a/src/routes/guilds/#id/index.ts +++ b/src/routes/guilds/#guild_id/index.ts @@ -5,26 +5,31 @@ import { getPermission, GuildDeleteEvent, GuildModel, + GuildUpdateEvent, InviteModel, MemberModel, MessageModel, RoleModel, + toObject, UserModel, } from "fosscord-server-util"; import { HTTPError } from "lambert-server"; import { GuildUpdateSchema } from "../../../schema/Guild"; import { emitEvent } from "../../../util/Event"; import { check } from "../../../util/instanceOf"; +import "missing-native-js-functions"; const router = Router(); router.get("/", async (req: Request, res: Response) => { - const guild_id = req.params.id; + const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await GuildModel.findOne({ id: guild_id }) + .populate({ path: "joined_at", match: { id: req.user_id } }) + .exec(); if (!guild) throw new HTTPError("Guild does not exist", 404); - const member = await MemberModel.findOne({ guild_id: guild_id, id: req.user_id }, "id").exec(); + const member = await MemberModel.exists({ guild_id: guild_id, id: req.user_id }); if (!member) throw new HTTPError("You are not a member of the guild you are trying to access", 401); return res.json(guild); @@ -32,20 +37,27 @@ 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 = req.params.id; - - const guild = await GuildModel.findOne({ id: guild_id }).exec(); - if (!guild) throw new HTTPError("This guild does not exist", 404); + const { guild_id } = req.params; + // TODO: guild update check image 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(); - return res.status(204); + const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body) + .populate({ path: "joined_at", match: { id: req.user_id } }) + .exec(); + + const data = toObject(guild); + + emitEvent({ event: "GUILD_UPDATE", data: data, guild_id } as GuildUpdateEvent); + + return res.send(data); }); -router.delete("/", async (req: Request, res: Response) => { - var guild_id = req.params.id; +// discord prefixes this route with /delete instead of using the delete method +// docs are wrong https://discord.com/developers/docs/resources/guild#delete-guild +router.post("/delete", async (req: Request, res: Response) => { + var { guild_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec(); if (!guild) throw new HTTPError("This guild does not exist", 404); @@ -67,7 +79,7 @@ router.delete("/", async (req: Request, res: Response) => { await InviteModel.deleteMany({ guild_id }).exec(); await MessageModel.deleteMany({ guild_id }).exec(); - return res.status(204).send(); + return res.sendStatus(204); }); export default router; diff --git a/src/routes/guilds/#id/members.ts b/src/routes/guilds/#guild_id/members.ts similarity index 89% rename from src/routes/guilds/#id/members.ts rename to src/routes/guilds/#guild_id/members.ts index 1c3cb33d..f4e6d4e8 100644 --- a/src/routes/guilds/#id/members.ts +++ b/src/routes/guilds/#guild_id/members.ts @@ -10,7 +10,7 @@ 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 = req.params.id; + const { guild_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); @@ -31,17 +31,16 @@ router.get("/", async (req: Request, res: Response) => { var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection) .limit(limit) - .populate({ path: "user", select: PublicUserProjection }) .exec(); return res.json(toObject(members)); }); router.get("/:member", async (req: Request, res: Response) => { - const guild_id = req.params.id; + const { guild_id } = req.params; const user_id = req.params.member; - const member = await MemberModel.findOne({ id: user_id, guild_id }).populate({ path: "user", select: PublicUserProjection }).exec(); + const member = await MemberModel.findOne({ id: user_id, guild_id }).exec(); if (!member) throw new HTTPError("Member not found", 404); return res.json(member); diff --git a/src/routes/users/@me/guilds.ts b/src/routes/users/@me/guilds.ts index e04107ac..d0fbaa3e 100644 --- a/src/routes/users/@me/guilds.ts +++ b/src/routes/users/@me/guilds.ts @@ -11,37 +11,42 @@ router.get("/", async (req: Request, res: Response) => { if (!user) throw new HTTPError("User not found", 404); var guildIDs = user.guilds || []; - var guild = await GuildModel.find({ id: { $in: guildIDs } }).exec(); + var guild = await GuildModel.find({ id: { $in: guildIDs } }) + .populate({ path: "joined_at", match: { id: req.user_id } }) + .exec(); + res.json(toObject(guild)); }); // user send to leave a certain guild router.delete("/:id", async (req: Request, res: Response) => { - const guildID = req.params.id; - const guild = await GuildModel.findOne({ id: guildID }).exec(); + const guild_id = req.params.id; + const guild = await GuildModel.findOne({ id: guild_id }, { guild_id: true }).exec(); if (!guild) throw new HTTPError("Guild doesn't exist", 404); if (guild.owner_id === req.user_id) throw new HTTPError("You can't leave your own guild", 400); - 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 Promise.all([ + MemberModel.deleteOne({ id: req.user_id, guild_id: guild_id }).exec(), + UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guild_id } }).exec(), + emitEvent({ + event: "GUILD_DELETE", + data: { + id: guild_id, + }, + user_id: req.user_id, + } as GuildDeleteEvent), + ]); - await emitEvent({ - event: "GUILD_DELETE", - data: { - id: guildID, - }, - user_id: req.user_id, - } as GuildDeleteEvent); + const user = await getPublicUser(req.user_id); await emitEvent({ event: "GUILD_MEMBER_REMOVE", data: { - guild_id: guildID, + guild_id: guild_id, user: user, }, - guild_id: guildID, + guild_id: guild_id, } as GuildMemberRemoveEvent); return res.status(204).send(); diff --git a/src/util/Event.ts b/src/util/Event.ts index 43c51d5c..8a24e4bb 100644 --- a/src/util/Event.ts +++ b/src/util/Event.ts @@ -5,6 +5,7 @@ export async function emitEvent(payload: Omit) { created_at: new Date(), // in seconds ...payload, }; + // TODO: bigint isn't working return await new EventModel(obj).save(); } diff --git a/src/util/Member.ts b/src/util/Member.ts index 3ce06962..4d1b8ac5 100644 --- a/src/util/Member.ts +++ b/src/util/Member.ts @@ -75,6 +75,7 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui }, guild_id: guild_id, } as GuildMemberAddEvent), + emitEvent({ event: "GUILD_CREATE", data: guild,