From f2f4f9b24b89fd229b5c5c61a6699cfa59413899 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 26 Feb 2022 05:59:21 +0300 Subject: [PATCH] Divided ChooseJoinAsProcess::start into methods. --- .../calls/group/calls_choose_join_as.cpp | 220 ++++++++++-------- .../calls/group/calls_choose_join_as.h | 5 + 2 files changed, 122 insertions(+), 103 deletions(-) diff --git a/Telegram/SourceFiles/calls/group/calls_choose_join_as.cpp b/Telegram/SourceFiles/calls/group/calls_choose_join_as.cpp index 4293feeea..38974f3d8 100644 --- a/Telegram/SourceFiles/calls/group/calls_choose_join_as.cpp +++ b/Telegram/SourceFiles/calls/group/calls_choose_join_as.cpp @@ -328,6 +328,7 @@ void ChooseJoinAsProcess::start( _request->showBox = std::move(showBox); _request->showToast = std::move(showToast); _request->done = std::move(done); + _request->changingJoinAsFrom = changingJoinAsFrom; return; } session->api().request(_request->id).cancel(); @@ -345,21 +346,14 @@ void ChooseJoinAsProcess::start( _request = nullptr; }, _request->lifetime); - const auto finish = [=](JoinInfo info) { - const auto done = std::move(_request->done); - const auto box = _request->box; - _request = nullptr; - done(std::move(info)); - if (const auto strong = box.data()) { - strong->closeBox(); - } - }; + requestList(); +} + +void ChooseJoinAsProcess::requestList() { + const auto session = &_request->peer->session(); _request->id = session->api().request(MTPphone_GetGroupCallJoinAs( _request->peer->input )).done([=](const MTPphone_JoinAsPeers &result) { - const auto peer = _request->peer; - const auto self = peer->session().user(); - auto info = JoinInfo{ .peer = peer, .joinAs = self }; auto list = result.match([&](const MTPDphone_joinAsPeers &data) { session->data().processUsers(data.vusers()); session->data().processChats(data.vchats()); @@ -376,97 +370,7 @@ void ChooseJoinAsProcess::start( } return list; }); - const auto selectedId = peer->groupCallDefaultJoinAs(); - if (list.empty()) { - _request->showToast(Lang::Hard::ServerError()); - return; - } - info.joinAs = [&]() -> not_null { - const auto loaded = selectedId - ? session->data().peerLoaded(selectedId) - : nullptr; - return (changingJoinAsFrom - && ranges::contains(list, not_null{ changingJoinAsFrom })) - ? not_null(changingJoinAsFrom) - : (loaded && ranges::contains(list, not_null{ loaded })) - ? not_null(loaded) - : ranges::contains(list, self) - ? self - : list.front(); - }(); - info.possibleJoinAs = std::move(list); - - const auto onlyByMe = (info.possibleJoinAs.size() == 1) - && (info.possibleJoinAs.front() == self); - - // We already joined this voice chat, just rejoin with the same. - const auto byAlreadyUsed = selectedId - && (info.joinAs->id == selectedId) - && (peer->groupCall() != nullptr); - - if (!changingJoinAsFrom && (onlyByMe || byAlreadyUsed)) { - auto confirmation = CreateOrJoinConfirmation( - peer, - context, - byAlreadyUsed); - if (confirmation.text.isEmpty()) { - finish(info); - return; - } - const auto livestream = peer->isBroadcast(); - const auto creating = !peer->groupCall(); - if (creating) { - confirmation - .append("\n\n") - .append(tr::lng_group_call_or_schedule( - tr::now, - lt_link, - Ui::Text::Link((livestream - ? tr::lng_group_call_schedule_channel - : tr::lng_group_call_schedule)(tr::now)), - Ui::Text::WithEntities)); - } - const auto guard = base::make_weak(&_request->guard); - const auto safeFinish = crl::guard(guard, [=] { finish(info); }); - const auto filter = [=](const auto &...) { - if (guard) { - _request->showBox(Box( - ScheduleGroupCallBox, - info, - crl::guard(guard, finish))); - } - return false; - }; - auto box = ConfirmBox({ - .text = confirmation, - .button = (creating - ? tr::lng_create_group_create() - : tr::lng_group_call_join()), - .callback = crl::guard(guard, [=] { finish(info); }), - .st = &st::boxLabel, - .filter = filter, - }); - box->boxClosing( - ) | rpl::start_with_next([=] { - _request = nullptr; - }, _request->lifetime); - - _request->box = box.data(); - _request->showBox(std::move(box)); - return; - } - auto box = Box( - ChooseJoinAsBox, - context, - std::move(info), - crl::guard(&_request->guard, finish)); - box->boxClosing( - ) | rpl::start_with_next([=] { - _request = nullptr; - }, _request->lifetime); - - _request->box = box.data(); - _request->showBox(std::move(box)); + processList(std::move(list)); }).fail([=] { finish({ .peer = _request->peer, @@ -475,4 +379,114 @@ void ChooseJoinAsProcess::start( }).send(); } +void ChooseJoinAsProcess::finish(JoinInfo info) { + const auto done = std::move(_request->done); + const auto box = _request->box; + _request = nullptr; + done(std::move(info)); + if (const auto strong = box.data()) { + strong->closeBox(); + } +} + +void ChooseJoinAsProcess::processList( + std::vector> &&list) { + const auto session = &_request->peer->session(); + const auto peer = _request->peer; + const auto self = peer->session().user(); + auto info = JoinInfo{ .peer = peer, .joinAs = self }; + const auto selectedId = peer->groupCallDefaultJoinAs(); + if (list.empty()) { + _request->showToast(Lang::Hard::ServerError()); + return; + } + info.joinAs = [&]() -> not_null { + const auto loaded = selectedId + ? session->data().peerLoaded(selectedId) + : nullptr; + const auto changingJoinAsFrom = _request->changingJoinAsFrom; + return (changingJoinAsFrom + && ranges::contains(list, not_null{ changingJoinAsFrom })) + ? not_null(changingJoinAsFrom) + : (loaded && ranges::contains(list, not_null{ loaded })) + ? not_null(loaded) + : ranges::contains(list, self) + ? self + : list.front(); + }(); + info.possibleJoinAs = std::move(list); + + const auto onlyByMe = (info.possibleJoinAs.size() == 1) + && (info.possibleJoinAs.front() == self); + + // We already joined this voice chat, just rejoin with the same. + const auto byAlreadyUsed = selectedId + && (info.joinAs->id == selectedId) + && (peer->groupCall() != nullptr); + + if (!_request->changingJoinAsFrom && (onlyByMe || byAlreadyUsed)) { + auto confirmation = CreateOrJoinConfirmation( + peer, + _request->context, + byAlreadyUsed); + if (confirmation.text.isEmpty()) { + finish(info); + return; + } + const auto livestream = peer->isBroadcast(); + const auto creating = !peer->groupCall(); + if (creating) { + confirmation + .append("\n\n") + .append(tr::lng_group_call_or_schedule( + tr::now, + lt_link, + Ui::Text::Link((livestream + ? tr::lng_group_call_schedule_channel + : tr::lng_group_call_schedule)(tr::now)), + Ui::Text::WithEntities)); + } + const auto guard = base::make_weak(&_request->guard); + const auto safeFinish = crl::guard(guard, [=] { finish(info); }); + const auto filter = [=](const auto &...) { + if (guard) { + _request->showBox(Box( + ScheduleGroupCallBox, + info, + crl::guard(guard, [=](auto info) { finish(info); }))); + } + return false; + }; + auto box = ConfirmBox({ + .text = confirmation, + .button = (creating + ? tr::lng_create_group_create() + : tr::lng_group_call_join()), + .callback = crl::guard(guard, [=] { finish(info); }), + .st = &st::boxLabel, + .filter = filter, + }); + box->boxClosing( + ) | rpl::start_with_next([=] { + _request = nullptr; + }, _request->lifetime); + + _request->box = box.data(); + _request->showBox(std::move(box)); + return; + } + auto box = Box( + ChooseJoinAsBox, + _request->context, + std::move(info), + crl::guard(&_request->guard, [=](auto info) { finish(info); })); + box->boxClosing( + ) | rpl::start_with_next([=] { + _request = nullptr; + }, _request->lifetime); + + _request->box = box.data(); + _request->showBox(std::move(box)); +} + } // namespace Calls::Group diff --git a/Telegram/SourceFiles/calls/group/calls_choose_join_as.h b/Telegram/SourceFiles/calls/group/calls_choose_join_as.h index aa9b9dcf3..67d005682 100644 --- a/Telegram/SourceFiles/calls/group/calls_choose_join_as.h +++ b/Telegram/SourceFiles/calls/group/calls_choose_join_as.h @@ -41,6 +41,10 @@ public: PeerData *changingJoinAsFrom = nullptr); private: + void requestList(); + void processList(std::vector> &&list); + void finish(JoinInfo info); + struct ChannelsListRequest { not_null peer; Fn)> showBox; @@ -51,6 +55,7 @@ private: rpl::lifetime lifetime; Context context = Context(); mtpRequestId id = 0; + PeerData *changingJoinAsFrom = nullptr; }; std::unique_ptr _request;