From 00e7381a03e96bdd1eaab3c9273e88d622d8f3b1 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sun, 22 Jan 2023 19:16:27 +0100 Subject: [PATCH] Voice Messages: Fix sending (#2326) --- src/components/middle/composer/Composer.tsx | 46 ++++++++++++++----- .../composer/helpers/buildAttachment.ts | 2 +- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/components/middle/composer/Composer.tsx b/src/components/middle/composer/Composer.tsx index 0b9d6fda5..e34f059f3 100644 --- a/src/components/middle/composer/Composer.tsx +++ b/src/components/middle/composer/Composer.tsx @@ -665,15 +665,25 @@ const Composer: FC = ({ return true; }, [isAdmin, lang, showDialog, slowMode]); - const handleSendAttachments = useCallback(( - sendCompressed: boolean, sendGrouped: boolean, isSilent?: boolean, scheduledAt?: number, - ) => { + const sendAttachments = useCallback(({ + attachments: attachmentsToSend, + sendCompressed, + sendGrouped, + isSilent, + scheduledAt, + } : { + attachments: ApiAttachment[]; + sendCompressed?: boolean; + sendGrouped?: boolean; + isSilent?: boolean; + scheduledAt?: number; + }) => { if (connectionState !== 'connectionStateReady') { return; } const { text, entities } = parseMessageInput(htmlRef.current!); - if (!text && !attachments.length) { + if (!text && !attachmentsToSend.length) { return; } if (!validateTextLength(text, true)) return; @@ -685,7 +695,7 @@ const Composer: FC = ({ scheduledAt, isSilent, shouldUpdateStickerSetsOrder: true, - attachments: prepareAttachmentsToSend(attachments, sendCompressed), + attachments: prepareAttachmentsToSend(attachmentsToSend, sendCompressed), shouldGroupMessages: sendGrouped, }); @@ -697,10 +707,22 @@ const Composer: FC = ({ requestAnimationFrame(() => { resetComposer(); }); - }, [ - attachments, chatId, checkSlowMode, clearDraft, htmlRef, resetComposer, sendMessage, validateTextLength, - connectionState, - ]); + }, [chatId, checkSlowMode, clearDraft, htmlRef, resetComposer, sendMessage, validateTextLength, connectionState]); + + const handleSendAttachments = useCallback(( + sendCompressed: boolean, + sendGrouped: boolean, + isSilent?: boolean, + scheduledAt?: number, + ) => { + sendAttachments({ + attachments, + sendCompressed, + sendGrouped, + isSilent, + scheduledAt, + }); + }, [attachments, sendAttachments]); const handleSend = useCallback(async (isSilent = false, scheduledAt?: number) => { if (connectionState !== 'connectionStateReady') { @@ -724,7 +746,9 @@ const Composer: FC = ({ const { text, entities } = parseMessageInput(htmlRef.current!); if (currentAttachments.length) { - handleSendAttachments(false, false); + sendAttachments({ + attachments: currentAttachments, + }); return; } @@ -769,7 +793,7 @@ const Composer: FC = ({ }); }, [ connectionState, attachments, activeVoiceRecording, htmlRef, isForwarding, validateTextLength, clearDraft, - chatId, stopRecordingVoice, handleSendAttachments, checkSlowMode, sendMessage, forwardMessages, resetComposer, + chatId, stopRecordingVoice, sendAttachments, checkSlowMode, sendMessage, forwardMessages, resetComposer, ]); const handleClickBotMenu = useCallback(() => { diff --git a/src/components/middle/composer/helpers/buildAttachment.ts b/src/components/middle/composer/helpers/buildAttachment.ts index b3da7c0de..49a52a0fa 100644 --- a/src/components/middle/composer/helpers/buildAttachment.ts +++ b/src/components/middle/composer/helpers/buildAttachment.ts @@ -77,6 +77,6 @@ export default async function buildAttachment( export function prepareAttachmentsToSend(attachments: ApiAttachment[], shouldSendCompressed?: boolean) { return !shouldSendCompressed - ? attachments.map((attachment) => ({ ...attachment, shouldSendAsFile: true })) + ? attachments.map((attachment) => ({ ...attachment, shouldSendAsFile: !attachment.voice ? true : undefined })) : attachments; }