mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 13:17:39 +00:00
refactor(line): share rich menu user batching
This commit is contained in:
@@ -10,6 +10,7 @@ type RichMenuRequest = messagingApi.RichMenuRequest;
|
|||||||
type RichMenuResponse = messagingApi.RichMenuResponse;
|
type RichMenuResponse = messagingApi.RichMenuResponse;
|
||||||
type RichMenuArea = messagingApi.RichMenuArea;
|
type RichMenuArea = messagingApi.RichMenuArea;
|
||||||
type Action = messagingApi.Action;
|
type Action = messagingApi.Action;
|
||||||
|
const USER_BATCH_SIZE = 500;
|
||||||
|
|
||||||
export interface RichMenuSize {
|
export interface RichMenuSize {
|
||||||
width: 2500;
|
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
|
* Create a new rich menu
|
||||||
* @returns The rich menu ID
|
* @returns The rich menu ID
|
||||||
@@ -187,13 +196,7 @@ export async function linkRichMenuToUsers(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const client = getClient(opts);
|
const client = getClient(opts);
|
||||||
|
|
||||||
// LINE allows max 500 users per request
|
for (const batch of chunkUserIds(userIds)) {
|
||||||
const batches = [];
|
|
||||||
for (let i = 0; i < userIds.length; i += 500) {
|
|
||||||
batches.push(userIds.slice(i, i + 500));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const batch of batches) {
|
|
||||||
await client.linkRichMenuIdToUsers({
|
await client.linkRichMenuIdToUsers({
|
||||||
richMenuId,
|
richMenuId,
|
||||||
userIds: batch,
|
userIds: batch,
|
||||||
@@ -230,13 +233,7 @@ export async function unlinkRichMenuFromUsers(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const client = getClient(opts);
|
const client = getClient(opts);
|
||||||
|
|
||||||
// LINE allows max 500 users per request
|
for (const batch of chunkUserIds(userIds)) {
|
||||||
const batches = [];
|
|
||||||
for (let i = 0; i < userIds.length; i += 500) {
|
|
||||||
batches.push(userIds.slice(i, i + 500));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const batch of batches) {
|
|
||||||
await client.unlinkRichMenuIdFromUsers({
|
await client.unlinkRichMenuIdFromUsers({
|
||||||
userIds: batch,
|
userIds: batch,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user