Moved request for attached stickers to separate file.

This commit is contained in:
23rd 2020-10-30 02:43:35 +03:00
parent 98afc99a8f
commit 1459e6f38e
9 changed files with 122 additions and 48 deletions

View file

@ -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

View file

@ -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<ApiWrap*> api)
: _api(&api->instance()) {
}
void AttachedStickers::requestAttachedStickerSets(
not_null<Window::SessionController*> controller,
not_null<PhotoData*> 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<MTPStickerSetCovered> &result) {
_requestId = 0;
const auto strongController = weak.get();
if (!strongController) {
return;
}
if (result.v.isEmpty()) {
Ui::show(Box<InformBox>(tr::lng_stickers_not_found(tr::now)));
return;
} else if (result.v.size() > 1) {
Ui::show(Box<StickersBox>(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<StickerSetBox>(strongController, setId),
Ui::LayerOption::KeepOther);
}).fail([=](const RPCError &error) {
_requestId = 0;
Ui::show(Box<InformBox>(tr::lng_stickers_not_found(tr::now)));
}).send();
}
} // namespace Api

View file

@ -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<ApiWrap*> api);
void requestAttachedStickerSets(
not_null<Window::SessionController*> controller,
not_null<PhotoData*> photo);
private:
MTP::Sender _api;
mtpRequestId _requestId = 0;
};
} // namespace Api

View file

@ -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<Main::Session*> session)
, _topPromotionTimer([=] { refreshTopPromotion(); })
, _updateNotifySettingsTimer([=] { sendNotifySettingsUpdates(); })
, _authorizations(std::make_unique<Api::Authorizations>(this))
, _attachedStickers(std::make_unique<Api::AttachedStickers>(this))
, _selfDestruct(std::make_unique<Api::SelfDestruct>(this))
, _sensitiveContent(std::make_unique<Api::SensitiveContent>(this))
, _globalPrivacy(std::make_unique<Api::GlobalPrivacy>(this)) {
@ -5227,6 +5229,10 @@ Api::Authorizations &ApiWrap::authorizations() {
return *_authorizations;
}
Api::AttachedStickers &ApiWrap::attachedStickers() {
return *_attachedStickers;
}
Api::SelfDestruct &ApiWrap::selfDestruct() {
return *_selfDestruct;
}

View file

@ -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> 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<BlockedPeersSlice> _blockedPeersChanges;
const std::unique_ptr<Api::Authorizations> _authorizations;
const std::unique_ptr<Api::AttachedStickers> _attachedStickers;
const std::unique_ptr<Api::SelfDestruct> _selfDestruct;
const std::unique_ptr<Api::SensitiveContent> _sensitiveContent;
const std::unique_ptr<Api::GlobalPrivacy> _globalPrivacy;

View file

@ -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);
});
}
};

View file

@ -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();
}

View file

@ -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<PhotoData*> photo) {
session().api().request(_attachedStickerSetsRequestId).cancel();
_attachedStickerSetsRequestId = session().api().request(
MTPmessages_GetAttachedStickers(
MTP_inputStickeredMediaPhoto(photo->mtpInput())
)).done([=](const MTPVector<MTPStickerSetCovered> &result) {
if (result.v.isEmpty()) {
Ui::show(Box<InformBox>(tr::lng_stickers_not_found(tr::now)));
return;
} else if (result.v.size() > 1) {
Ui::show(Box<StickersBox>(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<StickerSetBox>(this, setId), Ui::LayerOption::KeepOther);
}).fail([=](const RPCError &error) {
Ui::show(Box<InformBox>(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

View file

@ -230,6 +230,7 @@ public:
SessionController(
not_null<Main::Session*> session,
not_null<Controller*> 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<PhotoData*> 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;
};