From a3cd4a55dee19f5c13d418a8745e43ca9dc72dba Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 2 Oct 2025 11:49:22 +0300 Subject: [PATCH] Added ability to remove gift from collection with context menu. --- Telegram/Resources/langs/lang.strings | 1 + .../peer_gifts/info_peer_gifts_widget.cpp | 89 +++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 9aae07adba..1915a6cae0 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -3906,6 +3906,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_gift_collection_add_to" = "Add to Collection"; "lng_gift_collection_reorder" = "Reorder"; "lng_gift_collection_reorder_exit" = "Apply Reorder"; +"lng_gift_collection_remove_from" = "Remove from Collection"; "lng_gift_locked_title" = "Gift Locked"; "lng_accounts_limit_title" = "Limit Reached"; diff --git a/Telegram/SourceFiles/info/peer_gifts/info_peer_gifts_widget.cpp b/Telegram/SourceFiles/info/peer_gifts/info_peer_gifts_widget.cpp index 49d83390e8..e28882daf2 100644 --- a/Telegram/SourceFiles/info/peer_gifts/info_peer_gifts_widget.cpp +++ b/Telegram/SourceFiles/info/peer_gifts/info_peer_gifts_widget.cpp @@ -220,6 +220,9 @@ private: void collectionRenamed(int id, QString name); void collectionRemoved(int id); + void removeGiftFromCollection( + Data::SavedStarGiftId giftId, + int collectionId); void markPinned(std::vector::iterator i); void markUnpinned(std::vector::iterator i); @@ -1077,6 +1080,23 @@ void InnerWidget::showMenuFor(not_null button, QPoint point) { _menu.get(), entry, ::Settings::SavedStarGiftMenuType::List); + + const auto collectionId = _descriptor.current().collectionId; + if (collectionId > 0 && _peer->canManageGifts()) { + const auto &gift = (*_list)[index].gift; + if (ranges::contains(gift.collectionIds, collectionId)) { + const auto addAction = Ui::Menu::CreateAddActionCallback(_menu); + addAction({ + .text = tr::lng_gift_collection_remove_from(tr::now), + .handler = [=] { + removeGiftFromCollection(gift.manageId, collectionId); + }, + .icon = &st::menuIconDeleteAttention, + .isAttention = true, + }); + } + } + if (_menu->empty()) { return; } @@ -1460,6 +1480,75 @@ void InnerWidget::collectionRenamed(int id, QString name) { } } +void InnerWidget::removeGiftFromCollection( + Data::SavedStarGiftId giftId, + int collectionId) { + auto changes = Data::GiftsUpdate{ + .peer = _peer, + .collectionId = collectionId, + .removed = { giftId }, + }; + using Flag = MTPpayments_UpdateStarGiftCollection::Flag; + _window->session().api().request( + MTPpayments_UpdateStarGiftCollection( + MTP_flags(Flag::f_delete_stargift), + _peer->input, + MTP_int(collectionId), + MTPstring(), + MTP_vector({ + Api::InputSavedStarGiftId(giftId) + }), + MTPVector(), + MTPVector()) + ).done([=](const MTPStarGiftCollection &result) { + _window->session().data().notifyGiftsUpdate(base::duplicate(changes)); + + const auto i = ranges::find( + _collections, + collectionId, + &Data::GiftCollection::id); + if (i != end(_collections)) { + const auto updated = FromTL(&_window->session(), result); + *i = updated; + + auto &per = _perCollection[collectionId]; + per.total = updated.count; + + if (_descriptor.current().collectionId == collectionId) { + const auto it = ranges::find( + *_list, + giftId, + [](const Entry &entry) { return entry.gift.manageId; }); + if (it != end(*_list)) { + _list->erase(it); + if (_entries->total > 0) { + --_entries->total; + } + refreshButtons(); + } + } + + const auto giftIt = ranges::find( + per.list, + giftId, + [](const Entry &entry) { return entry.gift.manageId; }); + if (giftIt != end(per.list)) { + per.list.erase(giftIt); + } + + if (_addingToCollectionId == collectionId) { + auto currentChanges = _collectionChanges.current(); + currentChanges.removed.push_back(giftId); + _collectionChanges = std::move(currentChanges); + } + + refreshCollectionsTabs(); + } + }).fail([=, show = _window->uiShow()](const MTP::Error &error) { + show->showToast(error.type()); + }).send(); +} + void InnerWidget::collectionRemoved(int id) { auto now = _descriptor.current(); if (now.collectionId == id) {