From 6e3fb253b98a2ee44afa6d1011d2e988af6d493e Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 19 Aug 2020 17:04:05 +0300 Subject: [PATCH] Added ability to remove recent stickers. --- Telegram/Resources/langs/lang.strings | 1 + .../SourceFiles/api/api_toggling_media.cpp | 41 ++++++++--- Telegram/SourceFiles/api/api_toggling_media.h | 5 ++ .../chat_helpers/stickers_list_widget.cpp | 9 +++ .../data/stickers/data_stickers.cpp | 69 ++++++++++++------- .../SourceFiles/data/stickers/data_stickers.h | 1 + 6 files changed, 91 insertions(+), 35 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index f1cb48b8d..9c5cb4636 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -1219,6 +1219,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_recent_stickers" = "Frequently used"; "lng_faved_stickers_add" = "Add to Favorites"; "lng_faved_stickers_remove" = "Remove from Favorites"; +"lng_recent_stickers_remove" = "Remove from Recent"; "lng_group_stickers" = "Group stickers"; "lng_group_stickers_description" = "You can choose a sticker set which will be available for every member while in the group chat."; "lng_group_stickers_add" = "Choose sticker set"; diff --git a/Telegram/SourceFiles/api/api_toggling_media.cpp b/Telegram/SourceFiles/api/api_toggling_media.cpp index 91053d389..e1f542b3c 100644 --- a/Telegram/SourceFiles/api/api_toggling_media.cpp +++ b/Telegram/SourceFiles/api/api_toggling_media.cpp @@ -11,25 +11,24 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_document.h" #include "data/data_file_origin.h" #include "data/data_session.h" -#include "data/stickers/data_stickers.h" // Stickers::addSavedGif +#include "data/stickers/data_stickers.h" #include "main/main_session.h" namespace Api { namespace { -template +template void ToggleExistingMedia( not_null document, Data::FileOrigin origin, - bool saved, + ToggleRequest toggleRequest, DoneCallback &&done) { const auto api = &document->owner().session().api(); auto performRequest = [=](const auto &repeatRequest) -> void { const auto usedFileReference = document->fileReference(); - api->request(MTPToggleRequest( - document->mtpInput(), - MTP_bool(!saved) + api->request(std::move( + toggleRequest )).done([=](const MTPBool &result) { if (mtpIsTrue(result)) { done(); @@ -67,13 +66,35 @@ void ToggleFavedSticker( if (faved && !document->sticker()) { return; } - ToggleExistingMedia( + ToggleExistingMedia( document, std::move(origin), - faved, + MTPmessages_FaveSticker(document->mtpInput(), MTP_bool(!faved)), [=] { document->owner().stickers().setFaved(document, faved); }); } +void ToggleRecentSticker( + not_null document, + Data::FileOrigin origin, + bool saved) { + if (!document->sticker()) { + return; + } + auto done = [=] { + if (!saved) { + document->owner().stickers().removeFromRecentSet(document); + } + }; + ToggleExistingMedia( + document, + std::move(origin), + MTPmessages_SaveRecentSticker( + MTP_flags(MTPmessages_SaveRecentSticker::Flag(0)), + document->mtpInput(), + MTP_bool(!saved)), + std::move(done)); +} + void ToggleSavedGif( not_null document, Data::FileOrigin origin, @@ -86,10 +107,10 @@ void ToggleSavedGif( document->owner().stickers().addSavedGif(document); } }; - ToggleExistingMedia( + ToggleExistingMedia( document, std::move(origin), - saved, + MTPmessages_SaveGif(document->mtpInput(), MTP_bool(!saved)), std::move(done)); } diff --git a/Telegram/SourceFiles/api/api_toggling_media.h b/Telegram/SourceFiles/api/api_toggling_media.h index 3a78da2b9..97740247f 100644 --- a/Telegram/SourceFiles/api/api_toggling_media.h +++ b/Telegram/SourceFiles/api/api_toggling_media.h @@ -18,6 +18,11 @@ void ToggleFavedSticker( Data::FileOrigin origin, bool faved); +void ToggleRecentSticker( + not_null document, + Data::FileOrigin origin, + bool saved); + void ToggleSavedGif( not_null document, Data::FileOrigin origin, diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp index 8d2295adb..1d5dcc2f6 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp @@ -2093,6 +2093,15 @@ void StickersListWidget::fillContextMenu( menu->addAction(tr::lng_context_pack_info(tr::now), [=] { showStickerSetBox(document); }); + + if (const auto id = set.id; id == Data::Stickers::RecentSetId) { + menu->addAction(tr::lng_recent_stickers_remove(tr::now), [=] { + Api::ToggleRecentSticker( + document, + Data::FileOriginStickerSet(id, 0), + false); + }); + } } } diff --git a/Telegram/SourceFiles/data/stickers/data_stickers.cpp b/Telegram/SourceFiles/data/stickers/data_stickers.cpp index c60c3db5c..0ea087e99 100644 --- a/Telegram/SourceFiles/data/stickers/data_stickers.cpp +++ b/Telegram/SourceFiles/data/stickers/data_stickers.cpp @@ -29,6 +29,43 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Data { +namespace { + +void RemoveFromSet( + StickersSets &sets, + not_null document, + uint64 setId) { + const auto it = sets.find(setId); + if (it == sets.end()) { + return; + } + const auto set = it->second.get(); + const auto index = set->stickers.indexOf(document); + if (index < 0) { + return; + } + set->stickers.removeAt(index); + if (!set->dates.empty()) { + set->dates.erase(set->dates.begin() + index); + } + for (auto i = set->emoji.begin(); i != set->emoji.end();) { + const auto index = i->indexOf(document); + if (index >= 0) { + i->removeAt(index); + if (i->empty()) { + i = set->emoji.erase(i); + continue; + } + } + ++i; + } + if (set->stickers.empty()) { + sets.erase(it); + } +} + +} // namespace + Stickers::Stickers(not_null owner) : _owner(owner) { } @@ -513,32 +550,14 @@ void Stickers::requestSetToPushFaved(not_null document) { }).send(); } +void Stickers::removeFromRecentSet(not_null document) { + RemoveFromSet(setsRef(), document, CloudRecentSetId); + session().local().writeRecentStickers(); + notifyRecentUpdated(); +} + void Stickers::setIsNotFaved(not_null document) { - auto &sets = setsRef(); - auto it = sets.find(FavedSetId); - if (it == sets.end()) { - return; - } - const auto set = it->second.get(); - auto index = set->stickers.indexOf(document); - if (index < 0) { - return; - } - set->stickers.removeAt(index); - for (auto i = set->emoji.begin(); i != set->emoji.end();) { - auto index = i->indexOf(document); - if (index >= 0) { - i->removeAt(index); - if (i->empty()) { - i = set->emoji.erase(i); - continue; - } - } - ++i; - } - if (set->stickers.empty()) { - sets.erase(it); - } + RemoveFromSet(setsRef(), document, FavedSetId); session().local().writeFavedStickers(); notifyUpdated(); } diff --git a/Telegram/SourceFiles/data/stickers/data_stickers.h b/Telegram/SourceFiles/data/stickers/data_stickers.h index c846a96bd..bd295e95e 100644 --- a/Telegram/SourceFiles/data/stickers/data_stickers.h +++ b/Telegram/SourceFiles/data/stickers/data_stickers.h @@ -129,6 +129,7 @@ public: SavedGifs &savedGifsRef() { return _savedGifs; } + void removeFromRecentSet(not_null document); void addSavedGif(not_null document); void checkSavedGif(not_null item);