From 6e12b84412e41ad094e8631b21a863ed852b2ffc Mon Sep 17 00:00:00 2001 From: chupapee Date: Wed, 12 Jun 2024 17:35:19 +0500 Subject: [PATCH] :bug: fix: add pinned stories chunking for files over 50mb --- src/controllers/download-stories.ts | 4 ++-- src/controllers/send-stories.ts | 22 +++++++++++++--------- src/lib/helpers.ts | 11 ++++++++--- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/controllers/download-stories.ts b/src/controllers/download-stories.ts index 6185c41..ab2dea7 100644 --- a/src/controllers/download-stories.ts +++ b/src/controllers/download-stories.ts @@ -2,10 +2,10 @@ import { Userbot } from 'config/userbot'; import { timeout } from 'lib'; import { Api } from 'telegram'; -export type StoriesModel = typeof mapStories; +export type StoriesModel = ReturnType; export async function downloadStories( - stories: ReturnType, + stories: StoriesModel, storiesType: 'active' | 'pinned' ) { const client = await Userbot.getInstance(); diff --git a/src/controllers/send-stories.ts b/src/controllers/send-stories.ts index aa42d24..f31e6f8 100644 --- a/src/controllers/send-stories.ts +++ b/src/controllers/send-stories.ts @@ -86,7 +86,7 @@ async function sendActiveStories({ stories, task }: SendStoriesArgs) { album.map((x) => ({ media: { source: x.buffer! }, type: x.mediaType, - caption: 'Active stories', + caption: x.caption ?? 'Active stories', })) ); } @@ -172,14 +172,18 @@ async function sendPinnedStories({ stories, task }: SendStoriesArgs) { .catch(() => null); if (uploadableStories.length > 0) { - await bot.telegram.sendMediaGroup( - task.chatId, - uploadableStories.map((x) => ({ - media: { source: x.buffer! }, - type: x.mediaType, - caption: 'Pinned stories', - })) - ); + const chunkedList = chunkMediafiles(uploadableStories); + + for (const album of chunkedList) { + await bot.telegram.sendMediaGroup( + task.chatId, + album.map((x) => ({ + media: { source: x.buffer! }, + type: x.mediaType, + caption: x.caption ?? 'Pinned stories', + })) + ); + } } else { await bot.telegram.sendMessage( task.chatId, diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index a64137b..957326b 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -1,13 +1,18 @@ import { StoriesModel } from 'controllers/download-stories'; +const MAX_STORIES_SIZE = 45; + export const timeout = (sec: number): Promise => new Promise((ok) => setTimeout(ok, sec)); -export function chunkMediafiles(files: ReturnType) { +export function chunkMediafiles(files: StoriesModel) { return files.reduce( - (acc: Array>, curr) => { + (acc: Array, curr) => { const tempAccWithCurr = [...acc[acc.length - 1], curr]; - if (tempAccWithCurr.length === 10 || sumOfSizes(tempAccWithCurr) >= 50) { + if ( + tempAccWithCurr.length === 10 || + sumOfSizes(tempAccWithCurr) >= MAX_STORIES_SIZE + ) { acc.push([curr]); return acc; }