1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-07 19:32:34 +01:00

Merge pull request #136 from notsapinho/master

🐛 Fix channels and joined_at not getting populated
This commit is contained in:
Flam3rboy 2021-05-10 15:49:57 +02:00 committed by GitHub
commit 87bbf11843
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 14 deletions

View File

@ -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 });
});

View File

@ -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 });
});

View File

@ -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([
await 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) {