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

🐛 fix guild channel position update

This commit is contained in:
Flam3rboy 2021-08-11 01:25:09 +02:00
parent b4e973c3ca
commit 99cb4c048b

View File

@ -7,7 +7,8 @@ import {
Snowflake, Snowflake,
toObject, toObject,
ChannelUpdateEvent, ChannelUpdateEvent,
AnyChannel AnyChannel,
getPermission
} from "@fosscord/server-util"; } from "@fosscord/server-util";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { ChannelModifySchema } from "../../../schema/Channel"; import { ChannelModifySchema } from "../../../schema/Channel";
@ -25,7 +26,9 @@ router.get("/", async (req: Request, res: Response) => {
// TODO: check if channel type is permitted // TODO: check if channel type is permitted
// TODO: check if parent_id exists // TODO: check if parent_id exists
router.post("/", check(ChannelModifySchema), async (req: Request, res: Response) => { router.post("/", check(ChannelModifySchema), async (req: Request, res: Response) => {
// creates a new guild channel https://discord.com/developers/docs/resources/guild#create-guild-channel
const { guild_id } = req.params; const { guild_id } = req.params;
const body = req.body as ChannelModifySchema; const body = req.body as ChannelModifySchema;
@ -35,16 +38,36 @@ router.post("/", check(ChannelModifySchema), async (req: Request, res: Response)
}); });
// TODO: check if parent_id exists // TODO: check if parent_id exists
router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response) => { router.patch(
const { guild_id } = req.params; "/",
const body = req.body as ChannelModifySchema; check({ id: String, $position: Number, $lock_permissions: Boolean, $parent_id: String }),
async (req: Request, res: Response) => {
// changes guild channel position
const { guild_id } = req.params;
const body = req.body as { id: string; position?: number; lock_permissions?: boolean; parent_id?: string };
body.position = Math.floor(body.position || 0);
if (!body.position && !body.parent_id) throw new HTTPError(`You need to at least specify position or parent_id`, 400);
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); const permission = await getPermission(req.user_id, guild_id);
const channel = await ChannelModel.findOneAndUpdate({ guild_id }, body).exec(); permission.hasThrow("MANAGE_CHANNELS");
await emitEvent({ event: "CHANNEL_UPDATE", data: channel } as ChannelUpdateEvent); const opts: any = {};
if (body.position) opts.position = body.position;
res.json(toObject(channel)); if (body.parent_id) {
}); opts.parent_id = body.parent_id;
const parent_channel = await ChannelModel.findOne({ id: body.parent_id, guild_id }, { permission_overwrites: true }).exec();
if (body.lock_permissions) {
opts.permission_overwrites = parent_channel.permission_overwrites;
}
}
const channel = await ChannelModel.findOneAndUpdate({ id: req.body, guild_id }, opts).exec();
await emitEvent({ event: "CHANNEL_UPDATE", data: channel, channel_id: body.id } as ChannelUpdateEvent);
res.json(toObject(channel));
}
);
export default router; export default router;