Skip to content

Commit

Permalink
🐛 fix: add pinned stories chunking for files over 50mb
Browse files Browse the repository at this point in the history
  • Loading branch information
chupapee committed Jun 12, 2024
1 parent da17129 commit 6e12b84
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/controllers/download-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof mapStories>;

export async function downloadStories(
stories: ReturnType<StoriesModel>,
stories: StoriesModel,
storiesType: 'active' | 'pinned'
) {
const client = await Userbot.getInstance();
Expand Down
22 changes: 13 additions & 9 deletions src/controllers/send-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}))
);
}
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { StoriesModel } from 'controllers/download-stories';

const MAX_STORIES_SIZE = 45;

export const timeout = (sec: number): Promise<null> =>
new Promise((ok) => setTimeout(ok, sec));

export function chunkMediafiles(files: ReturnType<StoriesModel>) {
export function chunkMediafiles(files: StoriesModel) {
return files.reduce(
(acc: Array<ReturnType<StoriesModel>>, curr) => {
(acc: Array<StoriesModel>, 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;
}
Expand Down

0 comments on commit 6e12b84

Please sign in to comment.