diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index f1a46c481..0b9f4c334 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -2114,6 +2114,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_giveaway_maximum_countries_error#other" = "You can select maximum {count} countries."; "lng_giveaway_maximum_channels_error#one" = "You can select maximum {count} channel."; "lng_giveaway_maximum_channels_error#other" = "You can select maximum {count} channels."; +"lng_giveaway_channels_confirm_title" = "Channel is Private"; +"lng_giveaway_channels_confirm_about" = "Are you sure you want to add a private channel? Users won't be able to join it without an invite link."; "lng_prize_title" = "Congratulations!"; "lng_prize_about" = "You won a prize in a giveaway organized by {channel}."; diff --git a/Telegram/SourceFiles/info/boosts/giveaway/giveaway_list_controllers.cpp b/Telegram/SourceFiles/info/boosts/giveaway/giveaway_list_controllers.cpp index e9b7e50fe..8161553c5 100644 --- a/Telegram/SourceFiles/info/boosts/giveaway/giveaway_list_controllers.cpp +++ b/Telegram/SourceFiles/info/boosts/giveaway/giveaway_list_controllers.cpp @@ -7,8 +7,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "info/boosts/giveaway/giveaway_list_controllers.h" +#include "apiwrap.h" +#include "data/data_channel.h" #include "data/data_peer.h" +#include "data/data_session.h" #include "data/data_user.h" +#include "main/main_session.h" +#include "ui/boxes/confirm_box.h" #include "lang/lang_keys.h" namespace Giveaway { @@ -38,4 +43,111 @@ base::unique_qptr AwardMembersListController::rowContextMenu( return nullptr; } +MyChannelsListController::MyChannelsListController( + not_null peer, + std::shared_ptr show, + std::vector> selected) +: PeerListController( + std::make_unique(&peer->session())) +, _peer(peer) +, _show(show) +, _selected(std::move(selected)) { +} + +std::unique_ptr MyChannelsListController::createSearchRow( + not_null peer) { + if (const auto channel = peer->asChannel()) { + return createRow(channel); + } + return nullptr; +} + +std::unique_ptr MyChannelsListController::createRestoredRow( + not_null peer) { + if (const auto channel = peer->asChannel()) { + return createRow(channel); + } + return nullptr; +} + +void MyChannelsListController::rowClicked(not_null row) { + const auto channel = row->peer()->asChannel(); + const auto checked = !row->checked(); + if (checked && channel && channel->username().isEmpty()) { + _show->showBox(Box(Ui::ConfirmBox, Ui::ConfirmBoxArgs{ + .text = tr::lng_giveaway_channels_confirm_about(), + .confirmed = [=](Fn close) { + delegate()->peerListSetRowChecked(row, checked); + close(); + }, + .confirmText = tr::lng_filters_recommended_add(), + .title = tr::lng_giveaway_channels_confirm_title(), + })); + } else { + delegate()->peerListSetRowChecked(row, checked); + } +} + +Main::Session &MyChannelsListController::session() const { + return _peer->session(); +} + +void MyChannelsListController::prepare() { + delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled); + const auto api = _apiLifetime.make_state( + &session().api().instance()); + api->request( + MTPstories_GetChatsToSend() + ).done([=](const MTPmessages_Chats &result) { + _apiLifetime.destroy(); + const auto &chats = result.match([](const auto &data) { + return data.vchats().v; + }); + auto &owner = session().data(); + for (const auto &chat : chats) { + if (const auto peer = owner.processChat(chat)) { + if (!peer->isChannel() || (peer == _peer)) { + continue; + } + if (!delegate()->peerListFindRow(peer->id.value)) { + if (const auto channel = peer->asChannel()) { + auto row = createRow(channel); + const auto raw = row.get(); + delegate()->peerListAppendRow(std::move(row)); + if (ranges::contains(_selected, peer)) { + delegate()->peerListSetRowChecked(raw, true); + _selected.erase( + ranges::remove(_selected, peer), + end(_selected)); + } + } + } + } + } + for (const auto &selected : _selected) { + if (const auto channel = selected->asChannel()) { + auto row = createRow(channel); + const auto raw = row.get(); + delegate()->peerListAppendRow(std::move(row)); + delegate()->peerListSetRowChecked(raw, true); + } + } + delegate()->peerListRefreshRows(); + _selected.clear(); + }).send(); +} + +std::unique_ptr MyChannelsListController::createRow( + not_null channel) const { + if (channel->isMegagroup()) { + return nullptr; + } + auto row = std::make_unique(channel); + row->setCustomStatus(tr::lng_chat_status_subscribers( + tr::now, + lt_count, + channel->membersCount())); + return row; +} + } // namespace Giveaway diff --git a/Telegram/SourceFiles/info/boosts/giveaway/giveaway_list_controllers.h b/Telegram/SourceFiles/info/boosts/giveaway/giveaway_list_controllers.h index 404b65433..46c0a53a3 100644 --- a/Telegram/SourceFiles/info/boosts/giveaway/giveaway_list_controllers.h +++ b/Telegram/SourceFiles/info/boosts/giveaway/giveaway_list_controllers.h @@ -14,6 +14,7 @@ class PeerListRow; namespace Ui { class PopupMenu; +class Show; } // namespace Ui namespace Window { @@ -37,4 +38,33 @@ public: }; +class MyChannelsListController : public PeerListController { +public: + MyChannelsListController( + not_null peer, + std::shared_ptr show, + std::vector> selected); + + Main::Session &session() const override; + void prepare() override; + void rowClicked(not_null row) override; + + std::unique_ptr createSearchRow( + not_null peer) override; + std::unique_ptr createRestoredRow( + not_null peer) override; + +private: + std::unique_ptr createRow( + not_null channel) const; + + const not_null _peer; + const std::shared_ptr _show; + + std::vector> _selected; + + rpl::lifetime _apiLifetime; + +}; + } // namespace Giveaway