From 1459e6f38e17947ef943c4c2df6418bb4016e520 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 30 Oct 2020 02:43:35 +0300 Subject: [PATCH] Moved request for attached stickers to separate file. --- Telegram/CMakeLists.txt | 2 + .../SourceFiles/api/api_attached_stickers.cpp | 64 +++++++++++++++++++ .../SourceFiles/api/api_attached_stickers.h | 35 ++++++++++ Telegram/SourceFiles/apiwrap.cpp | 6 ++ Telegram/SourceFiles/apiwrap.h | 3 + .../history/history_inner_widget.cpp | 5 +- .../media/view/media_view_overlay_widget.cpp | 8 ++- .../window/window_session_controller.cpp | 40 +----------- .../window/window_session_controller.h | 7 +- 9 files changed, 122 insertions(+), 48 deletions(-) create mode 100644 Telegram/SourceFiles/api/api_attached_stickers.cpp create mode 100644 Telegram/SourceFiles/api/api_attached_stickers.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 19e7674101..51d5689ac0 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -157,6 +157,8 @@ nice_target_sources(Telegram ${src_loc} PRIVATE ${style_files} + api/api_attached_stickers.cpp + api/api_attached_stickers.h api/api_authorizations.cpp api/api_authorizations.h api/api_bot.cpp diff --git a/Telegram/SourceFiles/api/api_attached_stickers.cpp b/Telegram/SourceFiles/api/api_attached_stickers.cpp new file mode 100644 index 0000000000..fcdfe77213 --- /dev/null +++ b/Telegram/SourceFiles/api/api_attached_stickers.cpp @@ -0,0 +1,64 @@ +/* +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_attached_stickers.h" + +#include "apiwrap.h" +#include "boxes/confirm_box.h" +#include "boxes/sticker_set_box.h" +#include "boxes/stickers_box.h" +#include "data/data_photo.h" +#include "lang/lang_keys.h" +#include "window/window_session_controller.h" + +namespace Api { + +AttachedStickers::AttachedStickers(not_null api) +: _api(&api->instance()) { +} + +void AttachedStickers::requestAttachedStickerSets( + not_null controller, + not_null photo) { + const auto weak = base::make_weak(controller.get()); + _api.request(_requestId).cancel(); + _requestId = _api.request( + MTPmessages_GetAttachedStickers( + MTP_inputStickeredMediaPhoto(photo->mtpInput()) + )).done([=](const MTPVector &result) { + _requestId = 0; + const auto strongController = weak.get(); + if (!strongController) { + return; + } + if (result.v.isEmpty()) { + Ui::show(Box(tr::lng_stickers_not_found(tr::now))); + return; + } else if (result.v.size() > 1) { + Ui::show(Box(strongController, result)); + return; + } + // Single attached sticker pack. + const auto setData = result.v.front().match([&](const auto &data) { + return data.vset().match([&](const MTPDstickerSet &data) { + return &data; + }); + }); + + const auto setId = (setData->vid().v && setData->vaccess_hash().v) + ? MTP_inputStickerSetID(setData->vid(), setData->vaccess_hash()) + : MTP_inputStickerSetShortName(setData->vshort_name()); + Ui::show( + Box(strongController, setId), + Ui::LayerOption::KeepOther); + }).fail([=](const RPCError &error) { + _requestId = 0; + Ui::show(Box(tr::lng_stickers_not_found(tr::now))); + }).send(); +} + +} // namespace Api diff --git a/Telegram/SourceFiles/api/api_attached_stickers.h b/Telegram/SourceFiles/api/api_attached_stickers.h new file mode 100644 index 0000000000..bf186fef0a --- /dev/null +++ b/Telegram/SourceFiles/api/api_attached_stickers.h @@ -0,0 +1,35 @@ +/* +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 + +#include "mtproto/sender.h" + +class ApiWrap; +class PhotoData; + +namespace Window { +class SessionController; +} // namespace Window + +namespace Api { + +class AttachedStickers final { +public: + explicit AttachedStickers(not_null api); + + void requestAttachedStickerSets( + not_null controller, + not_null photo); + +private: + MTP::Sender _api; + mtpRequestId _requestId = 0; + +}; + +} // namespace Api diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 92805db3d0..a082d9b88a 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "apiwrap.h" #include "api/api_authorizations.h" +#include "api/api_attached_stickers.h" #include "api/api_hash.h" #include "api/api_media.h" #include "api/api_sending.h" @@ -188,6 +189,7 @@ ApiWrap::ApiWrap(not_null session) , _topPromotionTimer([=] { refreshTopPromotion(); }) , _updateNotifySettingsTimer([=] { sendNotifySettingsUpdates(); }) , _authorizations(std::make_unique(this)) +, _attachedStickers(std::make_unique(this)) , _selfDestruct(std::make_unique(this)) , _sensitiveContent(std::make_unique(this)) , _globalPrivacy(std::make_unique(this)) { @@ -5227,6 +5229,10 @@ Api::Authorizations &ApiWrap::authorizations() { return *_authorizations; } +Api::AttachedStickers &ApiWrap::attachedStickers() { + return *_attachedStickers; +} + Api::SelfDestruct &ApiWrap::selfDestruct() { return *_selfDestruct; } diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 3284a10cf5..bf81404969 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -53,6 +53,7 @@ namespace Api { class Updates; class Authorizations; +class AttachedStickers; class SelfDestruct; class SensitiveContent; class GlobalPrivacy; @@ -455,6 +456,7 @@ public: rpl::producer blockedPeersSlice(); [[nodiscard]] Api::Authorizations &authorizations(); + [[nodiscard]] Api::AttachedStickers &attachedStickers(); [[nodiscard]] Api::SelfDestruct &selfDestruct(); [[nodiscard]] Api::SensitiveContent &sensitiveContent(); [[nodiscard]] Api::GlobalPrivacy &globalPrivacy(); @@ -818,6 +820,7 @@ private: rpl::event_stream _blockedPeersChanges; const std::unique_ptr _authorizations; + const std::unique_ptr _attachedStickers; const std::unique_ptr _selfDestruct; const std::unique_ptr _sensitiveContent; const std::unique_ptr _globalPrivacy; diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 8f0b91a550..aea01efe89 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_attached_stickers.h" #include "api/api_toggling_media.h" #include "lang/lang_keys.h" #include "data/data_session.h" @@ -1590,7 +1591,9 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { }); if (photo->hasSticker) { _menu->addAction(tr::lng_context_attached_stickers(tr::now), [=] { - controller->requestAttachedStickerSets(photo); + session->api().attachedStickers().requestAttachedStickerSets( + controller, + photo); }); } }; diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 38e7062e01..7d9840f1bd 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "media/view/media_view_overlay_widget.h" #include "apiwrap.h" +#include "api/api_attached_stickers.h" #include "lang/lang_keys.h" #include "mainwidget.h" #include "mainwindow.h" @@ -1545,15 +1546,16 @@ void OverlayWidget::onCopy() { } void OverlayWidget::onAttachedStickers() { - const auto session = _session; - if (!session || !_photo) { + if (!_session || !_photo) { return; } const auto &active = _session->windows(); if (active.empty()) { return; } - active.front()->requestAttachedStickerSets(_photo); + _session->api().attachedStickers().requestAttachedStickerSets( + active.front(), + _photo); close(); } diff --git a/Telegram/SourceFiles/window/window_session_controller.cpp b/Telegram/SourceFiles/window/window_session_controller.cpp index 4f8ebf3a3c..7828489c0b 100644 --- a/Telegram/SourceFiles/window/window_session_controller.cpp +++ b/Telegram/SourceFiles/window/window_session_controller.cpp @@ -28,7 +28,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_user.h" #include "data/data_changes.h" #include "data/data_chat_filters.h" -#include "data/data_photo.h" // requestAttachedStickerSets. #include "passport/passport_form_controller.h" #include "chat_helpers/tabbed_selector.h" #include "core/shortcuts.h" @@ -40,10 +39,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/delayed_activation.h" #include "ui/toast/toast.h" #include "boxes/calendar_box.h" -#include "boxes/sticker_set_box.h" // requestAttachedStickerSets. -#include "boxes/confirm_box.h" // requestAttachedStickerSets. -#include "boxes/stickers_box.h" // requestAttachedStickerSets. -#include "lang/lang_keys.h" // requestAttachedStickerSets. +#include "boxes/confirm_box.h" #include "mainwidget.h" #include "mainwindow.h" #include "main/main_session.h" @@ -545,36 +541,6 @@ rpl::producer<> SessionController::filtersMenuChanged() const { return _filtersMenuChanged.events(); } -void SessionController::requestAttachedStickerSets( - not_null photo) { - session().api().request(_attachedStickerSetsRequestId).cancel(); - _attachedStickerSetsRequestId = session().api().request( - MTPmessages_GetAttachedStickers( - MTP_inputStickeredMediaPhoto(photo->mtpInput()) - )).done([=](const MTPVector &result) { - if (result.v.isEmpty()) { - Ui::show(Box(tr::lng_stickers_not_found(tr::now))); - return; - } else if (result.v.size() > 1) { - Ui::show(Box(this, result)); - return; - } - // Single attached sticker pack. - const auto setData = result.v.front().match([&](const auto &data) { - return data.vset().match([&](const MTPDstickerSet &data) { - return &data; - }); - }); - - const auto setId = (setData->vid().v && setData->vaccess_hash().v) - ? MTP_inputStickerSetID(setData->vid(), setData->vaccess_hash()) - : MTP_inputStickerSetShortName(setData->vshort_name()); - Ui::show(Box(this, setId), Ui::LayerOption::KeepOther); - }).fail([=](const RPCError &error) { - Ui::show(Box(tr::lng_stickers_not_found(tr::now))); - }).send(); -} - void SessionController::checkOpenedFilter() { if (const auto filterId = activeChatsFilterCurrent()) { const auto &list = session().data().chatsFilters().list(); @@ -1132,8 +1098,6 @@ void SessionController::setActiveChatsFilter(FilterId id) { } } -SessionController::~SessionController() { - session().api().request(_attachedStickerSetsRequestId).cancel(); -} +SessionController::~SessionController() = default; } // namespace Window diff --git a/Telegram/SourceFiles/window/window_session_controller.h b/Telegram/SourceFiles/window/window_session_controller.h index 980b7d2e75..2feaa18ec6 100644 --- a/Telegram/SourceFiles/window/window_session_controller.h +++ b/Telegram/SourceFiles/window/window_session_controller.h @@ -230,6 +230,7 @@ public: SessionController( not_null session, not_null window); + ~SessionController(); [[nodiscard]] Controller &window() const { return *_window; @@ -348,14 +349,10 @@ public: void toggleFiltersMenu(bool enabled); [[nodiscard]] rpl::producer<> filtersMenuChanged() const; - void requestAttachedStickerSets(not_null photo); - rpl::lifetime &lifetime() { return _lifetime; } - ~SessionController(); - private: void init(); void initSupportMode(); @@ -408,8 +405,6 @@ private: rpl::event_stream<> _filtersMenuChanged; - mtpRequestId _attachedStickerSetsRequestId = 0; - rpl::lifetime _lifetime; };