diff --git a/src/line/rich-menu.ts b/src/line/rich-menu.ts index c693778ddfc..6fa391de5ba 100644 --- a/src/line/rich-menu.ts +++ b/src/line/rich-menu.ts @@ -10,6 +10,7 @@ type RichMenuRequest = messagingApi.RichMenuRequest; type RichMenuResponse = messagingApi.RichMenuResponse; type RichMenuArea = messagingApi.RichMenuArea; type Action = messagingApi.Action; +const USER_BATCH_SIZE = 500; export interface RichMenuSize { width: 2500; @@ -66,6 +67,14 @@ function getBlobClient(opts: RichMenuOpts = {}): messagingApi.MessagingApiBlobCl }); } +function chunkUserIds(userIds: string[]): string[][] { + const batches: string[][] = []; + for (let i = 0; i < userIds.length; i += USER_BATCH_SIZE) { + batches.push(userIds.slice(i, i + USER_BATCH_SIZE)); + } + return batches; +} + /** * Create a new rich menu * @returns The rich menu ID @@ -187,13 +196,7 @@ export async function linkRichMenuToUsers( ): Promise { const client = getClient(opts); - // LINE allows max 500 users per request - const batches = []; - for (let i = 0; i < userIds.length; i += 500) { - batches.push(userIds.slice(i, i + 500)); - } - - for (const batch of batches) { + for (const batch of chunkUserIds(userIds)) { await client.linkRichMenuIdToUsers({ richMenuId, userIds: batch, @@ -230,13 +233,7 @@ export async function unlinkRichMenuFromUsers( ): Promise { const client = getClient(opts); - // LINE allows max 500 users per request - const batches = []; - for (let i = 0; i < userIds.length; i += 500) { - batches.push(userIds.slice(i, i + 500)); - } - - for (const batch of batches) { + for (const batch of chunkUserIds(userIds)) { await client.unlinkRichMenuIdFromUsers({ userIds: batch, });