code relies on things done in a certain order

This makes sure that the code gets done in the order that is expected
This commit is contained in:
MathMan05 2025-05-01 22:50:01 -05:00 committed by Rory&
parent 4f6211ce50
commit e219c1a33a

View File

@ -140,65 +140,61 @@ router.patch(
const withParents = body.filter((x) => x.parent_id !== undefined); const withParents = body.filter((x) => x.parent_id !== undefined);
const withPositions = body.filter((x) => x.position !== undefined); const withPositions = body.filter((x) => x.position !== undefined);
// You can't do it with Promise.all or the way this is being done is super incorrect
for await (const opt of withPositions) {
const channel = await Channel.findOneOrFail({
where: { id: opt.id },
});
await Promise.all( channel.position = opt.position as number;
withPositions.map(async (opt) => { notMentioned.splice(opt.position as number, 0, channel.id);
const channel = await Channel.findOneOrFail({
where: { id: opt.id },
});
channel.position = opt.position as number;
notMentioned.splice(opt.position as number, 0, channel.id);
await emitEvent({
event: "CHANNEL_UPDATE",
data: channel,
channel_id: channel.id,
guild_id,
} as ChannelUpdateEvent);
}),
);
await emitEvent({
event: "CHANNEL_UPDATE",
data: channel,
channel_id: channel.id,
guild_id,
} as ChannelUpdateEvent);
}
// Due to this also being able to change the order, this needs to be done in order
// have to do the parents after the positions // have to do the parents after the positions
await Promise.all( for await (const opt of withParents) {
withParents.map(async (opt) => { const [channel, parent] = await Promise.all([
const [channel, parent] = await Promise.all([ Channel.findOneOrFail({
Channel.findOneOrFail({ where: { id: opt.id },
where: { id: opt.id }, }),
}), opt.parent_id
opt.parent_id ? Channel.findOneOrFail({
? Channel.findOneOrFail({ where: { id: opt.parent_id },
where: { id: opt.parent_id }, select: {
select: { permission_overwrites: true,
permission_overwrites: true, id: true,
id: true, },
}, })
}) : null,
: null, ]);
]);
if (opt.lock_permissions && parent) if (opt.lock_permissions && parent)
await Channel.update( await Channel.update(
{ id: channel.id }, { id: channel.id },
{ permission_overwrites: parent.permission_overwrites }, { permission_overwrites: parent.permission_overwrites },
); );
if (parent && opt.position === undefined) { if (parent && opt.position === undefined) {
const parentPos = notMentioned.indexOf(parent.id); const parentPos = notMentioned.indexOf(parent.id);
notMentioned.splice(parentPos + 1, 0, channel.id); notMentioned.splice(parentPos + 1, 0, channel.id);
channel.position = (parentPos + 1) as number; channel.position = (parentPos + 1) as number;
} }
channel.parent = parent || undefined; channel.parent = parent || undefined;
channel.parent_id = parent?.id || null; channel.parent_id = parent?.id || null;
await channel.save(); await channel.save();
await emitEvent({ await emitEvent({
event: "CHANNEL_UPDATE", event: "CHANNEL_UPDATE",
data: channel, data: channel,
channel_id: channel.id, channel_id: channel.id,
guild_id, guild_id,
} as ChannelUpdateEvent); } as ChannelUpdateEvent);
}), }
);
await Guild.update( await Guild.update(
{ id: guild_id }, { id: guild_id },