From 240ced395bf0d64d3e765d450b1ba236edc98063 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 16 Jun 2017 16:47:44 +0300 Subject: [PATCH] Apply changes in ParticipantsBoxController. When we add admin / banned / restricted user in a channel using AddParticipantBoxController we now apply the added user in the box. --- .../profile/profile_channel_controllers.cpp | 110 ++++++++++++++---- .../profile/profile_channel_controllers.h | 8 +- 2 files changed, 91 insertions(+), 27 deletions(-) diff --git a/Telegram/SourceFiles/profile/profile_channel_controllers.cpp b/Telegram/SourceFiles/profile/profile_channel_controllers.cpp index 60c98fbd71..ed16c117b2 100644 --- a/Telegram/SourceFiles/profile/profile_channel_controllers.cpp +++ b/Telegram/SourceFiles/profile/profile_channel_controllers.cpp @@ -73,9 +73,13 @@ void ParticipantsBoxController::Start(gsl::not_null channel, Role void ParticipantsBoxController::addNewItem() { auto weak = base::weak_unique_ptr(this); - _addBox = Ui::show(Box(std::make_unique(_channel, _role, [weak] { - if (weak && weak->_addBox) { - weak->_addBox->closeBox(); + _addBox = Ui::show(Box(std::make_unique(_channel, _role, [weak](gsl::not_null user, const MTPChannelAdminRights &rights) { + if (weak) { + weak->editAdminDone(user, rights); + } + }, [weak](gsl::not_null user, const MTPChannelBannedRights &rights) { + if (weak) { + weak->editRestrictedDone(user, rights); } }), [](PeerListBox *box) { box->addButton(langFactory(lng_cancel), [box] { box->closeBox(); }); @@ -237,18 +241,34 @@ void ParticipantsBoxController::editAdmin(gsl::not_null user) { } void ParticipantsBoxController::editAdminDone(gsl::not_null user, const MTPChannelAdminRights &rights) { - if (_editBox) _editBox->closeBox(); - if (rights.c_channelAdminRights().vflags.v == 0) { - if (auto row = delegate()->peerListFindRow(user->id)) { - delegate()->peerListRemoveRow(row); - if (!delegate()->peerListFullRowsCount()) { - setDescriptionText(lang(lng_blocked_list_not_found)); - } - delegate()->peerListRefreshRows(); + if (_editBox) { + _editBox->closeBox(); + } + if (_addBox) { + _addBox->closeBox(); + } + auto notAdmin = (rights.c_channelAdminRights().vflags.v == 0); + if (notAdmin) { + _additional.adminRights.erase(user); + _additional.adminPromotedBy.erase(user); + _additional.adminCanEdit.erase(user); + if (_role == Role::Admins) { + removeRow(user); } } else { + // It won't be replaced if the entry already exists. + _additional.adminPromotedBy.emplace(user, App::self()); + _additional.adminCanEdit.emplace(user); _additional.adminRights[user] = rights; + _additional.kicked.erase(user); + _additional.restrictedRights.erase(user); + if (_role == Role::Admins) { + prependRow(user); + } else { + removeRow(user); + } } + delegate()->peerListRefreshRows(); } void ParticipantsBoxController::editRestricted(gsl::not_null user) { @@ -267,18 +287,43 @@ void ParticipantsBoxController::editRestricted(gsl::not_null user) { } void ParticipantsBoxController::editRestrictedDone(gsl::not_null user, const MTPChannelBannedRights &rights) { - if (_editBox) _editBox->closeBox(); - if (rights.c_channelBannedRights().vflags.v == 0 || rights.c_channelBannedRights().is_view_messages()) { - if (auto row = delegate()->peerListFindRow(user->id)) { - delegate()->peerListRemoveRow(row); - if (!delegate()->peerListFullRowsCount()) { - setDescriptionText(lang(lng_blocked_list_not_found)); - } - delegate()->peerListRefreshRows(); + if (_editBox) { + _editBox->closeBox(); + } + if (_addBox) { + _addBox->closeBox(); + } + auto notBanned = (rights.c_channelBannedRights().vflags.v == 0); + auto fullBanned = rights.c_channelBannedRights().is_view_messages(); + if (notBanned) { + _additional.kicked.erase(user); + _additional.restrictedRights.erase(user); + if (_role != Role::Admins) { + removeRow(user); } } else { - _additional.restrictedRights[user] = rights; + _additional.adminRights.erase(user); + _additional.adminCanEdit.erase(user); + _additional.adminPromotedBy.erase(user); + if (fullBanned) { + _additional.kicked.emplace(user); + _additional.restrictedRights.erase(user); + if (_role == Role::Kicked) { + prependRow(user); + } else { + removeRow(user); + } + } else { + _additional.restrictedRights[user] = rights; + _additional.kicked.erase(user); + if (_role == Role::Restricted) { + prependRow(user); + } else { + removeRow(user); + } + } } + delegate()->peerListRefreshRows(); } void ParticipantsBoxController::removeKicked(gsl::not_null row, gsl::not_null user) { @@ -310,6 +355,17 @@ bool ParticipantsBoxController::prependRow(gsl::not_null user) { return true; } +bool ParticipantsBoxController::removeRow(gsl::not_null user) { + if (auto row = delegate()->peerListFindRow(user->id)) { + delegate()->peerListRemoveRow(row); + if (!delegate()->peerListFullRowsCount()) { + setDescriptionText(lang(lng_blocked_list_not_found)); + } + return true; + } + return false; +} + std::unique_ptr ParticipantsBoxController::createRow(gsl::not_null user) const { auto row = std::make_unique(user); if (_role == Role::Admins) { @@ -436,10 +492,11 @@ void BannedBoxSearchController::searchDone(mtpRequestId requestId, const MTPchan } } -AddParticipantBoxController::AddParticipantBoxController(gsl::not_null channel, Role role, base::lambda doneCallback) : PeerListController(std::make_unique(channel, role, &_additional)) +AddParticipantBoxController::AddParticipantBoxController(gsl::not_null channel, Role role, AdminDoneCallback adminDoneCallback, BannedDoneCallback bannedDoneCallback) : PeerListController(std::make_unique(channel, role, &_additional)) , _channel(channel) , _role(role) -, _doneCallback(std::move(doneCallback)) { +, _adminDoneCallback(std::move(adminDoneCallback)) +, _bannedDoneCallback(std::move(bannedDoneCallback)) { if (_channel->mgInfo) { _additional.creator = _channel->mgInfo->creator; } @@ -638,7 +695,9 @@ void AddParticipantBoxController::editAdminDone(gsl::not_null user, c _additional.adminPromotedBy.emplace(user, App::self()); } } - _doneCallback(); + if (_adminDoneCallback) { + _adminDoneCallback(user, rights); + } } void AddParticipantBoxController::editRestricted(gsl::not_null user, bool sure) { @@ -680,7 +739,6 @@ void AddParticipantBoxController::editRestricted(gsl::not_null user, } void AddParticipantBoxController::restrictUserSure(gsl::not_null user, const MTPChannelBannedRights &rights) { - if (_editBox) _editBox->closeBox(); auto weak = base::weak_unique_ptr(this); MTP::send(MTPchannels_EditBanned(_channel->inputChannel, user->inputUser, rights), rpcDone([megagroup = _channel.get(), user, weak, rights](const MTPUpdates &result) { AuthSession::Current().api().applyUpdates(result); @@ -707,7 +765,9 @@ void AddParticipantBoxController::editRestrictedDone(gsl::not_null us _additional.kicked.erase(user); } } - _doneCallback(); + if (_bannedDoneCallback) { + _bannedDoneCallback(user, rights); + } } void AddParticipantBoxController::kickUser(gsl::not_null user, bool sure) { diff --git a/Telegram/SourceFiles/profile/profile_channel_controllers.h b/Telegram/SourceFiles/profile/profile_channel_controllers.h index 695f79fd3d..dc6ad9ad32 100644 --- a/Telegram/SourceFiles/profile/profile_channel_controllers.h +++ b/Telegram/SourceFiles/profile/profile_channel_controllers.h @@ -71,6 +71,7 @@ private: void removeKicked(gsl::not_null row, gsl::not_null user); bool appendRow(gsl::not_null user); bool prependRow(gsl::not_null user); + bool removeRow(gsl::not_null user); std::unique_ptr createRow(gsl::not_null user) const; gsl::not_null _channel; @@ -130,7 +131,9 @@ public: using Role = ParticipantsBoxController::Role; using Additional = ParticipantsBoxController::Additional; - AddParticipantBoxController(gsl::not_null channel, Role role, base::lambda doneCallback); + using AdminDoneCallback = base::lambda user, const MTPChannelAdminRights &adminRights)>; + using BannedDoneCallback = base::lambda user, const MTPChannelBannedRights &bannedRights)>; + AddParticipantBoxController(gsl::not_null channel, Role role, AdminDoneCallback adminDoneCallback, BannedDoneCallback bannedDoneCallback); void prepare() override; void rowClicked(gsl::not_null row) override; @@ -164,7 +167,8 @@ private: bool _allLoaded = false; Additional _additional; QPointer _editBox; - base::lambda _doneCallback; + AdminDoneCallback _adminDoneCallback; + BannedDoneCallback _bannedDoneCallback; };