summary refs log tree commit diff
path: root/api/src/routes/users/@me/guilds.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/routes/users/@me/guilds.ts')
-rw-r--r--api/src/routes/users/@me/guilds.ts19
1 files changed, 7 insertions, 12 deletions
diff --git a/api/src/routes/users/@me/guilds.ts b/api/src/routes/users/@me/guilds.ts
index e40bfec9..fb88281b 100644
--- a/api/src/routes/users/@me/guilds.ts
+++ b/api/src/routes/users/@me/guilds.ts
@@ -1,31 +1,26 @@
 import { Router, Request, Response } from "express";
-import { Guild, Member, User, GuildDeleteEvent, GuildMemberRemoveEvent, toObject, emitEvent } from "@fosscord/util";
+import { Guild, Member, User, GuildDeleteEvent, GuildMemberRemoveEvent, emitEvent } from "@fosscord/util";
 import { HTTPError } from "lambert-server";
-
-import { getPublicUser } from "../../../util/User";
+import { In } from "typeorm";
 
 const router: Router = Router();
 
 router.get("/", async (req: Request, res: Response) => {
-	const user = await User.findOneOrFail({ id: req.user_id }, { guilds: true });
-	if (!user) throw new HTTPError("User not found", 404);
+	const members = await Member.find({ relations: ["guild"], where: { id: req.user_id } });
 
-	var guildIDs = user.guilds || [];
-	var guild = await Guild.find({ id: { $in: guildIDs } }).populate({ path: "joined_at", match: { id: req.user_id } });
-	res.json(guild);
+	res.json(members.map((x) => x.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 Guild.findOneOrFail({ id: guild_id }, { guild_id: true });
+	const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: ["owner_id"] });
 
 	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([
-		Member.deleteOne({ id: req.user_id, guild_id: guild_id }),
-		User.update({ id: req.user_id }, { $pull: { guilds: guild_id } }),
+		Member.delete({ id: req.user_id, guild_id: guild_id }),
 		emitEvent({
 			event: "GUILD_DELETE",
 			data: {
@@ -35,7 +30,7 @@ router.delete("/:id", async (req: Request, res: Response) => {
 		} as GuildDeleteEvent)
 	]);
 
-	const user = await getPublicUser(req.user_id);
+	const user = await User.getPublicUser(req.user_id);
 
 	await emitEvent({
 		event: "GUILD_MEMBER_REMOVE",