improve performance *a lot*

This commit is contained in:
lumap 2025-02-05 03:22:11 +01:00
parent c586664aaf
commit 100d609793

View file

@ -21,7 +21,7 @@ export default definePlugin({
const currChannel = ChannelStore.getChannel(SelectedChannelStore.getChannelId()); const currChannel = ChannelStore.getChannel(SelectedChannelStore.getChannelId());
if (currChannel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, currChannel)) return null; if (currChannel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, currChannel)) return null;
if (![0,19].includes(msg.type) || msg.hasFlag(8192)) return null; if (![0, 19].includes(msg.type) || msg.hasFlag(8192)) return null;
return { return {
label: "Add attachments", label: "Add attachments",
@ -47,9 +47,9 @@ export default definePlugin({
return input.remove(); return input.remove();
} }
showToast("Uploading..."); showToast("Uploading, this can take a while...");
async function uploadLoop(file: File) { const uploadPromises = Array.from(input.files).map(async file => {
const attachmentsReq = (await Common.RestAPI.post({ const attachmentsReq = (await Common.RestAPI.post({
url: `/channels/${channelId}/attachments`, url: `/channels/${channelId}/attachments`,
body: { body: {
@ -69,6 +69,15 @@ export default definePlugin({
body: file body: file
}); });
return {
id: attachmentsReq.id,
uploaded_filename: attachmentsReq.upload_filename,
filename: file.name
};
});
const newAttachments = await Promise.all(uploadPromises);
const msg = MessageStore.getMessage(channelId, messageId); const msg = MessageStore.getMessage(channelId, messageId);
await Common.RestAPI.patch({ await Common.RestAPI.patch({
@ -76,19 +85,10 @@ export default definePlugin({
body: { body: {
attachments: [ attachments: [
...msg.attachments, ...msg.attachments,
{ ...newAttachments
id: attachmentsReq.id,
uploaded_filename: attachmentsReq.upload_filename,
filename: file.name
}
] ]
} }
}); });
}
for (let i = 0; i < input.files.length; i++) {
await uploadLoop(input.files[i]);
}
input.remove(); input.remove();
}); });