diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 589b842a13..eff5a0107e 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -3838,7 +3838,8 @@ void ApiWrap::sendFiles( TextWithTags &&caption, std::shared_ptr album, const SendAction &action) { - const auto haveCaption = !caption.text.isEmpty(); + auto sharedCaption = std::move(caption); + const auto haveCaption = !sharedCaption.text.isEmpty(); const auto captionAttached = !haveCaption ? false : (list.files.size() == 1) @@ -3856,10 +3857,12 @@ void ApiWrap::sendFiles( false); if (haveCaption && !captionAttached) { auto message = MessageToSend(action); - message.textWithTags = base::take(caption); + message.textWithTags = base::take(sharedCaption); message.action.clearDraft = false; sendMessage(std::move(message)); } + auto attachSharedCaption = haveCaption && captionAttached; + auto sharedCaptionConsumed = false; const auto to = FileLoadTaskOptions(action); if (album) { @@ -3868,6 +3871,13 @@ void ApiWrap::sendFiles( auto tasks = std::vector>(); tasks.reserve(list.files.size()); for (auto &file : list.files) { + auto fileCaption = std::move(file.caption); + if (attachSharedCaption && !sharedCaptionConsumed) { + sharedCaptionConsumed = true; + if (fileCaption.text.isEmpty()) { + fileCaption = base::take(sharedCaption); + } + } const auto uploadWithType = !album ? type : (file.type == Ui::PreparedFile::Type::Photo @@ -3899,14 +3909,13 @@ void ApiWrap::sendFiles( : nullptr), .type = uploadWithType, .to = to, - .caption = caption, + .caption = std::move(fileCaption), .spoiler = file.spoiler, .album = album, .forceFile = forceFile, .idOverride = 0, .displayName = file.displayName, })); - caption = TextWithTags(); } if (album) { _sendingAlbums.emplace(album->groupId, album); diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index b658e8fd89..ccd4d0536c 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -781,6 +781,65 @@ bool SendFilesBox::setDisplayNameInSingleFilePreview( return false; } +bool SendFilesBox::setCaptionInSingleFilePreview( + int fileIndex, + const TextWithTags &caption) { + for (auto &block : _blocks) { + if (fileIndex < block.fromIndex() || fileIndex >= block.tillIndex()) { + continue; + } + return block.setSingleFileCaption(fileIndex, caption); + } + return false; +} + +bool SendFilesBox::mainCaptionWillBeAttached() const { + const auto way = _sendWay.current(); + const auto slowmode = (_limits & SendFilesAllow::OnlyOne) + && (_list.files.size() > 1); + return Ui::CaptionWillBeAttached(_list, way, slowmode); +} + +void SendFilesBox::applyMainCaptionToFirstFile() { + if (!_caption + || _caption->isHidden() + || (_list.files.size() <= 1) + || _list.files.empty()) { + if (_mainCaptionAttachedToFirstFile && !_list.files.empty()) { + _list.files.front().caption = _firstFileCaptionBackup.value_or( + TextWithTags()); + if (!setCaptionInSingleFilePreview( + 0, + _list.files.front().caption)) { + } + } + _mainCaptionAttachedToFirstFile = false; + _firstFileCaptionBackup = std::nullopt; + return; + } + if (mainCaptionWillBeAttached()) { + if (!_mainCaptionAttachedToFirstFile) { + _firstFileCaptionBackup = _list.files.front().caption; + } + auto text = _caption->getTextWithAppliedMarkdown(); + _list.files.front().caption = text; + _mainCaptionAttachedToFirstFile = true; + if (!setCaptionInSingleFilePreview( + 0, + text)) { + } + } else if (_mainCaptionAttachedToFirstFile) { + _list.files.front().caption = _firstFileCaptionBackup.value_or( + TextWithTags()); + _mainCaptionAttachedToFirstFile = false; + _firstFileCaptionBackup = std::nullopt; + if (!setCaptionInSingleFilePreview( + 0, + _list.files.front().caption)) { + } + } +} + void SendFilesBox::openDialogToAddFileToAlbum() { const auto show = uiShow(); const auto checkResult = [=](const Ui::PreparedList &list) { @@ -810,10 +869,7 @@ void SendFilesBox::openDialogToAddFileToAlbum() { } void SendFilesBox::refreshMessagesCount() { - const auto way = _sendWay.current(); - const auto slowmode = (_limits & SendFilesAllow::OnlyOne) - && (_list.files.size() > 1); - const auto withCaption = Ui::CaptionWillBeAttached(_list, way, slowmode); + const auto withCaption = mainCaptionWillBeAttached(); const auto withComment = !withCaption && _caption && !_caption->isHidden() @@ -1423,6 +1479,7 @@ void SendFilesBox::refreshControls(bool initial) { refreshTitleText(); updateSendWayControls(); updateCaptionPlaceholder(); + applyMainCaptionToFirstFile(); } void SendFilesBox::setupSendWayControls() { @@ -1613,6 +1670,7 @@ void SendFilesBox::setupCaption() { _caption->changes() ) | rpl::on_next([=] { checkCharsLimitation(); + applyMainCaptionToFirstFile(); refreshMessagesCount(); }, _caption->lifetime()); } @@ -2028,14 +2086,20 @@ void SendFilesBox::saveSendWaySettings() { } bool SendFilesBox::validateLength(const QString &text) const { + const auto way = _sendWay.current(); + if (!_list.canAddCaption( + way.groupFiles() && way.sendImagesAsPhotos(), + way.sendImagesAsPhotos())) { + return true; + } + return validateSingleCaptionLength(text); +} + +bool SendFilesBox::validateSingleCaptionLength(const QString &text) const { const auto session = &_show->session(); const auto limit = Data::PremiumLimits(session).captionLengthCurrent(); const auto remove = int(text.size()) - limit; - const auto way = _sendWay.current(); - if (remove <= 0 - || !_list.canAddCaption( - way.groupFiles() && way.sendImagesAsPhotos(), - way.sendImagesAsPhotos())) { + if (remove <= 0) { return true; } _show->showBox( @@ -2074,6 +2138,7 @@ void SendFilesBox::send( applyBlockChanges(); Storage::ApplyModifications(_list); + applyMainCaptionToFirstFile(); _confirmed = true; if (_confirmedCallback) { @@ -2083,6 +2148,11 @@ void SendFilesBox::send( if (!validateLength(caption.text)) { return; } + for (const auto &file : _list.files) { + if (!validateSingleCaptionLength(file.caption.text)) { + return; + } + } options.invertCaption = _invertCaption; options.price = hasPrice() ? _price.current() : 0; if (options.price > 0) { diff --git a/Telegram/SourceFiles/boxes/send_files_box.h b/Telegram/SourceFiles/boxes/send_files_box.h index 18d1fec84f..cf0c9490f0 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.h +++ b/Telegram/SourceFiles/boxes/send_files_box.h @@ -256,6 +256,9 @@ private: [[nodiscard]] bool setDisplayNameInSingleFilePreview( int fileIndex, const QString &displayName); + [[nodiscard]] bool setCaptionInSingleFilePreview( + int fileIndex, + const TextWithTags &caption); void enqueueNextPrepare(); void addPreparedAsyncFile(Ui::PreparedFile &&file); @@ -264,6 +267,9 @@ private: void refreshMessagesCount(); void requestToTakeTextWithTags() const; + bool validateSingleCaptionLength(const QString &text) const; + bool mainCaptionWillBeAttached() const; + void applyMainCaptionToFirstFile(); [[nodiscard]] Fn prepareSendMenuDetails( const SendFilesBoxDescriptor &descriptor); @@ -294,6 +300,8 @@ private: QImage _priceTagBg; bool _confirmed = false; bool _invertCaption = false; + bool _mainCaptionAttachedToFirstFile = false; + std::optional _firstFileCaptionBackup; object_ptr _caption = { nullptr }; std::unique_ptr _autocomplete;