From cdc2d91d43cabd02f0cdb5f1a9ad586b8a3584d4 Mon Sep 17 00:00:00 2001 From: diondiondion Date: Thu, 4 Jun 2026 15:18:53 +0200 Subject: [PATCH] [Glitch] Fix ValidationError when loading many collections at once Port 2b6b2fcb6ff7f0c066260c0aa3f14f34e4bc3588 to glitch-soc Signed-off-by: Claire --- .../glitch/reducers/slices/collections.ts | 13 +++++++++---- .../flavours/glitch/utils/batch_array.ts | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 app/javascript/flavours/glitch/utils/batch_array.ts diff --git a/app/javascript/flavours/glitch/reducers/slices/collections.ts b/app/javascript/flavours/glitch/reducers/slices/collections.ts index 78b0db862d..57d57ef657 100644 --- a/app/javascript/flavours/glitch/reducers/slices/collections.ts +++ b/app/javascript/flavours/glitch/reducers/slices/collections.ts @@ -27,6 +27,7 @@ import { createAppSelector, createDataLoadingThunk, } from '@/flavours/glitch/store/typed_functions'; +import { batchArray } from '@/flavours/glitch/utils/batch_array'; import { inputToHashtag } from '@/flavours/glitch/utils/hashtags'; type QueryStatus = 'idle' | 'loading' | 'error'; @@ -337,10 +338,14 @@ async function importAccountsForPreviewCard( ) .filter((id): id is string => !!id); - await dispatch( - fetchAccounts({ - accountIds: previewAccountIds, - }), + // fetchAccounts can only process up to 40 item ids, so we'll + // batch the list of ids + const batchedAccountIdLists = batchArray(previewAccountIds, 40); + + await Promise.allSettled( + batchedAccountIdLists.map((accountIds) => + dispatch(fetchAccounts({ accountIds })), + ), ); } diff --git a/app/javascript/flavours/glitch/utils/batch_array.ts b/app/javascript/flavours/glitch/utils/batch_array.ts new file mode 100644 index 0000000000..9e54895d33 --- /dev/null +++ b/app/javascript/flavours/glitch/utils/batch_array.ts @@ -0,0 +1,15 @@ +/** + * Splits a long array so that the resulting nested arrays + * never exceed the `maxLength` provided. + * Useful when dealing with endpoints that accept a limited number + * of parameters + */ +export function batchArray(array: T[], maxLength: number) { + const result: T[][] = []; + + for (let i = 0; i < array.length; i += maxLength) { + result.push(array.slice(i, i + maxLength)); + } + + return result; +}