1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 05:02:37 +01:00

Merge pull request #137 from 9qz/master

Some refactor, fix channel_delete event and @everyone role appearing on member roles
This commit is contained in:
Flam3rboy 2021-05-11 17:24:07 +02:00 committed by GitHub
commit 35893c62bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 16 deletions

View File

@ -1,5 +1,6 @@
{
"tabWidth": 4,
"useTabs": true,
"printWidth": 140
"printWidth": 140,
"trailingComma": "none"
}

View File

@ -24,16 +24,18 @@ router.delete("/", async (req, res) => {
const { channel_id } = req.params;
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
if (!channel) throw new HTTPError("Channel not found", 404);
const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel });
permission.hasThrow("MANAGE_CHANNELS");
await ChannelModel.deleteOne({ id: channel_id });
// TODO: Dm channel "close" not delete
const data = toObject(channel);
await emitEvent({ event: "CHANNEL_DELETE", data, guild_id: channel?.guild_id, channel_id } as ChannelDeleteEvent);
await ChannelModel.deleteOne({ id: channel_id });
res.send(data);
});
@ -47,14 +49,16 @@ router.patch("/", check(ChannelModifySchema), async (req, res) => {
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec();
if (!channel) throw new HTTPError("Channel not found", 404);
const data = toObject(channel);
await emitEvent({
event: "CHANNEL_UPDATE",
data: toObject(channel),
data,
guild_id: channel.guild_id,
channel_id,
channel_id
} as ChannelUpdateEvent);
res.send(toObject(channel));
res.send(data);
});
export default router;

View File

@ -16,9 +16,11 @@ router.get("/", async (req, res) => {
router.post("/", check(ChannelModifySchema), async (req, res) => {
const { guild_id } = req.params;
const body = req.body as ChannelModifySchema;
if (!body.permission_overwrites) body.permission_overwrites = [];
if (!body.topic) body.topic = "";
if (!body.rate_limit_per_user) body.rate_limit_per_user = 0;
switch (body.type) {
case ChannelType.DM:
case ChannelType.GROUP_DM:
@ -31,9 +33,9 @@ router.post("/", check(ChannelModifySchema), async (req, res) => {
}
if (body.parent_id) {
const exists = await ChannelModel.findOne({ id: body.parent_id }, {guild_id:true}).exec();
const exists = await ChannelModel.findOne({ id: body.parent_id }, { guild_id: true }).exec();
if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400);
if (exists.guild_id !== guild_id) throw new HTTPError("The category channel needs to be in the guild")
if (exists.guild_id !== guild_id) throw new HTTPError("The category channel needs to be in the guild");
}
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
@ -43,8 +45,9 @@ router.post("/", check(ChannelModifySchema), async (req, res) => {
...body,
id: Snowflake.generate(),
created_at: new Date(),
guild_id,
guild_id
};
await new ChannelModel(channel).save();
await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id } as ChannelCreateEvent);
@ -63,7 +66,7 @@ router.patch("/", check(ChannelModifySchema), async (req, res) => {
...body
};
const channelm = await ChannelModel.find({ guild_id }).exec();
if(!channelm) throw new HTTPError("Channel not found", 404);
if (!channelm) throw new HTTPError("Channel not found", 404);
await new ChannelModel(channel).save();

View File

@ -58,10 +58,10 @@ 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,
widget_enabled: false
};
const [guild_doc, role] = await Promise.all([
@ -71,13 +71,13 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
guild_id: guild_id,
color: 0,
hoist: false,
managed: true,
mentionable: true,
managed: false,
mentionable: false,
name: "@everyone",
permissions: 2251804225n,
position: 0,
tags: null,
}).save(),
tags: null
}).save()
]);
await addMember(req.user_id, guild_id, { guild: guild_doc });