summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authornotsapinho <rafaelalibibi@gmail.com>2021-05-10 10:42:33 -0300
committernotsapinho <rafaelalibibi@gmail.com>2021-05-10 10:42:33 -0300
commitf9eba44d10b91bff0ab513bc5d952f437dab6d5c (patch)
tree96c8c38bd30baea263179e565abfb51f1e71194d /src
parent:bug: fix channel modify schema (diff)
downloadserver-f9eba44d10b91bff0ab513bc5d952f437dab6d5c.tar.xz
🐛 Fix channels and joined_at not getting populated
Diffstat (limited to 'src')
-rw-r--r--src/routes/guilds/index.ts9
-rw-r--r--src/routes/guilds/templates/index.ts5
-rw-r--r--src/util/Member.ts24
3 files changed, 24 insertions, 14 deletions
diff --git a/src/routes/guilds/index.ts b/src/routes/guilds/index.ts
index a655f890..176e4e69 100644
--- a/src/routes/guilds/index.ts
+++ b/src/routes/guilds/index.ts
@@ -1,5 +1,5 @@
 import { Router, Request, Response } from "express";
-import { RoleModel, GuildModel, Snowflake, Guild } from "@fosscord/server-util";
+import { RoleModel, GuildModel, Snowflake, Guild, RoleDocument } from "@fosscord/server-util";
 import { HTTPError } from "lambert-server";
 import { check } from "./../../util/instanceOf";
 import { GuildCreateSchema } from "../../schema/Guild";
@@ -58,13 +58,13 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
 		welcome_screen: {
 			enabled: false,
 			description: "No description",
-			welcome_channels: []
+			welcome_channels: [],
 		},
 		widget_channel_id: undefined,
 		widget_enabled: false,
 	};
 
-	await Promise.all([
+	const [guild_doc, role] = await Promise.all([
 		new GuildModel(guild).save(),
 		new RoleModel({
 			id: guild_id,
@@ -79,7 +79,8 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
 			tags: null,
 		}).save(),
 	]);
-	await addMember(req.user_id, guild_id, { guild });
+
+	await addMember(req.user_id, guild_id, { guild: guild_doc });
 
 	res.status(201).json({ id: guild.id });
 });
diff --git a/src/routes/guilds/templates/index.ts b/src/routes/guilds/templates/index.ts
index 2d8cdf0e..7e32e94c 100644
--- a/src/routes/guilds/templates/index.ts
+++ b/src/routes/guilds/templates/index.ts
@@ -40,7 +40,7 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res
 		owner_id: req.user_id,
 	};
 
-	await Promise.all([
+	const [guild_doc, role] = await Promise.all([
 		new GuildModel(guild).save(),
 		new RoleModel({
 			id: guild_id,
@@ -55,7 +55,8 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res
 			tags: null,
 		}).save(),
 	]);
-	await addMember(req.user_id, guild_id, { guild });
+
+	await addMember(req.user_id, guild_id, { guild: guild_doc });
 
 	res.status(201).json({ id: guild.id });
 });
diff --git a/src/util/Member.ts b/src/util/Member.ts
index 3d43dec5..afbaf3aa 100644
--- a/src/util/Member.ts
+++ b/src/util/Member.ts
@@ -10,7 +10,9 @@ import {
 	RoleModel,
 	toObject,
 	UserModel,
+	GuildDocument,
 } from "@fosscord/server-util";
+
 import { HTTPError } from "lambert-server";
 import Config from "./Config";
 import { emitEvent } from "./Event";
@@ -34,7 +36,7 @@ export async function isMember(user_id: string, guild_id: string) {
 	return exists;
 }
 
-export async function addMember(user_id: string, guild_id: string, cache?: { guild?: Guild }) {
+export async function addMember(user_id: string, guild_id: string, cache?: { guild?: GuildDocument }) {
 	const user = await getPublicUser(user_id, { guilds: true });
 
 	const { maxGuilds } = Config.get().limits.user;
@@ -43,6 +45,7 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui
 	}
 
 	const guild = cache?.guild || (await GuildModel.findOne({ id: guild_id }).exec());
+
 	if (!guild) throw new HTTPError("Guild not found", 404);
 
 	if (await MemberModel.exists({ id: user.id, guild_id })) throw new HTTPError("You are already a member of this guild", 400);
@@ -59,7 +62,7 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui
 		pending: false,
 	};
 
-	return Promise.all([
+	Promise.all([
 		new MemberModel({
 			...member,
 			settings: {
@@ -86,13 +89,18 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui
 			},
 			guild_id: guild_id,
 		} as GuildMemberAddEvent),
-
-		emitEvent({
-			event: "GUILD_CREATE",
-			data: toObject(guild),
-			user_id,
-		} as GuildCreateEvent),
 	]);
+
+	await emitEvent({
+		event: "GUILD_CREATE",
+		data: toObject(
+			await guild
+				.populate({ path: "members", match: { guild_id } })
+				.populate({ path: "joined_at", match: { id: user.id } })
+				.execPopulate()
+		),
+		user_id,
+	} as GuildCreateEvent);
 }
 
 export async function removeMember(user_id: string, guild_id: string) {