From d4bb62d0555fd894f669717379295d0355901730 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 9 Mar 2021 12:11:40 +0300 Subject: [PATCH] Slightly refactored uploading of edit media. --- Telegram/SourceFiles/api/api_common.h | 1 + Telegram/SourceFiles/api/api_sending.cpp | 13 +++++++------ Telegram/SourceFiles/api/api_sending.h | 3 +-- Telegram/SourceFiles/apiwrap.cpp | 13 +++++++------ Telegram/SourceFiles/apiwrap.h | 3 +-- Telegram/SourceFiles/boxes/edit_caption_box.cpp | 4 ++-- .../chat_helpers/stickers_dice_pack.cpp | 2 +- .../passport/passport_form_controller.cpp | 2 +- Telegram/SourceFiles/storage/file_upload.cpp | 2 +- .../SourceFiles/storage/localimageloader.cpp | 17 ++++------------- Telegram/SourceFiles/storage/localimageloader.h | 16 +++++++++------- 11 files changed, 35 insertions(+), 41 deletions(-) diff --git a/Telegram/SourceFiles/api/api_common.h b/Telegram/SourceFiles/api/api_common.h index beb73081f..63ee24691 100644 --- a/Telegram/SourceFiles/api/api_common.h +++ b/Telegram/SourceFiles/api/api_common.h @@ -33,6 +33,7 @@ struct SendAction { MsgId replyTo = 0; bool clearDraft = true; bool generateLocal = true; + MsgId replaceMediaOf = 0; }; struct MessageToSend { diff --git a/Telegram/SourceFiles/api/api_sending.cpp b/Telegram/SourceFiles/api/api_sending.cpp index 48e6440bb..cc4120df3 100644 --- a/Telegram/SourceFiles/api/api_sending.cpp +++ b/Telegram/SourceFiles/api/api_sending.cpp @@ -344,13 +344,15 @@ void FillMessagePostFlags( void SendConfirmedFile( not_null session, - const std::shared_ptr &file, - const std::optional &oldId) { - const auto isEditing = oldId.has_value(); + const std::shared_ptr &file) { + const auto isEditing = file->to.replaceMediaOf != 0; const auto channelId = peerToChannel(file->to.peer); - const auto newId = oldId.value_or( - FullMsgId(channelId, session->data().nextLocalMessageId())); + const auto newId = FullMsgId( + channelId, + isEditing + ? file->to.replaceMediaOf + : session->data().nextLocalMessageId()); auto groupId = file->album ? file->album->groupId : uint64(0); if (file->album) { const auto proj = [](const SendingAlbum::Item &item) { @@ -361,7 +363,6 @@ void SendConfirmedFile( it->msgId = newId; } - file->edit = isEditing; session->uploader().upload(newId, file); const auto itemToEdit = isEditing diff --git a/Telegram/SourceFiles/api/api_sending.h b/Telegram/SourceFiles/api/api_sending.h index 29184faea..b0187e325 100644 --- a/Telegram/SourceFiles/api/api_sending.h +++ b/Telegram/SourceFiles/api/api_sending.h @@ -34,7 +34,6 @@ void FillMessagePostFlags( void SendConfirmedFile( not_null session, - const std::shared_ptr &file, - const std::optional &oldId); + const std::shared_ptr &file); } // namespace Api diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 6f58543db..1122531bc 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -3834,8 +3834,7 @@ void ApiWrap::editMedia( Ui::PreparedList &&list, SendMediaType type, TextWithTags &&caption, - const SendAction &action, - MsgId msgIdToEdit) { + const SendAction &action) { if (list.files.empty()) return; auto &file = list.files.front(); @@ -3847,9 +3846,7 @@ void ApiWrap::editMedia( std::move(file.information), type, to, - caption, - nullptr, - msgIdToEdit)); + caption)); } void ApiWrap::sendFiles( @@ -4490,7 +4487,11 @@ void ApiWrap::sendAlbumIfReady(not_null album) { FileLoadTo ApiWrap::fileLoadTaskOptions(const SendAction &action) const { const auto peer = action.history->peer; - return FileLoadTo(peer->id, action.options, action.replyTo); + return FileLoadTo( + peer->id, + action.options, + action.replyTo, + action.replaceMediaOf); } void ApiWrap::uploadPeerPhoto(not_null peer, QImage &&image) { diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 05d19025a..4d59c1c35 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -394,8 +394,7 @@ public: Ui::PreparedList &&list, SendMediaType type, TextWithTags &&caption, - const SendAction &action, - MsgId msgIdToEdit); + const SendAction &action); void sendUploadedPhoto( FullMsgId localId, diff --git a/Telegram/SourceFiles/boxes/edit_caption_box.cpp b/Telegram/SourceFiles/boxes/edit_caption_box.cpp index 2a4734399..1a517ca21 100644 --- a/Telegram/SourceFiles/boxes/edit_caption_box.cpp +++ b/Telegram/SourceFiles/boxes/edit_caption_box.cpp @@ -1045,13 +1045,13 @@ void EditCaptionBox::save() { if (!_preparedList.files.empty()) { auto action = Api::SendAction(item->history()); action.options = options; + action.replaceMediaOf = item->fullId().msg; _controller->session().api().editMedia( std::move(_preparedList), (!_asFile && _photo) ? SendMediaType::Photo : SendMediaType::File, _field->getTextWithAppliedMarkdown(), - action, - item->fullId().msg); + action); closeBox(); return; } diff --git a/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.cpp b/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.cpp index 4d607e650..9e4605dcb 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_dice_pack.cpp @@ -125,7 +125,7 @@ void DicePack::generateLocal(int index, const QString &name) { QByteArray(), nullptr, SendMediaType::File, - FileLoadTo(0, {}, 0), + FileLoadTo(0, {}, 0, 0), {}); task.process({ .generateGoodThumbnail = false }); const auto result = task.peekResult(); diff --git a/Telegram/SourceFiles/passport/passport_form_controller.cpp b/Telegram/SourceFiles/passport/passport_form_controller.cpp index 30516bd12..4e9beb873 100644 --- a/Telegram/SourceFiles/passport/passport_form_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_form_controller.cpp @@ -1535,7 +1535,7 @@ void FormController::uploadEncryptedFile( auto prepared = std::make_shared( TaskId(), file.uploadData->fileId, - FileLoadTo(PeerId(0), Api::SendOptions(), MsgId(0)), + FileLoadTo(PeerId(0), Api::SendOptions(), MsgId(0), MsgId(0)), TextWithTags(), std::shared_ptr(nullptr)); prepared->type = SendMediaType::Secure; diff --git a/Telegram/SourceFiles/storage/file_upload.cpp b/Telegram/SourceFiles/storage/file_upload.cpp index 4f63f172d..d1afc7064 100644 --- a/Telegram/SourceFiles/storage/file_upload.cpp +++ b/Telegram/SourceFiles/storage/file_upload.cpp @@ -469,7 +469,7 @@ void Uploader::sendNext() { ? uploadingData.file->to.options : Api::SendOptions(); const auto edit = uploadingData.file && - uploadingData.file->edit; + uploadingData.file->to.replaceMediaOf; if (uploadingData.type() == SendMediaType::Photo) { auto photoFilename = uploadingData.filename(); if (!photoFilename.endsWith(qstr(".jpg"), Qt::CaseInsensitive)) { diff --git a/Telegram/SourceFiles/storage/localimageloader.cpp b/Telegram/SourceFiles/storage/localimageloader.cpp index d64bb5761..8a88e656b 100644 --- a/Telegram/SourceFiles/storage/localimageloader.cpp +++ b/Telegram/SourceFiles/storage/localimageloader.cpp @@ -494,8 +494,7 @@ FileLoadTask::FileLoadTask( SendMediaType type, const FileLoadTo &to, const TextWithTags &caption, - std::shared_ptr album, - MsgId msgIdToEdit) + std::shared_ptr album) : _id(openssl::RandomValue()) , _session(session) , _dcId(session->mainDcId()) @@ -505,10 +504,9 @@ FileLoadTask::FileLoadTask( , _content(content) , _information(std::move(information)) , _type(type) -, _caption(caption) -, _msgIdToEdit(msgIdToEdit) { +, _caption(caption) { Expects(to.options.scheduled - || (_msgIdToEdit == 0 || IsServerMsgId(_msgIdToEdit))); + || (to.replaceMediaOf == 0 || IsServerMsgId(to.replaceMediaOf))); } FileLoadTask::FileLoadTask( @@ -690,8 +688,6 @@ void FileLoadTask::process(Args &&args) { _caption, _album); - _result->edit = (_msgIdToEdit > 0); - QString filename, filemime; qint64 filesize = 0; QByteArray filedata; @@ -993,12 +989,7 @@ void FileLoadTask::finish() { Ui::LayerOption::KeepOther); removeFromAlbum(); } else if (const auto session = _session.get()) { - const auto fullId = _msgIdToEdit - ? std::make_optional(FullMsgId( - peerToChannel(_to.peer), - _msgIdToEdit)) - : std::nullopt; - Api::SendConfirmedFile(session, _result, fullId); + Api::SendConfirmedFile(session, _result); } } diff --git a/Telegram/SourceFiles/storage/localimageloader.h b/Telegram/SourceFiles/storage/localimageloader.h index f9458d052..97974efa3 100644 --- a/Telegram/SourceFiles/storage/localimageloader.h +++ b/Telegram/SourceFiles/storage/localimageloader.h @@ -212,14 +212,20 @@ struct SendingAlbum { }; struct FileLoadTo { - FileLoadTo(const PeerId &peer, Api::SendOptions options, MsgId replyTo) + FileLoadTo( + const PeerId &peer, + Api::SendOptions options, + MsgId replyTo, + MsgId replaceMediaOf) : peer(peer) , options(options) - , replyTo(replyTo) { + , replyTo(replyTo) + , replaceMediaOf(replaceMediaOf) { } PeerId peer; Api::SendOptions options; MsgId replyTo; + MsgId replaceMediaOf; }; struct FileLoadResult { @@ -261,8 +267,6 @@ struct FileLoadResult { PreparedPhotoThumbs photoThumbs; TextWithTags caption; - bool edit = false; - void setFileData(const QByteArray &filedata); void setThumbData(const QByteArray &thumbdata); @@ -287,8 +291,7 @@ public: SendMediaType type, const FileLoadTo &to, const TextWithTags &caption, - std::shared_ptr album = nullptr, - MsgId msgIdToEdit = 0); + std::shared_ptr album = nullptr); FileLoadTask( not_null session, const QByteArray &voice, @@ -346,7 +349,6 @@ private: VoiceWaveform _waveform; SendMediaType _type; TextWithTags _caption; - MsgId _msgIdToEdit = 0; std::shared_ptr _result;