From 4fff81291002ba6fb1a15e9a0a9b890d008bd20b Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 21 Feb 2022 01:41:51 +0300 Subject: [PATCH] Moved out importChatInvite from ApiWrap. --- Telegram/SourceFiles/api/api_chat_invite.cpp | 75 +++++++++++++++++++- Telegram/SourceFiles/apiwrap.cpp | 55 -------------- Telegram/SourceFiles/apiwrap.h | 3 +- 3 files changed, 76 insertions(+), 57 deletions(-) diff --git a/Telegram/SourceFiles/api/api_chat_invite.cpp b/Telegram/SourceFiles/api/api_chat_invite.cpp index 69e01639cb..f2ec676292 100644 --- a/Telegram/SourceFiles/api/api_chat_invite.cpp +++ b/Telegram/SourceFiles/api/api_chat_invite.cpp @@ -19,12 +19,85 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_user.h" #include "data/data_file_origin.h" #include "ui/boxes/confirm_box.h" +#include "ui/toasts/common_toasts.h" #include "boxes/abstract_box.h" #include "styles/style_boxes.h" #include "styles/style_layers.h" namespace Api { +namespace { + +void SubmitChatInvite( + base::weak_ptr weak, + not_null session, + const QString &hash, + bool isGroup) { + session->api().request(MTPmessages_ImportChatInvite( + MTP_string(hash) + )).done([=](const MTPUpdates &result) { + session->api().applyUpdates(result); + const auto strongController = weak.get(); + if (!strongController) { + return; + } + + strongController->hideLayer(); + const auto handleChats = [&](const MTPVector &chats) { + if (chats.v.isEmpty()) { + return; + } + const auto peerId = chats.v[0].match([](const MTPDchat &data) { + return peerFromChat(data.vid().v); + }, [](const MTPDchannel &data) { + return peerFromChannel(data.vid().v); + }, [](auto&&) { + return PeerId(0); + }); + if (const auto peer = session->data().peerLoaded(peerId)) { + // Shows in the primary window anyway. + strongController->showPeerHistory( + peer, + Window::SectionShow::Way::Forward); + } + }; + result.match([&](const MTPDupdates &data) { + handleChats(data.vchats()); + }, [&](const MTPDupdatesCombined &data) { + handleChats(data.vchats()); + }, [&](auto &&) { + LOG(("API Error: unexpected update cons %1 " + "(ApiWrap::importChatInvite)").arg(result.type())); + }); + }).fail([=](const MTP::Error &error) { + const auto strongController = weak.get(); + if (!strongController) { + return; + } + + const auto &type = error.type(); + strongController->hideLayer(); + Ui::ShowMultilineToast({ + .parentOverride = Window::Show(strongController).toastParent(), + .text = { [&] { + if (type == u"INVITE_REQUEST_SENT"_q) { + return isGroup + ? tr::lng_group_request_sent(tr::now) + : tr::lng_group_request_sent_channel(tr::now); + } else if (type == u"CHANNELS_TOO_MUCH"_q) { + return tr::lng_join_channel_error(tr::now); + } else if (type == u"USERS_TOO_MUCH"_q) { + return tr::lng_group_invite_no_room(tr::now); + } else { + return tr::lng_group_invite_bad_link(tr::now); + } + }() }, + .duration = ApiWrap::kJoinErrorDuration }); + }).send(); +} + +} // namespace + void CheckChatInvite( not_null controller, const QString &hash, @@ -43,7 +116,7 @@ void CheckChatInvite( session, data, invitePeekChannel, - [=] { session->api().importChatInvite(hash, isGroup); })); + [=] { SubmitChatInvite(weak, session, hash, isGroup); })); if (invitePeekChannel) { box->boxClosing( ) | rpl::filter([=] { diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 59d9e3e32d..5f176bf99a 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -104,7 +104,6 @@ constexpr auto kStickersByEmojiInvalidateTimeout = crl::time(6 * 1000); constexpr auto kNotifySettingSaveTimeout = crl::time(1000); constexpr auto kDialogsFirstLoad = 20; constexpr auto kDialogsPerPage = 500; -constexpr auto kJoinErrorDuration = 5 * crl::time(1000); using PhotoFileLocationId = Data::PhotoFileLocationId; using DocumentFileLocationId = Data::DocumentFileLocationId; @@ -347,60 +346,6 @@ void ApiWrap::checkChatInvite( )).done(std::move(done)).fail(std::move(fail)).send(); } -void ApiWrap::importChatInvite(const QString &hash, bool isGroup) { - request(MTPmessages_ImportChatInvite( - MTP_string(hash) - )).done([=](const MTPUpdates &result) { - applyUpdates(result); - - Ui::hideLayer(); - const auto handleChats = [&](const MTPVector &chats) { - if (chats.v.isEmpty()) { - return; - } - const auto peerId = chats.v[0].match([](const MTPDchat &data) { - return peerFromChat(data.vid().v); - }, [](const MTPDchannel &data) { - return peerFromChannel(data.vid().v); - }, [](auto&&) { - return PeerId(0); - }); - if (const auto peer = _session->data().peerLoaded(peerId)) { - const auto &windows = _session->windows(); - if (!windows.empty()) { - windows.front()->showPeerHistory( - peer, - Window::SectionShow::Way::Forward); - } - } - }; - result.match([&](const MTPDupdates &data) { - handleChats(data.vchats()); - }, [&](const MTPDupdatesCombined &data) { - handleChats(data.vchats()); - }, [&](auto &&) { - LOG(("API Error: unexpected update cons %1 " - "(ApiWrap::importChatInvite)").arg(result.type())); - }); - }).fail([=](const MTP::Error &error) { - const auto &type = error.type(); - Ui::hideLayer(); - Ui::ShowMultilineToast({ .text = { [&] { - if (type == qstr("INVITE_REQUEST_SENT")) { - return isGroup - ? tr::lng_group_request_sent(tr::now) - : tr::lng_group_request_sent_channel(tr::now); - } else if (type == qstr("CHANNELS_TOO_MUCH")) { - return tr::lng_join_channel_error(tr::now); - } else if (type == qstr("USERS_TOO_MUCH")) { - return tr::lng_group_invite_no_room(tr::now); - } else { - return tr::lng_group_invite_bad_link(tr::now); - } - }() }, .duration = kJoinErrorDuration }); - }).send(); -} - void ApiWrap::savePinnedOrder(Data::Folder *folder) { const auto &order = _session->data().pinnedChatsOrder( folder, diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index c0b0cb7a3c..df431ba748 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -196,7 +196,6 @@ public: const QString &hash, FnMut done, Fn fail); - void importChatInvite(const QString &hash, bool isGroup); void processFullPeer( not_null peer, @@ -358,6 +357,8 @@ public: void updatePrivacyLastSeens(); + static constexpr auto kJoinErrorDuration = 5 * crl::time(1000); + private: struct MessageDataRequest { using Callbacks = std::vector>;