From 8a34f2932917885d1e8e71b88a050b9241fcbf5e Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 17 Aug 2020 15:29:27 +0300 Subject: [PATCH] Moved toggling of existing media to separate file. --- Telegram/CMakeLists.txt | 2 + .../SourceFiles/api/api_toggling_media.cpp | 96 +++++++++++++++++++ Telegram/SourceFiles/api/api_toggling_media.h | 26 +++++ Telegram/SourceFiles/apiwrap.cpp | 66 ------------- Telegram/SourceFiles/apiwrap.h | 8 -- .../chat_helpers/gifs_list_widget.cpp | 4 +- .../chat_helpers/stickers_list_widget.cpp | 8 +- .../history/history_inner_widget.cpp | 8 +- .../view/history_view_context_menu.cpp | 6 +- 9 files changed, 135 insertions(+), 89 deletions(-) create mode 100644 Telegram/SourceFiles/api/api_toggling_media.cpp create mode 100644 Telegram/SourceFiles/api/api_toggling_media.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 89e073424..600e38aaa 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -217,6 +217,8 @@ PRIVATE api/api_single_message_search.h api/api_text_entities.cpp api/api_text_entities.h + api/api_toggling_media.cpp + api/api_toggling_media.h api/api_updates.cpp api/api_updates.h boxes/filters/edit_filter_box.cpp diff --git a/Telegram/SourceFiles/api/api_toggling_media.cpp b/Telegram/SourceFiles/api/api_toggling_media.cpp new file mode 100644 index 000000000..91053d389 --- /dev/null +++ b/Telegram/SourceFiles/api/api_toggling_media.cpp @@ -0,0 +1,96 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "api/api_toggling_media.h" + +#include "apiwrap.h" +#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 "main/main_session.h" + +namespace Api { +namespace { + +template +void ToggleExistingMedia( + not_null document, + Data::FileOrigin origin, + bool saved, + 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) + )).done([=](const MTPBool &result) { + if (mtpIsTrue(result)) { + done(); + } + }).fail([=](const RPCError &error) { + if (error.code() == 400 + && error.type().startsWith(u"FILE_REFERENCE_"_q)) { + auto refreshed = [=](const Data::UpdatedFileReferences &d) { + if (document->fileReference() != usedFileReference) { + repeatRequest(repeatRequest); + } + }; + api->refreshFileReference(origin, std::move(refreshed)); + } + }).send(); + }; + performRequest(performRequest); +} + +} // namespace + +void ToggleFavedSticker( + not_null document, + Data::FileOrigin origin) { + ToggleFavedSticker( + document, + std::move(origin), + !document->owner().stickers().isFaved(document)); +} + +void ToggleFavedSticker( + not_null document, + Data::FileOrigin origin, + bool faved) { + if (faved && !document->sticker()) { + return; + } + ToggleExistingMedia( + document, + std::move(origin), + faved, + [=] { document->owner().stickers().setFaved(document, faved); }); +} + +void ToggleSavedGif( + not_null document, + Data::FileOrigin origin, + bool saved) { + if (saved && !document->isGifv()) { + return; + } + auto done = [=] { + if (saved) { + document->owner().stickers().addSavedGif(document); + } + }; + ToggleExistingMedia( + document, + std::move(origin), + saved, + std::move(done)); +} + +} // namespace Api diff --git a/Telegram/SourceFiles/api/api_toggling_media.h b/Telegram/SourceFiles/api/api_toggling_media.h new file mode 100644 index 000000000..3a78da2b9 --- /dev/null +++ b/Telegram/SourceFiles/api/api_toggling_media.h @@ -0,0 +1,26 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +namespace Api { + +void ToggleFavedSticker( + not_null document, + Data::FileOrigin origin); + +void ToggleFavedSticker( + not_null document, + Data::FileOrigin origin, + bool faved); + +void ToggleSavedGif( + not_null document, + Data::FileOrigin origin, + bool saved); + +} // namespace Api diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 5ab07e02d..7964c4c71 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -2936,72 +2936,6 @@ std::vector> *ApiWrap::stickersByEmoji( return nullptr; } -void ApiWrap::toggleFavedSticker( - not_null document, - Data::FileOrigin origin, - bool faved) { - if (faved && !document->sticker()) { - return; - } - - auto performRequest = [=](const auto &repeatRequest) -> void { - const auto usedFileReference = document->fileReference(); - request(MTPmessages_FaveSticker( - document->mtpInput(), - MTP_bool(!faved) - )).done([=](const MTPBool &result) { - if (mtpIsTrue(result)) { - _session->data().stickers().setFaved(document, faved); - } - }).fail([=](const RPCError &error) { - if (error.code() == 400 - && error.type().startsWith(qstr("FILE_REFERENCE_"))) { - auto refreshed = [=](const UpdatedFileReferences &data) { - if (document->fileReference() != usedFileReference) { - repeatRequest(repeatRequest); - } - }; - refreshFileReference(origin, std::move(refreshed)); - } - }).send(); - }; - performRequest(performRequest); -} - -void ApiWrap::toggleSavedGif( - not_null document, - Data::FileOrigin origin, - bool saved) { - if (saved && !document->isGifv()) { - return; - } - - auto performRequest = [=](const auto &repeatRequest) -> void { - const auto usedFileReference = document->fileReference(); - request(MTPmessages_SaveGif( - document->mtpInput(), - MTP_bool(!saved) - )).done([=](const MTPBool &result) { - if (mtpIsTrue(result)) { - if (saved) { - _session->data().stickers().addSavedGif(document); - } - } - }).fail([=](const RPCError &error) { - if (error.code() == 400 - && error.type().startsWith(qstr("FILE_REFERENCE_"))) { - auto refreshed = [=](const UpdatedFileReferences &data) { - if (document->fileReference() != usedFileReference) { - repeatRequest(repeatRequest); - } - }; - refreshFileReference(origin, std::move(refreshed)); - } - }).send(); - }; - performRequest(performRequest); -} - void ApiWrap::requestStickers(TimeId now) { if (!_session->data().stickers().updateNeeded(now) || _stickersUpdateRequest) { diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 27c503176..bf47e6a75 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -275,14 +275,6 @@ public: const MTPInputStickerSet &set); std::vector> *stickersByEmoji( not_null emoji); - void toggleFavedSticker( - not_null document, - Data::FileOrigin origin, - bool faved); - void toggleSavedGif( - not_null document, - Data::FileOrigin origin, - bool saved); void joinChannel(not_null channel); void leaveChannel(not_null channel); diff --git a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp index 1fe231f52..7e7eaa63e 100644 --- a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp @@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "chat_helpers/gifs_list_widget.h" -#include "apiwrap.h" // ApiWrap::toggleSavedGif +#include "api/api_toggling_media.h" // Api::ToggleSavedGif #include "base/const_string.h" #include "data/data_photo.h" #include "data/data_document.h" @@ -48,7 +48,7 @@ constexpr auto kSearchBotUsername = "gif"_cs; void DeleteSavedGif(not_null document) { auto &data = document->owner(); - document->session().api().toggleSavedGif( + Api::ToggleSavedGif( document, Data::FileOriginSavedGifs(), false); diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp index 6015a7ff5..8d2295adb 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp @@ -37,6 +37,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "main/main_session_settings.h" #include "apiwrap.h" +#include "api/api_toggling_media.h" // Api::ToggleFavedSticker #include "app.h" #include "styles/style_chat_helpers.h" #include "styles/style_window.h" @@ -2079,10 +2080,9 @@ void StickersListWidget::fillContextMenu( SendMenu::DefaultScheduleCallback(this, type, send)); const auto toggleFavedSticker = [=] { - document->session().api().toggleFavedSticker( + Api::ToggleFavedSticker( document, - Data::FileOriginStickerSet(Data::Stickers::FavedSetId, 0), - !document->owner().stickers().isFaved(document)); + Data::FileOriginStickerSet(Data::Stickers::FavedSetId, 0)); }; menu->addAction( (document->owner().stickers().isFaved(document) @@ -2219,7 +2219,7 @@ void StickersListWidget::removeFavedSticker(int section, int index) { const auto &sticker = _mySets[section].stickers[index]; const auto document = sticker.document; session().data().stickers().setFaved(document, false); - session().api().toggleFavedSticker( + Api::ToggleFavedSticker( document, Data::FileOriginStickerSet(Data::Stickers::FavedSetId, 0), false); diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 56e3f43c4..7dc560440 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -45,6 +45,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session_settings.h" #include "core/application.h" #include "apiwrap.h" +#include "api/api_toggling_media.h" #include "lang/lang_keys.h" #include "data/data_session.h" #include "data/data_media_types.h" @@ -1718,10 +1719,7 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { showStickerPackInfo(document); }); _menu->addAction(session->data().stickers().isFaved(document) ? tr::lng_faved_stickers_remove(tr::now) : tr::lng_faved_stickers_add(tr::now), [=] { - session->api().toggleFavedSticker( - document, - itemId, - !session->data().stickers().isFaved(document)); + Api::ToggleFavedSticker(document, itemId); }); } _menu->addAction(tr::lng_context_save_image(tr::now), App::LambdaDelayed(st::defaultDropdownMenu.menu.ripple.hideDuration, this, [=] { @@ -1915,7 +1913,7 @@ void HistoryInner::saveContextGif(FullMsgId itemId) { if (const auto item = session().data().message(itemId)) { if (const auto media = item->media()) { if (const auto document = media->document()) { - session().api().toggleSavedGif(document, item->fullId(), true); + Api::ToggleSavedGif(document, item->fullId(), true); } } } diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp index 14c2fba92..9e534c19f 100644 --- a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp +++ b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/history_view_context_menu.h" #include "api/api_editing.h" +#include "api/api_toggling_media.h" // Api::ToggleFavedSticker #include "base/unixtime.h" #include "history/view/history_view_list_widget.h" #include "history/view/history_view_cursor_state.h" @@ -128,10 +129,7 @@ void ShowStickerPackInfo(not_null document) { void ToggleFavedSticker( not_null document, FullMsgId contextId) { - document->session().api().toggleFavedSticker( - document, - contextId, - !document->owner().stickers().isFaved(document)); + Api::ToggleFavedSticker(document, contextId); } void AddPhotoActions(