From 22f210ea8e599b8b29b2b3df1b8c14b3ff9a6561 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 18 Jul 2019 18:09:50 +0200 Subject: [PATCH] Support sending albums to slowmode groups. --- Telegram/SourceFiles/boxes/send_files_box.cpp | 8 ++- .../SourceFiles/history/history_message.cpp | 58 ++++++++++++------- .../SourceFiles/history/history_message.h | 8 ++- .../SourceFiles/history/history_widget.cpp | 26 ++++----- Telegram/SourceFiles/mainwidget.cpp | 9 ++- 5 files changed, 66 insertions(+), 43 deletions(-) diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index b0946a52a..1894b1e02 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -1483,6 +1483,11 @@ void SendFilesBox::prepare() { void SendFilesBox::initSendWay() { refreshAlbumMediaCount(); const auto value = [&] { + if (_sendLimit == SendLimit::One + && _list.albumIsPossible + && _list.files.size() > 1) { + return SendFilesWay::Album; + } if (_compressConfirm == CompressConfirm::None) { return SendFilesWay::Files; } else if (_compressConfirm == CompressConfirm::No) { @@ -1571,7 +1576,8 @@ void SendFilesBox::setupSendWayControls() { _sendAlbum.destroy(); _sendPhotos.destroy(); _sendFiles.destroy(); - if (_compressConfirm == CompressConfirm::None) { + if (_compressConfirm == CompressConfirm::None + || _sendLimit == SendLimit::One) { return; } const auto addRadio = [&]( diff --git a/Telegram/SourceFiles/history/history_message.cpp b/Telegram/SourceFiles/history/history_message.cpp index 417dce174..001e3531c 100644 --- a/Telegram/SourceFiles/history/history_message.cpp +++ b/Telegram/SourceFiles/history/history_message.cpp @@ -111,7 +111,8 @@ bool HasInlineItems(const HistoryItemsList &items) { QString GetErrorTextForForward( not_null peer, const HistoryItemsList &items, - const TextWithTags &comment) { + const TextWithTags &comment, + bool ignoreSlowmodeCountdown) { if (!peer->canWrite()) { return tr::lng_forward_cant(tr::now); } @@ -131,30 +132,42 @@ QString GetErrorTextForForward( return *error; } - if (peer->slowmodeApplied() && !comment.text.isEmpty()) { - return tr::lng_slowmode_no_many(tr::now); - } else if (peer->slowmodeApplied() && items.size() > 1) { - const auto albumForward = [&] { - if (const auto groupId = items.front()->groupId()) { - for (const auto item : items) { - if (item->groupId() != groupId) { - return false; - } - } - return true; + if (peer->slowmodeApplied()) { + if (const auto history = peer->owner().historyLoaded(peer)) { + if (!ignoreSlowmodeCountdown + && (history->latestSendingMessage() != nullptr) + && (!items.empty() || !comment.text.isEmpty())) { + return tr::lng_slowmode_no_many(tr::now); } - return false; - }(); - if (!albumForward) { + } + if (comment.text.size() > MaxMessageSize) { return tr::lng_slowmode_no_many(tr::now); + } else if (!items.empty() && !comment.text.isEmpty()) { + return tr::lng_slowmode_no_many(tr::now); + } else if (items.size() > 1) { + const auto albumForward = [&] { + if (const auto groupId = items.front()->groupId()) { + for (const auto item : items) { + if (item->groupId() != groupId) { + return false; + } + } + return true; + } + return false; + }(); + if (!albumForward) { + return tr::lng_slowmode_no_many(tr::now); + } } } - if (const auto left = peer->slowmodeSecondsLeft()) { - return tr::lng_slowmode_enabled( - tr::now, - lt_left, - formatDurationWords(left)); + if (!ignoreSlowmodeCountdown) { + return tr::lng_slowmode_enabled( + tr::now, + lt_left, + formatDurationWords(left)); + } } return QString(); @@ -327,8 +340,9 @@ MTPDmessage::Flags NewMessageFlags(not_null peer) { QString GetErrorTextForForward( not_null peer, - const HistoryItemsList &items) { - return GetErrorTextForForward(peer, items, TextWithTags()); + const HistoryItemsList &items, + bool ignoreSlowmodeCountdown) { + return GetErrorTextForForward(peer, items, {}, ignoreSlowmodeCountdown); } struct HistoryMessage::CreateConfig { diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index 0ab283c42..5a1fd8faf 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -20,7 +20,13 @@ Fn HistoryDependentItemCallback( MTPDmessage::Flags NewMessageFlags(not_null peer); QString GetErrorTextForForward( not_null peer, - const HistoryItemsList &items); + const HistoryItemsList &items, + bool ignoreSlowmodeCountdown = false); +QString GetErrorTextForForward( + not_null peer, + const HistoryItemsList &items, + const TextWithTags &comment, + bool ignoreSlowmodeCountdown = false); void FastShareMessage(not_null item); class HistoryMessage diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 109502d6a..edd4f2e3e 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -2836,14 +2836,13 @@ void HistoryWidget::send(Qt::KeyboardModifiers modifiers) { message.webPageId = webPageId; message.handleSupportSwitch = Support::HandleSwitch(modifiers); - if (_peer->slowmodeApplied()) { - if (_toForward.size() > 1 - || (!_toForward.empty() - && !message.textWithTags.text.isEmpty()) - || (message.textWithTags.text.size() > MaxMessageSize)) { - ShowErrorToast(tr::lng_slowmode_no_many(tr::now)); - return; - } + const auto error = GetErrorTextForForward( + _peer, + _toForward, + message.textWithTags); + if (!error.isEmpty()) { + ShowErrorToast(error); + return; } session().api().sendMessage(std::move(message)); @@ -3055,10 +3054,7 @@ void HistoryWidget::chooseAttach() { + cImgExtensions().join(qsl(" *")) + qsl(")"); - const auto method = _peer->slowmodeApplied() - ? &FileDialog::GetOpenPath - : &FileDialog::GetOpenPaths; - method(this, tr::lng_choose_files(tr::now), filter, crl::guard(this, [=]( + FileDialog::GetOpenPaths(this, tr::lng_choose_files(tr::now), filter, crl::guard(this, [=]( FileDialog::OpenResult &&result) { if (result.paths.isEmpty() && result.remoteContent.isEmpty()) { return; @@ -4000,7 +3996,9 @@ bool HistoryWidget::showSendingFilesError( } else if (!canWriteMessage()) { return tr::lng_forward_send_files_cant(tr::now); } - if (list.files.size() > 1 && _peer->slowmodeApplied()) { + if (list.files.size() > 1 + && _peer->slowmodeApplied() + && !list.albumIsPossible) { return tr::lng_slowmode_no_many(tr::now); } else if (const auto left = _peer->slowmodeSecondsLeft()) { return tr::lng_slowmode_enabled( @@ -4222,7 +4220,7 @@ void HistoryWidget::uploadFilesAfterConfirmation( const auto isAlbum = (album != nullptr); const auto compressImages = (type == SendMediaType::Photo); if (_peer->slowmodeApplied() - && (list.files.size() > 1 + && ((list.files.size() > 1 && !album) || (!list.files.empty() && !caption.text.isEmpty() && !list.canAddCaption(isAlbum, compressImages)))) { diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index de451843c..26f19fd4b 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -597,7 +597,8 @@ bool MainWidget::setForwardDraft(PeerId peerId, MessageIdsList &&items) { const auto peer = session().data().peer(peerId); const auto error = GetErrorTextForForward( peer, - session().data().idsToItems(items)); + session().data().idsToItems(items), + true); if (!error.isEmpty()) { Ui::show(Box(error), LayerOption::KeepOther); return false; @@ -681,10 +682,8 @@ void MainWidget::cancelForwarding(not_null history) { void MainWidget::finishForwarding(not_null history) { auto toForward = history->validateForwardDraft(); if (!toForward.empty()) { - if (history->peer->slowmodeSecondsLeft() - || (history->peer->slowmodeApplied() - && (toForward.size() > 1 - || history->latestSendingMessage() != nullptr))) { + const auto error = GetErrorTextForForward(history->peer, toForward); + if (!error.isEmpty()) { return; }