mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-13 06:02:39 +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:
commit
35893c62bc
@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"tabWidth": 4,
|
"tabWidth": 4,
|
||||||
"useTabs": true,
|
"useTabs": true,
|
||||||
"printWidth": 140
|
"printWidth": 140,
|
||||||
|
"trailingComma": "none"
|
||||||
}
|
}
|
||||||
|
@ -24,16 +24,18 @@ router.delete("/", async (req, res) => {
|
|||||||
const { channel_id } = req.params;
|
const { channel_id } = req.params;
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
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 });
|
const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel });
|
||||||
permission.hasThrow("MANAGE_CHANNELS");
|
permission.hasThrow("MANAGE_CHANNELS");
|
||||||
|
|
||||||
await ChannelModel.deleteOne({ id: channel_id });
|
|
||||||
|
|
||||||
// TODO: Dm channel "close" not delete
|
// TODO: Dm channel "close" not delete
|
||||||
const data = toObject(channel);
|
const data = toObject(channel);
|
||||||
|
|
||||||
await emitEvent({ event: "CHANNEL_DELETE", data, guild_id: channel?.guild_id, channel_id } as ChannelDeleteEvent);
|
await emitEvent({ event: "CHANNEL_DELETE", data, guild_id: channel?.guild_id, channel_id } as ChannelDeleteEvent);
|
||||||
|
|
||||||
|
await ChannelModel.deleteOne({ id: channel_id });
|
||||||
|
|
||||||
res.send(data);
|
res.send(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -47,14 +49,16 @@ router.patch("/", check(ChannelModifySchema), async (req, res) => {
|
|||||||
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec();
|
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
if (!channel) throw new HTTPError("Channel not found", 404);
|
||||||
|
|
||||||
|
const data = toObject(channel);
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
event: "CHANNEL_UPDATE",
|
event: "CHANNEL_UPDATE",
|
||||||
data: toObject(channel),
|
data,
|
||||||
guild_id: channel.guild_id,
|
guild_id: channel.guild_id,
|
||||||
channel_id,
|
channel_id
|
||||||
} as ChannelUpdateEvent);
|
} as ChannelUpdateEvent);
|
||||||
|
|
||||||
res.send(toObject(channel));
|
res.send(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
@ -16,9 +16,11 @@ router.get("/", async (req, res) => {
|
|||||||
router.post("/", check(ChannelModifySchema), async (req, res) => {
|
router.post("/", check(ChannelModifySchema), async (req, res) => {
|
||||||
const { guild_id } = req.params;
|
const { guild_id } = req.params;
|
||||||
const body = req.body as ChannelModifySchema;
|
const body = req.body as ChannelModifySchema;
|
||||||
|
|
||||||
if (!body.permission_overwrites) body.permission_overwrites = [];
|
if (!body.permission_overwrites) body.permission_overwrites = [];
|
||||||
if (!body.topic) body.topic = "";
|
if (!body.topic) body.topic = "";
|
||||||
if (!body.rate_limit_per_user) body.rate_limit_per_user = 0;
|
if (!body.rate_limit_per_user) body.rate_limit_per_user = 0;
|
||||||
|
|
||||||
switch (body.type) {
|
switch (body.type) {
|
||||||
case ChannelType.DM:
|
case ChannelType.DM:
|
||||||
case ChannelType.GROUP_DM:
|
case ChannelType.GROUP_DM:
|
||||||
@ -31,9 +33,9 @@ router.post("/", check(ChannelModifySchema), async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (body.parent_id) {
|
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) 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();
|
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
||||||
@ -43,8 +45,9 @@ router.post("/", check(ChannelModifySchema), async (req, res) => {
|
|||||||
...body,
|
...body,
|
||||||
id: Snowflake.generate(),
|
id: Snowflake.generate(),
|
||||||
created_at: new Date(),
|
created_at: new Date(),
|
||||||
guild_id,
|
guild_id
|
||||||
};
|
};
|
||||||
|
|
||||||
await new ChannelModel(channel).save();
|
await new ChannelModel(channel).save();
|
||||||
|
|
||||||
await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id } as ChannelCreateEvent);
|
await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id } as ChannelCreateEvent);
|
||||||
@ -63,7 +66,7 @@ router.patch("/", check(ChannelModifySchema), async (req, res) => {
|
|||||||
...body
|
...body
|
||||||
};
|
};
|
||||||
const channelm = await ChannelModel.find({ guild_id }).exec();
|
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();
|
await new ChannelModel(channel).save();
|
||||||
|
|
||||||
|
@ -58,10 +58,10 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
|
|||||||
welcome_screen: {
|
welcome_screen: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
description: "No description",
|
description: "No description",
|
||||||
welcome_channels: [],
|
welcome_channels: []
|
||||||
},
|
},
|
||||||
widget_channel_id: undefined,
|
widget_channel_id: undefined,
|
||||||
widget_enabled: false,
|
widget_enabled: false
|
||||||
};
|
};
|
||||||
|
|
||||||
const [guild_doc, role] = await Promise.all([
|
const [guild_doc, role] = await Promise.all([
|
||||||
@ -71,13 +71,13 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
|
|||||||
guild_id: guild_id,
|
guild_id: guild_id,
|
||||||
color: 0,
|
color: 0,
|
||||||
hoist: false,
|
hoist: false,
|
||||||
managed: true,
|
managed: false,
|
||||||
mentionable: true,
|
mentionable: false,
|
||||||
name: "@everyone",
|
name: "@everyone",
|
||||||
permissions: 2251804225n,
|
permissions: 2251804225n,
|
||||||
position: 0,
|
position: 0,
|
||||||
tags: null,
|
tags: null
|
||||||
}).save(),
|
}).save()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await addMember(req.user_id, guild_id, { guild: guild_doc });
|
await addMember(req.user_id, guild_id, { guild: guild_doc });
|
||||||
|
Loading…
Reference in New Issue
Block a user