diff options
Diffstat (limited to 'api/src/routes/users/@me/guilds.ts')
-rw-r--r-- | api/src/routes/users/@me/guilds.ts | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/api/src/routes/users/@me/guilds.ts b/api/src/routes/users/@me/guilds.ts index a9b53b75..e40bfec9 100644 --- a/api/src/routes/users/@me/guilds.ts +++ b/api/src/routes/users/@me/guilds.ts @@ -1,5 +1,5 @@ import { Router, Request, Response } from "express"; -import { GuildModel, MemberModel, UserModel, GuildDeleteEvent, GuildMemberRemoveEvent, toObject, emitEvent } from "@fosscord/util"; +import { Guild, Member, User, GuildDeleteEvent, GuildMemberRemoveEvent, toObject, emitEvent } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { getPublicUser } from "../../../util/User"; @@ -7,28 +7,25 @@ import { getPublicUser } from "../../../util/User"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }, { guilds: true }).exec(); + const user = await User.findOneOrFail({ id: req.user_id }, { guilds: true }); if (!user) throw new HTTPError("User not found", 404); var guildIDs = user.guilds || []; - var guild = await GuildModel.find({ id: { $in: guildIDs } }) - .populate({ path: "joined_at", match: { id: req.user_id } }) - .exec(); - - res.json(toObject(guild)); + var guild = await Guild.find({ id: { $in: guildIDs } }).populate({ path: "joined_at", match: { id: req.user_id } }); + res.json(guild); }); // user send to leave a certain guild router.delete("/:id", async (req: Request, res: Response) => { const guild_id = req.params.id; - const guild = await GuildModel.findOne({ id: guild_id }, { guild_id: true }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }, { guild_id: true }); 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 Promise.all([ - MemberModel.deleteOne({ id: req.user_id, guild_id: guild_id }).exec(), - UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guild_id } }).exec(), + Member.deleteOne({ id: req.user_id, guild_id: guild_id }), + User.update({ id: req.user_id }, { $pull: { guilds: guild_id } }), emitEvent({ event: "GUILD_DELETE", data: { |