1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-14 06:32:36 +01:00

Merge pull request #181 from luth31/master

[Channel] Implement type 4 channel and clean the code
This commit is contained in:
Flam3rboy 2021-08-01 22:56:16 +02:00 committed by GitHub
commit efe6a2d88a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,11 +13,21 @@ import { emitEvent } from "./Event";
// TODO: DM channel // TODO: DM channel
export async function createChannel(channel: Partial<TextChannel | VoiceChannel>, user_id: string = "0") { export async function createChannel(channel: Partial<TextChannel | VoiceChannel>, user_id: string = "0") {
if (!channel.permission_overwrites) channel.permission_overwrites = [];
// Always check if user has permission first
const permissions = await getPermission(user_id, channel.guild_id);
permissions.hasThrow("MANAGE_CHANNELS");
switch (channel.type) { switch (channel.type) {
case ChannelType.GUILD_TEXT: case ChannelType.GUILD_TEXT:
case ChannelType.GUILD_VOICE: case ChannelType.GUILD_VOICE:
if (channel.parent_id) {
const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec();
if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400);
if (exists.guild_id !== channel.guild_id) throw new HTTPError("The category channel needs to be in the guild");
}
break;
case ChannelType.GUILD_CATEGORY:
break; break;
case ChannelType.DM: case ChannelType.DM:
case ChannelType.GROUP_DM: case ChannelType.GROUP_DM:
@ -29,15 +39,7 @@ export async function createChannel(channel: Partial<TextChannel | VoiceChannel>
throw new HTTPError("Not yet supported"); throw new HTTPError("Not yet supported");
} }
const permissions = await getPermission(user_id, channel.guild_id); if (!channel.permission_overwrites) channel.permission_overwrites = [];
permissions.hasThrow("MANAGE_CHANNELS");
if (channel.parent_id) {
const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec();
if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400);
if (exists.guild_id !== channel.guild_id) throw new HTTPError("The category channel needs to be in the guild");
}
// TODO: auto generate position // TODO: auto generate position
channel = await new ChannelModel({ channel = await new ChannelModel({