diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index b0b6f27c5..2a6aef27f 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -4898,37 +4898,13 @@ void ApiWrap::sendAlbumWithUploaded( const auto albumIt = _sendingAlbums.find(groupId.raw()); Assert(albumIt != _sendingAlbums.end()); const auto &album = albumIt->second; - - const auto proj = [](const SendingAlbum::Item &item) { - return item.msgId; - }; - const auto itemIt = ranges::find(album->items, localId, proj); - Assert(itemIt != album->items.end()); - Assert(!itemIt->media); - - auto caption = item->originalText(); - TextUtilities::Trim(caption); - auto sentEntities = TextUtilities::EntitiesToMTP( - caption.entities, - TextUtilities::ConvertOption::SkipLocal); - const auto flags = !sentEntities.v.isEmpty() - ? MTPDinputSingleMedia::Flag::f_entities - : MTPDinputSingleMedia::Flag(0); - - itemIt->media = MTP_inputSingleMedia( - MTP_flags(flags), - media, - MTP_long(randomId), - MTP_string(caption.text), - sentEntities); - + album->fillMedia(item, media, randomId); sendAlbumIfReady(album.get()); } void ApiWrap::sendAlbumWithCancelled( not_null item, const MessageGroupId &groupId) { - const auto localId = item->fullId(); const auto albumIt = _sendingAlbums.find(groupId.raw()); if (albumIt == _sendingAlbums.end()) { // Sometimes we destroy item being sent already after the album @@ -4940,14 +4916,7 @@ void ApiWrap::sendAlbumWithCancelled( return; } const auto &album = albumIt->second; - - const auto proj = [](const SendingAlbum::Item &item) { - return item.msgId; - }; - const auto itemIt = ranges::find(album->items, localId, proj); - Assert(itemIt != album->items.end()); - album->items.erase(itemIt); - + album->removeItem(item); sendAlbumIfReady(album.get()); } diff --git a/Telegram/SourceFiles/boxes/create_poll_box.cpp b/Telegram/SourceFiles/boxes/create_poll_box.cpp index 57b1dc565..9d4546e31 100644 --- a/Telegram/SourceFiles/boxes/create_poll_box.cpp +++ b/Telegram/SourceFiles/boxes/create_poll_box.cpp @@ -564,7 +564,6 @@ int Options::findField(not_null field) const { &Option::field) - begin(_list); Ensures(result >= 0 && result < _list.size()); - return result; } diff --git a/Telegram/SourceFiles/data/data_auto_download.cpp b/Telegram/SourceFiles/data/data_auto_download.cpp index 31efcc02d..e5ce477bf 100644 --- a/Telegram/SourceFiles/data/data_auto_download.cpp +++ b/Telegram/SourceFiles/data/data_auto_download.cpp @@ -173,7 +173,6 @@ const Set &Full::setOrDefault(Source source, Type type) const { const auto &result = my.hasValue(type) ? my : Defaults().set(source); Ensures(result.hasValue(type)); - return result; } diff --git a/Telegram/SourceFiles/storage/localimageloader.cpp b/Telegram/SourceFiles/storage/localimageloader.cpp index 0ae5a4669..d6cf48586 100644 --- a/Telegram/SourceFiles/storage/localimageloader.cpp +++ b/Telegram/SourceFiles/storage/localimageloader.cpp @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/file_utilities.h" #include "core/mime_type.h" #include "media/media_audio.h" +#include "history/history_item.h" #include "boxes/send_files_box.h" #include "media/media_clip_reader.h" #include "mainwidget.h" @@ -24,6 +25,40 @@ namespace { constexpr auto kThumbnailQuality = 87; +auto FindAlbumItem( + std::vector &items, + not_null item) { + const auto result = ranges::find( + items, + item->fullId(), + &SendingAlbum::Item::msgId); + + Ensures(result != end(items)); + return result; +} + +MTPInputSingleMedia PrepareAlbumItemMedia( + not_null item, + const MTPInputMedia &media, + uint64 randomId) { + auto caption = item->originalText(); + TextUtilities::Trim(caption); + auto sentEntities = TextUtilities::EntitiesToMTP( + caption.entities, + TextUtilities::ConvertOption::SkipLocal); + const auto flags = !sentEntities.v.isEmpty() + ? MTPDinputSingleMedia::Flag::f_entities + : MTPDinputSingleMedia::Flag(0); + + return MTP_inputSingleMedia( + MTP_flags(flags), + media, + MTP_long(randomId), + MTP_string(caption.text), + sentEntities); +} + + } // namespace using Storage::ValidateThumbDimensions; @@ -241,6 +276,44 @@ void TaskQueueWorker::onTaskAdded() { SendingAlbum::SendingAlbum() : groupId(rand_value()) { } +void SendingAlbum::fillMedia( + not_null item, + const MTPInputMedia &media, + uint64 randomId) { + const auto i = FindAlbumItem(items, item); + Assert(!i->media); + + i->media = PrepareAlbumItemMedia(item, media, randomId); +} + +void SendingAlbum::refreshMediaCaption(not_null item) { + const auto i = FindAlbumItem(items, item); + if (!i->media) { + return; + } + i->media = i->media->match([&](const MTPDinputSingleMedia &data) { + return PrepareAlbumItemMedia(item, data.vmedia, data.vrandom_id.v); + }); +} + +void SendingAlbum::removeItem(not_null item) { + const auto localId = item->fullId(); + const auto i = ranges::find(items, localId, &Item::msgId); + const auto moveCaption = (items.size() > 1) && (i == begin(items)); + Assert(i != end(items)); + items.erase(i); + if (moveCaption) { + const auto caption = item->originalText(); + const auto firstId = items.front().msgId; + if (const auto first = App::histItemById(firstId)) { + // We don't need to finishEdition() here, because the whole + // album will be rebuilt after one item was removed from it. + first->setText(caption); + refreshMediaCaption(first); + } + } +} + FileLoadResult::FileLoadResult( TaskId taskId, uint64 id, diff --git a/Telegram/SourceFiles/storage/localimageloader.h b/Telegram/SourceFiles/storage/localimageloader.h index 3c5db0a24..e1106dd99 100644 --- a/Telegram/SourceFiles/storage/localimageloader.h +++ b/Telegram/SourceFiles/storage/localimageloader.h @@ -163,6 +163,7 @@ struct SendingAlbum { struct Item { explicit Item(TaskId taskId) : taskId(taskId) { } + TaskId taskId; FullMsgId msgId; std::optional media; @@ -170,6 +171,13 @@ struct SendingAlbum { SendingAlbum(); + void fillMedia( + not_null item, + const MTPInputMedia &media, + uint64 randomId); + void refreshMediaCaption(not_null item); + void removeItem(not_null item); + uint64 groupId = 0; std::vector items; bool silent = false;