refactor(line): share rich menu user batching

This commit is contained in:
Peter Steinberger
2026-02-18 18:30:23 +00:00
parent 4f36c813a7
commit 2789eb7512

View File

@@ -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<void> {
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<void> {
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,
});