diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 71e56d6269..6c8d3e117a 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -4198,7 +4198,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_context_unpin_msg" = "Unpin"; "lng_context_cancel_upload" = "Cancel Upload"; "lng_context_upload_edit_caption" = "Edit Caption"; -"lng_context_upload_edit_caption_error" = "Sorry, the file is already uploaded."; "lng_context_copy_selected" = "Copy Selected Text"; "lng_context_copy_selected_items" = "Copy Selected as Text"; "lng_context_forward_selected" = "Forward Selected"; diff --git a/Telegram/SourceFiles/boxes/send_gif_with_caption_box.cpp b/Telegram/SourceFiles/boxes/send_gif_with_caption_box.cpp index 1eaf9f6c4f..b94f000768 100644 --- a/Telegram/SourceFiles/boxes/send_gif_with_caption_box.cpp +++ b/Telegram/SourceFiles/boxes/send_gif_with_caption_box.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "boxes/send_gif_with_caption_box.h" +#include "api/api_editing.h" #include "base/event_filter.h" #include "boxes/premium_preview_box.h" #include "chat_helpers/field_autocomplete.h" @@ -357,34 +358,59 @@ void SendGifWithCaptionBox( void EditCaptionBox( not_null box, not_null view) { - const auto window = Core::App().findWindow(box); - Assert(window != nullptr); - const auto controller = window->sessionController(); - Assert(controller != nullptr); - box->setTitle(tr::lng_context_upload_edit_caption()); - - const auto item = view->data(); - const auto peer = item->history()->peer; - using namespace TextUtilities; - auto done = [=](Api::SendOptions, TextWithTags textWithTags) { + box->setTitle(tr::lng_context_upload_edit_caption()); + + const auto data = &view->data()->history()->peer->owner(); + + struct State { + FullMsgId fullId; + }; + const auto state = box->lifetime().make_state(); + state->fullId = view->data()->fullId(); + + data->itemIdChanged( + ) | rpl::start_with_next([=](Data::Session::IdChange event) { + if (event.oldId == state->fullId.msg) { + state->fullId = event.newId; + } + }, box->lifetime()); + + auto done = [=, show = box->uiShow()]( + Api::SendOptions, + TextWithTags textWithTags) { + const auto item = data->message(state->fullId); + if (!item) { + show->showToast(tr::lng_message_not_found(tr::now)); + return; + } + if (!(item->media() && item->media()->allowsEditCaption())) { + show->showToast(tr::lng_edit_error(tr::now)); + return; + } + auto text = TextWithEntities{ + base::take(textWithTags.text), + ConvertTextTagsToEntities(base::take(textWithTags.tags)), + }; if (item->isUploading()) { - item->setText({ - base::take(textWithTags.text), - ConvertTextTagsToEntities(base::take(textWithTags.tags)), - }); - peer->owner().requestViewResize(view); + item->setText(std::move(text)); + data->requestViewResize(view); if (item->groupId()) { - peer->owner().groups().refreshMessage(item, true); + data->groups().refreshMessage(item, true); } box->closeBox(); } else { - controller->showToast( - tr::lng_context_upload_edit_caption_error(tr::now)); + Api::EditCaption( + item, + std::move(text), + { .invertCaption = item->invertMedia() }, + [=] { box->closeBox(); }, + [=](const QString &e) { box->uiShow()->showToast(e); }); } }; + const auto item = view->data(); CaptionBox( box, tr::lng_settings_save(), @@ -392,7 +418,7 @@ void EditCaptionBox( .text = item->originalText().text, .tags = ConvertEntitiesToTextTags(item->originalText().entities), }, - peer, + item->history()->peer, {}, std::move(done)); } diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 4946e4929e..dd6031275a 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -2719,16 +2719,13 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { if (item->canDelete()) { const auto callback = [=] { deleteItem(itemId); }; if (item->isUploading()) { - _menu->addAction( - tr::lng_context_upload_edit_caption(tr::now), - [=] { - if (const auto view = viewByItem(item)) { - controller->uiShow()->show(Box( - Ui::EditCaptionBox, - view)); - } - }, - &st::menuIconEdit); + if (item->media() + && item->media()->allowsEditCaption()) { + _menu->addAction( + tr::lng_context_upload_edit_caption(tr::now), + [=] { editCaptionUploadLayer(item); }, + &st::menuIconEdit); + } _menu->addAction(tr::lng_context_cancel_upload(tr::now), callback, &st::menuIconCancel); } else { _menu->addAction(Ui::DeleteMessageContextAction( @@ -2973,16 +2970,13 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { deleteAsGroup(itemId); }; if (item->isUploading()) { - _menu->addAction( - tr::lng_context_upload_edit_caption(tr::now), - [=] { - if (const auto view = viewByItem(item)) { - controller->uiShow()->show(Box( - Ui::EditCaptionBox, - view)); - } - }, - &st::menuIconEdit); + if (item->media() + && item->media()->allowsEditCaption()) { + _menu->addAction( + tr::lng_context_upload_edit_caption(tr::now), + [=] { editCaptionUploadLayer(item); }, + &st::menuIconEdit); + } _menu->addAction(tr::lng_context_cancel_upload(tr::now), callback, &st::menuIconCancel); } else { _menu->addAction(Ui::DeleteMessageContextAction( @@ -3119,6 +3113,14 @@ void HistoryInner::copySelectedText() { } } +void HistoryInner::editCaptionUploadLayer(not_null item) { + if (const auto view = viewByItem(item)) { + if (item->isUploading()) { + _controller->uiShow()->show(Box(Ui::EditCaptionBox, view)); + } + } +} + void HistoryInner::savePhotoToFile(not_null photo) { const auto media = photo->activeMediaView(); if (photo->isNull() || !media || !media->loaded()) { diff --git a/Telegram/SourceFiles/history/history_inner_widget.h b/Telegram/SourceFiles/history/history_inner_widget.h index 33dd734966..8d180cf670 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.h +++ b/Telegram/SourceFiles/history/history_inner_widget.h @@ -418,6 +418,7 @@ private: void blockSenderItem(FullMsgId itemId); void blockSenderAsGroup(FullMsgId itemId); void copySelectedText(); + void editCaptionUploadLayer(not_null item); [[nodiscard]] auto reactionButtonParameters( not_null view,